最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python寫捕魚達人的游戲?qū)崿F(xiàn)

 更新時間:2020年03月31日 10:20:57   作者:新建文本文檔2020  
這篇文章主要介紹了Python寫捕魚達人的游戲?qū)崿F(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

當今最火的莫過于用Python寫出捕魚達人的效果了。啥都不用說,亮代碼~~~

# coding:utf-8
# 導(dǎo)入模塊
import pygame,sys,time,random
from pygame.locals import *
# 初始化pygame環(huán)境
pygame.init()
# 創(chuàng)建一個長寬分別為800/480的窗口
canvas = pygame.display.set_mode((800,480))
canvas.fill((255,255,255))
# 設(shè)置窗口標題
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")
# 定義事件監(jiān)聽函數(shù)
def handleEvent():
  for event in pygame.event.get():
    if event.type == QUIT:
      pygame.quit()
      sys.exit()
    # 添加鼠標移動事件,讓鼠標控制網(wǎng)的移動
    if event.type == MOUSEMOTION:
      Game.net.x = event.pos[0] - Game.net.width/2
      Game.net.y = event.pos[1] - Game.net.height/2
# 定義時間間隔判斷函數(shù)
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
# 定義網(wǎng)類
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))
  # 定義越界函數(shù)
  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
  # 定義碰撞函數(shù)
  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
# 定義存儲游戲數(shù)據(jù)的類
class Game():
  # 游戲狀態(tài)
  state = 'RUNNING'
  # 魚的列表
  fish = []
  # 網(wǎng)的對象
  net = Net(100,100)
  # 分數(shù)
  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]]
# 定義產(chǎn)生魚的函數(shù)
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")))
# 定義畫組件函數(shù)
def conPaint():
  canvas.blit(bg,(0,0))
  Game.net.paint()
  showScore()
  showTime()
  for fish in Game.fish:
    fish.paint()
# 定義組件移動函數(shù)
def conStep():
  Game.net.outOfBounds()
  for fish in Game.fish:
    fish.step()
# 定義碰撞檢測函數(shù)
def checkHit():
  for fish in Game.fish:
    if Game.net.hit(fish) and len(Game.fish) != 0:
      Game.fish.remove(fish)
      Game.score += 1
# 定義繪制分數(shù)函數(shù)
def showScore():
  TextFont = pygame.font.SysFont('SimHei',40)
  TextScore = TextFont.render('得分:'+str(Game.score),True,(255,255,255))
  canvas.blit(TextScore,(20,20))
# 定義繪制時間函數(shù)
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'
# 定義主控制函數(shù)
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:
  # 調(diào)用主控制函數(shù)
  control()
  # 更新屏幕內(nèi)容
  pygame.display.update()
  # 延遲10毫秒
  pygame.time.delay(10)
  # 監(jiān)聽事件
  handleEvent()

這段代碼用了一些Python的基礎(chǔ)知識,包括事件,定義函數(shù),取余,循環(huán),判斷,定義類,創(chuàng)建對象等。這些沒什么好說的。導(dǎo)入的幾個庫也是很常用的庫,基本算是程序員必備。把代碼擺這里主要是為了讓大家借鑒。要是寫不出來真是沒臉繼續(xù)寫Python了…

大家可以利用我的代碼,在做事件監(jiān)聽等函數(shù)時應(yīng)該會方便一些。

圖片我發(fā)在下面了哈,需要的自取。

源碼下載

到此這篇關(guān)于Python寫捕魚達人的游戲?qū)崿F(xiàn)的文章就介紹到這了,更多相關(guān)Python 捕魚達人內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • PyQT5速成教程之Qt Designer介紹與入門

    PyQT5速成教程之Qt Designer介紹與入門

    這篇文章主要介紹了PyQT5速成教程之Qt Designer介紹與入門,本文以PyCharm為例通過實例代碼圖文相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • Python讀取excel文件中的數(shù)據(jù),繪制折線圖及散點圖

    Python讀取excel文件中的數(shù)據(jù),繪制折線圖及散點圖

    這篇文章主要介紹了Python讀取excel文件中的數(shù)據(jù),繪制折線圖及散點圖,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • Python SqlAlchemy動態(tài)添加數(shù)據(jù)表字段實例解析

    Python SqlAlchemy動態(tài)添加數(shù)據(jù)表字段實例解析

    這篇文章主要介紹了Python SqlAlchemy動態(tài)添加數(shù)據(jù)表字段實例解析,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • Python中requests做接口測試的方法

    Python中requests做接口測試的方法

    Requests是一個很實用的Python HTTP客戶端庫,編寫爬蟲和測試服務(wù)器響應(yīng)數(shù)據(jù)時經(jīng)常會用到,本文主要介紹了Python中requests做接口測試的方法,感興趣的可以了解一下
    2021-05-05
  • Python中requirements.txt簡介(推薦)

    Python中requirements.txt簡介(推薦)

    Python項目中必須包含一個?requirements.txt?文件,用于記錄所有依賴包及其精確的版本號,以便新環(huán)境部署,這篇文章主要介紹了Python中requirements.txt簡介,需要的朋友可以參考下
    2022-11-11
  • Python混合使用同步和異步函數(shù)的方法

    Python混合使用同步和異步函數(shù)的方法

    Python是一種非常靈活的編程語言,可以混合使用同步和異步函數(shù)來實現(xiàn)更高效的編程。本文將介紹如何在Python中混合使用同步和異步函數(shù),以及如何在不同場景下選擇合適的函數(shù),感興趣的可以了解一下
    2023-03-03
  • Python類方法@classmethod()的具體使用

    Python類方法@classmethod()的具體使用

    @classmethod 是一個函數(shù)修飾符,它表示接下來的是一個類方法,而對于平常我們見到的則叫做實例方法,本文主要介紹了Python類方法@classmethod()的具體使用,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • python實現(xiàn)隨機梯度下降(SGD)

    python實現(xiàn)隨機梯度下降(SGD)

    這篇文章主要為大家詳細介紹了python實現(xiàn)隨機梯度下降SGD,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 使用Python畫出小人發(fā)射愛心的代碼

    使用Python畫出小人發(fā)射愛心的代碼

    今天小編就為大家分享一篇使用Python畫出小人發(fā)射愛心的代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • 使用Python在Word文檔中添加,刪除和回復(fù)批注

    使用Python在Word文檔中添加,刪除和回復(fù)批注

    在文檔協(xié)作與審閱場景中,高效管理批注是提升團隊效率的關(guān)鍵環(huán)節(jié),下面我們就來看看如何使用Python在Word文檔中實現(xiàn)添加、刪除和回復(fù)批注的操作吧
    2025-03-03

最新評論

无为县| 灌南县| 云梦县| 富蕴县| 华池县| 吉木萨尔县| 马山县| 桃园市| 凤城市| 汪清县| 中牟县| 遂宁市| 达孜县| 忻州市| 石楼县| 阿城市| 娄底市| 永川市| 青川县| 永泰县| 虎林市| 伊春市| 凌云县| 万载县| 余江县| 五常市| 峨眉山市| 禄丰县| 湖南省| 松潘县| 安顺市| 项城市| 大宁县| 堆龙德庆县| 临沧市| 微山县| 苗栗县| 赤城县| 西林县| 仁怀市| 苍溪县|