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如何通过protobuf实现rpc
Mar 06 Python
python3使用urllib模块制作网络爬虫
Apr 08 Python
python数据处理 根据颜色对图片进行分类的方法
Dec 08 Python
python 实现提取某个索引中某个时间段的数据方法
Feb 01 Python
5款Python程序员高频使用开发工具推荐
Apr 10 Python
Python实现多线程/多进程的TCP服务器
Sep 03 Python
Python版中国省市经纬度
Feb 11 Python
使用Python将Exception异常错误堆栈信息写入日志文件
Apr 08 Python
Python使用xlrd实现读取合并单元格
Jul 09 Python
Python 如何对文件目录操作
Jul 10 Python
python 监控logcat关键字功能
Sep 04 Python
python实现scrapy爬虫每天定时抓取数据的示例代码
Jan 27 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
Netflix将与CLAMP、乙一以及冲方丁等6名知名制作人合伙展开原创动画计划!
2020/03/06 日漫
php函数的常用方法及注意之处小结
2011/07/10 PHP
linux系统下php安装mbstring扩展的二种方法
2014/01/20 PHP
启用Csrf后POST数据时出现的400错误
2015/07/05 PHP
PHP编程开发怎么提高编程效率 提高PHP编程技术
2015/11/09 PHP
PHP反射学习入门示例
2019/06/14 PHP
PHP实现微信提现功能(微信商城)
2019/11/21 PHP
js/html光标定位的实现代码
2013/09/23 Javascript
js实现Select下拉框具有输入功能的方法
2015/02/06 Javascript
jQuery.deferred对象使用详解
2016/03/18 Javascript
js继承实现方法详解
2016/12/16 Javascript
vue数据双向绑定的注意点
2017/06/23 Javascript
angular动态删除ng-repaeat添加的dom节点的方法
2017/07/20 Javascript
Angular使用 ng-img-max 调整浏览器中的图片的示例代码
2017/08/17 Javascript
解决Vue2.0中使用less给元素添加背景图片出现的问题
2018/09/03 Javascript
微信小程序收藏功能的实现代码
2020/06/19 Javascript
Python的Tornado框架异步编程入门实例
2015/04/24 Python
Java分治归并排序算法实例详解
2017/12/12 Python
78行Python代码实现现微信撤回消息功能
2018/07/26 Python
python用plt画图时,cmp设置方法
2018/12/13 Python
python实现一组典型数据格式转换
2018/12/15 Python
Django+JS 实现点击头像即可更改头像的方法示例
2018/12/26 Python
Python 异常处理Ⅳ过程图解
2019/10/18 Python
python  logging日志打印过程解析
2019/10/22 Python
使用Python函数进行模块化的实现
2019/11/15 Python
利用Python计算KS的实例详解
2020/03/03 Python
使用CSS3制作版头动画效果
2020/12/24 HTML / CSS
ECCO英国官网:丹麦鞋履品牌
2019/09/03 全球购物
DeinDesign德国:设计自己的手机壳
2019/12/14 全球购物
小学生防溺水广播稿
2014/01/12 职场文书
奉献爱心演讲稿
2014/09/04 职场文书
房屋租赁协议书
2014/10/18 职场文书
2015年基建工作总结范文
2015/05/23 职场文书
唐山大地震观后感
2015/06/05 职场文书
运动员加油词
2015/07/18 职场文书
Javascript设计模式之原型模式详细
2021/10/05 Javascript