鱼C论坛

 找回密码
 立即注册
查看: 99|回复: 14

[作品展示] FlappedBird 飞翔的小鸟源码及素材

[复制链接]
回帖奖励 88 鱼币 回复本帖可获得 8 鱼币奖励! 每人限 1 次
发表于 昨天 10:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 pyzyd 于 2025-5-24 17:36 编辑

pygame制作的小游戏,顺便发点鱼币
flappedbird.gif

  1. import pygame
  2. from pygame.locals import *
  3. from pygame.sprite import Sprite
  4. import sys
  5. import traceback
  6. from random import randint

  7. class Bird(Sprite):
  8.     def __init__(self, bg_size):
  9.         Sprite.__init__(self)
  10.         # 初始化小鸟的状态和属性
  11.         # 定义鸟
  12.         self.image = pygame.image.load("./res/bird0_0.png").convert_alpha()
  13.         self.image2 = pygame.image.load("./res/bird0_2.png").convert_alpha()
  14.         self.dead_image =  pygame.image.load("./res/dead.png").convert_alpha()
  15.         # 初始化鸟的运动方向
  16.         self.is_up = False

  17.         self.width, self.height = bg_size

  18.         self.rect = self.image.get_rect()  # 获取当前图像的矩形区域
  19.         self.mask = pygame.mask.from_surface(self.image)
  20.         # 初始速度
  21.         self.velocity = 0
  22.         # 加速度
  23.         self.acceleration = 2

  24.         self.rect.left = 0
  25.         self.rect.centery = self.height//2



  26.     def move(self):
  27.         """控制小鸟上移"""
  28.         self.is_up = True
  29.         self.velocity = -4  # 小鸟向上飞行时的速度
  30.         self.acceleration = 0  # 重置加速度


  31.     def drop(self):
  32.         """小鸟下落"""
  33.         if self.acceleration != 0:
  34.             self.is_up = False
  35.         self.acceleration = 1  # 恢复加速度
  36.         self.velocity += self.acceleration  # 更新速度


  37.     def reset(self):
  38.         self.rect.left = 0
  39.         self.rect.centery = self.height//2
  40.         self.is_up = False
  41.         self.velocity = 0

  42. class Pipeline(Sprite):
  43.     index = 0
  44.     def __init__(self, bg_size, pipetype):
  45.         Sprite.__init__(self)
  46.         self.width, self.height = bg_size
  47.         self.down_images =[pygame.image.load("./res/pipe_down.png").convert_alpha(),
  48.                            pygame.image.load("./res/pipe2_down.png").convert_alpha()]

  49.         self.up_images = [pygame.image.load("./res/pipe_up.png").convert_alpha(),
  50.                        pygame.image.load("./res/pipe2_up.png").convert_alpha()]
  51.         self.index = Pipeline.index
  52.         self.pipetype = pipetype
  53.         if pipetype == 'down':
  54.             self.image = self.down_images[self.index]
  55.             self.rect = self.image.get_rect()
  56.             self.mask = pygame.mask.from_surface(self.image)
  57.             self.rect.right = self.width
  58.             self.rect.bottom = randint(self.rect.height // 3 + 10, self.rect.height)
  59.         elif pipetype == 'up':
  60.             self.image = self.up_images[self.index]
  61.             self.rect = self.image.get_rect()
  62.             self.mask = pygame.mask.from_surface(self.image)
  63.             self.rect.right = self.width
  64.             self.rect.top = self.height

  65.         Pipeline.index = 1 - Pipeline.index
  66.         self.velocity = -3

  67.     def reset(self,bg_size):
  68.         if self.pipetype == 'down':
  69.             self.rect.right= bg_size[0]
  70.             self.rect.bottom = randint(self.rect.height // 3, self.rect.height)
  71.         elif self.pipetype == 'up':
  72.             self.rect.right = bg_size[0]
  73.             self.rect.top = bg_size[1]

  74.     def move(self):
  75.         self.rect.left += self.velocity

  76. class Score():
  77.     def __init__(self, score_type=1):

  78.         self.num_images1 = [pygame.image.load("./res/font_048.png").convert_alpha(), pygame.image.load("./res/font_049.png").convert_alpha(),
  79.                             pygame.image.load("./res/font_050.png").convert_alpha(),pygame.image.load("./res/font_051.png").convert_alpha(),
  80.                             pygame.image.load("./res/font_052.png").convert_alpha(), pygame.image.load("./res/font_053.png").convert_alpha(),
  81.                             pygame.image.load("./res/font_054.png").convert_alpha(), pygame.image.load("./res/font_055.png").convert_alpha(),
  82.                             pygame.image.load("./res/font_056.png").convert_alpha(), pygame.image.load("./res/font_057.png").convert_alpha()]
  83.         self.num_images2 = [pygame.image.load("./res/number_context_00.png").convert_alpha(), pygame.image.load("./res/number_context_01.png").convert_alpha(),
  84.                             pygame.image.load("./res/number_context_02.png").convert_alpha(), pygame.image.load("./res/number_context_03.png").convert_alpha(),
  85.                             pygame.image.load("./res/number_context_04.png").convert_alpha(), pygame.image.load("./res/number_context_05.png").convert_alpha(),
  86.                             pygame.image.load("./res/number_context_06.png").convert_alpha(), pygame.image.load("./res/number_context_07.png").convert_alpha(),
  87.                             pygame.image.load("./res/number_context_08.png").convert_alpha(), pygame.image.load("./res/number_context_09.png").convert_alpha(),]
  88.         self.num_images3 = [pygame.image.load("./res/number_score_00.png").convert_alpha(), pygame.image.load("./res/number_score_01.png").convert_alpha(),
  89.                             pygame.image.load("./res/number_score_02.png").convert_alpha(), pygame.image.load("./res/number_score_03.png").convert_alpha(),
  90.                             pygame.image.load("./res/number_score_04.png").convert_alpha(), pygame.image.load("./res/number_score_05.png").convert_alpha(),
  91.                             pygame.image.load("./res/number_score_06.png").convert_alpha(), pygame.image.load("./res/number_score_07.png").convert_alpha(),
  92.                             pygame.image.load("./res/number_score_08.png").convert_alpha(), pygame.image.load("./res/number_score_09.png").convert_alpha(), ]
  93.         if score_type == 1:
  94.             self.num_images = self.num_images1
  95.         elif score_type == 2:
  96.             self.num_images = self.num_images2
  97.         elif score_type == 3:
  98.             self.num_images = self.num_images3
  99.         self.zero_image = self.num_images[0]
  100.         self.one_image = self.num_images[1]
  101.         self.two_image = self.num_images[2]
  102.         self.three_image = self.num_images[3]
  103.         self.four_image = self.num_images[4]
  104.         self.five_image = self.num_images[5]
  105.         self.six_image = self.num_images[6]
  106.         self.seven_image = self.num_images[7]
  107.         self.eight_image = self.num_images[8]
  108.         self.nine_image = self.num_images[9]
  109.         self.rect = self.zero_image.get_rect()


  110.     def draw_score(self, screen, num_str, pos=(10, 10)):
  111.         self.rect.left, self.rect.top = pos
  112.         for s in num_str:
  113.             if s == '0':
  114.                 screen.blit(self.zero_image, self.rect)
  115.             elif s == '1':
  116.                 screen.blit(self.one_image, self.rect)
  117.             elif s == '2':
  118.                 screen.blit(self.two_image, self.rect)
  119.             elif s == '3':
  120.                 screen.blit(self.three_image, self.rect)
  121.             elif s == '4':
  122.                 screen.blit(self.four_image, self.rect)
  123.             elif s == '5':
  124.                 screen.blit(self.five_image, self.rect)
  125.             elif s == '6':
  126.                 screen.blit(self.six_image, self.rect)
  127.             elif s == '7':
  128.                 screen.blit(self.seven_image, self.rect)
  129.             elif s == '8':
  130.                 screen.blit(self.eight_image, self.rect)
  131.             elif s == '9':
  132.                 screen.blit(self.nine_image, self.rect)
  133.             self.rect.left += 5 * self.rect.width // 4

  134. class FlappyBird():
  135.     def __init__(self):
  136.         pygame.init()

  137.         # 设置屏幕
  138.         self.bg_size = (288, 512)
  139.         self.screen = pygame.display.set_mode(self.bg_size)
  140.         pygame.display.set_caption("Flappy Bird")
  141.         self.bg_day_image = pygame.image.load(r'res\bg_day.png').convert_alpha()
  142.         self.bg_night_image = pygame.image.load(r'res\bg_night.png').convert_alpha()

  143.         # 陆地
  144.         self.land_image = pygame.image.load(r'res\land.png').convert_alpha()
  145.         self.land_rect = self.land_image.get_rect()
  146.         self.land_rect.left = 0
  147.         self.land_rect.bottom = self.bg_size[1]

  148.         # 加载封面图像
  149.         self.title_image = pygame.image.load(r'res\title.png').convert_alpha()
  150.         self.title_rect = self.title_image.get_rect()
  151.         self.tutorial_image = pygame.image.load(r'res\tutorial.png').convert_alpha()
  152.         self.tutorial_rect = self.tutorial_image.get_rect()
  153.         self.text_ready_image = pygame.image.load(r'res\text_ready.png').convert_alpha()
  154.         self.text_ready_rect = self.text_ready_image.get_rect()
  155.         self.button_play_image = pygame.image.load(r'res\button_play.png').convert_alpha()
  156.         self.button_play_rect = self.button_play_image.get_rect()

  157.         self.game_over_image = pygame.image.load(r'res\text_game_over.png').convert_alpha()
  158.         self.game_over_rect = self.game_over_image.get_rect()
  159.         self.is_over = False

  160.         self.new_image = pygame.image.load(r'res\new.png').convert_alpha()
  161.         self.new_rect = self.new_image.get_rect()

  162.         # 分数
  163.         self.score = Score()
  164.         self.score_num = 0
  165.         self.score_str = str(self.score_num)
  166.         self.score2 = Score(2)
  167.         self.score3 = Score(3)

  168.         #
  169.         self.score_panel_image = pygame.image.load(r'res\score_panel.png').convert_alpha()
  170.         self.score_panel_rect = self.score_panel_image.get_rect()

  171.         self.medal_0_image = pygame.image.load(r'res\medals_0.png').convert_alpha()
  172.         self.medal_1_image = pygame.image.load(r'res\medals_1.png').convert_alpha()
  173.         self.medal_2_image = pygame.image.load(r'res\medals_2.png').convert_alpha()
  174.         self.medal_3_image = pygame.image.load(r'res\medals_3.png').convert_alpha()
  175.         self.medal_rect = self.medal_0_image.get_rect()

  176.         # 用于阻止重复打开记录文件
  177.         self.recorded = False

  178.         # 一天计时器
  179.         self.DAYORNIGHT = USEREVENT + 1
  180.         self.is_day = True

  181.         # 在游戏初始化时设置计时器
  182.         pygame.time.set_timer(self.DAYORNIGHT, 10 * 1000)

  183.         # 初始化小鸟
  184.         self.bird = Bird(self.bg_size)

  185.         # 初始化管道
  186.         self.pipe_interval = 2*self.bg_size[0] // 3
  187.         self.down1_size = self.bg_size
  188.         self.pipe_down1 = Pipeline(self.down1_size, pipetype='down')
  189.         self.down2_size = (self.bg_size[0] + self.pipe_interval, self.bg_size[1])
  190.         self.pipe_down2 = Pipeline(self.down2_size, pipetype='down')
  191.         self.up1_size = (self.bg_size[0], self.pipe_down1.rect.bottom + 150)
  192.         self.pipe_up1 = Pipeline(self.up1_size, pipetype='up')
  193.         self.up2_size = (self.bg_size[0] + self.pipe_interval,self.pipe_down2.rect.bottom + 150)
  194.         self.pipe_up2 = Pipeline(self.up2_size, pipetype='up')

  195.         # 绘制背景
  196.         self.eclipse()

  197.         # 游戏时钟
  198.         self.clock = pygame.time.Clock()

  199.         # 设置延迟
  200.         self.delay = 100

  201.         # 是否开始游戏
  202.         self.is_start = False

  203.     def eclipse(self):
  204.         """绘制白天黑夜交替"""
  205.         if self.is_day:
  206.             self.screen.blit(self.bg_day_image, (0, 0))
  207.         else:
  208.             self.screen.blit(self.bg_night_image, (0, 0))
  209.         self.screen.blit(self.land_image, self.land_rect)

  210.     def reset_pipes(self):
  211.         self.pipe_interval = 2 * self.bg_size[0] // 3
  212.         self.down1_size = self.bg_size
  213.         self.pipe_down1 = Pipeline(self.down1_size, pipetype='down')
  214.         self.down2_size = (self.bg_size[0] + self.pipe_interval, self.bg_size[1])
  215.         self.pipe_down2 = Pipeline(self.down2_size, pipetype='down')
  216.         self.up1_size = (self.bg_size[0], self.pipe_down1.rect.bottom + 150)
  217.         self.pipe_up1 = Pipeline(self.up1_size, pipetype='up')
  218.         self.up2_size = (self.bg_size[0] + self.pipe_interval, self.pipe_down2.rect.bottom + 150)
  219.         self.pipe_up2 = Pipeline(self.up2_size, pipetype='up')

  220.     def reset(self):
  221.         self.is_over = False
  222.         self.is_start = False
  223.         self.bird.reset()
  224.         self.reset_pipes()
  225.         self.score_num = 0
  226.         self.score_str = str(self.score_num)
  227.         pygame.time.set_timer(self.DAYORNIGHT, 10 * 1000)

  228.     def draw_over(self):
  229.         """绘制结束界面"""
  230.         best_score_str = ''
  231.         if not self.recorded:
  232.             self.recorded = True
  233.             # 读取历史最高分
  234.             try:
  235.                 with open("record.txt", 'r') as f:
  236.                     best_score_str = f.read()
  237.             except:
  238.                 with open("record.txt", 'w') as f:
  239.                     best_score_str = self.score_str
  240.                     f.write(self.score_str)
  241.             else:
  242.                 # 如果玩家得分高于历史最高得分,则存档
  243.                 if self.score_str > best_score_str:
  244.                     with open("record.txt", 'w') as f:
  245.                         f.write(self.score_str)
  246.         if self.score_num == 0:
  247.             self.medal_image = self.medal_0_image
  248.         elif self.score_num < 20:
  249.             self.medal_image = self.medal_1_image
  250.         elif self.score_num < 50:
  251.             self.medal_image = self.medal_2_image
  252.         else:
  253.             self.medal_image = self.medal_3_image
  254.         self.game_over_rect.center = (self.bg_size[0]//2, self.bg_size[1]//2)
  255.         self.score_panel_rect.centerx = self.game_over_rect.centerx
  256.         self.score_panel_rect.bottom = self.game_over_rect.top - 20
  257.         self.new_rect.centerx = self.game_over_rect.centerx
  258.         self.new_rect.top = self.game_over_rect.bottom + 20
  259.         self.medal_rect.centery = 67
  260.         self.medal_rect.left = 30
  261.         self.screen.blit(self.new_image, self.new_rect)
  262.         self.screen.blit(self.score_panel_image, self.score_panel_rect)
  263.         self.score_panel_image.blit(self.medal_image, self.medal_rect)
  264.         print(self.score_str)
  265.         self.score2.draw_score(self.score_panel_image, self.score_str, (self.score_panel_rect.width - 60, self.score_panel_rect.height - 90))
  266.         self.score3.draw_score(self.score_panel_image, best_score_str, (self.score_panel_rect.width - 60, self.score_panel_rect.height - 40))
  267.         self.screen.blit(self.game_over_image, self.game_over_rect)
  268.         self.screen.blit(self.button_play_image, self.button_play_rect)

  269.     def draw_pipe(self):
  270.         self.up_down_interval = randint(90,150)
  271.         if self.pipe_down1.rect.right < 0:
  272.             self.down1_size = (self.pipe_down2.rect.right + self.pipe_interval, self.bg_size[1])
  273.             self.pipe_down1.reset(self.down1_size)
  274.         elif self.pipe_up1.rect.right < 0:
  275.             self.up1_size = (self.pipe_down2.rect.right + self.pipe_interval, self.pipe_down1.rect.bottom + self.up_down_interval)
  276.             self.pipe_up1.reset(self.up1_size)
  277.             self.score_num += 1
  278.         elif self.pipe_down2.rect.right < 0:
  279.             self.down2_size = (self.pipe_down1.rect.right + self.pipe_interval, self.bg_size[1])
  280.             self.pipe_down2.reset(self.down2_size)
  281.         elif self.pipe_up2.rect.right < 0:
  282.             self.up2_size = (self.pipe_down1.rect.right + self.pipe_interval, self.pipe_down2.rect.bottom + self.up_down_interval)
  283.             self.pipe_up2.reset(self.up2_size)
  284.             self.score_num += 1
  285.         self.screen.blit(self.pipe_down1.image, self.pipe_down1.rect)
  286.         self.screen.blit(self.pipe_up1.image, self.pipe_up1.rect)
  287.         self.screen.blit(self.pipe_down2.image, self.pipe_down2.rect)
  288.         self.screen.blit(self.pipe_up2.image, self.pipe_up2.rect)

  289.     def draw_bird(self):
  290.         if self.bird.rect.bottom >= self.screen.get_rect().bottom:
  291.             self.bird.rect.bottom = self.screen.get_rect().bottom
  292.             # 结束游戏
  293.             self.is_over = True
  294.         else:
  295.             self.bird.rect.centery += self.bird.velocity  # 根据速度更新位置
  296.         if not self.is_over:
  297.             if self.bird.is_up:
  298.                 self.screen.blit(self.bird.image, self.bird.rect)
  299.             else:
  300.                 self.screen.blit(self.bird.image2, self.bird.rect)
  301.         else:
  302.             self.screen.blit(self.bird.dead_image, self.bird.rect)

  303.     def _update(self):
  304.         """更新屏幕"""
  305.         self.eclipse()
  306.         self.draw_pipe()
  307.         self.draw_bird()
  308.         self.score_str = str(self.score_num)
  309.         self.score.draw_score(self.screen, self.score_str)
  310.         if self.is_over:
  311.             self.draw_over()
  312.             pygame.time.set_timer(self.DAYORNIGHT, 0)
  313.         pygame.display.update()

  314.     def cover(self):
  315.         """开始界面"""
  316.         self.screen.blit(self.bg_night_image, (0,0))

  317.         self.title_rect.centerx = self.screen.get_rect().centerx
  318.         self.title_rect.top = 50
  319.         self.screen.blit(self.title_image, self.title_rect)
  320.         self.tutorial_rect.centerx = self.screen.get_rect().centerx
  321.         self.tutorial_rect.top = self.title_rect.bottom + 20
  322.         self.screen.blit(self.tutorial_image, self.tutorial_rect)
  323.         self.text_ready_rect.centerx = self.screen.get_rect().centerx
  324.         self.text_ready_rect.top = self.tutorial_rect.bottom + 20
  325.         self.screen.blit(self.text_ready_image, self.text_ready_rect)
  326.         self.button_play_rect.centerx = self.screen.get_rect().centerx
  327.         self.button_play_rect.top = self.text_ready_rect.bottom + 20
  328.         self.screen.blit(self.button_play_image, self.button_play_rect)
  329.         pygame.display.update()

  330.     def play(self):
  331.         """游戏开始"""
  332.         self.eclipse()
  333.         while True:
  334.             for event in pygame.event.get():
  335.                 if event.type == pygame.QUIT:
  336.                     pygame.quit()
  337.                     sys.exit()
  338.                 # 只有在接收到计时器事件时,才切换白天黑夜
  339.                 elif event.type == self.DAYORNIGHT:
  340.                     self.is_day = not self.is_day
  341.                 # 鼠标点击
  342.                 elif event.type == pygame.MOUSEBUTTONDOWN:
  343.                     if pygame.mouse.get_pressed()[0] == 1:
  344.                         pos = pygame.mouse.get_pos()
  345.                         if self.button_play_rect.collidepoint(pos):
  346.                             if not self.is_start:
  347.                                 self.is_start = True
  348.                             elif self.is_over:
  349.                                 self.is_over = False
  350.                                 self.reset()
  351.                         self.bird.move()

  352.             if pygame.sprite.collide_mask(self.bird, self.pipe_down1) or pygame.sprite.collide_mask(self.bird, self.pipe_down2) or\
  353.                 pygame.sprite.collide_mask(self.bird, self.pipe_up1) or pygame.sprite.collide_mask(self.bird, self.pipe_up2):
  354.                 self.is_over = True

  355.             if not self.is_start:
  356.                 self.cover()
  357.             else:
  358.                 if self.delay % 6 == 0:
  359.                     self.bird.drop()
  360.                 self.pipe_down1.move()
  361.                 self.pipe_up1.move()
  362.                 self.pipe_down2.move()
  363.                 self.pipe_up2.move()
  364.                 if self.is_over:
  365.                     self.pipe_down1.velocity = 0
  366.                     self.pipe_down2.velocity = 0
  367.                     self.pipe_up1.velocity = 0
  368.                     self.pipe_up2.velocity = 0
  369.                 self._update()

  370.                 self.clock.tick(60)

  371.                 self.delay -= 1
  372.                 if self.delay == 0:
  373.                     self.delay = 100


  374. if __name__ == '__main__':
  375.     flappybird = FlappyBird()
  376.     try:
  377.         flappybird.play()
  378.     except SystemExit:
  379.         pass
  380.     except:
  381.         traceback.print_exc()
  382.         pygame.quit()
复制代码

自取即可

FlappedBird.zip (90.93 KB, 下载次数: 1)
上面那个有个小bug,

FlappedBird.zip (90.93 KB, 下载次数: 0)

FlappedBird.zip

90.93 KB, 下载次数: 1

评分

参与人数 3荣誉 +5 鱼币 +5 贡献 +5 C币 +5 收起 理由
小甲鱼 + 2 + 2 + 2 + 2 鱼C有你更精彩^_^
不二如是 + 3 鱼C有你更精彩^_^
小甲鱼的二师兄 + 3 + 3 + 3 鱼C有你更精彩^_^

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 昨天 10:06 | 显示全部楼层
Flapped Bird游戏源码(基于pygame)


  1. import pygame
  2. import sys
  3. import random

  4. # 初始化pygame
  5. pygame.init()

  6. # 游戏窗口设置
  7. WIN_WIDTH = 400
  8. WIN_HEIGHT = 600
  9. win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
  10. pygame.display.set_caption("Flapped Bird")

  11. # 颜色定义
  12. SKY_BLUE = (135, 206, 235)
  13. YELLOW = (255, 255, 0)
  14. GREEN = (0, 128, 0)

  15. # 小鸟属性
  16. bird_x = 50
  17. bird_y = 300
  18. bird_radius = 15
  19. gravity = 0.5
  20. jump_speed = -8
  21. bird_velocity = 0

  22. # 管道属性
  23. pipe_width = 50
  24. pipe_gap = 150
  25. pipe_list = []
  26. SPAWNPIPE = pygame.USEREVENT
  27. pygame.time.set_timer(SPAWNPIPE, 1500)

  28. # 分数
  29. score = 0
  30. font = pygame.font.SysFont("arial", 30)

  31. def draw_bird():
  32.     pygame.draw.circle(win, YELLOW, (bird_x, int(bird_y)), bird_radius)

  33. def create_pipe():
  34.     gap_y = random.randint(200, 400)
  35.     return [WIN_WIDTH, gap_y]

  36. def move_pipes(pipes):
  37.     for pipe in pipes:
  38.         pipe[0] -= 3
  39.     return [p for p in pipes if p[0] > -pipe_width]

  40. def draw_pipes(pipes):
  41.     for pipe in pipes:
  42.         # 上管道
  43.         pygame.draw.rect(win, GREEN, (pipe[0], 0, pipe_width, pipe[1] - pipe_gap//2))
  44.         # 下管道
  45.         pygame.draw.rect(win, GREEN, (pipe[0], pipe[1] + pipe_gap//2, pipe_width, WIN_HEIGHT))

  46. def check_collision(pipes):
  47.     for pipe in pipes:
  48.         # 检测与上下管道的碰撞
  49.         if (bird_x + bird_radius > pipe[0] and bird_x - bird_radius < pipe[0] + pipe_width):
  50.             if (bird_y - bird_radius < pipe[1] - pipe_gap//2 or
  51.                 bird_y + bird_radius > pipe[1] + pipe_gap//2):
  52.                 return True
  53.     # 检测上下边界
  54.     if bird_y - bird_radius < 0 or bird_y + bird_radius > WIN_HEIGHT:
  55.         return True
  56.     return False

  57. # 游戏主循环
  58. clock = pygame.time.Clock()
  59. running = True

  60. while running:
  61.     clock.tick(30)
  62.    
  63.     # 事件处理
  64.     for event in pygame.event.get():
  65.         if event.type == pygame.QUIT:
  66.             running = False
  67.         if event.type == pygame.KEYDOWN:
  68.             if event.key == pygame.K_SPACE:
  69.                 bird_velocity = jump_speed
  70.         if event.type == SPAWNPIPE:
  71.             pipe_list.append(create_pipe())
  72.    
  73.     # 物理计算
  74.     bird_velocity += gravity
  75.     bird_y += bird_velocity
  76.    
  77.     # 移动管道
  78.     pipe_list = move_pipes(pipe_list)
  79.    
  80.     # 碰撞检测
  81.     if check_collision(pipe_list):
  82.         running = False
  83.    
  84.     # 绘制画面
  85.     win.fill(SKY_BLUE)
  86.     draw_pipes(pipe_list)
  87.     draw_bird()
  88.    
  89.     # 显示分数
  90.     score_text = font.render(f"Score: {len(pipe_list)}", True, (0, 0, 0))
  91.     win.blit(score_text, (10, 10))
  92.    
  93.     pygame.display.update()

  94. pygame.quit()
  95. sys.exit()
复制代码


素材建议:
  • 小鸟图片:可以使用圆形黄色图案代替
  • 管道素材:绿色矩形即可
  • 背景图:建议使用天空蓝色渐变图片


(需要完整素材包可私信联系,代码可直接复制到本地pygame环境运行)

https://fishc.com.cn/static/image/filetype/zip.gif [下载素材包](示例链接,需替换真实地址)

温馨提示:
1. 需要先安装pygame库:pip install pygame
2. 按空格键控制小鸟跳跃
3. 碰撞检测和分数系统已内置
4. 难度可通过调整pipe_gap和管道移动速度修改

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 10:21 | 显示全部楼层

回帖奖励 +8 鱼币

不错不错,这个版本很精致~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 10:54 | 显示全部楼层

回帖奖励 +8 鱼币

有趣~
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 昨天 11:21 | 显示全部楼层

回帖奖励 +8 鱼币

评分限制到了,但还是来支持下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 12:40 | 显示全部楼层

回帖奖励 +8 鱼币

挺不错k
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 14:12 | 显示全部楼层

回帖奖励 +8 鱼币

不错不错~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 15:25 | 显示全部楼层

回帖奖励 +8 鱼币

牛啊牛啊!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 16:04 | 显示全部楼层

回帖奖励 +8 鱼币

哈哈哈,我也有这个素材
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 19:26 | 显示全部楼层

回帖奖励 +8 鱼币

好厉害
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 19:35 From FishC Mobile | 显示全部楼层
建议发到GitHub
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 昨天 20:08 | 显示全部楼层

git都没搞明白,账号也没有一个,技术也不好,只会在github上下载东西
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 20:10 From FishC Mobile | 显示全部楼层
pyzyd 发表于 2025-5-24 20:08
git都没搞明白,账号也没有一个,技术也不好,只会在github上下载东西

论坛上有小甲鱼发的教程的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 昨天 20:19 | 显示全部楼层
player-none 发表于 2025-5-24 20:10
论坛上有小甲鱼发的教程的

有时间去研究一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 21:02 | 显示全部楼层

回帖奖励 +8 鱼币

1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-25 02:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表