用户:
魔侠查看:38 回复:1 评论:0 创建时间:2020-08-09T10:45:57
【作品展示】

【作品介绍】
按鼠标右键飞行
【作品源代码】
import pygame
import random
# 游戏初始化
pygame.init()
screen = pygame.display.set_mode((288, 512))
background = pygame.image.load("背景.png")
pygame.display.set_caption("翱翔吧!源码精灵1")
class Bird():
def __init__(self):
self.birdSprites = [pygame.image.load(
"鸟{}.png".format(i)) for i in range(0, 4)]
self.a = 0
self.birdX = 50
self.birdY = 100
self.jumpSpeed = 7
self.gravity = 0.4
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:
self.a = 1
if self.jumpSpeed > 0:
self.a = 2
def birdCrush(self):
resultU = self.rect.colliderect(newWall.wallUpRect)
resultD = self.rect.colliderect(newWall.wallDownRect)
if resultU or resultD or newBird.rect.top > 512:
return True
class Wall():
def __init__(self):
self.wallDown = pygame.image.load("bottom.png")
self.wallUp = pygame.image.load("top.png")
self.count = 0
self.wallx = 288
self.gap = 100 # 两个柱子间的间隔
self.offset = random.randint(-50, 50) # 偏移量
self.wallUpRect = self.wallUp.get_rect()
self.wallDownRect = self.wallDown.get_rect()
self.wallUpRect.center = (self.wallx, 0 - self.gap - self.offset)
self.wallDownRect.center = (self.wallx, 520 - self.gap - self.offset)
def wallUpdate(self):
self.wallx -= 2 # 设置柱子向鸟移动
if self.wallx < -30:
self.wallx = 310
self.offset = random.randint(-50, 50)
self.count += 1
self.wallUpRect.center = (self.wallx, 0 - self.gap - self.offset)
self.wallDownRect.center = (self.wallx, 520 - self.gap - self.offset)
newBird = Bird()
newWall = Wall()
clock = pygame.time.Clock()
# pygame循环
keep_going = True
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
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()
if newBird.birdCrush():
print("撞上了!!!")
if newWall.count == 0:
print("很遗憾,您没有躲过任何水管!")
elif newWall.count > 0 and newWall.count < 10:
print("您成功躲过{}个水管,再接再厉!".format(newWall.count))
elif newWall.count >= 10:
print("你成功躲过{}个水管,太棒了!!!".format(newWall.count))
keep_going = False
pygame.display.update()
clock.tick(80)
# 退出游戏
pygame.quit()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn