您好,登錄后才能下訂單哦!
使用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") 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") fish7 = pygame.image.load("./images/fish7_0.png") fish8 = pygame.image.load("./images/fish8_0.png") fish9 = pygame.image.load("./images/fish9_0.png") fish20 = pygame.image.load("./images/fish20_0.png") fish21 = pygame.image.load("./images/fish21_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()
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。