Sunday, July 10, 2022
HomeGame Developmenttiles - How one can convert an inventory into coordinates in pygame

tiles – How one can convert an inventory into coordinates in pygame


You are query is just a little unclear. Issues transfer on maps, and/or maps transfer beneath issues. Objects on maps are sometimes saved in lists or another knowledge construction.

For a zero-indexed matrix with (0,0) within the high left-hand nook the objects on the map might be discover by y * mapWidth + x:

map = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]

for y in vary(3):
    for x in vary(5):
        print("({},{}) = {}".format(x, y, map[y*5+x]))

You’d in all probability have this in a perform:

def whatsatthecoordinates(x, y):
   international map
   return map[y*5+x]

or you may very well cross the map to the perform, which is a bit more versatile:

def whatsatthecoordinates(map, x, y):
   return map[y*5+x]

or you could possibly objectify map:

class Map():
    def __init__(self, xSize, ySize, listofmapcontents):
        if not kind(listofmapcontents) == kind(checklist()):
            increase RuntimeError
        self.xSize = xSize
        self.ySize = ySize
        self.array = listofmapcontents

    def whatsatthesecoordinates(self, x, y):
        if x < self.xSize and y < self.ySize:
            return self.array[y * self.xSize + x]
        else:
            print("Coordinates ({}, {}) out of vary.")
            print("Needs to be x = 0 to {} and y = 0 to {}".format(self.xSize-1, self.ySize-1))
            return None

    def placearock(self, x, y):
        map[y * xSize + x] = 1

    def destroyarock(self, x, y):
        map[y * xSize + x] = 0

map = Map(5, 3, [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0])

for y in vary(3):
    for x in vary(5):
        print("({},{}) = {}".format(x, y, map.whatsatthesecoordinates(x, y)))

Not absolutely examined. If this does not reply your query, please make clear.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments