用户:活泼的编程猫Lcp8查看:0 回复:0 评论:0 创建时间:2020-02-16T18:48:07
【作品展示】

【作品介绍】
1
【作品源代码】
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((288, 512))
font = pygame.font.SysFont("Comic Sans MS", 24)
pygame.display.set_caption("翱翔吧!源码精灵")
pygame.mixer.music.load('BGM.mp3')
background, clock, keep_going = pygame.image.load(
"背景.png"), pygame.time.Clock(), True
class Bird():
def __init__(self):
self.birdSprites = [pygame.image.load(
"鸟{}.png".format(i))for i in range(3)]
self.a = 0
self.bird_x = 50
self.bird_y = 100
self.jumpSpeed = 0
self.gravity = 0.7
def birdUpdate(self):
self.jumpSpeed -= self.gravity
self.bird_y -= self.jumpSpeed
self.rect = self.birdSprites[self.a % 3].get_rect()
self.rect.center = (self.bird_x, self.bird_y)
def birdCrush(self):
resultU = self.rect.colliderect(wall.wallUprect)
resultD = self.rect.colliderect(wall.wallDownrect)
if resultU or resultD:
return True
class Wall():
def __init__(self):
self.wallDown = pygame.image.load("bottom.png")
self.wallUp = pygame.image.load("top.png")
self.wall_x = 288
self.gap = 100
self.offset = random.randint(-50, 50)
self.wallup = 0-self.gap-self.offset
self.walldown = self.wallup+random.randint(480, 520)
self.score = 0
def wallUpdate(self):
self.wall_x -= 2
self.wallUprect = self.wallUp.get_rect()
self.wallUprect.center = (self.wall_x, self.wallup)
self.wallDownrect = self.wallDown.get_rect()
self.wallDownrect.center = (self.wall_x, self.walldown)
if self.wall_x+self.wallUp.get_width() <= 0:
self.wall_x = 288
self.offset = random.randint(-50, 50)
self.wallup = 0-self.gap-self.offset
self.walldown = self.wallup+random.randint(520,570)
self.score += 1
bird = Bird()
wall = Wall()
pygame.mixer.music.play(-1)
while keep_going:
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
if event.type == pygame.MOUSEBUTTONDOWN:
bird.jumpSpeed = 10
bird.birdUpdate()
wall.wallUpdate()
if bird.birdCrush():
break
screen.blit(background, (0, 0))
screen.blit(bird.birdSprites[bird.a % 3], bird.rect)
screen.blit(wall.wallDown, wall.wallDownrect)
screen.blit(wall.wallUp, wall.wallUprect)
text = font.render("score="+str(wall.score), False,
(255, 215, 0), (0, 40, 255))
screen.blit(text, (0, 0))
pygame.display.update()
bird.a += 1
clock.tick(15)
bird.jumpSpeed = 15
while keep_going:
for event in pygame.event.get():
if event.type == pygame.QUIT:
keep_going = False
bird.birdUpdate()
text1 = font.render("GAME OVER", False,
(255,0, 0))
screen.blit(text1, (100,100))
screen.blit(bird.birdSprites[bird.a % 3], bird.rect)
pygame.display.update()
clock.tick(15)
pygame.quit()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn