2017-03-09 2 views
0

私は授業のために鳥のプログラムを作成していますが、パイプは移動しないか、表示されません。誰も私の問題の解決策を知っていますか?パイプが現れたり動かないのはなぜですか?

### SETUP ### 
import pygame, random 
pygame.init() 
c=pygame.time.Clock() 
window = pygame.display.set_mode([500, 400]) 
window.fill((255, 255, 255)) 
playing=True 
drop=1 
x=0 
points=0 
highscore=0 
dead=False 
height=random.randint(50,280) 
t=0 
point_counter=1 
play_again=False 
#start 
print("Press any key to start.") 
while not play_again: 
    for event in pygame.event.get(): 
     if event.type==pygame.KEYDOWN: 
      play_again=True 
### MAIN LOOP ### 
while playing: 
    play_again=False 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      playing = False 
    ### GAME LOOP ### 
    while playing and not dead: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       playing = False 
      #jump 
      if event.type==pygame.KEYDOWN: 
       drop=-10 
     top_pipe=pygame.Rect(500+t,0,75,height) 
     bird=pygame.Rect(200,200+x,75,50) 
     window.fill((255,255,255)) 
     #hitting ground or ceiling 
     if bird.y>=350 or bird.y<=0: 
      dead=True 
     if top_pipe.x==-75: 
      top_pipe.x=500 
      t=0 
      point_counter=1 
      height=random.randint(50,280) 
     bottom_pipe=pygame.Rect(500-t,height-70,75,1000) 
     t-=1 
     # if hits pipe 
     if top_pipe.x<250 and top_pipe.x>150 and (bird.x<height or bird.x>height+20): 
      print("You died!") 
      dead=True 
     elif top_pipe.x<150 and point_counter==1: 
      points+=1 
      point_counter=0 
     #gravity 
     drop+=1 
     x+=drop 
     #body 
     pygame.draw.rect(window,(255,255,0),bird) 
     #eye 
     pygame.draw.circle(window,(0,0,0),(bird.x+60,bird.y+10),2) 
     pygame.display.flip() 
     #pipes 
     pygame.draw.rect(window,(0,255,0),top_pipe) 
     pygame.draw.rect(window,(0,255,0),bottom_pipe) 
     #framerate 
     c.tick(30) 
    x=0 
    drop=1 
    window.fill((255,255,255)) 
    bird=pygame.Rect(200,200+x,75,50) 
    #body 
    pygame.draw.rect(window,(255,255,0),bird) 
    #eye 
    pygame.draw.circle(window,(0,0,0),(bird.x+60,bird.y+10),2) 
    pygame.display.flip() 
    if points>highscore: 
     print("New highscore!: "+str(points)) 
     highscore=points 
    elif points==highscore: 
     print("You matched your highscore!") 
    else: 
     print("Try again!") 
    pygame.time.wait(2000) 
    print("\n"*100) 
    print("Highscore: "+str(highscore)) 
    print("Press any key to play again.") 
    dead=False 
    while playing and not play_again: 
     for event in pygame.event.get(): 
      if event.type==pygame.QUIT: 
       playing=False 
      elif event.type==pygame.KEYDOWN: 
       play_again=True 

パイプが移動できない場所が見つからない。

+1

助けを求める必要がある場合は、コードをできるだけ読みやすくするようにしてください。演算子(=、==、<, >、+、 - 、/、*など)の間にスペースを入れ、コードをいくつかの段落に分割してください(あなたのコメントの一部を置くのが良い場所です)。それ以外の場合は、パラグラフなしでエッセイを読むようなものであり、すぐに読むことが難しくなり、エネルギーを消費します。 'c'のような変数の名前を' clock'に変更することで、読者は毎回その定義を調べる必要がないように説明します。最も重要なのは、[mcve]を作ることです。不要なものはすべて削除し、問題が残るようにします。 –

答えて

0

パイプ矩形を描画する前にpygame.display.flip()と呼ぶので、ウィンドウがレンダリングされた後に描画します。

また、衝突の検出に問題があるようですが、それを把握するのはあなたの仕事です。 pygame.Rectのcollision detection methodsを見てください。

+0

ありがとうございます!私は今これを行う方法を知っています。 – Jdog

関連する問題