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