基于Python-Pycharm实现的猴子摘桃小游戏(源代码)
程序员文章站
2022-07-04 16:55:07
源码及注释:import pygamefrom sys import exitfrom random import randintimport timeimport os# 定义窗口分辨率screen...
源码及注释:
import pygame from sys import exit from random import randint import time import os # 定义窗口分辨率 screen_width = 700 screen_height = 600 current_path = os.path.abspath(os.path.dirname(__file__)) root_path = current_path[:current_path.find("monkey-picking-peach\\") + len("monkey-picking-peach\\")] \ + "resource\\images\\" # 图片 background_image_path = root_path + "background.jpg" monkey_image_path = root_path + "monkey.png" apple_image_path = root_path + "apple.png" jump_status = false over_flag = false start_time = none offset = {pygame.k_left: 0, pygame.k_right: 0, pygame.k_up: 0, pygame.k_down: 0} # 定义画面帧率 frame_rate = 60 # 定义动画周期(帧数) animate_cycle = 30 ticks = 0 clock = pygame.time.clock() # 猴子类 class monkey(pygame.sprite.sprite): # 苹果的数量 apple_num = 0 def __init__(self, mon_surface, monkey_pos): pygame.sprite.sprite.__init__(self) self.image = mon_surface self.rect = self.image.get_rect() self.rect.topleft = monkey_pos self.speed = 5 # 控制猴子的移动 def move(self, _offset): global jump_status x = self.rect.left + _offset[pygame.k_right] - _offset[pygame.k_left] y = self.rect.top + _offset[pygame.k_down] - _offset[pygame.k_up] if y < 0: self.rect.top = 0 jump_status = true elif y >= screen_height - self.rect.height: self.rect.top = screen_height - self.rect.height jump_status = false else: self.rect.top = y jump_status = true if x < 0: self.rect.left = 0 elif x > screen_width - self.rect.width: self.rect.left = screen_width - self.rect.width else: self.rect.left = x # 接苹果 def picking_apple(self, app_group): # 判断接到几个苹果 picked_apples = pygame.sprite.spritecollide(self, app_group, true) # 添加分数 self.apple_num += len(picked_apples) # 接到的苹果消失 for picked_apple in picked_apples: picked_apple.kill() # 苹果类 class apple(pygame.sprite.sprite): def __init__(self, app_surface, apple_pos): pygame.sprite.sprite.__init__(self) self.image = app_surface self.rect = self.image.get_rect() self.rect.topleft = apple_pos self.speed = 1 def update(self): global start_time if start_time is none: start_time = time.time() self.rect.top += (self.speed * (1 + (time.time() - start_time) / 40)) if self.rect.top > screen_height: # 苹果落地游戏结束 global over_flag over_flag = true self.kill() # 初始化游戏 pygame.init() screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("猴子接苹果") # 载入图片 background_surface = pygame.image.load(background_image_path).convert() monkey_surface = pygame.image.load(monkey_image_path).convert_alpha() apple_surface = pygame.image.load(apple_image_path).convert_alpha() # 创建猴子 monkey = monkey(monkey_surface, (200, 500)) # 创建苹果组 apple_group = pygame.sprite.group() # 分数字体 score_font = pygame.font.sysfont("arial", 40) # 主循环 while true: if over_flag: break # 控制游戏最大帧率 clock.tick(frame_rate) # 绘制背景 screen.blit(background_surface, (0, 0)) if ticks >= animate_cycle: ticks = 0 # 产生苹果 if ticks % 30 == 0: apple = apple(apple_surface, [randint(0, screen_width - apple_surface.get_width()), -apple_surface.get_height()]) apple_group.add(apple) # 控制苹果 apple_group.update() # 绘制苹果组 apple_group.draw(screen) # 绘制猴子 screen.blit(monkey_surface, monkey.rect) ticks += 1 # 接苹果 monkey.picking_apple(apple_group) # 更新分数 score_surface = score_font.render(str(monkey.apple_num), true, (0, 0, 255)) screen.blit(score_surface, (620, 10)) # 更新屏幕 pygame.display.update() for event in pygame.event.get(): if event.type == pygame.quit: pygame.quit() exit() # 控制方向 if event.type == pygame.keydown: if event.key in offset: if event.key == pygame.k_up: offset[event.key] = 80 else: offset[event.key] = monkey.speed elif event.type == pygame.keyup: if event.key in offset: offset[event.key] = 0 # 移动猴子 if jump_status: offset[pygame.k_down] = 5 offset[pygame.k_up] = 0 monkey.move(offset) # 游戏结束推出界面 score_surface = score_font.render(str(monkey.apple_num), true, (0, 0, 255)) over_surface = score_font.render(u"game over!", true, (0, 0, 255)) screen.blit(background_surface, (0, 0)) screen.blit(score_surface, (620, 10)) screen.blit(over_surface, (250, 270)) while true: pygame.display.update() for event in pygame.event.get(): if event.type == pygame.quit: pygame.quit() exit()
食用指南: 使用的图片
monkey.png:
background.jpg:
apple.png:
这是我的文件目录,学习者也可改为自己的:
更改的代码位置:
root_path = current_path[:current_path.find("monkey-picking-peach\\") + len("monkey-picking-peach\\")] \ + "resource\\images\\"
游戏截图:
到此这篇关于基于python-pycharm实现的猴子摘桃小游戏的文章就介绍到这了,更多相关python 猴子摘桃小游戏内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 帝瓦雷108dB 和103dB旗舰新品Phantom I试听体验
下一篇: 炖出的鱼汤太腥怎么办