0

私は現在、Python 3.2 を使用して Pygame で「Flappy Bird」のリメイクに取り組んでいます。練習用で、比較的簡単だと思いました。しかし、それは難しいことが証明されています。現在、異なる高さで長方形を描画するときに問題が発生していますが、長方形を設定された高さに保ちます。

ここに私のパイプクラスがあります

class Pipe:
    def __init__(self,x):
        self.drawn = True
        self.randh = random.randint(30,350)
        self.rect = Rect((x,0),(30,self.randh))

    def update(self):
        self.rect.move_ip(-2,0)

    def draw(self,screen):
        self.drawn = True
        pygame.draw.rect(screen,(0,130,30),self.rect)

私の while ループは次のとおりです。

while True:
    for event in pygame.event.get():
        movey = +0.8
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_SPACE:
                movey = -2


    x += movex
    y += movey


    screen.blit(background,(0,0))
    screen.blit(bird,(x,y))

    Pipe1 = Pipe(scrollx)

    if Pipe1.drawn == True:
        Pipe1.update()
    else:
        Pipe1 = Pipe(scrollx)
        Pipe1.draw(screen)

    scrollx -= 0.3

    pygame.display.update()

私はこのコードと 1 週間以上格闘してきました。

4

2 に答える 2

1

私はこの部分の論理に従っていません:

Pipe1 = Pipe(scrollx)

if Pipe1.drawn == True:
    Pipe1.update()
else:
    Pipe1 = Pipe(scrollx)
    Pipe1.draw(screen)

コンストラクターでdrawn属性が設定されているため、いつ条件がトリガーされるとTrue予想されますか? フレームごとにelseこのパイプを再作成していることを思い出してください。

鳥と同じようにパイプを描いてみましたか?

編集:ループのためのあなたへの提案:

PIPE_TIME_INTERVAL = 2

pipes = []    # Keep the pipes in a list.
next_pipe_time = 0

while True:
    [... existing code to handle events and draw the bird ...]

    for pipe in pipes:
        pipe.move(10)     # You'll have to write this `move` function.
        if pipe.x < 0:    # If the pipe has moved out of the screen...
            pipes.pop(0)  # Remove it from the list.

    if current_time >= next_pipe_time:   # Find a way to get the current time/frame.
        pipes.append(Pipe())  # Create new pipe.
        next_pipe_time += PIPE_TIME_INTERVAL  # Schedule next pipe creation.
于 2014-02-14T18:08:01.333 に答える
0

ループごとに新しいものを作成してPipeいますが、古いものに固執することはないため、毎回新しいランダムな高さを取得します。次の行を移動します。

Pipe1 = Pipe(scrollx)

whileループの外。さらに良いことに、新しいパイプを追加してすべてを簡単に更新できるパイプのリストを用意してください。どちらにも設定self.drawn = FalseしないでPipeください。

moveyまた、イベントごとにリセットしている場合は、次を試してください。

movey = 0.8 # no need for plus 
for event in pygame.event.get():
于 2014-02-14T18:07:29.630 に答える