我现在正在pygame中创建一个基本游戏,其中一部分是当你离开屏幕时程序生成新区域。作为测试,我希望通过定义其变量为每个区域生成一个对象,然后将该区域的对象保存在类中,如果您稍后再回到它。这就是我现在所拥有的:
#area_gen is set to "true" if you move to a new area
#swidth and sheight are set to the size of the screen
#x_area and y_area are defined as you change areas, acting as sector coordinates
#Red is defined in globals
class areas:
def __init__(self, coords):
self.coordinates = coords
self.generated = False
def gen_objects(self):
if not self.generated:
self.objs = []
obj_type = "test object"
center_x = random.randrange(105, swidth-25)
center_y = random.randrange(25, swidth - 175)
self.objs.append([obj_type, center_x, center_y])
self.generated = True
#Within The Game Loop
if area_gen == "true":
coords = str(str(x_area) + " " + str(y_area))
area = areas(coords)
area.gen_objects()
for thing in area.objs:
if thing[0] == "test object":
pygame.draw.rect(screen, Red, (thing[1], thing[2], 250, 250))
#Bottom of the Game Loop
area_gen = "false"
我认为self.generated变量会做的是停止新对象生成(如果已经存在),但这似乎不起作用。即使该区域已被访问,该广场仍会在新位置生成。我对课程的了解相对有限,所以我对这里的去处感到有些困惑。
答案 0 :(得分:0)
将area_gen
传递给区域类的构造函数,并设置self.generated = area_gen
。这有用吗?说实话,我无法看到足够的代码。