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


Posted in Python onFebruary 20, 2021

源码及注释:

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:

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

background.jpg:

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

apple.png:

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

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

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

更改的代码位置:

root_path = current_path[:current_path.find("monkey-picking-peach\\") + len("monkey-picking-peach\\")] \
   + "resource\\images\\"

游戏截图:

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

到此这篇关于基于Python-Pycharm实现的猴子摘桃小游戏的文章就介绍到这了,更多相关Python 猴子摘桃小游戏内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python进度条实时显示处理进度的示例代码
Jan 30 Python
python list元素为tuple时的排序方法
Apr 18 Python
python 重命名轴索引的方法
Nov 10 Python
Python字典遍历操作实例小结
Mar 05 Python
Python3.5装饰器典型案例分析
Apr 30 Python
Falsk 与 Django 过滤器的使用与区别详解
Jun 04 Python
pandas基于时间序列的固定时间间隔求均值的方法
Jul 04 Python
windows下python虚拟环境virtualenv安装和使用详解
Jul 16 Python
python 插入日期数据到Oracle实例
Mar 02 Python
简单了解python shutil模块原理及使用方法
Apr 28 Python
关于探究python中sys.argv时遇到的问题详解
Feb 23 Python
Pytorch如何切换 cpu和gpu的使用详解
Mar 01 Python
pandas按条件筛选数据的实现
Feb 20 #Python
python实现b站直播自动发送弹幕功能
Feb 20 #Python
如何用 Python 制作 GitHub 消息助手
Feb 20 #Python
详解tf.device()指定tensorflow运行的GPU或CPU设备实现
Feb 20 #Python
Python 的 f-string 可以连接字符串与数字的原因解析
Feb 20 #Python
安装不同版本的tensorflow与models方法实现
Feb 20 #Python
python爬虫scrapy基本使用超详细教程
Feb 20 #Python
You might like
PHP 向右侧拉菜单实现代码,测试使用中
2009/11/03 PHP
php 代码优化之经典示例
2011/03/24 PHP
php数组相加 array(“a”)+array(“b”)结果还是array(“a”)
2012/09/19 PHP
网页运行时提示对象不支持abigimage属性或方法
2014/08/10 Javascript
jQuery匹配文档链接并添加class的方法
2015/06/26 Javascript
学习JavaScript设计模式之策略模式
2016/01/12 Javascript
表单元素值获取方式js及java方式的简单实例
2016/10/15 Javascript
JavaScript中利用for循环遍历数组
2017/01/15 Javascript
import与export在node.js中的使用详解
2017/09/28 Javascript
mui上拉加载更多下拉刷新数据的封装过程
2017/11/03 Javascript
详解webpack + react + react-router 如何实现懒加载
2017/11/20 Javascript
vue-cli3脚手架的配置及使用教程
2018/08/28 Javascript
jquery登录的异步验证操作示例
2019/05/09 jQuery
Vue.js 中的实用工具方法【推荐】
2019/07/04 Javascript
JS监听组合按键思路及实现过程
2020/04/17 Javascript
[01:23]2014DOTA2国际邀请赛 球迷无处不在Ti现场世界杯受关注
2014/07/10 DOTA
[51:29]Alliance vs TNC 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/18 DOTA
python 中文乱码问题深入分析
2011/03/13 Python
Python中的一些陷阱与技巧小结
2015/07/10 Python
详解Django中的过滤器
2015/07/16 Python
git使用.gitignore设置不生效或不起作用问题的解决方法
2017/06/01 Python
对Python中画图时候的线类型详解
2019/07/07 Python
css3实现画半圆弧线的示例代码
2017/11/06 HTML / CSS
css3新单位vw、vh的使用教程
2018/03/23 HTML / CSS
HTML5之HTML元素扩展(下)—增强的Form表单元素值得关注
2013/01/31 HTML / CSS
波兰家具和室内装饰品购物网站:Vivre
2018/04/10 全球购物
Lookfantastic西班牙官网:英国知名美妆购物网站
2018/06/13 全球购物
DJI全球:DJI Global
2021/03/15 全球购物
集团薪酬管理制度
2014/01/13 职场文书
消防安全宣传标语
2014/06/07 职场文书
小学生纪念九一八事变演讲稿
2014/09/14 职场文书
入党介绍人意见范文
2015/06/01 职场文书
就业推荐表院系意见
2015/06/05 职场文书
经典法律座右铭(50句)
2019/08/15 职场文书
JVM入门之类加载与字节码技术(类加载与类的加载器)
2021/06/15 Java/Android
5行Python代码实现一键批量扣图
2021/06/29 Python