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 相关文章推荐
pytyon 带有重复的全排列
Aug 13 Python
python选择排序算法实例总结
Jul 01 Python
用Python实现随机森林算法的示例
Aug 24 Python
python2 与 python3 实现共存的方法
Jul 12 Python
Python多进程方式抓取基金网站内容的方法分析
Jun 03 Python
python elasticsearch从创建索引到写入数据的全过程
Aug 04 Python
python创建学生成绩管理系统
Nov 22 Python
python 命名规范知识点汇总
Feb 14 Python
Django 5种类型Session使用方法解析
Apr 29 Python
keras之权重初始化方式
May 21 Python
详解Python利用configparser对配置文件进行读写操作
Nov 03 Python
Python网络编程之ZeroMQ知识总结
Apr 25 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使用mb_check_encoding检查字符串在指定的编码里是否有效
2013/11/07 PHP
php中strtotime函数用法详解
2014/11/15 PHP
PHP中使用foreach()遍历二维数组的简单实例
2016/06/13 PHP
php微信开发接入
2016/08/27 PHP
thinkPHP5 tablib标签库自定义方法详解
2017/05/10 PHP
ExtJS如何设置与获取radio控件的选取状态
2014/01/22 Javascript
nodejs教程之环境安装及运行
2014/11/21 NodeJs
json定义及jquery操作json的方法
2016/10/03 Javascript
angular forEach方法遍历源码解读
2017/01/25 Javascript
JS实现页面打印功能
2017/03/16 Javascript
js使用Promise实现简单的Ajax缓存
2018/11/14 Javascript
JS代码屏蔽F12,右键,粘贴,复制,剪切,选中,操作实例
2019/09/17 Javascript
vue使用screenfull插件实现全屏功能
2020/09/17 Javascript
使用Vant完成通知栏Notify的提示操作
2020/11/11 Javascript
[36:09]Secret vs VG 2019国际邀请赛淘汰赛 败者组 BO3 第一场 8.24
2019/09/10 DOTA
python利用不到一百行代码实现一个小siri
2017/03/02 Python
Python request设置HTTPS代理代码解析
2018/02/12 Python
django admin 后台实现三级联动的示例代码
2018/06/22 Python
python 平衡二叉树实现代码示例
2018/07/07 Python
Python实现识别图片内容的方法分析
2018/07/11 Python
解决Python3中的中文字符编码的问题
2018/07/18 Python
influx+grafana自定义python采集数据和一些坑的总结
2018/09/17 Python
python opencv根据颜色进行目标检测的方法示例
2020/01/15 Python
python 调整图片亮度的示例
2020/12/03 Python
html5桌面通知(Web Notifications)实例解析
2014/07/07 HTML / CSS
phpquery中文手册
2021/03/18 PHP
全球性的在线时尚男装零售商:boohooMAN
2016/12/17 全球购物
UNIONBAY官网:美国青少年服装品牌
2019/03/26 全球购物
如何用JQuery进行表单验证
2013/05/29 面试题
电气工程和自动化自荐信范文
2013/12/25 职场文书
法律专业应届生自荐信范文
2014/01/06 职场文书
2014离婚协议书范文两篇
2014/09/15 职场文书
工作作风懒散检讨书
2014/10/29 职场文书
单位实习鉴定评语
2015/01/04 职场文书
2016年秋季运动会通讯稿
2015/11/25 职场文书
Javascript设计模式之原型模式详细
2021/10/05 Javascript