用户:晨翊查看:0 回复:2 评论:0 创建时间:2021-05-28T19:29:56
【作品展示】

【作品介绍】
我花了一周的时间做了经典游戏像素鸟
【作品源代码】
import pygame
import random
import os
W, H = 288, 512
FPS = 30
pygame.init()
SCREEN = pygame.display.set_mode((W, H))
pygame.display.set_caption('flapp_bird')
CLOCK = pygame.time.Clock()
IMAGES = {}
for image in os.listdir('photo'):
name, extension = os.path.splitext(image)
path = os.path.join('photo', image)
IMAGES[name] = pygame.image.load(path)
FLOOR_Y = H - IMAGES['floor'].get_height()
def main():
while True:
IMAGES['bgpic'] = IMAGES[random.choice(['day', 'night'])]
color = random.choice(['red', 'yellow', 'blue'])
IMAGES['birds'] = [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
bird_x = W * 0.2
bird_y = (H - IMAGES['birds'][0].get_height())/2
bird_y_vel = 1
bird_y_range = [bird_y - 8, bird_y + 8]
idx = 0
frames = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1]
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
bird_y += bird_y_vel
if bird_y < bird_y_range[0] or bird_y > bird_y_range[1]:
bird_y_vel *= -1
idx += 1
idx %= len(frames)
floor_x -= 4
if floor_x <= - floor_gap:
floor_x = 0
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['birds'][frames[idx]], (bird_x, bird_y))
pygame.display.update()
CLOCK.tick(FPS)
def game_window():
floor_gap = IMAGES['floor'].get_width() - W
floor_x = 0
bird = Bird(W * 0.3, H * 0.4)
distance = 150
n_pairs = 4
pipe_gap = 100
pipe_group = pygame.sprite.Group()
for i in range(n_pairs):
pipe_y = random.randint(int(H*0.5), int(H*0.7))
pipe_up = Pipes(W + i * distance, pipe_y, True)
pipe_down = Pipes(W + i * distance, pipe_y - pipe_gap, False)
pipe_group.add(pipe_up)
pipe_group.add(pipe_down)
while True:
flap = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
flap = True
floor_x -= 5
if floor_x <= - floor_gap:
floor_x = 0
bird.update(flap)
first_pipe_up = pipe_group.sprites()[0]
first_pipe_down = pipe_group.sprites()[1]
if first_pipe_up.rect.right < 0:
pipe_y = random.randint(int(H * 0.3), int(H * 0.7))
new_pipe_up = Pipes(first_pipe_up.rect.x + n_pairs * distance, pipe_y, True)
new_pipe_down = Pipes(first_pipe_up.rect.x + n_pairs * distance, pipe_y - pipe_gap, False)
pipe_group.add(new_pipe_up)
pipe_group.add(new_pipe_down)
first_pipe_up.kill()
first_pipe_down.kill()
enemy = pygame.sprite.spritecollideany(bird, pipe_group)
if enemy:
# result = {'bird': bird}
return bird
pipe_group.update()
if bird.rect.y > FLOOR_Y or bird.rect.y < 0:
# result = {'bird': bird}
return bird
SCREEN.blit(IMAGES['bgpic'], (0, 0))
pipe_group.draw(SCREEN)
SCREEN.blit(IMAGES['floor'], (floor_x, FLOOR_Y))
SCREEN.blit(bird.image, bird.rect)
pygame.display.update()
CLOCK.tick(FPS)
def end_window(bird):
if not bird:
print('error')
return
gameover_x = (W - IMAGES['gameover'].get_width())/2
gameover_y = (FLOOR_Y - IMAGES['gameover'].get_height())/2
#bird = result['bird']
while True:
bird.go_die()
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
return
SCREEN.blit(IMAGES['bgpic'], (0, 0))
SCREEN.blit(IMAGES['gameover'], (gameover_x, gameover_y))
SCREEN.blit(IMAGES['floor'], (0, FLOOR_Y))
SCREEN.blit(bird.image, bird.rect)
pygame.display.update()
CLOCK.tick(FPS)
class Bird:
def __init__(self, x, y):
self.frames = [0] * 5 + [1] * 5 + [2] * 5 + [1] * 5
self.idx = 0
self.images = IMAGES['birds']
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.rotate = 45
self.max_y_vel = 10
self.gravity = 1
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 = IMAGES['birds'][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 = -98
self.image = self.images[self.frames[self.idx]]
self.image = pygame.transform.rotate(self.image, self.rotate)
class Pipes(pygame.sprite.Sprite):
def __init__(self, x, y, upwards=True):
pygame.sprite.Sprite.__init__(self)
if upwards:
self.image = IMAGES['pipes'][0]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.top = y
else:
self.image = IMAGES['pipes'][1]
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.bottom = y
self.x_vel = -5
def update(self):
self.rect.x += self.x_vel
main()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn