import pygame # 导入pygame模块 from pygame.locals import * # 导入pygame中常量 from itertools import cycle # 导入迭代工具 SCREENWIDTH = 640 # 屏幕宽度 SCREENHEIGHT = 230 # 屏幕高度 FPS = 30 # 更新画面的时间 # 定义一个地图类 class MyMap(): # 加载背景 def __init__(self, x, y): self.bg = pygame.image.load("image/bg.png") self.x = x self.y = y def map_rolling(self): if self.x < -790: # 小于-790说明地图已经完全移动完毕 self.x = 800 # 给图一个新的坐标点 else: self.x -= 5 # 5个像素向左移动 # 更新地图 def map_update(self): SCREEN.blit(self.bg, (self.x, self.y)) # 恐龙类 class Dinosaur(): def __init__(self): # 初始化小恐龙矩形 self.rect = pygame.Rect(0, 0, 0, 0) self.jumpState = False # 跳跃的状态 self.jumpHeight = 140 # 跳跃的高度 self.lowest_y = 140 # 最低坐标 self.jumpValue = 0 # 跳跃增变量 self.dinosaurIndex = 0 self.dinosaurIndexGen = cycle([0, 1, 2]) # 加载小恐龙图片 self.dinosaur_img = (pygame.image.load("image/dinosaur1.png").convert_alpha(), pygame.image.load("image/dinosaur2.png").convert_alpha(), pygame.image.load("image/dinosaur3.png").convert_alpha(),) self.jump_audio = pygame.mixer.Sound("audio/jump.wav") # 加载跳跃音效 self.rect.size = self.dinosaur_img[0].get_size() self.x = 50 # 绘制小恐龙的x坐标 self.y = self.lowest_y # 绘制小恐龙的y坐标 self.rect.topleft = (self.x, self.y) # 矩形左上角为准 # 跳跃 def jump(self): self.jumpState = True # 小恐龙的移动 def move(self): if self.jumpState: # 可以起跳 if self.rect.y >= self.lowest_y: self.jumpValue = -5 # 以5个像素向上移动 if self.rect.y <= self.lowest_y-self.jumpHeight: self.jumpValue = 5 # 以5个想素向下移动 self.rect.y += self.jumpValue # 通过循环改变恐龙的坐标 if self.rect.y >= self.lowest_y: # 小恐龙回到地面 self.jumpState = False # 关闭跳跃状态 # 绘制恐龙 def draw_dinosaur(self): # 匹配恐龙动图 dinosaurIndex = next(self.dinosaurIndexGen) # 实现绘制 SCREEN.blit(self.dinosaur_img[dinosaurIndex], (self.x, self.rect.y)) def mainGame(): score = 0 # 记录得分 over = False global SCREEN, FPSCLOCK pygame.init() # 初始化pygame FPSCLOCK = pygame.time.Clock() # 刷新屏幕的时间锁 SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT)) # 设置屏幕的大小 pygame.display.set_caption("小恐龙") # 设置主窗体 bg1 = MyMap(0, 0) # 地图1 bg2 = MyMap(800, 0) # 地图2 # 创建恐龙对象 dinosaur = Dinosaur() while True: # 判断是否单击了关闭窗口 for event in pygame.event.get(): if event.type == QUIT: over = True exit() # 关闭窗体 # 判断按下空格键时 if event.type == KEYDOWN and event.key == K_SPACE: if dinosaur.rect.y >= dinosaur.lowest_y: # 如果恐龙在地面上 dinosaur.jump() # 开启跳跃状态 dinosaur.jump_audio.play() # 播放恐龙跳跃时音效 if over == False: bg1.map_update() # 绘制地图起到更新地图的作用 # 地图移动 bg1.map_rolling() bg2.map_update() bg2.map_rolling() dinosaur.move() # 移动恐龙 # 绘制恐龙 dinosaur.draw_dinosaur() pygame.display.update() # 更新整个窗口 FPSCLOCK.tick(FPS) # 多久更新一次 if __name__ == '__main__': # 创建程序入口 mainGame() # 调用主函数
根据您提供的代码,发现恐龙不会跳的原因是因为事件处理的代码放在了错误的位置。
在您的代码中,事件处理代码被放置在了一个错误的缩进级别,导致它不会被正确执行。
请将事件处理的代码调整到正确的位置,使其位于主循环的正确缩进级别下。以下是修复后的代码供您参考:
import pygame
from pygame.locals import *
from itertools import cycle
SCREENWIDTH = 640
SCREENHEIGHT = 230
FPS = 30
class MyMap():
def __init__(self, x, y):
self.bg = pygame.image.load("image/bg.png")
self.x = x
self.y = y
def map_rolling(self):
if self.x < -790:
self.x = 800
else:
self.x -= 5
def map_update(self):
SCREEN.blit(self.bg, (self.x, self.y))
class Dinosaur():
def __init__(self):
self.rect = pygame.Rect(0, 0, 0, 0)
self.jumpState = False
self.jumpHeight = 140
self.lowest_y = 140
self.jumpValue = 0
self.dinosaurIndex = 0
self.dinosaurIndexGen = cycle([0, 1, 2])
self.dinosaur_img = (
pygame.image.load("image/dinosaur1.png").convert_alpha(),
pygame.image.load("image/dinosaur2.png").convert_alpha(),
pygame.image.load("image/dinosaur3.png").convert_alpha(),
)
self.jump_audio = pygame.mixer.Sound("audio/jump.wav")
self.rect.size = self.dinosaur_img[0].get_size()
self.x = 50
self.y = self.lowest_y
self.rect.topleft = (self.x, self.y)
def jump(self):
self.jumpState = True
def move(self):
if self.jumpState:
if self.rect.y >= self.lowest_y:
self.jumpValue = -5
if self.rect.y <= self.lowest_y - self.jumpHeight:
self.jumpValue = 5
self.rect.y += self.jumpValue
if self.rect.y >= self.lowest_y:
self.jumpState = False
def draw_dinosaur(self):
dinosaurIndex = next(self.dinosaurIndexGen)
SCREEN.blit(self.dinosaur_img[dinosaurIndex], (self.x, self.rect.y))
def mainGame():
score = 0
over = False
global SCREEN, FPSCLOCK
pygame.init()
FPSCLOCK = pygame.time.Clock()
SCREEN = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
pygame.display.set_caption("小恐龙")
bg1 = MyMap(0, 0)
bg2 = MyMap(800, 0)
dinosaur = Dinosaur()
while True:
for event in pygame.event.get():
if event.type == QUIT:
over = True
exit()
if event.type == KEYDOWN and event.key == K_SPACE:
if dinosaur.rect.y >= dinosaur.lowest_y:
dinosaur.jump()
dinosaur.jump_audio.play()
if over == False:
bg1.map_update()
bg1.map_rolling()
bg2.map_update()
bg2.map_rolling()
dinosaur.move()
dinosaur.draw_dinosaur()
pygame.display.update()
FPSCLOCK.tick(FPS)
if __name__ == '__main__':
mainGame()