用户:32.36℉VX4N查看:3 回复:1 评论:3 创建时间:2022-07-21T20:27:13
【作品展示】

【作品介绍】
做的是小鸟飞向游戏
【作品源代码】
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)
#设置分数
score=0
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
self.birdX = 50 #初始x坐标
self.birdY = 100 #初始y坐标
self.jumpSpeed = 7 #跳跃速度
self.gravity = 0.3
self.rect=self.birdSprites[self.a].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)
if self.jumpSpeed < 0: #当y向量<0时,鸟下坠
self.a = 1
if self.jumpSpeed > 0: #当y向量>0时,鸟上升
self.a = 2
global score
if self.rect.left==newWall.wallUpRect.right:
score +=1
#小鸟碰撞
def birdCrush(self):
global keep_going
resultU=self.rect.colliderect(newWall.wallUpRect)
resultD=self.rect.colliderect(newWall.wallDownRect)
if resultU or resultD or newBird.rect.top>512:
hit=pygame.mixer.Sound('sound/hit.wav')
channel_3=pygame.mixer.Channel(3)
channel_3.play(hit)
keep_going=False
#定义管道属性(类)
class Wall():
def __init__(self):
self.wallUp = pygame.image.load("assets/bottom.png")
self.wallDown = pygame.image.load("assets/top.png")
self.wallUpRect=self.wallUp.get_rect()
self.wallDownRect=self.wallDown.get_rect()
self.gap = 50 #缝隙间隔
self.wallx = 288
self.offset = random.randint(-50, 50) #offset是相对于当前的位置移动的距离
self.wallUpY=360 + self.gap - self.offset
self.wallDownY=0- self.gap - self.offset
self.wallUpRect.center=(self.wallx,self.wallUpY)
self.wallDownRect.center=(self.wallx,self.wallDownY)
#墙移动
def wallUpdate(self):
self.wallx -= 2 #速度为2
self.wallUpRect.center=(self.wallx,self.wallUpY)
self.wallDownRect.center=(self.wallx,self.wallDownY)
if self.wallx < -80: #循环
self.wallx = 288
self.offset = random.randint(-50, 50)
self.wallUpY=360 + self.gap - self.offset
self.wallDownY=0- self.gap - self.offset
#初始化界面,创建小鸟,水管,文字对象
newBird=Bird()
newWall=Wall()
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 = 4
channel_2=pygame.mixer.Channel(2)
fly=pygame.mixer.Sound('sound/fly.WAV')
channel_2.play(fly)
screen.blit(background,(0, 0))
screen.blit(newBird.birdSprites[newBird.a],newBird.rect)
screen.blit(newWall.wallUp,newWall.wallUpRect)
screen.blit(newWall.wallDown,newWall.wallDownRect)
newWall.wallUpdate()
newBird.birdUpdate()
newBird.birdCrush()
pygame.display.update()
clock.tick(60)
print(score)
pygame.quit()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn