日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

用 Python 实现植物大战僵尸代码

發布時間:2023/12/20 python 239 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用 Python 实现植物大战僵尸代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


導讀:以前很火的植物大戰僵尸游戲,本想在網上找個Python版本游戲學習下,無奈沒有發現比較完整的,那就自己來寫一個吧。

本文經授權轉自公眾號CSDN(ID:CSDNnews)

作者:marble_xu

原文鏈接:

https://blog.csdn.net/marble_xu/article/details/103100614

01 功能介紹

最近一直在給這個植物大戰僵尸游戲添加新的植物和僵尸,因為網上的圖片資源有限,能加的植物和僵尸比較少, 目前進展如下。

功能實現如下:

  • 支持的植物類型:太陽花,豌豆射手,寒冰射手,堅果,櫻桃炸彈。新增加植物:雙重豌豆射手,三重豌豆射手,食人花 ,小噴菇,土豆地雷,倭瓜。

  • 支持的僵尸類型:普通僵尸,旗子僵尸,路障僵尸,鐵桶僵尸。新增加讀報僵尸。

  • 使用json文件保存關卡信息,設置僵尸出現的時間和位置。

  • 增加每關開始時選擇上場植物。

  • 增加除草機。

下面是游戲的截圖:

▲圖1:新增的植物和僵尸

▲圖2:每關開始選擇上場植物卡片

▲圖3:選擇植物在哪里種植

02 植物卡片選擇和種植

如圖3所示,游戲中可以種植物的方格一共有45個(有5行,每行9列)。

這篇文章要介紹的是:

  • 上方植物卡片欄的實現。

  • 點擊植物卡片,鼠標切換為植物圖片。

  • 鼠標移動時,判斷當前在哪個方格中,并顯示半透明的植物作為提示。

03 完整代碼

游戲實現代碼的github鏈接:

https://github.com/marblexu/PythonPlantsVsZombies

這邊是csdn的下載鏈接:

https://download.csdn.net/download/marble_xu/11982275

04 代碼實現

所有的植物卡片的名稱和屬性都保存在單獨的list中,每個list index都對應一種植物。

比如list index 0 就是太陽花:

  • card_name_list[0] 是太陽花卡片的名字,用來獲取太陽花卡片的圖片。

  • plant_name_list[0]?是太陽花的名字,用來獲取太陽花卡片的圖片。

  • plant_sun_list[0] 是種植太陽花需要花費的太陽點數。

  • plant_frozen_time_list[0] 是太陽花的冷卻時間。

代碼在source\component\menubar.py中:

card_name_list?=?[c.CARD_SUNFLOWER,?c.CARD_PEASHOOTER,?c.CARD_SNOWPEASHOOTER,?c.CARD_WALLNUT,c.CARD_CHERRYBOMB,?c.CARD_THREEPEASHOOTER,?c.CARD_REPEATERPEA,?c.CARD_CHOMPER,c.CARD_PUFFMUSHROOM,?c.CARD_POTATOMINE,?c.CARD_SQUASH] plant_name_list?=?[c.SUNFLOWER,?c.PEASHOOTER,?c.SNOWPEASHOOTER,?c.WALLNUT,c.CHERRYBOMB,?c.THREEPEASHOOTER,?c.REPEATERPEA,?c.CHOMPER,c.PUFFMUSHROOM,?c.POTATOMINE,?c.SQUASH] plant_sun_list?=?[50,?100,?175,?50,?150,?325,?200,?150,?0,?25,?50] plant_frozen_time_list?=?[0,?5000,?5000,?10000,?5000,?5000,?5000,?5000,?8000,?8000,?8000] all_card_list?=?[0,?1,?2,?3,?4,?5,?6,?7,?8,?9,?10]

05 植物卡片類

代碼在source\component\menubar.py中,每個植物卡片是一個單獨的Card類,用來顯示這個植物。

  • checkMouseClick函數:判斷鼠標是否點擊到這個卡片;

  • canClick:判斷這個卡片是否能種植(有沒有足夠的點數,是否還在冷卻時間內);

  • update 函數:通過設置圖片的透明度來表示這個卡片是否能選擇。

class?Card():def?__init__(self,?x,?y,?name_index,?scale=0.78):self.loadFrame(card_name_list[name_index],?scale)self.rect?=?self.image.get_rect()self.rect.x?=?xself.rect.y?=?yself.name_index?=?name_indexself.sun_cost?=?plant_sun_list[name_index]self.frozen_time?=?plant_frozen_time_list[name_index]self.frozen_timer?=?-self.frozen_timeself.select?=?Truedef?loadFrame(self,?name,?scale):frame?=?tool.GFX[name]rect?=?frame.get_rect()width,?height?=?rect.w,?rect.hself.image?=?tool.get_image(frame,?0,?0,?width,?height,?c.BLACK,?scale)def?checkMouseClick(self,?mouse_pos):x,?y?=?mouse_posif(x?>=?self.rect.x?and?x?<=?self.rect.right?andy?>=?self.rect.y?and?y?<=?self.rect.bottom):return?Truereturn?Falsedef?canClick(self,?sun_value,?current_time):if?self.sun_cost?<=?sun_value?and?(current_time?-?self.frozen_timer)?>?self.frozen_time:return?Truereturn?Falsedef?canSelect(self):return?self.selectdef?setSelect(self,?can_select):self.select?=?can_selectif?can_select:self.image.set_alpha(255)else:self.image.set_alpha(128)def?setFrozenTime(self,?current_time):self.frozen_timer?=?current_timedef?update(self,?sun_value,?current_time):if?(self.sun_cost?>?sun_value?or(current_time?-?self.frozen_timer)?<=?self.frozen_time):self.image.set_alpha(128)else:self.image.set_alpha(255)def?draw(self,?surface):surface.blit(self.image,?self.rect)

06 卡片欄類

代碼在source\component\menubar.py中,MenuBar類顯示圖3中的植物卡片欄:

  • self.sun_value:當前采集的太陽點數;

  • self.card_list: 植物卡片的list;

  • setupCards函數:遍歷初始化__init__函數中傳入這個關卡選好的植物卡片list,依次創建Card類,設置每個卡片的顯示位置;

  • checkCardClick函數:檢查鼠標是否點擊了卡片欄上的某個植物卡片,如果選擇了一個可種植的卡片,返回結果。

class?MenuBar():def?__init__(self,?card_list,?sun_value):self.loadFrame(c.MENUBAR_BACKGROUND)self.rect?=?self.image.get_rect()self.rect.x?=?10self.rect.y?=?0self.sun_value?=?sun_valueself.card_offset_x?=?32self.setupCards(card_list)def?loadFrame(self,?name):frame?=?tool.GFX[name]rect?=?frame.get_rect()frame_rect?=?(rect.x,?rect.y,?rect.w,?rect.h)self.image?=?tool.get_image(tool.GFX[name],?*frame_rect,?c.WHITE,?1)def?update(self,?current_time):self.current_time?=?current_timefor?card?in?self.card_list:card.update(self.sun_value,?self.current_time)def?createImage(self,?x,?y,?num):if?num?==?1:returnimg?=?self.imagerect?=?self.image.get_rect()width?=?rect.wheight?=?rect.hself.image?=?pg.Surface((width?*?num,?height)).convert()self.rect?=?self.image.get_rect()self.rect.x?=?xself.rect.y?=?yfor?i?in?range(num):x?=?i?*?widthself.image.blit(img,?(x,0))self.image.set_colorkey(c.BLACK)def?setupCards(self,?card_list):self.card_list?=?[]x?=?self.card_offset_xy?=?8for?index?in?card_list:x?+=?55self.card_list.append(Card(x,?y,?index))def?checkCardClick(self,?mouse_pos):result?=?Nonefor?card?in?self.card_list:if?card.checkMouseClick(mouse_pos):if?card.canClick(self.sun_value,?self.current_time):result?=?(plant_name_list[card.name_index],?card.sun_cost)breakreturn?resultdef?checkMenuBarClick(self,?mouse_pos):x,?y?=?mouse_posif(x?>=?self.rect.x?and?x?<=?self.rect.right?andy?>=?self.rect.y?and?y?<=?self.rect.bottom):return?Truereturn?Falsedef?decreaseSunValue(self,?value):self.sun_value?-=?valuedef?increaseSunValue(self,?value):self.sun_value?+=?valuedef?setCardFrozenTime(self,?plant_name):for?card?in?self.card_list:if?plant_name_list[card.name_index]?==?plant_name:card.setFrozenTime(self.current_time)breakdef?drawSunValue(self):self.value_image?=?getSunValueImage(self.sun_value)self.value_rect?=?self.value_image.get_rect()self.value_rect.x?=?21self.value_rect.y?=?self.rect.bottom?-?21self.image.blit(self.value_image,?self.value_rect)def?draw(self,?surface):self.drawSunValue()surface.blit(self.image,?self.rect)for?card?in?self.card_list:card.draw(surface)

07 鼠標圖片切換

代碼在source\state\level.py中,setupMouseImage 函數實現鼠標圖片切換為選中的植物:

  • self.mouse_image :根據 plant_name 獲取選中的植物圖片;

  • self.mouse_rect:選中植物圖片的位置,在drawMouseShow函數中,需要將植物圖片的位置設置成當前鼠標的位置;

  • pg.mouse.set_visible(False):隱藏默認的鼠標顯示,這樣效果就是鼠標圖片切換為選中的植物了。

????def?setupMouseImage(self,?plant_name,?plant_cost):frame_list?=?tool.GFX[plant_name]if?plant_name?in?tool.PLANT_RECT:data?=?tool.PLANT_RECT[plant_name]x,?y,?width,?height?=?data['x'],?data['y'],?data['width'],?data['height']else:x,?y?=?0,?0rect?=?frame_list[0].get_rect()width,?height?=?rect.w,?rect.hif?plant_name?==?c.POTATOMINE?or?plant_name?==?c.SQUASH:color?=?c.WHITEelse:color?=?c.BLACKself.mouse_image?=?tool.get_image(frame_list[0],?x,?y,?width,?height,?color,?1)self.mouse_rect?=?self.mouse_image.get_rect()pg.mouse.set_visible(False)self.drag_plant?=?Trueself.plant_name?=?plant_nameself.plant_cost?=?plant_costdef?drawMouseShow(self,?surface):if?self.hint_plant:surface.blit(self.hint_image,?self.hint_rect)x,?y?=?pg.mouse.get_pos()self.mouse_rect.centerx?=?xself.mouse_rect.centery?=?ysurface.blit(self.mouse_image,?self.mouse_rect)

08 提示種在哪個方格中

先看下map類,代碼在source\component\map.py 中:

  • self.map:二維list,用來保存每個方格的狀態。每個entry初始化為 0, 表示可以種植物,值為1時表示這個方格已經種了植物。

  • getMapIndex 函數:傳入參數是游戲中的坐標位置(比如當前鼠標的位置),返回該位置在地圖的哪個方格中。

  • getMapGridPos 函數:傳入一個方格的index,返回在該方格中種植物的坐標位置。

  • showPlant 函數:根據傳入的坐標位置,判斷該位置所在的方格是否能種植物,如果能種,就返回返回在該方格中種植物的坐標位置。

MAP_EMPTY?=?0 MAP_EXIST?=?1class?Map():def?__init__(self,?width,?height):self.width?=?widthself.height?=?heightself.map?=?[[0?for?x?in?range(self.width)]?for?y?in?range(self.height)]def?isValid(self,?map_x,?map_y):if?(map_x?<?0?or?map_x?>=?self.width?ormap_y?<?0?or?map_y?>=?self.height):return?Falsereturn?Truedef?isMovable(self,?map_x,?map_y):return?(self.map[map_y][map_x]?==?c.MAP_EMPTY)def?getMapIndex(self,?x,?y):x?-=?c.MAP_OFFSET_Xy?-=?c.MAP_OFFSET_Yreturn?(x?//?c.GRID_X_SIZE,?y?//?c.GRID_Y_SIZE)def?getMapGridPos(self,?map_x,?map_y):return?(map_x?*?c.GRID_X_SIZE?+?c.GRID_X_SIZE//2?+?c.MAP_OFFSET_X,map_y?*?c.GRID_Y_SIZE?+?c.GRID_Y_SIZE//5?*?3?+?c.MAP_OFFSET_Y)def?setMapGridType(self,?map_x,?map_y,?type):self.map[map_y][map_x]?=?typedef?getRandomMapIndex(self):map_x?=?random.randint(0,?self.width-1)map_y?=?random.randint(0,?self.height-1)return?(map_x,?map_y)def?showPlant(self,?x,?y):pos?=?Nonemap_x,?map_y?=?self.getMapIndex(x,?y)if?self.isValid(map_x,?map_y)?and?self.isMovable(map_x,?map_y):pos?=?self.getMapGridPos(map_x,?map_y)return?pos

代碼在source\state\level.py中:

  • canSeedPlant 函數:判斷當前鼠標位置能否種植物;

  • setupHintImage 函數:如果當前鼠標位置能種植物,且有選擇了一個植物卡片,則設置self.hint_image 顯示當前會在哪一個方格中種植物,self.hint_rect 是植物種的坐標位置。

????def?canSeedPlant(self):x,?y?=?pg.mouse.get_pos()return?self.map.showPlant(x,?y)def?setupHintImage(self):pos?=?self.canSeedPlant()if?pos?and?self.mouse_image:if?(self.hint_image?and?pos[0]?==?self.hint_rect.x?andpos[1]?==?self.hint_rect.y):returnwidth,?height?=?self.mouse_rect.w,?self.mouse_rect.himage?=?pg.Surface([width,?height])image.blit(self.mouse_image,?(0,?0),?(0,?0,?width,?height))image.set_colorkey(c.BLACK)image.set_alpha(128)self.hint_image?=?imageself.hint_rect?=?image.get_rect()self.hint_rect.centerx?=?pos[0]self.hint_rect.bottom?=?pos[1]self.hint_plant?=?Trueelse:self.hint_plant?=?False

編譯環境:python3.7 + pygame1.9。

有話要說????

Q:?你還想用Python嘗試做哪款游戲?

歡迎留言與大家分享

猜你想看????

  • 什么是氣泡圖?怎樣用Python繪制?怎么用?終于有人講明白了

  • 50年后的地球什么樣?大數據、AI、量子計算的書單給你答案

  • 數據中臺VS業務中臺、數據中臺VS數據倉庫,到底有什么區別?

  • 企業數字化轉型與中臺建設全攻略:什么階段進行?有哪些方法?

更多精彩????

在公眾號對話框輸入以下關鍵詞

查看更多優質內容!

PPT?|?報告?|?讀書?|?書單?|?干貨?

大數據?|?揭秘?|?Python?|?可視化

AI?|?人工智能?|?5G?|?中臺

機器學習?|?深度學習?|?神經網絡

合伙人?|?1024?|?段子?|?數學

據統計,99%的大咖都完成了這個神操作

????

覺得不錯,請把這篇文章分享給你的朋友

轉載 / 投稿請聯系:baiyu@hzbook.com

更多精彩,請在后臺點擊“歷史文章”查看

總結

以上是生活随笔為你收集整理的用 Python 实现植物大战僵尸代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 黄网站在线免费 | 激情吧| 欧美91精品久久久久国产性生爱 | 久久重口味 | 国产福利小视频 | 国产一级久久久久毛片精品 | 波多野吉衣中文字幕 | 国产成人午夜视频 | 亚洲av无码一区二区二三区 | 亚洲乱码视频在线观看 | 女人下部全棵看视频 | 日韩在线视频免费观看 | 国产精品福利影院 | 美女被草视频在线观看 | 国产精品丝袜在线观看 | 亚洲乱码一区二区 | 欧美亚洲国产成人 | 亚洲一卡二卡在线 | 亚洲区中文字幕 | 男人懂得网站 | 中文字幕蜜臀 | 成人自拍偷拍 | 久久久久久久免费 | 国产午夜精品理论片在线 | 日韩素人 | 黄色a视频 | 日p视频在线观看 | 国产av一区二区不卡 | www.色就是色 | 日韩欧美亚洲一区二区三区 | 视频在线看 | 奇米网久久| 91久久一区二区 | 有声小说 成人专区 | 色婷婷综合久久久久中文字幕 | 亚洲色偷精品一区二区三区 | 亚洲图片欧美色图 | 久久视频免费观看 | 三a大片| 五月开心激情 | 男人和女人在床的app | 国产精品一区一区三区 | 亚洲天堂中文字幕 | av在线电影网 | 国产一区二区亚洲 | 深夜在线免费视频 | 精品视频一区二区在线观看 | 久久久久久69 | 午夜成年人 | 国产污污网站 | 欧美涩涩涩 | 成人在线a| 久久久久国产一区二区三区 | 国产一区免费在线观看 | 一区二区三区视频在线观看 | 在线免费黄色 | 欧美精品一区二区三区视频 | 欧美性视频网站 | 日韩成人av一区 | 五月婷婷综合久久 | 欧美激情久久久久久久 | 精品999久久久 | 国产精品自拍一区 | 天天操妹子 | 一区二区乱码 | www.黄色av| 一级一级黄色片 | 成人h动漫精品一区二区 | 日韩成人在线网站 | 国产午夜在线播放 | 波多野结衣黄色网址 | 97涩涩网 | 爱草在线| 欧美日韩色视频 | 97成人超碰 | 亚州激情视频 | 欧美日韩一区二区三区不卡视频 | 国产精品白浆一区二小说 | 91高清视频 | 欧美大片一区二区三区 | 小泽玛利亚一区二区三区视频 | 国产一线二线在线观看 | 免费看欧美大片 | 久久久久久一区 | 桃色成人网 | 午夜在线播放视频 | 无遮挡裸光屁屁打屁股男男 | 亚洲欧美日韩综合在线 | 色欧美在线 | 欧美夫妻性生活视频 | 国产精品无码粉嫩小泬 | 99精品在线视频观看 | 毛茸茸成熟亚洲人 | 国产乱国产乱300精品 | 国产一级在线视频 | 中文字幕免费一区二区 | 麻豆成人免费视频 | 麻豆视频国产精品 | 久久中文字幕人妻 |