Python写捕鱼达人的游戏实现


Posted in Python onMarch 31, 2020

当今最火的莫过于用Python写出捕鱼达人的效果了。啥都不用说,亮代码~~~

# coding:utf-8
# 导入模块
import pygame,sys,time,random
from pygame.locals import *
# 初始化pygame环境
pygame.init()
# 创建一个长宽分别为800/480的窗口
canvas = pygame.display.set_mode((800,480))
canvas.fill((255,255,255))
# 设置窗口标题
pygame.display.set_caption('捕鱼达人')
# 加载图片
bg = pygame.image.load("./images/bg.jpg")
fish1 = pygame.image.load("./images/fish1_0.png")
fish2 = pygame.image.load("./images/fish2_0.png")
fish3 = pygame.image.load("./images/fish3_0.png")
fish4 = pygame.image.load("./images/fish4_0.png")
fish5 = pygame.image.load("./images/fish5_0.png")
fish6 = pygame.image.load("./images/fish6_0.png")
fish7 = pygame.image.load("./images/fish7_0.png")
fish8 = pygame.image.load("./images/fish8_0.png")
fish9 = pygame.image.load("./images/fish9_0.png")
fish10 = pygame.image.load("./images/fish10_0.png")
fish11 = pygame.image.load("./images/fish11_0.png")
net = pygame.image.load("./images/net.png")
gameover = pygame.image.load("./images/gameover.jpg")
# 定义事件监听函数
def handleEvent():
  for event in pygame.event.get():
    if event.type == QUIT:
      pygame.quit()
      sys.exit()
    # 添加鼠标移动事件,让鼠标控制网的移动
    if event.type == MOUSEMOTION:
      Game.net.x = event.pos[0] - Game.net.width/2
      Game.net.y = event.pos[1] - Game.net.height/2
# 定义时间间隔判断函数
def isActionTime(lastTime,interval):
  if lastTime == 0:
    return True
  currentTime = time.time()
  return currentTime - lastTime >= interval
# 定义鱼类
class Fish():
  def __init__(self,width,height,y,img):
    self.width = width
    self.height = height
    self.x = 800 - self.width
    self.y = y
    self.img = img
  def paint(self):
    canvas.blit(self.img,(self.x,self.y))
  def step(self):
    self.x -= 10
# 定义网类
class Net():
  def __init__(self,x,y):
    self.x = x
    self.y = y
    self.width = 160
    self.height = 160
    self.img = net
  def paint(self):
    canvas.blit(self.img,(self.x,self.y))
  # 定义越界函数
  def outOfBounds(self):
    if self.x <= 0:
      self.x = 0
    elif self.x >= 800 - self.width:
      self.x = 800 - self.width
    elif self.y <= 0:
      self.y = 0
    elif self.y >= 480 - self.height:
      self.y = 480 - self.height
  # 定义碰撞函数
  def hit(self,c):
    return c.x > self.x - c.width and c.x < self.x + self.width and c.y > self.y - c.height and c.y < self.y + self.height
# 定义存储游戏数据的类
class Game():
  # 游戏状态
  state = 'RUNNING'
  # 鱼的列表
  fish = []
  # 网的对象
  net = Net(100,100)
  # 分数
  score = 0
  # 时间
  t = 60
  n = 1
  # 上一次时间
  lastTime = 0
  # 时间间隔
  interval = 0.5
  # 所有鱼的宽高
  fish_pos = [[22,13],[50,48],[55,55],[73,73],[104,80],[60,60],[93,93],[94,81],[99,103],[180,140],[320,206],[100,96]]
# 定义产生鱼的函数
def conEnter():
  if not isActionTime(Game.lastTime,Game.interval):
    return
  Game.lastTime = time.time()
  r = random.randint(1,11)
  if Game.t <= 60:
    Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load("./images/fish"+str(r)+"_0.png")))
  elif Game.t <= 30:
    Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load("./images/fish"+str(r)+"_0.png")))
    Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load("./images/fish"+str(r)+"_0.png")))
  elif Game.t <= 10:
    Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load("./images/fish"+str(r)+"_0.png")))
    Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load("./images/fish"+str(r)+"_0.png")))
    Game.fish.append(Fish(Game.fish_pos[r][0],Game.fish_pos[r][1],random.randint(0,480 - Game.fish_pos[r][1]),pygame.image.load("./images/fish"+str(r)+"_0.png")))
# 定义画组件函数
def conPaint():
  canvas.blit(bg,(0,0))
  Game.net.paint()
  showScore()
  showTime()
  for fish in Game.fish:
    fish.paint()
# 定义组件移动函数
def conStep():
  Game.net.outOfBounds()
  for fish in Game.fish:
    fish.step()
# 定义碰撞检测函数
def checkHit():
  for fish in Game.fish:
    if Game.net.hit(fish) and len(Game.fish) != 0:
      Game.fish.remove(fish)
      Game.score += 1
# 定义绘制分数函数
def showScore():
  TextFont = pygame.font.SysFont('SimHei',40)
  TextScore = TextFont.render('得分:'+str(Game.score),True,(255,255,255))
  canvas.blit(TextScore,(20,20))
# 定义绘制时间函数
def showTime():
  TextFont = pygame.font.SysFont('SimHei',40)
  TextScore = TextFont.render('剩余时间:'+str(Game.t),True,(255,255,255))
  canvas.blit(TextScore,(550,20))
  if Game.n % 50 == 1:
    Game.t -= 1
  Game.n += 1
  if Game.t == 0:
    Game.state = 'END'
# 定义主控制函数
def control():
  if Game.state == 'RUNNING':
    conEnter()
    conPaint()
    conStep()
    checkHit()
  elif Game.state == 'END':
    canvas.blit(gameover,(0,0))
    TextFont = pygame.font.SysFont('SimHei',40)
    TextScore = TextFont.render('最终得分:'+str(Game.score),True,(0,0,0))
    canvas.blit(TextScore,(50,50))
while True:
  # 调用主控制函数
  control()
  # 更新屏幕内容
  pygame.display.update()
  # 延迟10毫秒
  pygame.time.delay(10)
  # 监听事件
  handleEvent()

这段代码用了一些Python的基础知识,包括事件,定义函数,取余,循环,判断,定义类,创建对象等。这些没什么好说的。导入的几个库也是很常用的库,基本算是程序员必备。把代码摆这里主要是为了让大家借鉴。要是写不出来真是没脸继续写Python了…

大家可以利用我的代码,在做事件监听等函数时应该会方便一些。

图片我发在下面了哈,需要的自取。

到此这篇关于Python写捕鱼达人的游戏实现的文章就介绍到这了,更多相关Python 捕鱼达人内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python开发编码规范
Sep 08 Python
实例讲解Python中的私有属性
Aug 21 Python
python利用datetime模块计算时间差
Aug 04 Python
python下读取公私钥做加解密实例详解
Mar 29 Python
Tornado协程在python2.7如何返回值(实现方法)
Jun 22 Python
利用Python批量压缩png方法实例(支持过滤个别文件与文件夹)
Jul 30 Python
python3中的md5加密实例
May 29 Python
python matlibplot绘制3D图形
Jul 02 Python
python文件拆分与重组实例
Dec 10 Python
Django实现跨域请求过程详解
Jul 25 Python
python中用logging实现日志滚动和过期日志删除功能
Aug 20 Python
python实现简单俄罗斯方块
Mar 13 Python
Django 多对多字段的更新和插入数据实例
Mar 31 #Python
基于python爬取有道翻译过程图解
Mar 31 #Python
django实现将修改好的新模型写入数据库
Mar 31 #Python
Python urlencode和unquote函数使用实例解析
Mar 31 #Python
Python响应对象text属性乱码解决方案
Mar 31 #Python
django执行数据库查询之后实现返回的结果集转json
Mar 31 #Python
Python super()方法原理详解
Mar 31 #Python
You might like
星际争霸任务指南——神族
2020/03/04 星际争霸
php生成excel文件的简单方法
2014/02/08 PHP
php cookie中点号(句号)自动转为下划线问题
2014/10/21 PHP
PHP下SSL加密解密、验证、签名方法(很简单)
2020/06/28 PHP
PHP编写文件多服务器同步程序
2016/07/02 PHP
PHP实现普通hash分布式算法简单示例
2018/08/06 PHP
PHP封装的数据库模型Model类完整示例【基于PDO】
2019/03/14 PHP
PHP应用跨时区功能的实现方法
2019/03/21 PHP
基于jQuery的可用于选项卡及幻灯的切换插件
2011/03/28 Javascript
jQuery $.get 的妙用 访问本地文本文件
2012/07/12 Javascript
百度判断手机终端并自动跳转js代码及使用实例
2014/06/11 Javascript
jquery使用animate方法实现控制元素移动
2015/03/27 Javascript
WebGL利用FBO完成立方体贴图效果完整实例(附demo源码下载)
2016/01/26 Javascript
基于Marquee.js插件实现的跑马灯效果示例
2017/01/25 Javascript
基于JavaScript实现的折半查找算法示例
2017/04/14 Javascript
JavaScript获取tr td 的三种方式全面总结(推荐)
2017/08/15 Javascript
vue 中引用gojs绘制E-R图的方法示例
2018/08/24 Javascript
vue-cli 3.0 自定义vue.config.js文件,多页构建的方法
2018/09/19 Javascript
浅谈layui 表单元素的选中问题
2019/10/25 Javascript
python实现数据写入excel表格
2018/03/25 Python
浅谈pandas中shift和diff函数关系
2018/04/08 Python
基于python中theano库的线性回归
2018/08/31 Python
python中的不可变数据类型与可变数据类型详解
2018/09/16 Python
python实现日志按天分割
2019/07/22 Python
使用pycharm在本地开发并实时同步到服务器
2019/08/02 Python
Python递归求出列表(包括列表中的子列表)的最大值实例
2020/02/27 Python
意大利拉斐尔时尚购物网:Raffaello Network(支持中文)
2018/11/09 全球购物
遇到的Mysql的面试题
2014/06/29 面试题
大专生简历的自我评价
2013/11/26 职场文书
单位门卫岗位职责
2013/12/20 职场文书
信息学院毕业生自荐信范文
2014/03/04 职场文书
服装设计师求职信
2014/06/04 职场文书
2014广电局实施党的群众路线教育实践活动方案思想汇报
2014/09/22 职场文书
汉语拼音教学反思
2016/02/22 职场文书
python之np.argmax()及对axis=0或者1的理解
2021/06/02 Python
python 字典和列表嵌套用法详解
2021/06/29 Python