本站消息

  出租广告位,需要合作请联系站长

  今日名言-想象你自己对困难作出的反应,不是逃避或绕开它们,而是面对它们,同它们打交道,以一种进取的和明智的方式同它们奋斗 。——马克斯威尔·马尔兹

  今日名言-用谅解、宽恕的目光和心理看人、待人。人就会觉得葱笼的世界里,春意盎然,到处充满温暖。——蔡文甫


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

为了上班摸鱼我用Python制作十五个小游戏,普通到地狱级难度,看看你能挑战到哪【内附源码】

发布于2021-07-24 20:55     阅读(1199)     评论(0)     点赞(24)     收藏(3)


大家好,我是老白。

今天给大家带来十五个Python小游戏,找回童年的同时学习编程还可以摸鱼,源码附上结尾领取。

一、接金币(1分)

普通难度:❤

玩法介绍:吃金币,控制左右键,有手就行。

 源码分享

  1. import os
  2. import cfg
  3. import sys
  4. import pygame
  5. import random
  6. from modules import *
  7. '''游戏初始化'''
  8. def initGame():
  9. # 初始化pygame, 设置展示窗口
  10. pygame.init()
  11. screen = pygame.display.set_mode(cfg.SCREENSIZE)
  12. pygame.display.set_caption('catch coins —— ')
  13. # 加载必要的游戏素材
  14. game_images = {}
  15. for key, value in cfg.IMAGE_PATHS.items():
  16. if isinstance(value, list):
  17. images = []
  18. for item in value: images.append(pygame.image.load(item))
  19. game_images[key] = images
  20. else:
  21. game_images[key] = pygame.image.load(value)
  22. game_sounds = {}
  23. for key, value in cfg.AUDIO_PATHS.items():
  24. if key == 'bgm': continue
  25. game_sounds[key] = pygame.mixer.Sound(value)
  26. # 返回初始化数据
  27. return screen, game_images, game_sounds
  28. '''主函数'''
  29. def main():
  30. # 初始化
  31. screen, game_images, game_sounds = initGame()
  32. # 播放背景音乐
  33. pygame.mixer.music.load(cfg.AUDIO_PATHS['bgm'])
  34. pygame.mixer.music.play(-1, 0.0)
  35. # 字体加载
  36. font = pygame.font.Font(cfg.FONT_PATH, 40)
  37. # 定义hero
  38. hero = Hero(game_images['hero'], position=(375, 520))
  39. # 定义食物组
  40. food_sprites_group = pygame.sprite.Group()
  41. generate_food_freq = random.randint(10, 20)
  42. generate_food_count = 0
  43. # 当前分数/历史最高分
  44. score = 0
  45. highest_score = 0 if not os.path.exists(cfg.HIGHEST_SCORE_RECORD_FILEPATH) else int(open(cfg.HIGHEST_SCORE_RECORD_FILEPATH).read())
  46. # 游戏主循环
  47. clock = pygame.time.Clock()
  48. while True:
  49. # --填充背景
  50. screen.fill(0)
  51. screen.blit(game_images['background'], (0, 0))
  52. # --倒计时信息
  53. countdown_text = 'Count down: ' + str((90000 - pygame.time.get_ticks()) // 60000) + ":" + str((90000 - pygame.time.get_ticks()) // 1000 % 60).zfill(2)
  54. countdown_text = font.render(countdown_text, True, (0, 0, 0))
  55. countdown_rect = countdown_text.get_rect()
  56. countdown_rect.topright = [cfg.SCREENSIZE[0]-30, 5]
  57. screen.blit(countdown_text, countdown_rect)
  58. # --按键检测
  59. for event in pygame.event.get():
  60. if event.type == pygame.QUIT:
  61. pygame.quit()
  62. sys.exit()
  63. key_pressed = pygame.key.get_pressed()
  64. if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:
  65. hero.move(cfg.SCREENSIZE, 'left')
  66. if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
  67. hero.move(cfg.SCREENSIZE, 'right')
  68. # --随机生成食物
  69. generate_food_count += 1
  70. if generate_food_count > generate_food_freq:
  71. generate_food_freq = random.randint(10, 20)
  72. generate_food_count = 0
  73. food = Food(game_images, random.choice(['gold',] * 10 + ['apple']), cfg.SCREENSIZE)
  74. food_sprites_group.add(food)
  75. # --更新食物
  76. for food in food_sprites_group:
  77. if food.update(): food_sprites_group.remove(food)
  78. # --碰撞检测
  79. for food in food_sprites_group:
  80. if pygame.sprite.collide_mask(food, hero):
  81. game_sounds['get'].play()
  82. food_sprites_group.remove(food)
  83. score += food.score
  84. if score > highest_score: highest_score = score
  85. # --画hero
  86. hero.draw(screen)
  87. # --画食物
  88. food_sprites_group.draw(screen)
  89. # --显示得分
  90. score_text = f'Score: {score}, Highest: {highest_score}'
  91. score_text = font.render(score_text, True, (0, 0, 0))
  92. score_rect = score_text.get_rect()
  93. score_rect.topleft = [5, 5]
  94. screen.blit(score_text, score_rect)
  95. # --判断游戏是否结束
  96. if pygame.time.get_ticks() >= 90000:
  97. break
  98. # --更新屏幕
  99. pygame.display.flip()
  100. clock.tick(cfg.FPS)
  101. # 游戏结束, 记录最高分并显示游戏结束画面
  102. fp = open(cfg.HIGHEST_SCORE_RECORD_FILEPATH, 'w')
  103. fp.write(str(highest_score))
  104. fp.close()
  105. return showEndGameInterface(screen, cfg, score, highest_score)
  106. '''run'''
  107. if __name__ == '__main__':
  108. while main():
  109. pass

二、滑雪(1分)

普通难度:❤

玩法介绍:滑雪吃小旗子,控制上下左右键,关键控制好速度,有手就行。

 源码分享

  1. import sys
  2. import cfg
  3. import pygame
  4. import random
  5. '''滑雪者类'''
  6. class SkierClass(pygame.sprite.Sprite):
  7. def __init__(self):
  8. pygame.sprite.Sprite.__init__(self)
  9. # 滑雪者的朝向(-2到2)
  10. self.direction = 0
  11. self.imagepaths = cfg.SKIER_IMAGE_PATHS[:-1]
  12. self.image = pygame.image.load(self.imagepaths[self.direction])
  13. self.rect = self.image.get_rect()
  14. self.rect.center = [320, 100]
  15. self.speed = [self.direction, 6-abs(self.direction)*2]
  16. '''改变滑雪者的朝向. 负数为向左,正数为向右,0为向前'''
  17. def turn(self, num):
  18. self.direction += num
  19. self.direction = max(-2, self.direction)
  20. self.direction = min(2, self.direction)
  21. center = self.rect.center
  22. self.image = pygame.image.load(self.imagepaths[self.direction])
  23. self.rect = self.image.get_rect()
  24. self.rect.center = center
  25. self.speed = [self.direction, 6-abs(self.direction)*2]
  26. return self.speed
  27. '''移动滑雪者'''
  28. def move(self):
  29. self.rect.centerx += self.speed[0]
  30. self.rect.centerx = max(20, self.rect.centerx)
  31. self.rect.centerx = min(620, self.rect.centerx)
  32. '''设置为摔倒状态'''
  33. def setFall(self):
  34. self.image = pygame.image.load(cfg.SKIER_IMAGE_PATHS[-1])
  35. '''设置为站立状态'''
  36. def setForward(self):
  37. self.direction = 0
  38. self.image = pygame.image.load(self.imagepaths[self.direction])
  39. '''
  40. Function:
  41. 障碍物类
  42. Input:
  43. img_path: 障碍物图片路径
  44. location: 障碍物位置
  45. attribute: 障碍物类别属性
  46. '''
  47. class ObstacleClass(pygame.sprite.Sprite):
  48. def __init__(self, img_path, location, attribute):
  49. pygame.sprite.Sprite.__init__(self)
  50. self.img_path = img_path
  51. self.image = pygame.image.load(self.img_path)
  52. self.location = location
  53. self.rect = self.image.get_rect()
  54. self.rect.center = self.location
  55. self.attribute = attribute
  56. self.passed = False
  57. '''移动'''
  58. def move(self, num):
  59. self.rect.centery = self.location[1] - num
  60. '''创建障碍物'''
  61. def createObstacles(s, e, num=10):
  62. obstacles = pygame.sprite.Group()
  63. locations = []
  64. for i in range(num):
  65. row = random.randint(s, e)
  66. col = random.randint(0, 9)
  67. location = [col*64+20, row*64+20]
  68. if location not in locations:
  69. locations.append(location)
  70. attribute = random.choice(list(cfg.OBSTACLE_PATHS.keys()))
  71. img_path = cfg.OBSTACLE_PATHS[attribute]
  72. obstacle = ObstacleClass(img_path, location, attribute)
  73. obstacles.add(obstacle)
  74. return obstacles
  75. '''合并障碍物'''
  76. def AddObstacles(obstacles0, obstacles1):
  77. obstacles = pygame.sprite.Group()
  78. for obstacle in obstacles0:
  79. obstacles.add(obstacle)
  80. for obstacle in obstacles1:
  81. obstacles.add(obstacle)
  82. return obstacles
  83. '''显示游戏开始界面'''
  84. def ShowStartInterface(screen, screensize):
  85. screen.fill((255, 255, 255))
  86. tfont = pygame.font.Font(cfg.FONTPATH, screensize[0]//5)
  87. cfont = pygame.font.Font(cfg.FONTPATH, screensize[0]//20)
  88. title = tfont.render(u'滑雪游戏', True, (255, 0, 0))
  89. content = cfont.render(u'按任意键开始游戏', True, (0, 0, 255))
  90. trect = title.get_rect()
  91. trect.midtop = (screensize[0]/2, screensize[1]/5)
  92. crect = content.get_rect()
  93. crect.midtop = (screensize[0]/2, screensize[1]/2)
  94. screen.blit(title, trect)
  95. screen.blit(content, crect)
  96. while True:
  97. for event in pygame.event.get():
  98. if event.type == pygame.QUIT:
  99. pygame.quit()
  100. sys.exit()
  101. elif event.type == pygame.KEYDOWN:
  102. return
  103. pygame.display.update()
  104. '''显示分数'''
  105. def showScore(screen, score, pos=(10, 10)):
  106. font = pygame.font.Font(cfg.FONTPATH, 30)
  107. score_text = font.render("Score: %s" % score, True, (0, 0, 0))
  108. screen.blit(score_text, pos)
  109. '''更新当前帧的游戏画面'''
  110. def updateFrame(screen, obstacles, skier, score):
  111. screen.fill((255, 255, 255))
  112. obstacles.draw(screen)
  113. screen.blit(skier.image, skier.rect)
  114. showScore(screen, score)
  115. pygame.display.update()
  116. '''主程序'''
  117. def main():
  118. # 游戏初始化
  119. pygame.init()
  120. pygame.mixer.init()
  121. pygame.mixer.music.load(cfg.BGMPATH)
  122. pygame.mixer.music.set_volume(0.4)
  123. pygame.mixer.music.play(-1)
  124. # 设置屏幕
  125. screen = pygame.display.set_mode(cfg.SCREENSIZE)
  126. pygame.display.set_caption('滑雪游戏 —— 九歌')
  127. # 游戏开始界面
  128. ShowStartInterface(screen, cfg.SCREENSIZE)
  129. # 实例化游戏精灵
  130. # --滑雪者
  131. skier = SkierClass()
  132. # --创建障碍物
  133. obstacles0 = createObstacles(20, 29)
  134. obstacles1 = createObstacles(10, 19)
  135. obstaclesflag = 0
  136. obstacles = AddObstacles(obstacles0, obstacles1)
  137. # 游戏clock
  138. clock = pygame.time.Clock()
  139. # 记录滑雪的距离
  140. distance = 0
  141. # 记录当前的分数
  142. score = 0
  143. # 记录当前的速度
  144. speed = [0, 6]
  145. # 游戏主循环
  146. while True:
  147. # --事件捕获
  148. for event in pygame.event.get():
  149. if event.type == pygame.QUIT:
  150. pygame.quit()
  151. sys.exit()
  152. if event.type == pygame.KEYDOWN:
  153. if event.key == pygame.K_LEFT or event.key == pygame.K_a:
  154. speed = skier.turn(-1)
  155. elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
  156. speed = skier.turn(1)
  157. # --更新当前游戏帧的数据
  158. skier.move()
  159. distance += speed[1]
  160. if distance >= 640 and obstaclesflag == 0:
  161. obstaclesflag = 1
  162. obstacles0 = createObstacles(20, 29)
  163. obstacles = AddObstacles(obstacles0, obstacles1)
  164. if distance >= 1280 and obstaclesflag == 1:
  165. obstaclesflag = 0
  166. distance -= 1280
  167. for obstacle in obstacles0:
  168. obstacle.location[1] = obstacle.location[1] - 1280
  169. obstacles1 = createObstacles(10, 19)
  170. obstacles = AddObstacles(obstacles0, obstacles1)
  171. for obstacle in obstacles:
  172. obstacle.move(distance)
  173. # --碰撞检测
  174. hitted_obstacles = pygame.sprite.spritecollide(skier, obstacles, False)
  175. if hitted_obstacles:
  176. if hitted_obstacles[0].attribute == "tree" and not hitted_obstacles[0].passed:
  177. score -= 50
  178. skier.setFall()
  179. updateFrame(screen, obstacles, skier, score)
  180. pygame.time.delay(1000)
  181. skier.setForward()
  182. speed = [0, 6]
  183. hitted_obstacles[0].passed = True
  184. elif hitted_obstacles[0].attribute == "flag" and not hitted_obstacles[0].passed:
  185. score += 10
  186. obstacles.remove(hitted_obstacles[0])
  187. # --更新屏幕
  188. updateFrame(screen, obstacles, skier, score)
  189. clock.tick(cfg.FPS)
  190. '''run'''
  191. if __name__ == '__main__':
  192. main()

三、乒乓球(1分)

普通难度:❤

玩法介绍:双人小游戏,可以选择单打或者双打,有手就行。

源码分享

  1. import sys
  2. import cfg
  3. import pygame
  4. from modules import *
  5. '''定义按钮'''
  6. def Button(screen, position, text, button_size=(200, 50)):
  7. left, top = position
  8. bwidth, bheight = button_size
  9. pygame.draw.line(screen, (150, 150, 150), (left, top), (left+bwidth, top), 5)
  10. pygame.draw.line(screen, (150, 150, 150), (left, top-2), (left, top+bheight), 5)
  11. pygame.draw.line(screen, (50, 50, 50), (left, top+bheight), (left+bwidth, top+bheight), 5)
  12. pygame.draw.line(screen, (50, 50, 50), (left+bwidth, top+bheight), (left+bwidth, top), 5)
  13. pygame.draw.rect(screen, (100, 100, 100), (left, top, bwidth, bheight))
  14. font = pygame.font.Font(cfg.FONTPATH, 30)
  15. text_render = font.render(text, 1, (255, 235, 205))
  16. return screen.blit(text_render, (left+50, top+10))
  17. '''
  18. Function:
  19. 开始界面
  20. Input:
  21. --screen: 游戏界面
  22. Return:
  23. --game_mode: 1(单人模式)/2(双人模式)
  24. '''
  25. def startInterface(screen):
  26. clock = pygame.time.Clock()
  27. while True:
  28. screen.fill((41, 36, 33))
  29. button_1 = Button(screen, (150, 175), '1 Player')
  30. button_2 = Button(screen, (150, 275), '2 Player')
  31. for event in pygame.event.get():
  32. if event.type == pygame.QUIT:
  33. pygame.quit()
  34. sys.exit()
  35. if event.type == pygame.MOUSEBUTTONDOWN:
  36. if button_1.collidepoint(pygame.mouse.get_pos()):
  37. return 1
  38. elif button_2.collidepoint(pygame.mouse.get_pos()):
  39. return 2
  40. clock.tick(10)
  41. pygame.display.update()
  42. '''结束界面'''
  43. def endInterface(screen, score_left, score_right):
  44. clock = pygame.time.Clock()
  45. font1 = pygame.font.Font(cfg.FONTPATH, 30)
  46. font2 = pygame.font.Font(cfg.FONTPATH, 20)
  47. msg = 'Player on left won!' if score_left > score_right else 'Player on right won!'
  48. texts = [font1.render(msg, True, cfg.WHITE),
  49. font2.render('Press ESCAPE to quit.', True, cfg.WHITE),
  50. font2.render('Press ENTER to continue or play again.', True, cfg.WHITE)]
  51. positions = [[120, 200], [155, 270], [80, 300]]
  52. while True:
  53. screen.fill((41, 36, 33))
  54. for event in pygame.event.get():
  55. if event.type == pygame.QUIT:
  56. pygame.quit()
  57. sys.exit()
  58. if event.type == pygame.KEYDOWN:
  59. if event.key == pygame.K_RETURN:
  60. return
  61. elif event.key == pygame.K_ESCAPE:
  62. sys.exit()
  63. pygame.quit()
  64. for text, pos in zip(texts, positions):
  65. screen.blit(text, pos)
  66. clock.tick(10)
  67. pygame.display.update()
  68. '''运行游戏Demo'''
  69. def runDemo(screen):
  70. # 加载游戏素材
  71. hit_sound = pygame.mixer.Sound(cfg.HITSOUNDPATH)
  72. goal_sound = pygame.mixer.Sound(cfg.GOALSOUNDPATH)
  73. pygame.mixer.music.load(cfg.BGMPATH)
  74. pygame.mixer.music.play(-1, 0.0)
  75. font = pygame.font.Font(cfg.FONTPATH, 50)
  76. # 开始界面
  77. game_mode = startInterface(screen)
  78. # 游戏主循环
  79. # --左边球拍(ws控制, 仅双人模式时可控制)
  80. score_left = 0
  81. racket_left = Racket(cfg.RACKETPICPATH, 'LEFT', cfg)
  82. # --右边球拍(↑↓控制)
  83. score_right = 0
  84. racket_right = Racket(cfg.RACKETPICPATH, 'RIGHT', cfg)
  85. # --球
  86. ball = Ball(cfg.BALLPICPATH, cfg)
  87. clock = pygame.time.Clock()
  88. while True:
  89. for event in pygame.event.get():
  90. if event.type == pygame.QUIT:
  91. pygame.quit()
  92. sys.exit(-1)
  93. screen.fill((41, 36, 33))
  94. # 玩家操作
  95. pressed_keys = pygame.key.get_pressed()
  96. if pressed_keys[pygame.K_UP]:
  97. racket_right.move('UP')
  98. elif pressed_keys[pygame.K_DOWN]:
  99. racket_right.move('DOWN')
  100. if game_mode == 2:
  101. if pressed_keys[pygame.K_w]:
  102. racket_left.move('UP')
  103. elif pressed_keys[pygame.K_s]:
  104. racket_left.move('DOWN')
  105. else:
  106. racket_left.automove(ball)
  107. # 球运动
  108. scores = ball.move(ball, racket_left, racket_right, hit_sound, goal_sound)
  109. score_left += scores[0]
  110. score_right += scores[1]
  111. # 显示
  112. # --分隔线
  113. pygame.draw.rect(screen, cfg.WHITE, (247, 0, 6, 500))
  114. # --球
  115. ball.draw(screen)
  116. # --拍
  117. racket_left.draw(screen)
  118. racket_right.draw(screen)
  119. # --得分
  120. screen.blit(font.render(str(score_left), False, cfg.WHITE), (150, 10))
  121. screen.blit(font.render(str(score_right), False, cfg.WHITE), (300, 10))
  122. if score_left == 11 or score_right == 11:
  123. return score_left, score_right
  124. clock.tick(100)
  125. pygame.display.update()
  126. '''主函数'''
  127. def main():
  128. # 初始化
  129. pygame.init()
  130. pygame.mixer.init()
  131. screen = pygame.display.set_mode((cfg.WIDTH, cfg.HEIGHT))
  132. pygame.display.set_caption('pingpong —— 九歌')
  133. # 开始游戏
  134. while True:
  135. score_left, score_right = runDemo(screen)
  136. endInterface(screen, score_left, score_right)
  137. '''run'''
  138. if __name__ == '__main__':
  139. main()

四、俄罗斯方块 (2分)

困难难度:❤❤

玩法介绍:小时候的经典游戏,加速玩法有一点点难度。

 源码分享

  1. import os
  2. import sys
  3. import random
  4. from modules import *
  5. from PyQt5.QtGui import *
  6. from PyQt5.QtCore import *
  7. from PyQt5.QtWidgets import *
  8. '''定义俄罗斯方块游戏类'''
  9. class TetrisGame(QMainWindow):
  10. def __init__(self, parent=None):
  11. super(TetrisGame, self).__init__(parent)
  12. # 是否暂停ing
  13. self.is_paused = False
  14. # 是否开始ing
  15. self.is_started = False
  16. self.initUI()
  17. '''界面初始化'''
  18. def initUI(self):
  19. # icon
  20. self.setWindowIcon(QIcon(os.path.join(os.getcwd(), 'resources/icon.jpg')))
  21. # 块大小
  22. self.grid_size = 22
  23. # 游戏帧率
  24. self.fps = 200
  25. self.timer = QBasicTimer()
  26. # 焦点
  27. self.setFocusPolicy(Qt.StrongFocus)
  28. # 水平布局
  29. layout_horizontal = QHBoxLayout()
  30. self.inner_board = InnerBoard()
  31. self.external_board = ExternalBoard(self, self.grid_size, self.inner_board)
  32. layout_horizontal.addWidget(self.external_board)
  33. self.side_panel = SidePanel(self, self.grid_size, self.inner_board)
  34. layout_horizontal.addWidget(self.side_panel)
  35. self.status_bar = self.statusBar()
  36. self.external_board.score_signal[str].connect(self.status_bar.showMessage)
  37. self.start()
  38. self.center()
  39. self.setWindowTitle('Tetris —— 九歌')
  40. self.show()
  41. self.setFixedSize(self.external_board.width() + self.side_panel.width(), self.side_panel.height() + self.status_bar.height())
  42. '''游戏界面移动到屏幕中间'''
  43. def center(self):
  44. screen = QDesktopWidget().screenGeometry()
  45. size = self.geometry()
  46. self.move((screen.width() - size.width()) // 2, (screen.height() - size.height()) // 2)
  47. '''更新界面'''
  48. def updateWindow(self):
  49. self.external_board.updateData()
  50. self.side_panel.updateData()
  51. self.update()
  52. '''开始'''
  53. def start(self):
  54. if self.is_started:
  55. return
  56. self.is_started = True
  57. self.inner_board.createNewTetris()
  58. self.timer.start(self.fps, self)
  59. '''暂停/不暂停'''
  60. def pause(self):
  61. if not self.is_started:
  62. return
  63. self.is_paused = not self.is_paused
  64. if self.is_paused:
  65. self.timer.stop()
  66. self.external_board.score_signal.emit('Paused')
  67. else:
  68. self.timer.start(self.fps, self)
  69. self.updateWindow()
  70. '''计时器事件'''
  71. def timerEvent(self, event):
  72. if event.timerId() == self.timer.timerId():
  73. removed_lines = self.inner_board.moveDown()
  74. self.external_board.score += removed_lines
  75. self.updateWindow()
  76. else:
  77. super(TetrisGame, self).timerEvent(event)
  78. '''按键事件'''
  79. def keyPressEvent(self, event):
  80. if not self.is_started or self.inner_board.current_tetris == tetrisShape().shape_empty:
  81. super(TetrisGame, self).keyPressEvent(event)
  82. return
  83. key = event.key()
  84. # P键暂停
  85. if key == Qt.Key_P:
  86. self.pause()
  87. return
  88. if self.is_paused:
  89. return
  90. # 向左
  91. elif key == Qt.Key_Left:
  92. self.inner_board.moveLeft()
  93. # 向右
  94. elif key == Qt.Key_Right:
  95. self.inner_board.moveRight()
  96. # 旋转
  97. elif key == Qt.Key_Up:
  98. self.inner_board.rotateAnticlockwise()
  99. # 快速坠落
  100. elif key == Qt.Key_Space:
  101. self.external_board.score += self.inner_board.dropDown()
  102. else:
  103. super(TetrisGame, self).keyPressEvent(event)
  104. self.updateWindow()
  105. '''run'''
  106. if __name__ == '__main__':
  107. app = QApplication([])
  108. tetris = TetrisGame()
  109. sys.exit(app.exec_())

 五、消消乐(2分)

困难难度:❤❤

玩法介绍:以前经典的游戏,给我感觉趣味性少了点

源码分享 

  1. import os
  2. import sys
  3. import cfg
  4. import pygame
  5. from modules import *
  6. '''游戏主程序'''
  7. def main():
  8. pygame.init()
  9. screen = pygame.display.set_mode(cfg.SCREENSIZE)
  10. pygame.display.set_caption('Gemgem —— 九歌')
  11. # 加载背景音乐
  12. pygame.mixer.init()
  13. pygame.mixer.music.load(os.path.join(cfg.ROOTDIR, "resources/audios/bg.mp3"))
  14. pygame.mixer.music.set_volume(0.6)
  15. pygame.mixer.music.play(-1)
  16. # 加载音效
  17. sounds = {}
  18. sounds['mismatch'] = pygame.mixer.Sound(os.path.join(cfg.ROOTDIR, 'resources/audios/badswap.wav'))
  19. sounds['match'] = []
  20. for i in range(6):
  21. sounds['match'].append(pygame.mixer.Sound(os.path.join(cfg.ROOTDIR, 'resources/audios/match%s.wav' % i)))
  22. # 加载字体
  23. font = pygame.font.Font(os.path.join(cfg.ROOTDIR, 'resources/font/font.TTF'), 25)
  24. # 图片加载
  25. gem_imgs = []
  26. for i in range(1, 8):
  27. gem_imgs.append(os.path.join(cfg.ROOTDIR, 'resources/images/gem%s.png' % i))
  28. # 主循环
  29. game = gemGame(screen, sounds, font, gem_imgs, cfg)
  30. while True:
  31. score = game.start()
  32. flag = False
  33. # 一轮游戏结束后玩家选择重玩或者退出
  34. while True:
  35. for event in pygame.event.get():
  36. if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE):
  37. pygame.quit()
  38. sys.exit()
  39. elif event.type == pygame.KEYUP and event.key == pygame.K_r:
  40. flag = True
  41. if flag:
  42. break
  43. screen.fill((135, 206, 235))
  44. text0 = 'Final score: %s' % score
  45. text1 = 'Press <R> to restart the game.'
  46. text2 = 'Press <Esc> to quit the game.'
  47. y = 150
  48. for idx, text in enumerate([text0, text1, text2]):
  49. text_render = font.render(text, 1, (85, 65, 0))
  50. rect = text_render.get_rect()
  51. if idx == 0:
  52. rect.left, rect.top = (212, y)
  53. elif idx == 1:
  54. rect.left, rect.top = (122.5, y)
  55. else:
  56. rect.left, rect.top = (126.5, y)
  57. y += 100
  58. screen.blit(text_render, rect)
  59. pygame.display.update()
  60. game.reset()
  61. '''run'''
  62. if __name__ == '__main__':
  63. main()

 六、飞机大战(2分)

困难难度:❤❤

玩法介绍:并夕夕版飞机大战,也是个双人小游戏。

七、保卫森林(3分)

抓狂难度:❤❤❤

玩法介绍:塔防类小游戏,老白喜欢玩的一类,比较考验策略和大局安排。

八、24点游戏(3分)

抓狂难度:❤❤❤

玩法介绍:几个数字通过加减乘除最后结果为24,老白开始觉得很简单,后面越玩越觉得有点意思。

九、外星人入侵(3分)

抓狂难度:❤❤❤

玩法介绍:打怪走位都要控制好,还是考验操作的。

十、坦克大战(3分)

抓狂难度:❤❤❤

玩法介绍:强烈推荐,童年系列来了,太好玩了,现在玩也不觉得过时,双人玩美滋滋。

十一、吃豆豆(4分)

恶梦难度:❤❤❤❤

玩法介绍:这个很考验走位啊,一不小心就没了。

十二、推箱子(4分)

恶梦难度:❤❤❤❤

​​​​​​​玩法介绍:小时候拿着大人的手机玩这个,越到后面越玩,有通过的大佬吗?

十三、2048(4分)

恶梦难度:❤❤❤❤

 ​​​​​​​玩法介绍:也是一款经典的游戏,前面还好,越到后面越难。

十四、塔防(5分)

地狱难度:❤❤❤❤❤

​​​​​​​玩法介绍:大家到的我可没加速,就是这么快,坚持三分钟就不容易了。

十五、拼图(5分)

地狱难度:❤❤❤❤❤

玩法介绍:这个拼图我认为在这个十五个里算是最难的,真的很难拼!

 其他源码领取点击

往期推送


爆肝熬夜整理了Python最新学习资料和视频,分享给学弟学妹们【全站最新】

再次爆肝熬夜整理Python编程工具、面试题库、学习资料总集,结尾自取【免费分享】

我用Python空闲时月入10000+方法分享,8000粉丝福利

我是白云,一名喜欢分享知识的程序员,感兴趣的就赶紧来点击关注我把,哪里有不明白或有不同观点的地方欢迎留言!
 

原文链接:https://blog.csdn.net/lland5201314/article/details/118936201



所属网站分类: 程序员的那点事

作者:天使是怎样炼成的

链接:http://www.pythonpdf.com/blog/article/257/1f032898f9dee065cff1/

来源:编程知识网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

24 0
收藏该文
已收藏

评论内容:(最多支持255个字符)