私は現在、pygameで基本的なゲームを作成しています。そのうちの1つは、画面から外れるにつれて手続き的に新しい領域が生成されることです。テストとして、変数を定義して領域ごとにオブジェクトを生成し、その領域のオブジェクトをクラス内に保存して、後でそのクラスに戻ってくるようにしたいと考えています。ここで私は現時点で持っているものだ:クラスを使用した手続き生成Pygame
私はself.generated変数が何だろうと思った何#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"
1がすでに存在していた場合は、新しいオブジェクトの生成を停止しましたが、それは動作していないようです。エリアがすでに訪問されていても、正方形はまだ新しい場所に生成されます。クラスに関する私の知識は比較的限られているので、私はここからどこへ行くのかちょっと固まっています。
なぜブール値の代わりに文字列を使用するのですか?それは問題の一部かもしれません...(あなたは 'area_gen ==" true "の代わりに' area_gen == "true"を書きました) –
私のコンピュータプログラミングクラスはTrue/Falseになり、それぞれの構文は "ブール"章私は、代わりに文字列を使うことから始めました。適切な構文を調べるまでに、私はすでにこのプログラム内で文字列を使い始めました。 とにかく私はついに何が起こっているのか把握しました。クラスオブジェクトがどのように機能するかについての私の知識は正確ではありませんでした。修正案を新しい回答に投稿します。 – 1adog1