# 1。倒入游戏的工具包
import pygame
import sys
"""
1.bug:
2.想法:
- 撞到管道会结束 √
- 管道随机出现 √
- 得分
- 主界面
"""
# 类:1000架飞机,写图纸,类
# 类名
# 属性:皮肤,x,y,窗口
# 方法:
class niao:
def __init__(self, window):
self.x = 100
self.y = 200
self.picture = pygame.image.load("picture/flappy Bird 素材-0_爱给网_aigei_com.png")
self.window = window
self.zhongli = 0.1
self.wei_zhi = pygame.Rect(self.x+7,self.y+12,34,24)
def fly(self):
self.y = self.y - 50
self.zhongli = 0.2
if self.y<0:
self.y=0
def show(self):
self.wei_zhi = pygame.Rect(self.x + 7, self.y + 12, 34, 24)
self.zhongli += 0.02
self.y += self.zhongli
self.window.blit(self.picture, (self.x, self.y))
class Guan:
def __init__(self,window,i):
picture = ['picture/flappy Bird 素材-安静下来(down)_爱给网_aigei_com.png',
'picture/flappy Bird 素材-管(up)_爱给网_aigei_com.png']
self.x=250
self.y=0
self.picture=pygame.image.load(picture[i])
self.window=window
self.wei_zhi = pygame.Rect(self.x,self.y,52,320)
def move(self):
self.x-=1
if self.x<-52:
self.x=288
def show(self):
self.wei_zhi = pygame.Rect(self.x, self.y, 52, 320)
self.window.blit(self.picture, (self.x, self.y))
# 2。初始化,loading,加载中
pygame.init()
# 3。设置游戏窗口大小
window = pygame.display.set_mode((288, 512))
# ***4。置游戏名称 set_caption(字符串)
pygame.display.set_caption('管道小鸟游戏')
# 图片1.导入图片
background = pygame.image.load('picture/flappy Bird 素材-BG 的一天(day)_爱给网_aigei_com.png')
tudi = pygame.image.load('picture/flappy Bird 素材-土地(land)_爱给网_aigei_com.png')
tudi_x = 0
get_ready=pygame.image.load('picture/flappy Bird 素材-准备好的文本(ready)_爱给网_aigei_com.png')
jiaocheng=pygame.image.load('picture/flappy Bird 素材-教程(tutorial)_爱给网_aigei_com.png')
# 实例化一个对象,按照小鸟图纸制作一个真实的小鸟 格式i: 对像名 = 类名(窗口)
bird = niao(window)
guan0 = Guan(window,0)
guan0.y = -200
guan1 = Guan(window,1)
guan1.y = 250
zhuang = 0
# 主界面
Game_over = True
# 5。无限循环刷新
while True:
# 玩游戏的时候,对电脑进行哪些操作?
# 鼠标,键盘
# 事件,event pygame.event.get()
# print(pygame.event.get())
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if Game_over == False:
bird.fly()
# 图片2 .把图片放在窗口上
window.blit(background, (0, 0))
window.blit(tudi, (tudi_x, 400))
tudi_x -= 1
if tudi_x <= 288 - 336:
tudi_x = 0
if Game_over:
window.blit(bird.picture,(100,200))
window.blit(get_ready, (70, 50))
window.blit(jiaocheng, (90, 300))
else:
bird.show()
guan0.show()
guan0.move()
guan1.show()
guan1.move()
# 判断小鸟和管道是否相撞collide 矩形rect
# print(bird.wei_zhi)
# print(guan0.wei_zhi)
if bird.wei_zhi.colliderect(guan0.wei_zhi) or bird.wei_zhi.colliderect(guan1.wei_zhi):
print("啊,我撞了%d"%zhuang)
zhuang += 1
pygame.display.update()
tina - 小鸟管道4.0
最新推荐文章于 2023-06-23 20:35:49 发布