小言_互联网的博客

Python代码,能玩30多款童年游戏!这些有几个是你玩过的

532人阅读  评论(0)

大游戏小游戏有千千万万,这些小游戏应该只有90后才玩过和懂吧

儿童节即将到来,虽然秃头程序员没有头发,但是童心还是一直都在的,今天就分享一些私藏的童年游戏,十几行代码就能进入使用Python开发的小游戏快乐玩耍! 

使用工具:

Python3.8、pycharm2020

也可以不安装pycharm2020也可以直接运行~但是安装了些会好一点

1.五子棋

源码:


  
  1. '''游戏开始界面'''
  2. class gameStartUI(QWidget):
  3. def __init__(self, parent=None, **kwargs):
  4. super(gameStartUI, self).__init__(parent)
  5. self.setFixedSize( 760, 650)
  6. self.setWindowTitle( '五子棋 —— 九歌')
  7. self.setWindowIcon(QIcon(cfg.ICON_FILEPATH))
  8. # 背景图片
  9. palette = QPalette()
  10. palette.setBrush(self.backgroundRole(), QBrush(QPixmap(cfg.BACKGROUND_IMAGEPATHS.get( 'bg_start'))))
  11. self.setPalette(palette)
  12. # 按钮
  13. # --人机对战
  14. self.ai_button = PushButton(cfg.BUTTON_IMAGEPATHS.get( 'ai'), self)
  15. self.ai_button.move( 250, 200)
  16. self.ai_button.show()
  17. self.ai_button.click_signal.connect(self.playWithAI)
  18. # --联机对战
  19. self.online_button = PushButton(cfg.BUTTON_IMAGEPATHS.get( 'online'), self)
  20. self.online_button.move( 250, 350)
  21. self.online_button.show()
  22. self.online_button.click_signal.connect(self.playOnline)
  23. '''人机对战'''
  24. def playWithAI(self):
  25. self.close()
  26. self.gaming_ui = playWithAIUI(cfg)
  27. self.gaming_ui.exit_signal.connect( lambda: sys.exit())
  28. self.gaming_ui.back_signal.connect(self.show)
  29. self.gaming_ui.show()
  30. '''联机对战'''
  31. def playOnline(self):
  32. self.close()
  33. self.gaming_ui = playOnlineUI(cfg, self)
  34. self.gaming_ui.show()
  35. '''run'''
  36. if __name__ == '__main__':
  37. app = QApplication(sys.argv)
  38. handle = gameStartUI()
  39. font = QFont()
  40. font.setPointSize( 12)
  41. handle.setFont(font)
  42. handle.show()
  43. sys.exit(app.exec_())

 

 2.超级玛丽

这个源码超级多多,文件很大很大~


  
  1. else:
  2. break
  3. Interface(screen, cfg, mode= 'game_end')
  4. 可以关注公众号:学习py最风sao的方式
  5. pygame.display.update()
  6. clock.tick(cfg.FPS)
  7. if is_win_flag:

 3.炸弹人

 源码:


  
  1. '''游戏主程序'''
  2. def main(cfg):
  3. # 初始化
  4. pygame.init()
  5. pygame.mixer.init()
  6. pygame.mixer.music.load(cfg.BGMPATH)
  7. pygame.mixer.music.play( -1, 0.0)
  8. screen = pygame.display.set_mode(cfg.SCREENSIZE)
  9. pygame.display.set_caption( 'Bomber Man —— 九歌')
  10. # 开始界面
  11. Interface(screen, cfg, mode= 'game_start')
  12. # 游戏主循环
  13. font = pygame.font.SysFont( 'Consolas', 15)
  14. for gamemap_path in cfg.GAMEMAPPATHS:
  15. # -地图
  16. map_parser = mapParser(gamemap_path, bg_paths=cfg.BACKGROUNDPATHS, wall_paths=cfg.WALLPATHS, blocksize=cfg.BLOCKSIZE)
  17. # -水果
  18. fruit_sprite_group = pygame.sprite.Group()
  19. used_spaces = []
  20. for i in range( 5):
  21. coordinate = map_parser.randomGetSpace(used_spaces)
  22. used_spaces.append(coordinate)
  23. fruit_sprite_group.add(Fruit(random.choice(cfg.FRUITPATHS), coordinate=coordinate, blocksize=cfg.BLOCKSIZE))
  24. # -我方Hero
  25. coordinate = map_parser.randomGetSpace(used_spaces)
  26. used_spaces.append(coordinate)
  27. ourhero = Hero(imagepaths=cfg.HEROZELDAPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser, hero_name= 'ZELDA')
  28. # -电脑Hero
  29. aihero_sprite_group = pygame.sprite.Group()
  30. coordinate = map_parser.randomGetSpace(used_spaces)
  31. aihero_sprite_group.add(Hero(imagepaths=cfg.HEROBATMANPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser, hero_name= 'BATMAN'))
  32. used_spaces.append(coordinate)
  33. coordinate = map_parser.randomGetSpace(used_spaces)
  34. aihero_sprite_group.add(Hero(imagepaths=cfg.HERODKPATHS, coordinate=coordinate, blocksize=cfg.BLOCKSIZE, map_parser=map_parser, hero_name= 'DK'))
  35. used_spaces.append(coordinate)
  36. # -炸弹bomb
  37. bomb_sprite_group = pygame.sprite.Group()
  38. # -用于判断游戏胜利或者失败的flag
  39. is_win_flag = False
  40. # -主循环
  41. screen = pygame.display.set_mode(map_parser.screen_size)
  42. clock = pygame.time.Clock()
  43. while True:
  44. dt = clock.tick(cfg.FPS)
  45. for event in pygame.event.get():
  46. if event.type == pygame.QUIT:
  47. pygame.quit()
  48. sys.exit( -1)
  49. # --↑↓←→键控制上下左右, 空格键丢炸弹
  50. elif event.type == pygame.KEYDOWN:
  51. if event.key == pygame.K_UP:
  52. ourhero.move( 'up')
  53. elif event.key == pygame.K_DOWN:
  54. ourhero.move( 'down')
  55. elif event.key == pygame.K_LEFT:
  56. ourhero.move( 'left')
  57. elif event.key == pygame.K_RIGHT:
  58. ourhero.move( 'right')
  59. elif event.key == pygame.K_SPACE:
  60. if ourhero.bomb_cooling_count <= 0:
  61. bomb_sprite_group.add(ourhero.generateBomb(imagepath=cfg.BOMBPATH, digitalcolor=cfg.YELLOW, explode_imagepath=cfg.FIREPATH))
  62. screen.fill(cfg.WHITE)
  63. # --电脑Hero随机行动
  64. for hero in aihero_sprite_group:
  65. action, flag = hero.randomAction(dt)
  66. if flag and action == 'dropbomb':
  67. bomb_sprite_group.add(hero.generateBomb(imagepath=cfg.BOMBPATH, digitalcolor=cfg.YELLOW, explode_imagepath=cfg.FIREPATH))
  68. # --吃到水果加生命值(只要是Hero, 都能加)
  69. ourhero.eatFruit(fruit_sprite_group)
  70. for hero in aihero_sprite_group:
  71. hero.eatFruit(fruit_sprite_group)
  72. # --游戏元素都绑定到屏幕上
  73. map_parser.draw(screen)
  74. for bomb in bomb_sprite_group:
  75. if not bomb.is_being:
  76. bomb_sprite_group.remove(bomb)
  77. explode_area = bomb.draw(screen, dt, map_parser)
  78. if explode_area:
  79. # --爆炸火焰范围内的Hero生命值将持续下降
  80. if ourhero.coordinate in explode_area:
  81. ourhero.health_value -= bomb.harm_value
  82. for hero in aihero_sprite_group:
  83. if hero.coordinate in explode_area:
  84. hero.health_value -= bomb.harm_value
  85. fruit_sprite_group.draw(screen)
  86. for hero in aihero_sprite_group:
  87. hero.draw(screen, dt)
  88. ourhero.draw(screen, dt)
  89. # --左上角显示生命值
  90. pos_x = showText(screen, font, text=ourhero.hero_name+ '(our):'+str(ourhero.health_value), color=cfg.YELLOW, position=[ 5, 5])
  91. for hero in aihero_sprite_group:
  92. pos_x, pos_y = pos_x+ 15, 5
  93. pos_x = showText(screen, font, text=hero.hero_name+ '(ai):'+str(hero.health_value), color=cfg.YELLOW, position=[pos_x, pos_y])
  94. # --我方玩家生命值小于等于0/电脑方玩家生命值均小于等于0则判断游戏结束
  95. if ourhero.health_value <= 0:
  96. is_win_flag = False
  97. break
  98. for hero in aihero_sprite_group:
  99. if hero.health_value <= 0:
  100. aihero_sprite_group.remove(hero)
  101. if len(aihero_sprite_group) == 0:
  102. is_win_flag = True
  103. break
  104. pygame.display.update()
  105. clock.tick(cfg.FPS)
  106. if is_win_flag:
  107. Interface(screen, cfg, mode= 'game_switch')
  108. else:
  109. break
  110. Interface(screen, cfg, mode= 'game_end')
  111. '''run'''
  112. if __name__ == '__main__':
  113. while True:
  114. main(cfg)

 4.推箱子

 源码:


  
  1. '''开始游戏'''
  2. def startGame(screen):
  3. clock = pygame.time.Clock()
  4. # 加载字体
  5. font = pygame.font.SysFont( 'arial', 18)
  6. if not os.path.isfile( 'score'):
  7. f = open( 'score', 'w')
  8. f.write( '0')
  9. f.close()
  10. with open( 'score', 'r') as f:
  11. highest_score = int(f.read().strip())
  12. # 敌方
  13. enemies_group = pygame.sprite.Group()
  14. for i in range( 55):
  15. if i < 11:
  16. enemy = enemySprite( 'small', i, cfg.WHITE, cfg.WHITE)
  17. elif i < 33:
  18. enemy = enemySprite( 'medium', i, cfg.WHITE, cfg.WHITE)
  19. else:
  20. enemy = enemySprite( 'large', i, cfg.WHITE, cfg.WHITE)
  21. enemy.rect.x = 85 + (i % 11) * 50
  22. enemy.rect.y = 120 + (i // 11) * 45
  23. enemies_group.add(enemy)
  24. boomed_enemies_group = pygame.sprite.Group()
  25. en_bullets_group = pygame.sprite.Group()
  26. ufo = ufoSprite(color=cfg.RED)
  27. # 我方
  28. myaircraft = aircraftSprite(color=cfg.GREEN, bullet_color=cfg.WHITE)
  29. my_bullets_group = pygame.sprite.Group()
  30. # 用于控制敌方位置更新
  31. # --移动一行
  32. enemy_move_count = 24
  33. enemy_move_interval = 24
  34. enemy_move_flag = False
  35. # --改变移动方向(改变方向的同时集体下降一次)
  36. enemy_change_direction_count = 0
  37. enemy_change_direction_interval = 60
  38. enemy_need_down = False
  39. enemy_move_right = True
  40. enemy_need_move_row = 6
  41. enemy_max_row = 5
  42. # 用于控制敌方发射子弹
  43. enemy_shot_interval = 100
  44. enemy_shot_count = 0
  45. enemy_shot_flag = False
  46. # 游戏进行中
  47. running = True
  48. is_win = False
  49. # 主循环
  50. while running:
  51. screen.fill(cfg.BLACK)
  52. for event in pygame.event.get():
  53. # --点右上角的X或者按Esc键退出游戏
  54. if event.type == pygame.QUIT:
  55. pygame.quit()
  56. sys.exit()
  57. if event.type == pygame.KEYDOWN:
  58. if event.key == pygame.K_ESCAPE:
  59. pygame.quit()
  60. sys.exit()
  61. # --射击
  62. if event.type == pygame.MOUSEBUTTONDOWN:
  63. my_bullet = myaircraft.shot()
  64. if my_bullet:
  65. my_bullets_group.add(my_bullet)
  66. # --我方子弹与敌方/UFO碰撞检测
  67. for enemy in enemies_group:
  68. if pygame.sprite.spritecollide(enemy, my_bullets_group, True, None):
  69. boomed_enemies_group.add(enemy)
  70. enemies_group.remove(enemy)
  71. myaircraft.score += enemy.reward
  72. if pygame.sprite.spritecollide(ufo, my_bullets_group, True, None):
  73. ufo.is_dead = True
  74. myaircraft.score += ufo.reward
  75. # --更新并画敌方
  76. # ----敌方子弹
  77. enemy_shot_count += 1
  78. if enemy_shot_count > enemy_shot_interval:
  79. enemy_shot_flag = True
  80. enemies_survive_list = [enemy.number for enemy in enemies_group]
  81. shot_number = random.choice(enemies_survive_list)
  82. enemy_shot_count = 0
  83. # ----敌方移动
  84. enemy_move_count += 1
  85. if enemy_move_count > enemy_move_interval:
  86. enemy_move_count = 0
  87. enemy_move_flag = True
  88. enemy_need_move_row -= 1
  89. if enemy_need_move_row == 0:
  90. enemy_need_move_row = enemy_max_row
  91. enemy_change_direction_count += 1
  92. if enemy_change_direction_count > enemy_change_direction_interval:
  93. enemy_change_direction_count = 1
  94. enemy_move_right = not enemy_move_right
  95. enemy_need_down = True
  96. # ----每次下降提高移动和射击速度
  97. enemy_move_interval = max( 15, enemy_move_interval -3)
  98. enemy_shot_interval = max( 50, enemy_move_interval -10)
  99. # ----遍历更新
  100. for enemy in enemies_group:
  101. if enemy_shot_flag:
  102. if enemy.number == shot_number:
  103. en_bullet = enemy.shot()
  104. en_bullets_group.add(en_bullet)
  105. if enemy_move_flag:
  106. if enemy.number in range((enemy_need_move_row -1)* 11, enemy_need_move_row* 11):
  107. if enemy_move_right:
  108. enemy.update( 'right', cfg.SCREENSIZE[ 1])
  109. else:
  110. enemy.update( 'left', cfg.SCREENSIZE[ 1])
  111. else:
  112. enemy.update( None, cfg.SCREENSIZE[ 1])
  113. if enemy_need_down:
  114. if enemy.update( 'down', cfg.SCREENSIZE[ 1]):
  115. running = False
  116. is_win = False
  117. enemy.change_count -= 1
  118. enemy.draw(screen)
  119. enemy_move_flag = False
  120. enemy_need_down = False
  121. enemy_shot_flag = False
  122. # ----敌方爆炸特效
  123. for boomed_enemy in boomed_enemies_group:
  124. if boomed_enemy.boom(screen):
  125. boomed_enemies_group.remove(boomed_enemy)
  126. del boomed_enemy
  127. # --敌方子弹与我方飞船碰撞检测
  128. if not myaircraft.one_dead:
  129. if pygame.sprite.spritecollide(myaircraft, en_bullets_group, True, None):
  130. myaircraft.one_dead = True
  131. if myaircraft.one_dead:
  132. if myaircraft.boom(screen):
  133. myaircraft.resetBoom()
  134. myaircraft.num_life -= 1
  135. if myaircraft.num_life < 1:
  136. running = False
  137. is_win = False
  138. else:
  139. # ----更新飞船
  140. myaircraft.update(cfg.SCREENSIZE[ 0])
  141. # ----画飞船
  142. myaircraft.draw(screen)
  143. if ( not ufo.has_boomed) and (ufo.is_dead):
  144. if ufo.boom(screen):
  145. ufo.has_boomed = True
  146. else:
  147. # ----更新UFO
  148. ufo.update(cfg.SCREENSIZE[ 0])
  149. # ----画UFO
  150. ufo.draw(screen)
  151. # --画我方飞船子弹
  152. for bullet in my_bullets_group:
  153. if bullet.update():
  154. my_bullets_group.remove(bullet)
  155. del bullet
  156. else:
  157. bullet.draw(screen)
  158. # --画敌方子弹
  159. for bullet in en_bullets_group:
  160. if bullet.update(cfg.SCREENSIZE[ 1]):
  161. en_bullets_group.remove(bullet)
  162. del bullet
  163. else:
  164. bullet.draw(screen)
  165. if myaircraft.score > highest_score:
  166. highest_score = myaircraft.score
  167. # --得分每增加2000我方飞船增加一条生命
  168. if (myaircraft.score % 2000 == 0) and (myaircraft.score > 0) and (myaircraft.score != myaircraft.old_score):
  169. myaircraft.old_score = myaircraft.score
  170. myaircraft.num_life = min(myaircraft.num_life + 1, myaircraft.max_num_life)
  171. # --敌人都死光了的话就胜利了
  172. if len(enemies_group) < 1:
  173. is_win = True
  174. running = False
  175. # --显示文字
  176. # ----当前得分
  177. showText(screen, 'SCORE: ', cfg.WHITE, font, 200, 8)
  178. showText(screen, str(myaircraft.score), cfg.WHITE, font, 200, 24)
  179. # ----敌人数量
  180. showText(screen, 'ENEMY: ', cfg.WHITE, font, 370, 8)
  181. showText(screen, str(len(enemies_group)), cfg.WHITE, font, 370, 24)
  182. # ----历史最高分
  183. showText(screen, 'HIGHEST: ', cfg.WHITE, font, 540, 8)
  184. showText(screen, str(highest_score), cfg.WHITE, font, 540, 24)
  185. # ----FPS
  186. showText(screen, 'FPS: ' + str(int(clock.get_fps())), cfg.RED, font, 8, 8)
  187. # --显示剩余生命值
  188. showLife(screen, myaircraft.num_life, cfg.GREEN)
  189. pygame.display.update()
  190. clock.tick(cfg.FPS)
  191. with open( 'score', 'w') as f:
  192. f.write(str(highest_score))
  193. return is_win
  194. '''主函数'''
  195. def main():
  196. # 初始化
  197. pygame.init()
  198. pygame.display.set_caption( '外星人入侵 —— 九歌')
  199. screen = pygame.display.set_mode(cfg.SCREENSIZE)
  200. pygame.mixer.init()
  201. pygame.mixer.music.load(cfg.BGMPATH)
  202. pygame.mixer.music.set_volume( 0.4)
  203. pygame.mixer.music.play( -1)
  204. while True:
  205. is_win = startGame(screen)
  206. endInterface(screen, cfg.BLACK, is_win)
  207. '''run'''
  208. if __name__ == '__main__':
  209. main()

 5.扫雷

上学时恨不得一直待在电脑房玩这个游戏,哈哈哈

 

第二种AI贪吃蛇 

经典游戏俄罗斯方块

 

太多了就不一一展示啦


转载:https://blog.csdn.net/A_7878520/article/details/116091351
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场