The most popular thing today is to write the effect of fishing masters in Python. Needless to say, bright code~~~
# coding:utf-8
# Import module
import pygame,sys,time,random
from pygame.locals import*
# Initialize the pygame environment
pygame.init()
# Create a length and width of 800/480 windows
canvas = pygame.display.set_mode((800,480))
canvas.fill((255,255,255))
# Set window title
pygame.display.set_caption('Fishing expert')
# Load picture
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")
# Define event listener function
def handleEvent():for event in pygame.event.get():if event.type == QUIT:
pygame.quit()
sys.exit()
# Add mouse movement events, let the mouse control the movement of the net
if event.type == MOUSEMOTION:
Game.net.x = event.pos[0]- Game.net.width/2
Game.net.y = event.pos[1]- Game.net.height/2
# Define the time interval judgment function
def isActionTime(lastTime,interval):if lastTime ==0:return True
currentTime = time.time()return currentTime - lastTime = interval
# Define fish
classFish():
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
# Define net class
classNet():
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))
# Define out-of-bounds function
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
# Define collision function
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
# Define the class for storing game data
classGame():
# Game state
state ='RUNNING'
# List of fish
fish =[]
# Net object
net =Net(100,100)
# fraction
score =0
# time
t =60
n =1
# Last time
lastTime =0
# time interval
interval =0.5
# Width and height of all fish
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]]
# Define the function that produces fish
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")))
# Define drawing component function
def conPaint():
canvas.blit(bg,(0,0))
Game.net.paint()showScore()showTime()for fish in Game.fish:
fish.paint()
# Define component movement function
def conStep():
Game.net.outOfBounds()for fish in Game.fish:
fish.step()
# Define collision detection function
def checkHit():for fish in Game.fish:if Game.net.hit(fish) and len(Game.fish)!=0:
Game.fish.remove(fish)
Game.score +=1
# Define the plot score function
def showScore():
TextFont = pygame.font.SysFont('SimHei',40)
TextScore = TextFont.render('Score:'+str(Game.score),True,(255,255,255))
canvas.blit(TextScore,(20,20))
# Define drawing time function
def showTime():
TextFont = pygame.font.SysFont('SimHei',40)
TextScore = TextFont.render('the remaining time:'+str(Game.t),True,(255,255,255))
canvas.blit(TextScore,(550,20))if Game.n %50==1:
Game.t -=1
Game.n +=1if Game.t ==0:
Game.state ='END'
# Define the main control function
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('Final score:'+str(Game.score),True,(0,0,0))
canvas.blit(TextScore,(50,50))while True:
# Call the main control function
control()
# Update screen content
pygame.display.update()
# 10 ms delay
pygame.time.delay(10)
# Listen for events
handleEvent()
This code uses some basic knowledge of Python, including events, defining functions, taking remainders, loops, judgments, defining classes, creating objects, etc. There is nothing to say about this. The imported libraries are also very commonly used libraries, which are basically necessary for programmers. The code is put here mainly for reference. If I can’t write it, I’m so embarrassed to continue writing Python...
You can use my code, it should be more convenient when doing event monitoring and other functions.
I posted the picture below, and you need to pick it up.
Source download
So far, this article about the implementation of the game of writing fishing masters in Python is introduced. For more related content of fishing masters in Python, please search the previous articles of ZaLou.Cn or continue to browse related articles below. Hope you will have more in the future. Support ZaLou.Cn!
Recommended Posts