• 售前

  • 售后

热门帖子
入门百科

基于Python-Pycharm实现的猴子摘桃小游戏(源代码)

[复制链接]
李松泰李c 显示全部楼层 发表于 2021-10-25 19:52:40 |阅读模式 打印 上一主题 下一主题
源码及注释:
  1. import pygame
  2. from sys import exit
  3. from random import randint
  4. import time
  5. import os
  6. # 定义窗口分辨率
  7. SCREEN_WIDTH = 700
  8. SCREEN_HEIGHT = 600
  9. current_path = os.path.abspath(os.path.dirname(__file__))
  10. root_path = current_path[:current_path.find("monkey-picking-peach\") + len("monkey-picking-peach\")] \
  11.    + "resource\\images\"
  12. # 图片
  13. BACKGROUND_IMAGE_PATH = root_path + "background.jpg"
  14. MONKEY_IMAGE_PATH = root_path + "monkey.png"
  15. APPLE_IMAGE_PATH = root_path + "apple.png"
  16. JUMP_STATUS = False
  17. OVER_FLAG = False
  18. START_TIME = None
  19. offset = {pygame.K_LEFT: 0, pygame.K_RIGHT: 0, pygame.K_UP: 0, pygame.K_DOWN: 0}
  20. # 定义画面帧率
  21. FRAME_RATE = 60
  22. # 定义动画周期(帧数)
  23. ANIMATE_CYCLE = 30
  24. ticks = 0
  25. clock = pygame.time.Clock()
  26. # 猴子类
  27. class Monkey(pygame.sprite.Sprite):
  28. # 苹果的数量
  29. apple_num = 0
  30. def __init__(self, mon_surface, monkey_pos):
  31.   pygame.sprite.Sprite.__init__(self)
  32.   self.image = mon_surface
  33.   self.rect = self.image.get_rect()
  34.   self.rect.topleft = monkey_pos
  35.   self.speed = 5
  36. # 控制猴子的移动
  37. def move(self, _offset):
  38.   global JUMP_STATUS
  39.   x = self.rect.left + _offset[pygame.K_RIGHT] - _offset[pygame.K_LEFT]
  40.   y = self.rect.top + _offset[pygame.K_DOWN] - _offset[pygame.K_UP]
  41.   if y < 0:
  42.    self.rect.top = 0
  43.    JUMP_STATUS = True
  44.   elif y >= SCREEN_HEIGHT - self.rect.height:
  45.    self.rect.top = SCREEN_HEIGHT - self.rect.height
  46.    JUMP_STATUS = False
  47.   else:
  48.    self.rect.top = y
  49.    JUMP_STATUS = True
  50.   if x < 0:
  51.    self.rect.left = 0
  52.   elif x > SCREEN_WIDTH - self.rect.width:
  53.    self.rect.left = SCREEN_WIDTH - self.rect.width
  54.   else:
  55.    self.rect.left = x
  56. # 接苹果
  57. def picking_apple(self, app_group):
  58.   # 判断接到几个苹果
  59.   picked_apples = pygame.sprite.spritecollide(self, app_group, True)
  60.   # 添加分数
  61.   self.apple_num += len(picked_apples)
  62.   # 接到的苹果消失
  63.   for picked_apple in picked_apples:
  64.    picked_apple.kill()
  65. # 苹果类
  66. class Apple(pygame.sprite.Sprite):
  67. def __init__(self, app_surface, apple_pos):
  68.   pygame.sprite.Sprite.__init__(self)
  69.   self.image = app_surface
  70.   self.rect = self.image.get_rect()
  71.   self.rect.topleft = apple_pos
  72.   self.speed = 1
  73. def update(self):
  74.   global START_TIME
  75.   if START_TIME is None:
  76.    START_TIME = time.time()
  77.   self.rect.top += (self.speed * (1 + (time.time() - START_TIME) / 40))
  78.   if self.rect.top > SCREEN_HEIGHT:
  79.    # 苹果落地游戏结束
  80.    global OVER_FLAG
  81.    OVER_FLAG = True
  82.    self.kill()
  83. # 初始化游戏
  84. pygame.init()
  85. screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  86. pygame.display.set_caption("猴子接苹果")
  87. # 载入图片
  88. background_surface = pygame.image.load(BACKGROUND_IMAGE_PATH).convert()
  89. monkey_surface = pygame.image.load(MONKEY_IMAGE_PATH).convert_alpha()
  90. apple_surface = pygame.image.load(APPLE_IMAGE_PATH).convert_alpha()
  91. # 创建猴子
  92. monkey = Monkey(monkey_surface, (200, 500))
  93. # 创建苹果组
  94. apple_group = pygame.sprite.Group()
  95. # 分数字体
  96. score_font = pygame.font.SysFont("arial", 40)
  97. # 主循环
  98. while True:
  99. if OVER_FLAG:
  100.   break
  101. # 控制游戏最大帧率
  102. clock.tick(FRAME_RATE)
  103. # 绘制背景
  104. screen.blit(background_surface, (0, 0))
  105. if ticks >= ANIMATE_CYCLE:
  106.   ticks = 0
  107. # 产生苹果
  108. if ticks % 30 == 0:
  109.   apple = Apple(apple_surface,
  110.       [randint(0, SCREEN_WIDTH - apple_surface.get_width()), -apple_surface.get_height()])
  111.   apple_group.add(apple)
  112. # 控制苹果
  113. apple_group.update()
  114. # 绘制苹果组
  115. apple_group.draw(screen)
  116. # 绘制猴子
  117. screen.blit(monkey_surface, monkey.rect)
  118. ticks += 1
  119. # 接苹果
  120. monkey.picking_apple(apple_group)
  121. # 更新分数
  122. score_surface = score_font.render(str(monkey.apple_num), True, (0, 0, 255))
  123. screen.blit(score_surface, (620, 10))
  124. # 更新屏幕
  125. pygame.display.update()
  126. for event in pygame.event.get():
  127.   if event.type == pygame.QUIT:
  128.    pygame.quit()
  129.    exit()
  130.   # 控制方向
  131.   if event.type == pygame.KEYDOWN:
  132.    if event.key in offset:
  133.     if event.key == pygame.K_UP:
  134.      offset[event.key] = 80
  135.     else:
  136.      offset[event.key] = monkey.speed
  137.   elif event.type == pygame.KEYUP:
  138.    if event.key in offset:
  139.     offset[event.key] = 0
  140. # 移动猴子
  141. if JUMP_STATUS:
  142.   offset[pygame.K_DOWN] = 5
  143.   offset[pygame.K_UP] = 0
  144. monkey.move(offset)
  145. # 游戏结束推出界面
  146. score_surface = score_font.render(str(monkey.apple_num), True, (0, 0, 255))
  147. over_surface = score_font.render(u"Game Over!", True, (0, 0, 255))
  148. screen.blit(background_surface, (0, 0))
  149. screen.blit(score_surface, (620, 10))
  150. screen.blit(over_surface, (250, 270))
  151. while True:
  152. pygame.display.update()
  153. for event in pygame.event.get():
  154.   if event.type == pygame.QUIT:
  155.    pygame.quit()
  156.    exit()
复制代码
食用指南: 使用的图片
monkey.png:

background.jpg:

apple.png:

这是我的文件目录,学习者也可改为本身的:

更改的代码位置:
  1. root_path = current_path[:current_path.find("monkey-picking-peach\") + len("monkey-picking-peach\")] \
  2.    + "resource\\images\"
复制代码
游戏截图:

到此这篇关于基于Python-Pycharm实现的猴子摘桃小游戏的文章就介绍到这了,更多相干Python 猴子摘桃小游戏内容请搜索草根技术分享从前的文章或继续欣赏下面的相干文章盼望各人以后多多支持草根技术分享!

本帖子中包含更多资源

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

x

帖子地址: 

回复

使用道具 举报

分享
推广
火星云矿 | 预约S19Pro,享500抵1000!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

草根技术分享(草根吧)是全球知名中文IT技术交流平台,创建于2021年,包含原创博客、精品问答、职业培训、技术社区、资源下载等产品服务,提供原创、优质、完整内容的专业IT技术开发社区。
  • 官方手机版

  • 微信公众号

  • 商务合作