|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import sys
import pygame
import random
class Bird(object):
'''定义一个鸟类'''
def __init__(self):
'''定义初始化方法'''
self.birdRect = pygame.Rect(30, 30, 30, 30) # 鸟的矩形
# 定义鸟的3种状态列表
self.birdStatus = [pygame.image.load('niao.png'), pygame.image.load('niao.png'), pygame.image.load('niao.png')]
self.status = 0 # 默认飞行状态
self.birdX = 120 # 鸟所在 X 轴坐标
self.birdY = 350 # 鸟所在 Y 轴坐标, 及上下飞行高度
self.jump = False # 默认情况下小鸟自动降落
self.jumpspeed = 10 # 跳跃高度
self.gravity = 3 # 重力
self.dead = False # 默认小鸟生命状态为活着
def birdUpdate(self):
if self.jump:
self.jumpspeed -= 1 # 速度递减 上升越来越慢
self.birdY -= self.jumpspeed # 鸟 Y 轴坐标减小 小鸟上升
else:
# 小鸟坠落
self.gravity += 0.2 # 重力递增,下降越来越快
self.birdY += self.gravity # 鸟 Y 轴坐标增加 小鸟下降
self.birdRect[1] = self.birdY # 更改 Y 轴位置
class Guandao(object):
'''定义一个管道类'''
def __init__(self):
'''定义初始化方法'''
self.wallx = 400;
self.guandaoup = pygame.image.load("up.png")
self.guandaobottom = pygame.image.load("bottom.png")
def updateGuandao(self):
'''管道移动方法'''
self.wallx -= 5
if self.wallx < -80:
self.wallx = 400
def ditu():
'''定义创建地图的方法'''
screen.fill((255, 255, 255)) # 填充颜色
screen.blit(background, (0, 0)) # 填入到背景
# 显示管道
screen.blit(Guandao.guandaoup, (Guandao.wallx, -300)) # 上管道坐标位置
screen.blit(Guandao.guandaobottom, (Guandao.wallx, 500)) # 下管道坐标位置
Guandao.updateGuandao() # 管道移动
# 显示小鸟
if Bird.dead:
Bird.status = 2
elif Bird.jump: # 起飞状态
Bird.status = 1
screen.blit(Bird.birdStatus[Bird.status], (Bird.birdX, Bird.birdY)) # 设置小鸟的坐标
Bird.birdUpdate() # 鸟移动
pygame.display.update() # 更新显示
if __name__ == '__main__':
'''主程序'''
pygame.init() # 初始化 pygame
size = width, height = 400, 680 # 设置窗口大小
screen = pygame.display.set_mode(size) # 显示窗口
clock = pygame.time.Clock() # 设置时钟
guandao = Guandao() # 实例化管道
Bird = Bird() # 实例化鸟
# 执行死循环,确保窗口一直显示
while True:
clock.tick(75)
# 轮询事件
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('女孩.jpg')
ditu()
如上,窗口闪退,怎么回事
本帖最后由 学习编程中的Ben 于 2023-8-21 16:46 编辑
根据提供的代码,窗口闪退的原因可能是由于以下几个问题:
1. 缺少必要的资源文件
2. 事件处理问题
并且,根据提供的代码,我注意到其中存在一些问题。首先,在 Bird类的 birdUpdate方法中,没有对小鸟跳跃速度和重力进行限制,导致小鸟在跳跃过程中速度会越来越快,下降过程中速度也会继续增加。这可能导致窗口闪退或产生其他错误。
此外,还需要将 Guandao类和 Bird类的实例化对象名与类名区分开来,以免发生冲突。
解决方案:
1. 首先,修改 Bird类的 birdUpdate方法,添加对跳跃速度和重力的限制。
- def birdUpdate(self):
- if self.jump:
- if self.jumpspeed < 10: # 设置最大跳跃速度
- self.jumpspeed += 1
- self.birdY -= self.jumpspeed
- else:
- if self.gravity < 15: # 设置最大重力
- self.gravity += 0.5
- self.birdY += self.gravity
- self.birdRect[1] = self.birdY
复制代码
2. 将 Guandao类的实例化对象名修改为 guandao,将 Bird类的实例化对象名修改为 bird。
- guandao = Guandao() # 实例化管道
- bird = Bird() # 实例化鸟
复制代码
3. 最后,修改 ditu函数中的 screen.blit代码,将 Guandao.guandaoup和 Guandao.guandaobottom修改为 guandao.guandaoup和 guandao.guandaobottom。
- # 显示管道
- screen.blit(guandao.guandaoup, (guandao.wallx, -300)) # 上管道坐标位置
- screen.blit(guandao.guandaobottom, (guandao.wallx, 500)) # 下管道坐标位置
复制代码
修改后的完整代码如下:
- import sys
- import pygame
- import random
- class Bird(object):
- '''定义一个鸟类'''
- def __init__(self):
- '''定义初始化方法'''
- self.birdRect = pygame.Rect(30, 30, 30, 30) # 鸟的矩形
- # 定义鸟的3种状态列表
- self.birdStatus = [pygame.image.load('niao.png'), pygame.image.load('niao.png'), pygame.image.load('niao.png')]
- self.status = 0 # 默认飞行状态
- self.birdX = 120 # 鸟所在 X 轴坐标
- self.birdY = 350 # 鸟所在 Y 轴坐标, 及上下飞行高度
- self.jump = False # 默认情况下小鸟自动降落
- self.jumpspeed = 10 # 跳跃高度
- self.gravity = 3 # 重力
- self.dead = False # 默认小鸟生命状态为活着
- def birdUpdate(self):
- if self.jump:
- if self.jumpspeed < 10: # 设置最大跳跃速度
- self.jumpspeed += 1
- self.birdY -= self.jumpspeed
- else:
- if self.gravity < 15: # 设置最大重力
- self.gravity += 0.5
- self.birdY += self.gravity
- self.birdRect[1] = self.birdY
- class Guandao(object):
- '''定义一个管道类'''
- def __init__(self):
- '''定义初始化方法'''
- self.wallx = 400
- self.guandaoup = pygame.image.load("up.png")
- self.guandaobottom = pygame.image.load("bottom.png")
- def updateGuandao(self):
- '''管道移动方法'''
- self.wallx -= 5
- if self.wallx < -80:
- self.wallx = 400
- def ditu():
- '''定义创建地图的方法'''
- screen.fill((255, 255, 255))
- screen.blit(background, (0, 0))
- # 显示管道
- screen.blit(guandao.guandaoup, (guandao.wallx, -300)) # 上管道坐标位置
- screen.blit(guandao.guandaobottom, (guandao.wallx, 500)) # 下管道坐标位置
- guandao.updateGuandao() # 管道移动
- # 显示小鸟
- if bird.dead:
- bird.status = 2
- elif bird.jump:
- bird.status = 1 # 起飞状态
- screen.blit(bird.birdStatus[bird.status], (bird.birdX, bird.birdY)) # 设置小鸟的坐标
- bird.birdUpdate() # 鸟移动
- pygame.display.update() # 更新显示
- if __name__ == '__main__':
- '''主程序'''
- pygame.init()
- size = width, height = 400, 680
- screen = pygame.display.set_mode(size)
- clock = pygame.time.Clock()
- guandao = Guandao()
- bird = Bird()
- while True:
- clock.tick(75)
- 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('女孩.jpg')
- ditu()
复制代码
请尝试使用修改后的代码运行程序,并检查是否解决了窗口闪退的问题。如果问题仍然存在,请提供相关错误信息进行进一步调试。
如果回答对你有帮助,请给我一个最佳答案! 
|
|