資源簡介
本文件包含Python編寫的植物大戰僵尸,親測有效,并且代碼加入了足夠的注釋,理解簡單,半天便可學會
聯系方式:upqcyjp@163.com
代碼片段和文件信息
import?pygame
import?random
from?pygame.locals?import?*
#?窗體的高度和寬度
WIDTH?=?1200
HEIGHT?=?600
#?豌豆對象
class?Peas:
????def?__init__(self):
????????self.image?=?pygame.image.load(“./res/peas.jpg“)
????????self.image_rect?=?self.image.get_rect()
????????self.image_rect.top?=?280
????????self.image_rect.left?=?30
????????#?設置豌豆是否上下移動
????????self.is_move_down?=?False
????????self.is_move_up?=?False
????????self.is_shout?=?False
????def?display(self):
????????“““豌豆顯示在頁面上“““
????????screen.blit(self.image?self.image_rect)
????def?move_up(self):
????????if?self.image_rect.top?>?20:
????????????self.image_rect.move_ip(0?-6)
????????for?z?in?Zombie.zombie_list:
????????????if?self.image_rect.colliderect(z.image_rect)?:
????????????????pygame.quit()
????????????????exit()
????def?move_down(self):
????????if?self.image_rect.bottom?580:
????????????self.image_rect.move_ip(0?6)
????????for?z?in?Zombie.zombie_list:
????????????if?self.image_rect.colliderect(z.image_rect):
????????????????pygame.quit()
????????????????exit()
????def?shout_bullet(self):
????????#?創建一個炮彈對象
????????bullet?=?Bullet(self)
????????#?保存創建的炮彈對象
????????Bullet.bullet_list.append(bullet)
#?炮彈對象
class?Bullet:
????#?顯示所有的炮彈信息
????bullet_list?=?[]
????#?炮彈的間隔信息
????interval?=?0
????def?__init__(self?pea):
????????self.image?=?pygame.image.load(“./res/bullet.jpg“)
????????self.image_rect?=?self.image.get_rect()
????????self.image_rect.top?=?pea.image_rect.top+10
????????self.image_rect.left?=?pea.image_rect.right
????????#?設置豌豆是否上下移動
????????self.is_move_down?=?False
????????self.is_move_up?=?False
????????self.is_shout?=?False
????def?display(self):
????????“““炮彈顯示在頁面上“““
????????screen.blit(self.image?self.image_rect)
????def?move(self):
????????self.image_rect.move_ip(100)
????????#?如果炮彈越界,刪除炮彈
????????if?self.image_rect.left?>?WIDTH?-?50?:
????????????Bullet.bullet_list.remove(self)
????????for?z?in?Zombie.zombie_list:
????????????if?self.image_rect.colliderect(z.image_rect)?:
????????????????Bullet.bullet_list.remove(self)
????????????????Zombie.zombie_list.remove(z)
????????????????break
#?僵尸對象
class?Zombie:
????#?創建頻率
????interval?=?0
????#?保存多個僵尸
????zombie_list?=?[]
????def?__init__(self):
????????self.image?=?pygame.image.load(“./res/zombie.jpg“)
????????#?改變圖片大小
????????self.image?=?pygame.transform.scale(self.image?(70?70))
????????self.image_rect?=?self.image.get_rect()
????????self.image_rect.top?=?random.randint(15?HEIGHT-80)
????????self.image_rect.left?=?WIDTH-100
????def?display(self):
????????“““僵尸顯示在頁面上“““
????????screen.blit(self.image?self.image_rect)
????def?move(self):
????????“““僵尸移動“““
????????self.image_rect.move_ip(-20)
????????if?self.image_rect.right?<=?0?:
????????????Zombie.zombie_list.remove(self)
????????for?b?in?Bullet.bullet_list?:
????????????if?self.image_rect.colliderect(b.image_rect)?:
???????????????Zombie.zombie_list.remove(self)
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2020-02-27?17:32??植物大戰僵尸\
?????目錄???????????0??2020-02-27?17:32??植物大戰僵尸\.idea\
?????文件?????????138??2020-02-26?18:35??植物大戰僵尸\.idea\encodings.xm
?????文件?????????448??2020-02-26?18:52??植物大戰僵尸\.idea\game.iml
?????文件?????????301??2020-02-26?18:52??植物大戰僵尸\.idea\misc.xm
?????文件?????????267??2020-02-26?18:35??植物大戰僵尸\.idea\modules.xm
?????文件???????10541??2020-02-27?16:59??植物大戰僵尸\.idea\workspace.xm
?????文件????????6299??2020-02-27?16:56??植物大戰僵尸\main.py
?????目錄???????????0??2020-02-27?17:32??植物大戰僵尸\res\
?????文件????????7361??2020-02-26?18:45??植物大戰僵尸\res\background.jpg
?????文件????????1114??2020-02-26?21:33??植物大戰僵尸\res\bullet.jpg
?????文件????????8177??2020-02-27?12:58??植物大戰僵尸\res\peas.jpg
?????文件???????10823??2020-02-26?21:39??植物大戰僵尸\res\zombie.jpg
?????目錄???????????0??2020-02-27?17:32??植物大戰僵尸\venv\
?????目錄???????????0??2020-02-27?17:32??植物大戰僵尸\venv\Include\
?????目錄???????????0??2020-02-27?17:32??植物大戰僵尸\venv\Include\site\
?????目錄???????????0??2020-02-27?17:32??植物大戰僵尸\venv\Include\site\python3.5\
?????目錄???????????0??2020-02-27?17:32??植物大戰僵尸\venv\Include\site\python3.5\pygame\
?????文件?????????840??2020-02-26?19:06??植物大戰僵尸\venv\Include\site\python3.5\pygame\_camera.h
?????文件???????28409??2020-02-26?19:06??植物大戰僵尸\venv\Include\site\python3.5\pygame\_pygame.h
?????文件?????????958??2020-02-26?19:06??植物大戰僵尸\venv\Include\site\python3.5\pygame\_surface.h
?????文件????????4777??2020-02-26?19:06??植物大戰僵尸\venv\Include\site\python3.5\pygame\bitmask.h
?????文件????????6957??2020-02-26?19:06??植物大戰僵尸\venv\Include\site\python3.5\pygame\camera.h
?????文件????????1643??2020-02-26?19:06??植物大戰僵尸\venv\Include\site\python3.5\pygame\fastevents.h
?????文件????????1836??2020-02-26?19:06??植物大戰僵尸\venv\Include\site\python3.5\pygame\font.h
?????文件????????4175??2020-02-26?19:06??植物大戰僵尸\venv\Include\site\python3.5\pygame\freetype.h
?????文件?????????620??2020-02-26?19:06??植物大戰僵尸\venv\Include\site\python3.5\pygame\mask.h
?????文件????????2258??2020-02-26?19:06??植物大戰僵尸\venv\Include\site\python3.5\pygame\mixer.h
?????文件????????7057??2020-02-26?19:06??植物大戰僵尸\venv\Include\site\python3.5\pygame\palette.h
?????文件????????1060??2020-02-26?19:06??植物大戰僵尸\venv\Include\site\python3.5\pygame\pgarrinter.h
?????文件????????1961??2020-02-26?19:06??植物大戰僵尸\venv\Include\site\python3.5\pygame\pgbufferproxy.h
............此處省略929個文件信息
評論
共有 條評論