素材要和py文件放在一个文件夹里面 界面设计 import pygame import random import sys pygame.init()# 设置背景 screen = pygame.display.set_mode((288, 512))# 屏幕大小 background = pygame.image.load("assets/background.png")# 背景图片 pygame.display.set_caption("Flappy Bird")# 标题 bgm = pygame.mixer.Sound('sound/bgm.wav')# 设置背景音乐 channel_1 = pygame.mixer.Channel(1)# 设置背景音乐 channel_1.play(bgm)# 设置背景音乐 pic = pygame.image.load('assets/0.png')# 加载小鸟图片 keep_going = True clock = pygame.time.Clock() # 运行 while keep_going: for event in pygame.event.get(): if (event.type == pygame.QUIT): keep_going = False screen.blit(background, (0, 0))# 显示背景图片 screen.blit(pic, (100, 100)) pygame.display.update() clock.tick(60) pygame.quit() 控制小鸟上下飞翔 import pygame import random import sys class Bird(pygame.sprite.Sprite):#定义小鸟的属性(类) def __init__(self): pygame.sprite.Sprite.__init__(self) self.birdSprites = pygame.image.load("assets/0.png") self.birdX = 50 # 初始x坐标 self.birdY = 100 # 初始y坐标 self.jumpSpeed = 7 # 跳跃速度 self.gravity = 0.3 #下坠速度 self.rect = self.birdSprites.get_rect() self.rect.center = (self.birdX, self.birdY) def birdUpdate(self):#计算小鸟新位置 self.jumpSpeed -= self.gravity self.birdY -= self.jumpSpeed self.rect.center = (self.birdX, self.birdY) pygame.init()# 设置背景 screen = pygame.display.set_mode((288, 512))# 屏幕大小 background = pygame.image.load("assets/background.png")# 背景图片 pygame.display.set_caption("Flappy Bird")# 标题 bgm = pygame.mixer.Sound('sound/bgm.wav')# 设置背景音乐 channel_1 = pygame.mixer.Channel(1)# 设置背景音乐 channel_1.play(bgm)# 设置背景音乐 pic = pygame.image.load('assets/0.png')# 加载小鸟图片 newBird = Bird()#初始化界面,创建小鸟,文字对象 keep_going = True clock = pygame.time.Clock() #运行 while keep_going: # 主循环 for event in pygame.event.get(): if event.type == pygame.QUIT: keep_going = False if (event.type == pygame.MOUSEBUTTONDOWN):#侦听鼠标点击事件 newBird.jumpSpeed = 7 channel_2 = pygame.mixer.Channel(2) screen.blit(background, (0, 0)) screen.blit(newBird.birdSprites, newBird.rect) newBird.birdUpdate() pygame.display.update() clock.tick(60) pygame.quit() 让小鸟拍动翅膀 import pygame import random import sys class Bird(pygame.sprite.Sprite):#定义小鸟的属性(类) def __init__(self): pygame.sprite.Sprite.__init__(self) self.birdSprites = [pygame.image.load("assets/0.png"), pygame.image.load("assets/1.png"), pygame.image.load("assets/2.png")] self.a = 0 #初始显示动作0.png self.rect = self.birdSprites[self.a].get_rect() self.birdX = 50 # 初始x坐标 self.birdY = 100 # 初始y坐标 self.jumpSpeed = 7 # 跳跃速度 self.gravity = 0.3 #下坠速度 self.rect.center = (self.birdX, self.birdY) def birdUpdate(self):#计算小鸟新位置 self.jumpSpeed -= self.gravity self.birdY -= self.jumpSpeed self.rect.center = (self.birdX, self.birdY) if self.jumpSpeed < 0: # 当y向量<0时,鸟下坠,显示图片1.png self.a = 1 if self.jumpSpeed > 0: # 当y向量>0时,鸟上升,显示图片2.png self.a = 2 pygame.ini ... ...
~~ 您好,已阅读到文档的结尾了 ~~