2017-03-09 40 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

相關問題