|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 pyzyd 于 2025-5-24 17:36 编辑
pygame制作的小游戏,顺便发点鱼币
- import pygame
- from pygame.locals import *
- from pygame.sprite import Sprite
- import sys
- import traceback
- from random import randint
- class Bird(Sprite):
- def __init__(self, bg_size):
- Sprite.__init__(self)
- # 初始化小鸟的状态和属性
- # 定义鸟
- self.image = pygame.image.load("./res/bird0_0.png").convert_alpha()
- self.image2 = pygame.image.load("./res/bird0_2.png").convert_alpha()
- self.dead_image = pygame.image.load("./res/dead.png").convert_alpha()
- # 初始化鸟的运动方向
- self.is_up = False
- self.width, self.height = bg_size
- self.rect = self.image.get_rect() # 获取当前图像的矩形区域
- self.mask = pygame.mask.from_surface(self.image)
- # 初始速度
- self.velocity = 0
- # 加速度
- self.acceleration = 2
- self.rect.left = 0
- self.rect.centery = self.height//2
- def move(self):
- """控制小鸟上移"""
- self.is_up = True
- self.velocity = -4 # 小鸟向上飞行时的速度
- self.acceleration = 0 # 重置加速度
- def drop(self):
- """小鸟下落"""
- if self.acceleration != 0:
- self.is_up = False
- self.acceleration = 1 # 恢复加速度
- self.velocity += self.acceleration # 更新速度
- def reset(self):
- self.rect.left = 0
- self.rect.centery = self.height//2
- self.is_up = False
- self.velocity = 0
- class Pipeline(Sprite):
- index = 0
- def __init__(self, bg_size, pipetype):
- Sprite.__init__(self)
- self.width, self.height = bg_size
- self.down_images =[pygame.image.load("./res/pipe_down.png").convert_alpha(),
- pygame.image.load("./res/pipe2_down.png").convert_alpha()]
- self.up_images = [pygame.image.load("./res/pipe_up.png").convert_alpha(),
- pygame.image.load("./res/pipe2_up.png").convert_alpha()]
- self.index = Pipeline.index
- self.pipetype = pipetype
- if pipetype == 'down':
- self.image = self.down_images[self.index]
- self.rect = self.image.get_rect()
- self.mask = pygame.mask.from_surface(self.image)
- self.rect.right = self.width
- self.rect.bottom = randint(self.rect.height // 3 + 10, self.rect.height)
- elif pipetype == 'up':
- self.image = self.up_images[self.index]
- self.rect = self.image.get_rect()
- self.mask = pygame.mask.from_surface(self.image)
- self.rect.right = self.width
- self.rect.top = self.height
- Pipeline.index = 1 - Pipeline.index
- self.velocity = -3
- def reset(self,bg_size):
- if self.pipetype == 'down':
- self.rect.right= bg_size[0]
- self.rect.bottom = randint(self.rect.height // 3, self.rect.height)
- elif self.pipetype == 'up':
- self.rect.right = bg_size[0]
- self.rect.top = bg_size[1]
- def move(self):
- self.rect.left += self.velocity
- class Score():
- def __init__(self, score_type=1):
- self.num_images1 = [pygame.image.load("./res/font_048.png").convert_alpha(), pygame.image.load("./res/font_049.png").convert_alpha(),
- pygame.image.load("./res/font_050.png").convert_alpha(),pygame.image.load("./res/font_051.png").convert_alpha(),
- pygame.image.load("./res/font_052.png").convert_alpha(), pygame.image.load("./res/font_053.png").convert_alpha(),
- pygame.image.load("./res/font_054.png").convert_alpha(), pygame.image.load("./res/font_055.png").convert_alpha(),
- pygame.image.load("./res/font_056.png").convert_alpha(), pygame.image.load("./res/font_057.png").convert_alpha()]
- self.num_images2 = [pygame.image.load("./res/number_context_00.png").convert_alpha(), pygame.image.load("./res/number_context_01.png").convert_alpha(),
- pygame.image.load("./res/number_context_02.png").convert_alpha(), pygame.image.load("./res/number_context_03.png").convert_alpha(),
- pygame.image.load("./res/number_context_04.png").convert_alpha(), pygame.image.load("./res/number_context_05.png").convert_alpha(),
- pygame.image.load("./res/number_context_06.png").convert_alpha(), pygame.image.load("./res/number_context_07.png").convert_alpha(),
- pygame.image.load("./res/number_context_08.png").convert_alpha(), pygame.image.load("./res/number_context_09.png").convert_alpha(),]
- self.num_images3 = [pygame.image.load("./res/number_score_00.png").convert_alpha(), pygame.image.load("./res/number_score_01.png").convert_alpha(),
- pygame.image.load("./res/number_score_02.png").convert_alpha(), pygame.image.load("./res/number_score_03.png").convert_alpha(),
- pygame.image.load("./res/number_score_04.png").convert_alpha(), pygame.image.load("./res/number_score_05.png").convert_alpha(),
- pygame.image.load("./res/number_score_06.png").convert_alpha(), pygame.image.load("./res/number_score_07.png").convert_alpha(),
- pygame.image.load("./res/number_score_08.png").convert_alpha(), pygame.image.load("./res/number_score_09.png").convert_alpha(), ]
- if score_type == 1:
- self.num_images = self.num_images1
- elif score_type == 2:
- self.num_images = self.num_images2
- elif score_type == 3:
- self.num_images = self.num_images3
- self.zero_image = self.num_images[0]
- self.one_image = self.num_images[1]
- self.two_image = self.num_images[2]
- self.three_image = self.num_images[3]
- self.four_image = self.num_images[4]
- self.five_image = self.num_images[5]
- self.six_image = self.num_images[6]
- self.seven_image = self.num_images[7]
- self.eight_image = self.num_images[8]
- self.nine_image = self.num_images[9]
- self.rect = self.zero_image.get_rect()
- def draw_score(self, screen, num_str, pos=(10, 10)):
- self.rect.left, self.rect.top = pos
- for s in num_str:
- if s == '0':
- screen.blit(self.zero_image, self.rect)
- elif s == '1':
- screen.blit(self.one_image, self.rect)
- elif s == '2':
- screen.blit(self.two_image, self.rect)
- elif s == '3':
- screen.blit(self.three_image, self.rect)
- elif s == '4':
- screen.blit(self.four_image, self.rect)
- elif s == '5':
- screen.blit(self.five_image, self.rect)
- elif s == '6':
- screen.blit(self.six_image, self.rect)
- elif s == '7':
- screen.blit(self.seven_image, self.rect)
- elif s == '8':
- screen.blit(self.eight_image, self.rect)
- elif s == '9':
- screen.blit(self.nine_image, self.rect)
- self.rect.left += 5 * self.rect.width // 4
- class FlappyBird():
- def __init__(self):
- pygame.init()
- # 设置屏幕
- self.bg_size = (288, 512)
- self.screen = pygame.display.set_mode(self.bg_size)
- pygame.display.set_caption("Flappy Bird")
- self.bg_day_image = pygame.image.load(r'res\bg_day.png').convert_alpha()
- self.bg_night_image = pygame.image.load(r'res\bg_night.png').convert_alpha()
- # 陆地
- self.land_image = pygame.image.load(r'res\land.png').convert_alpha()
- self.land_rect = self.land_image.get_rect()
- self.land_rect.left = 0
- self.land_rect.bottom = self.bg_size[1]
- # 加载封面图像
- self.title_image = pygame.image.load(r'res\title.png').convert_alpha()
- self.title_rect = self.title_image.get_rect()
- self.tutorial_image = pygame.image.load(r'res\tutorial.png').convert_alpha()
- self.tutorial_rect = self.tutorial_image.get_rect()
- self.text_ready_image = pygame.image.load(r'res\text_ready.png').convert_alpha()
- self.text_ready_rect = self.text_ready_image.get_rect()
- self.button_play_image = pygame.image.load(r'res\button_play.png').convert_alpha()
- self.button_play_rect = self.button_play_image.get_rect()
- self.game_over_image = pygame.image.load(r'res\text_game_over.png').convert_alpha()
- self.game_over_rect = self.game_over_image.get_rect()
- self.is_over = False
- self.new_image = pygame.image.load(r'res\new.png').convert_alpha()
- self.new_rect = self.new_image.get_rect()
- # 分数
- self.score = Score()
- self.score_num = 0
- self.score_str = str(self.score_num)
- self.score2 = Score(2)
- self.score3 = Score(3)
- #
- self.score_panel_image = pygame.image.load(r'res\score_panel.png').convert_alpha()
- self.score_panel_rect = self.score_panel_image.get_rect()
- self.medal_0_image = pygame.image.load(r'res\medals_0.png').convert_alpha()
- self.medal_1_image = pygame.image.load(r'res\medals_1.png').convert_alpha()
- self.medal_2_image = pygame.image.load(r'res\medals_2.png').convert_alpha()
- self.medal_3_image = pygame.image.load(r'res\medals_3.png').convert_alpha()
- self.medal_rect = self.medal_0_image.get_rect()
- # 用于阻止重复打开记录文件
- self.recorded = False
- # 一天计时器
- self.DAYORNIGHT = USEREVENT + 1
- self.is_day = True
- # 在游戏初始化时设置计时器
- pygame.time.set_timer(self.DAYORNIGHT, 10 * 1000)
- # 初始化小鸟
- self.bird = Bird(self.bg_size)
- # 初始化管道
- self.pipe_interval = 2*self.bg_size[0] // 3
- self.down1_size = self.bg_size
- self.pipe_down1 = Pipeline(self.down1_size, pipetype='down')
- self.down2_size = (self.bg_size[0] + self.pipe_interval, self.bg_size[1])
- self.pipe_down2 = Pipeline(self.down2_size, pipetype='down')
- self.up1_size = (self.bg_size[0], self.pipe_down1.rect.bottom + 150)
- self.pipe_up1 = Pipeline(self.up1_size, pipetype='up')
- self.up2_size = (self.bg_size[0] + self.pipe_interval,self.pipe_down2.rect.bottom + 150)
- self.pipe_up2 = Pipeline(self.up2_size, pipetype='up')
- # 绘制背景
- self.eclipse()
- # 游戏时钟
- self.clock = pygame.time.Clock()
- # 设置延迟
- self.delay = 100
- # 是否开始游戏
- self.is_start = False
- def eclipse(self):
- """绘制白天黑夜交替"""
- if self.is_day:
- self.screen.blit(self.bg_day_image, (0, 0))
- else:
- self.screen.blit(self.bg_night_image, (0, 0))
- self.screen.blit(self.land_image, self.land_rect)
- def reset_pipes(self):
- self.pipe_interval = 2 * self.bg_size[0] // 3
- self.down1_size = self.bg_size
- self.pipe_down1 = Pipeline(self.down1_size, pipetype='down')
- self.down2_size = (self.bg_size[0] + self.pipe_interval, self.bg_size[1])
- self.pipe_down2 = Pipeline(self.down2_size, pipetype='down')
- self.up1_size = (self.bg_size[0], self.pipe_down1.rect.bottom + 150)
- self.pipe_up1 = Pipeline(self.up1_size, pipetype='up')
- self.up2_size = (self.bg_size[0] + self.pipe_interval, self.pipe_down2.rect.bottom + 150)
- self.pipe_up2 = Pipeline(self.up2_size, pipetype='up')
- def reset(self):
- self.is_over = False
- self.is_start = False
- self.bird.reset()
- self.reset_pipes()
- self.score_num = 0
- self.score_str = str(self.score_num)
- pygame.time.set_timer(self.DAYORNIGHT, 10 * 1000)
- def draw_over(self):
- """绘制结束界面"""
- best_score_str = ''
- if not self.recorded:
- self.recorded = True
- # 读取历史最高分
- try:
- with open("record.txt", 'r') as f:
- best_score_str = f.read()
- except:
- with open("record.txt", 'w') as f:
- best_score_str = self.score_str
- f.write(self.score_str)
- else:
- # 如果玩家得分高于历史最高得分,则存档
- if self.score_str > best_score_str:
- with open("record.txt", 'w') as f:
- f.write(self.score_str)
- if self.score_num == 0:
- self.medal_image = self.medal_0_image
- elif self.score_num < 20:
- self.medal_image = self.medal_1_image
- elif self.score_num < 50:
- self.medal_image = self.medal_2_image
- else:
- self.medal_image = self.medal_3_image
- self.game_over_rect.center = (self.bg_size[0]//2, self.bg_size[1]//2)
- self.score_panel_rect.centerx = self.game_over_rect.centerx
- self.score_panel_rect.bottom = self.game_over_rect.top - 20
- self.new_rect.centerx = self.game_over_rect.centerx
- self.new_rect.top = self.game_over_rect.bottom + 20
- self.medal_rect.centery = 67
- self.medal_rect.left = 30
- self.screen.blit(self.new_image, self.new_rect)
- self.screen.blit(self.score_panel_image, self.score_panel_rect)
- self.score_panel_image.blit(self.medal_image, self.medal_rect)
- print(self.score_str)
- self.score2.draw_score(self.score_panel_image, self.score_str, (self.score_panel_rect.width - 60, self.score_panel_rect.height - 90))
- self.score3.draw_score(self.score_panel_image, best_score_str, (self.score_panel_rect.width - 60, self.score_panel_rect.height - 40))
- self.screen.blit(self.game_over_image, self.game_over_rect)
- self.screen.blit(self.button_play_image, self.button_play_rect)
- def draw_pipe(self):
- self.up_down_interval = randint(90,150)
- if self.pipe_down1.rect.right < 0:
- self.down1_size = (self.pipe_down2.rect.right + self.pipe_interval, self.bg_size[1])
- self.pipe_down1.reset(self.down1_size)
- elif self.pipe_up1.rect.right < 0:
- self.up1_size = (self.pipe_down2.rect.right + self.pipe_interval, self.pipe_down1.rect.bottom + self.up_down_interval)
- self.pipe_up1.reset(self.up1_size)
- self.score_num += 1
- elif self.pipe_down2.rect.right < 0:
- self.down2_size = (self.pipe_down1.rect.right + self.pipe_interval, self.bg_size[1])
- self.pipe_down2.reset(self.down2_size)
- elif self.pipe_up2.rect.right < 0:
- self.up2_size = (self.pipe_down1.rect.right + self.pipe_interval, self.pipe_down2.rect.bottom + self.up_down_interval)
- self.pipe_up2.reset(self.up2_size)
- self.score_num += 1
- self.screen.blit(self.pipe_down1.image, self.pipe_down1.rect)
- self.screen.blit(self.pipe_up1.image, self.pipe_up1.rect)
- self.screen.blit(self.pipe_down2.image, self.pipe_down2.rect)
- self.screen.blit(self.pipe_up2.image, self.pipe_up2.rect)
- def draw_bird(self):
- if self.bird.rect.bottom >= self.screen.get_rect().bottom:
- self.bird.rect.bottom = self.screen.get_rect().bottom
- # 结束游戏
- self.is_over = True
- else:
- self.bird.rect.centery += self.bird.velocity # 根据速度更新位置
- if not self.is_over:
- if self.bird.is_up:
- self.screen.blit(self.bird.image, self.bird.rect)
- else:
- self.screen.blit(self.bird.image2, self.bird.rect)
- else:
- self.screen.blit(self.bird.dead_image, self.bird.rect)
- def _update(self):
- """更新屏幕"""
- self.eclipse()
- self.draw_pipe()
- self.draw_bird()
- self.score_str = str(self.score_num)
- self.score.draw_score(self.screen, self.score_str)
- if self.is_over:
- self.draw_over()
- pygame.time.set_timer(self.DAYORNIGHT, 0)
- pygame.display.update()
- def cover(self):
- """开始界面"""
- self.screen.blit(self.bg_night_image, (0,0))
- self.title_rect.centerx = self.screen.get_rect().centerx
- self.title_rect.top = 50
- self.screen.blit(self.title_image, self.title_rect)
- self.tutorial_rect.centerx = self.screen.get_rect().centerx
- self.tutorial_rect.top = self.title_rect.bottom + 20
- self.screen.blit(self.tutorial_image, self.tutorial_rect)
- self.text_ready_rect.centerx = self.screen.get_rect().centerx
- self.text_ready_rect.top = self.tutorial_rect.bottom + 20
- self.screen.blit(self.text_ready_image, self.text_ready_rect)
- self.button_play_rect.centerx = self.screen.get_rect().centerx
- self.button_play_rect.top = self.text_ready_rect.bottom + 20
- self.screen.blit(self.button_play_image, self.button_play_rect)
- pygame.display.update()
- def play(self):
- """游戏开始"""
- self.eclipse()
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- sys.exit()
- # 只有在接收到计时器事件时,才切换白天黑夜
- elif event.type == self.DAYORNIGHT:
- self.is_day = not self.is_day
- # 鼠标点击
- elif event.type == pygame.MOUSEBUTTONDOWN:
- if pygame.mouse.get_pressed()[0] == 1:
- pos = pygame.mouse.get_pos()
- if self.button_play_rect.collidepoint(pos):
- if not self.is_start:
- self.is_start = True
- elif self.is_over:
- self.is_over = False
- self.reset()
- self.bird.move()
- if pygame.sprite.collide_mask(self.bird, self.pipe_down1) or pygame.sprite.collide_mask(self.bird, self.pipe_down2) or\
- pygame.sprite.collide_mask(self.bird, self.pipe_up1) or pygame.sprite.collide_mask(self.bird, self.pipe_up2):
- self.is_over = True
- if not self.is_start:
- self.cover()
- else:
- if self.delay % 6 == 0:
- self.bird.drop()
- self.pipe_down1.move()
- self.pipe_up1.move()
- self.pipe_down2.move()
- self.pipe_up2.move()
- if self.is_over:
- self.pipe_down1.velocity = 0
- self.pipe_down2.velocity = 0
- self.pipe_up1.velocity = 0
- self.pipe_up2.velocity = 0
- self._update()
- self.clock.tick(60)
- self.delay -= 1
- if self.delay == 0:
- self.delay = 100
- if __name__ == '__main__':
- flappybird = FlappyBird()
- try:
- flappybird.play()
- except SystemExit:
- pass
- except:
- traceback.print_exc()
- pygame.quit()
复制代码
自取即可
FlappedBird.zip
(90.93 KB, 下载次数: 1)
上面那个有个小bug,
FlappedBird.zip
(90.93 KB, 下载次数: 0)
|
评分
-
参与人数 3 | 荣誉 +5 |
鱼币 +5 |
贡献 +5 |
C币 +5 |
收起
理由
|
小甲鱼
| + 2 |
+ 2 |
+ 2 |
+ 2 |
鱼C有你更精彩^_^ |
不二如是
| |
|
|
+ 3 |
鱼C有你更精彩^_^ |
小甲鱼的二师兄
| + 3 |
+ 3 |
+ 3 |
|
鱼C有你更精彩^_^ |
查看全部评分
|