用户:A亿瓶可乐🥤查看:0 回复:1 评论:0 创建时间:2023-03-13T12:32:43
【作品展示】

【作品介绍】
Flappy Brid半成品
【作品源代码】
import pygame
import random
import time
import os
W, H = 288, 512
FPS = 30
pygame.init()
SCREEN = pygame.display.set_mode((W, H))
pygame.display.set_caption('Flappy Brid')
CLOCK = pygame.time.Clock()
IMAGES = {}
for image in os.listdir('assets/sprites'):
name, extension = os.path.splitext(image)
path = os.path.join('assets/sprites', image)
IMAGES[name] = pygame.image.load(path)
print(IMAGES)
FLOOR_Y = H - IMAGES['floor'].get_height()
AUDIO = {}
for audio in os.listdir('assets/audio'):
name, extension = os.path.splitext(audio)
path = os.path.join('assets/audio', audio)
AUDIO[name] = pygame.mixer.Sound(path)
def main():
while True:
AUDIO['start'].play()
IMAGES['bgpic'] = IMAGES[random.choice(['day', 'night'])]
color = random.choice(['red', 'yellow', 'blue'])
IMAGES['brids'] = [IMAGES[color+'-up'],
IMAGES[color+'-mid'], IMAGES[color+'-down']]
pipe = IMAGES[random.choice(['green-pipe', 'red-pipe'])]
IMAGES['pipes'] = [pipe, pygame.transform.flip(pipe, False, True)]
menu_window()
result = game_window()
end_window(result)
def menu_window():
floor_gap = IMAGES['floor'].get_width() - W
floor_x = 0
guide_x = (W - IMAGES['guide'].get_width())/2
guide_y = (FLOOR_Y - IMAGES['guide'].get_height())/2
brid_x = W * 0.2
brid_y = (H - IMAGES['brids'][0].get_height())/2
brid_y_vel = 1
brid_y_range = [brid_y - 8, brid_y + 8]
idx = 0
repeat = 10
frames = [0] * repeat + [1] * repeat + [2] * repeat + [1] * repeat
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
return
floor_x -= 4
if floor_x <= -floor_gap:
floor_x = 0
brid_y += brid_y_vel
if brid_y < brid_y_range[0] or brid_y > brid_y_range[1]:
brid_y_vel *= -1
idx += 1
idx %= len(frames)
SCREEN.blit(IMAGES['bgpic'], (0, 0))
SCREEN.blit(IMAGES['floor'], (floor_x, FLOOR_Y))
SCREEN.blit(IMAGES['guide'], (guide_x, guide_y))
SCREEN.blit(IMAGES['brids'][frames[idx]], (brid_x, brid_y))
pygame.display.update()
CLOCK.tick(FPS)
def game_window():
AUDIO['flap'].play()
floor_gap = IMAGES['floor'].get_width() - W
floor_x = 0
brid = Brid(W * 0.2, H * 0.4)
pipe = Pipe(W, H * 0.5)
while True:
flap = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
flap = True
AUDIO['flap'].play()
floor_x -= 4
if floor_x <= -floor_gap:
floor_x = 0
brid.update(flap)
pipe.update()
if brid.rect.y > FLOOR_Y or brid.rect.y < 0:
AUDIO['hit'].play()
AUDIO['die'].play()
result = {'brid': brid}
return result
SCREEN.blit(IMAGES['bgpic'], (0, 0))
SCREEN.blit(pipe.image, pipe.rect)
SCREEN.blit(IMAGES['floor'], (floor_x, FLOOR_Y))
SCREEN.blit(brid.image, brid.rect)
pygame.display.update()
CLOCK.tick(FPS)
def end_window(result):
gameover_x = (W - IMAGES['gameover'].get_width())/2
gameover_y = (FLOOR_Y - IMAGES['gameover'].get_height())/2
brid = result['brid']
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
return
brid.go_die()
SCREEN.blit(IMAGES['bgpic'], (0, 0))
SCREEN.blit(IMAGES['floor'], (0, FLOOR_Y))
SCREEN.blit(IMAGES['gameover'], (gameover_x, gameover_y))
SCREEN.blit(brid.image, brid.rect)
pygame.display.update()
CLOCK.tick(FPS)
class Brid:
def __init__(self, x, y):
self.frames = [0] * 5 + [1] * 5 + [2] * 5 + [1] * 5
self.idx = 0
self.images = IMAGES['brids']
self.image = self.images[self.frames[self.idx]]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.y_vel = -10
self.max_y_vel = 10
self.gravity = 1
self.rotate = 45
self.max_rotate = -20
self.rotate_vel = -3
self.y_vel_after_flap = -10
self.rotate_after_flap = 45
def update(self, flap=False):
if flap:
self.y_vel = self.y_vel_after_flap
self.rotate = self.rotate_after_flap
self.y_vel = min(self.y_vel + self.gravity, self.max_y_vel)
self.rect.y += self.y_vel
self.rotate = max(self.rotate+self.rotate_vel, self.max_rotate)
self.idx += 1
self.idx %= len(self.frames)
self.image = self.images[self.frames[self.idx]]
self.image = pygame.transform.rotate(self.image, self.rotate)
def go_die(self):
if self.rect.y < FLOOR_Y:
self.rect.y += self.max_y_vel
self.rotate = -90
self.image = self.images[self.frames[self.idx]]
self.image = pygame.transform.rotate(self.image, self.rotate)
class Pipe:
def __init__(self, x, y):
self.image = IMAGES['pipes'][0]
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.x_vel = -4
def update(self):
self.rect.x += self.x_vel
main()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn