Python写捕鱼达人
当今最火的莫过于用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 = 800 - self.width:
self.x = 800 - self.width
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.y - c.height and c.y |