Python 为什么赢了';管道是不是出现了,还是移动了?
我正在为类创建一个flappy bird程序,但是管道要么不会移动,要么不会出现。有人知道我问题的解决办法吗Python 为什么赢了';管道是不是出现了,还是移动了?,python,pygame,Python,Pygame,我正在为类创建一个flappy bird程序,但是管道要么不会移动,要么不会出现。有人知道我问题的解决办法吗 ### 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
### 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
###设置###
导入pygame,随机
pygame.init()
c=pygame.time.Clock()
window=pygame.display.set_模式([500400])
窗口填充((255,255,255))
玩=真
下降=1
x=0
分数=0
高分=0
死=假
高度=random.randint(50280)
t=0
点计数器=1
再次播放=错误
#开始
打印(“按任意键开始”)
当不再玩时:
对于pygame.event.get()中的事件:
如果event.type==pygame.KEYDOWN:
再次播放=正确
###主回路###
玩的时候:
再次播放=错误
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
播放=错误
###游戏循环###
玩的时候没有死:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
播放=错误
#跳跃
如果event.type==pygame.KEYDOWN:
下降=-10
顶部管道=pygame.Rect(500+t,0,75,高度)
bird=pygame.Rect(200200+x,75,50)
窗口填充((255255))
#撞击地面或天花板
如果bird.y>=350或bird.y在绘制管道矩形之前调用pygame.display.flip()
,则在渲染窗口之后绘制管道矩形
碰撞检测似乎也有问题,但解决这个问题是你们的工作。看看pygame.Rect的用法。如果需要帮助,请尽量使代码可读。在运算符(=、==、+、-、/、*,等等)之间留出一个空格,并偶尔将代码分成几段(在此处放置一些注释将是一个很好的位置)。否则,就像读一篇没有段落的文章,很快就会变得难以阅读,而且会消耗大量精力。将c
等变量重命名为clock
,这样它们就可以解释它们是什么,这样读者就不必每次都查看它的定义。最重要的是,做出一个决定。删除所有不必要的内容,但问题仍然存在。谢谢!我现在知道怎么做了。