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文件和目录操作函数小结
Jul 11 Python
简单的抓取淘宝图片的Python爬虫
Dec 25 Python
python批量制作雷达图的实现方法
Jul 26 Python
Python 列表理解及使用方法
Oct 27 Python
用pandas按列合并两个文件的实例
Apr 12 Python
python读取excel指定列数据并写入到新的excel方法
Jul 10 Python
python对离散变量的one-hot编码方法
Jul 11 Python
python提取包含关键字的整行数据方法
Dec 11 Python
Python使用MyQR制作专属动态彩色二维码功能
Jun 04 Python
python 图片去噪的方法示例
Jul 09 Python
Python开发之QT解决无边框界面拖动卡屏问题(附带源码)
May 27 Python
python-opencv 中值滤波{cv2.medianBlur(src, ksize)}的用法
Jun 05 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
PHP Directory 函数的详解
2013/03/07 PHP
CI框架开发新浪微博登录接口源码完整版
2014/05/28 PHP
增强的 JavaScript 的 trim 函数的代码
2007/08/13 Javascript
jquery插件 cluetip 关键词注释
2010/01/12 Javascript
jQuery 入门级学习笔记及源码
2010/01/22 Javascript
javascript 命名规则 变量命名规则
2010/02/25 Javascript
JavaScript中使用stopPropagation函数停止事件传播例子
2014/08/27 Javascript
Javascript字符串拼接小技巧(推荐)
2016/06/02 Javascript
完美JQuery图片切换效果的简单实现
2016/07/21 Javascript
AngularJS ng-change 指令的详解及简单实例
2016/07/30 Javascript
浅谈jQuery hover(over, out)事件函数
2016/12/03 Javascript
深入理解AngularJS中的ng-bind-html指令
2017/03/27 Javascript
JavaScript选取(picking)和反选(rejecting)对象的属性方法
2017/08/16 Javascript
自适应布局meta标签中viewport、content、width、initial-scale、minimum-scale、maximum-scale总结
2017/08/18 Javascript
vue select二级联动第二级默认选中第一个option值的实例
2018/01/10 Javascript
JavaScript实现计算多边形质心的方法示例
2018/01/31 Javascript
解决easyui日期时间框ie的兼容的问题
2018/03/01 Javascript
vue iview组件表格 render函数的使用方法详解
2018/03/15 Javascript
JS监听事件的叠加和移除功能
2018/11/19 Javascript
Angular(5.2-&gt;6.1)升级小结
2018/12/27 Javascript
[02:22:36]《加油!DOTA》总决赛
2014/09/19 DOTA
python 中文乱码问题深入分析
2011/03/13 Python
详解Python 序列化Serialize 和 反序列化Deserialize
2017/08/20 Python
python下解压缩zip文件并删除文件的实例
2018/04/24 Python
Pandas删除数据的几种情况(小结)
2019/06/21 Python
python在OpenCV里实现投影变换效果
2019/08/30 Python
python 截取XML中bndbox的坐标中的图像,另存为jpg的实例
2020/03/10 Python
html svg生成环形进度条的实现方法
2019/09/23 HTML / CSS
化工专业个人的求职信范文
2013/11/28 职场文书
十八届三中全会感言
2014/03/10 职场文书
小学新学期寄语
2014/04/02 职场文书
食品工程专业求职信
2014/06/15 职场文书
采购部年度工作总结
2015/08/13 职场文书
导游词之长城八达岭
2019/09/24 职场文书
HTML+VUE分页实现炫酷物联网大屏功能
2021/05/27 Vue.js
python创建字典及相关管理操作
2022/04/13 Python