基于Python-Pycharm实现的猴子摘桃小游戏(源代码)
作者:一个超会写Bug的安太狼 时间:2023-07-07 06:31:22
源码及注释:
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\\"
游戏截图:
来源:https://blog.csdn.net/m0_46278037/article/details/113872428
标签:Python,Pycharm,猴子摘桃,小游戏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
跟老齐学Python之关于循环的小伎俩
2022-07-20 07:03:36
Python操作MySQL数据库的两种方式实例分析【pymysql和pandas】
2024-01-13 13:34:59
![](https://img.aspxhome.com/file/2023/8/78688_0s.jpg)
Python实现将长图制作成一个视频
2022-09-28 06:09:09
vue+iview实现手机号分段输入框
2024-04-26 17:40:35
![](https://img.aspxhome.com/file/2023/2/133132_0s.jpg)
抽取oracle数据到mysql数据库的实现过程
2024-01-14 03:07:12
一个免刷新页面的JavaScript日历
2007-12-26 12:57:00
![](https://img.aspxhome.com/file/UploadPic/200712/26/20071226131443435s.jpg)
Pytorch模型迁移和迁移学习,导入部分模型参数的操作
2021-08-05 09:18:16
![](https://img.aspxhome.com/file/2023/5/94095_0s.jpg)
Python发送邮件封装实现过程详解
2021-06-13 09:19:41
![](https://img.aspxhome.com/file/2023/4/118744_0s.png)
Mysql 数据库双机热备的配置方法
2010-06-09 19:13:00
python实现SOM算法
2023-05-27 03:18:42
![](https://img.aspxhome.com/file/2023/8/117598_0s.png)
Django原生sql也能使用Paginator分页的示例代码
2023-11-09 10:00:06
XMLHttp ASP远程获取网页内容代码
2011-04-10 10:41:00
php的优点总结 php有哪些优点
2023-11-14 16:03:25
Python利用Pillow(PIL)库实现验证码图片的全过程
2022-05-18 21:27:47
![](https://img.aspxhome.com/file/2023/7/81457_0s.png)
详解Vue 事件驱动和依赖追踪
2024-04-10 10:32:10
![](https://img.aspxhome.com/file/2023/0/139770_0s.png)
python实现计算器功能
2021-02-06 09:45:23
![](https://img.aspxhome.com/file/2023/2/86612_0s.jpg)
Python 如何给图像分类(图像识别模型构建)
2022-07-24 20:34:08
Django rest framework分页接口实现原理解析
2023-08-22 21:26:36
![](https://img.aspxhome.com/file/2023/8/62548_0s.png)
对比国内门户网站对Flash激活限制的处理
2007-08-23 11:29:00
python txt中的文件,逐行读取并且每行赋值给变量问题
2021-04-28 21:12:02
![](https://img.aspxhome.com/file/2023/1/117341_0s.png)