cdsn_Python 2022-07-04 21:45 采纳率: 69%
浏览 32
已结题

关于#pygame#的flappy bird的问题,如何解决?

问题遇到的现象和发生背景

正在学习pygame(入门),使用pycharm制作flappy bird小游戏。而对于下方代码,小鸟总是在第一个档口死亡(并没有触碰管道);又或是不能触发死亡机制(在下方代码基础上,checkDead()中检验碰撞管道板块也加入return True的时候)。

问题相关代码,请勿粘贴截图

import sys
import pygame

class Bird:
def init(self):
self.birdRect=pygame.Rect(65,50,48,48)
self.birdStatus=[pygame.image.load("bird0_0.png"),pygame.image.load("bird0_1.png"),pygame.image.load("bird0_2.png")]
self.status=0
self.birdX=120
self.birdY=350
self.jump=False
self.jumpSpeed=10
self.gravity=5
self.dead=False
def birdUpdate(self):
if self.jump:
self.jumpSpeed-=1
self.birdY-=self.jumpSpeed
else:
self.gravity+=0.2
self.birdY+=self.gravity

class Pipeline:
def init(self):
self.wallx=400
self.pineUp=pygame.image.load("pipe_up.png")
self.pineDown=pygame.image.load("pipe_down.png")

def updatePipeline(self):
    self.wallx-=5
    if self.wallx<-80:
        self.wallx=400
        global score
        score+=1

def createMap():
screen.fill((255,255,255))
screen.blit(background,(0,0))

screen.blit(Pipeline.pineUp,(Pipeline.wallx,-500))
screen.blit(Pipeline.pineDown,(Pipeline.wallx,500))
Pipeline.updatePipeline()
if Bird.dead:
    Bird.status=2
else:
    Bird.status=1
screen.blit(Bird.birdStatus[Bird.status],(Bird.birdX,Bird.birdY))
Bird.birdUpdate()

screen.blit(font.render("Score:"+str(score),-1,(255,255,255)),(100,50))
pygame.display.update()

def checkDead():
upRect=pygame.Rect(Pipeline.wallx,-500,Pipeline.pineUp.get_width()-10,Pipeline.pineUp.get_height())
downRect=pygame.Rect(Pipeline.wallx,500,Pipeline.pineDown.get_width() - 10, Pipeline.pineDown.get_height())
if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect):
Bird.dead=True

if not 0<Bird.birdRect[1]<height:
    Bird.dead=True

    return True
else:
    return False

def getResut1():
final_test1="Game Over"
final_test2="Your Final Score Is:" +str(score)
ft1_font=pygame.font.SysFont("Arial",70)
ft1_surf=font.render(final_test1,1,(242,3,36))
ft2_font=pygame.font.SysFont("Arial",50)
ft2_surf=font.render(final_test2,1,(253,177,36))
screen.blit(ft1_surf,(screen.get_width()/2-ft1_surf.get_width(),100))
screen.blit(ft2_surf,(screen.get_width()/2-ft1_surf.get_width(),200))
pygame.display.flip()

if name=="main":
pygame.init()
pygame.font.init()
font=pygame.font.SysFont(None,50)
size=width,height=400,650
screen=pygame.display.set_mode(size)
clock=pygame.time.Clock()
Pipeline=Pipeline()
Bird=Bird()
score=0
while True:

    clock.tick(60)
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()
            sys.exit()
        if (event.type==pygame.KEYDOWN or event.type==pygame.MOUSEBUTTONDOWN) and not Bird.dead:
            Bird.jump=True
            Bird.gravity=5
            Bird.jumpSpeed=10
    background=pygame.image.load("bg_day.png")
    if checkDead():
        getResut1()
    else:
        createMap()
运行结果及报错内容

游戏运行不正常,无报错

我的解答思路和尝试过的方法

代码本身几乎照抄教材,后续又稍加改动。
尝试过上网搜寻,认为与小鸟、管道rect边界设置有关,又或是checkDead()出现了某种错误导致

我想要达到的结果

希望能指出上方代码的错误(最好),或者依据我的代码给出正确的代码

  • 写回答

3条回答 默认 最新

  • 请叫我问哥 Python领域新星创作者 2022-07-05 00:36
    关注

    碰撞检测那里比较的两个Rect和你的图片位置不一致,所以看到的并不是Rect真正的位置,也就不难解释为什么看上去没撞到或撞到了,实际却没有反应了。
    if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect):
    这里的Bird.birdRect自从初始化以后,就一直没变过,它的位置一直都是初始化的位置和大小,所以你的代码一直再检测这个位置有没有碰撞。
    self.birdRect=pygame.Rect(65,50,48,48)
    通常Pygame是在创建Rect后,然后再在Rect里绘制图片,所以推荐做以下修改:
    class Bird()里改成:

        def birdUpdate(self):
            if self.jump:
                self.jumpSpeed-=1
                self.birdY-=self.jumpSpeed
            else:
                self.gravity+=0.2
                self.birdY+=self.gravity
            self.birdRect.x=self.birdX
            self.birdRect.y=self.birdY
    

    然后
    def createMap()里绘制小鸟的代码改成:
    screen.blit(Bird.birdStatus[Bird.status],Bird.birdRect)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 7月13日
  • 已采纳回答 7月5日
  • 创建了问题 7月4日

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效