用python實現植物大戰殭屍(遊戲截圖+動態演示+原始碼分享)
import os
import json
from abc import abstractmethod
import pygame as pg
from . import constants as c
class State():
def __init__(self):
self.start_time = 0.0
self.current_time = 0.0
self.done = False
self.next = None
self.persist = {}
@abstractmethod
def startup(self, current_time, persist):
'''abstract method'''
def cleanup(self):
self.done = False
return self.persist
@abstractmethod
def update(self, surface, keys, current_time):
'''abstract method'''
class Control():
def __init__(self):
self.screen = pg.display.get_surface()
self.done = False
self.clock = pg.time.Clock()
self.fps = 60
self.keys = pg.key.get_pressed()
self.mouse_pos = None
self.mouse_click = [False, False] # value:[left mouse click, right mouse click]
self.current_time = 0.0
self.state_dict = {}
self.state_name = None
self.state = None
self.game_info = {c.CURRENT_TIME:0.0,
c.LEVEL_NUM:c.START_LEVEL_NUM}
def setup_states(self, state_dict, start_state):
self.state_dict = state_dict
self.state_name = start_state
self.state = self.state_dict[self.state_name]
self.state.startup(self.current_time, self.game_info)
def update(self):
self.current_time = pg.time.get_ticks()
if self.state.done:
self.flip_state()
self.state.update(self.screen, self.current_time, self.mouse_pos, self.mouse_click)
self.mouse_pos = None
self.mouse_click[0] = False
self.mouse_click[1] = False
def flip_state(self):
previous, self.state_name = self.state_name, self.state.next
persist = self.state.cleanup()
self.state = self.state_dict[self.state_name]
self.state.startup(self.current_time, persist)
def event_loop(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
elif event.type == pg.KEYDOWN:
self.keys = pg.key.get_pressed()
elif event.type == pg.KEYUP:
self.keys = pg.key.get_pressed()
elif event.type == pg.MOUSEBUTTONDOWN:
self.mouse_pos = pg.mouse.get_pos()
self.mouse_click[0], _, self.mouse_click[1] = pg.mouse.get_pressed()
print('pos:', self.mouse_pos, ' mouse:', self.mouse_click)
def main(self):
while not self.done:
self.event_loop()
self.update()
pg.display.update()
self.clock.tick(self.fps)
print('game over')
def get_image(sheet, x, y, width, height, colorkey=c.BLACK, scale=1):
image = pg.Surface([width, height])
rect = image.get_rect()
image.blit(sheet, (0, 0), (x, y, width, height))
image.set_colorkey(colorkey)
image = pg.transform.scale(image,
(int(rect.width*scale),
int(rect.height*scale)))
return image
def load_image_frames(directory, image_name, colorkey, accept):
frame_list = []
tmp = {}
# image_name is "Peashooter", pic name is 'Peashooter_1', get the index 1
index_start = len(image_name) + 1
frame_num = 0;
for pic in os.listdir(directory):
name, ext = os.path.splitext(pic)
if ext.lower() in accept:
index = int(name[index_start:])
img = pg.image.load(os.path.join(directory, pic))
if img.get_alpha():
img = img.convert_alpha()
else:
img = img.convert()
img.set_colorkey(colorkey)
tmp[index]= img
frame_num += 1
for i in range(frame_num):
frame_list.append(tmp[i])
return frame_list
def load_all_gfx(directory, colorkey=c.WHITE, accept=('.png', '.jpg', '.bmp', '.gif')):
graphics = {}
for name1 in os.listdir(directory):
# subfolders under the folder resources\graphics
dir1 = os.path.join(directory, name1)
if os.path.isdir(dir1):
for name2 in os.listdir(dir1):
dir2 = os.path.join(dir1, name2)
if os.path.isdir(dir2):
# e.g. subfolders under the folder resources\graphics\Zombies
for name3 in os.listdir(dir2):
dir3 = os.path.join(dir2, name3)
# e.g. subfolders or pics under the folder resources\graphics\Zombies\ConeheadZombie
if os.path.isdir(dir3):
# e.g. it's the folder resources\graphics\Zombies\ConeheadZombie\ConeheadZombieAttack
image_name, _ =外匯跟單gendan5.com os.path.splitext(name3)
graphics[image_name] = load_image_frames(dir3, image_name, colorkey, accept)
else:
# e.g. pics under the folder resources\graphics\Plants\Peashooter
image_name, _ = os.path.splitext(name2)
graphics[image_name] = load_image_frames(dir2, image_name, colorkey, accept)
break
else:
# e.g. pics under the folder resources\graphics\Screen
name, ext = os.path.splitext(name2)
if ext.lower() in accept:
img = pg.image.load(dir2)
if img.get_alpha():
img = img.convert_alpha()
else:
img = img.convert()
img.set_colorkey(colorkey)
graphics[name] = img
return graphics
def loadZombieImageRect():
file_path = os.path.join('source', 'data', 'entity', 'zombie.json')
f = open(file_path)
data = json.load(f)
f.close()
return data[c.ZOMBIE_IMAGE_RECT]
def loadPlantImageRect():
file_path = os.path.join('source', 'data', 'entity', 'plant.json')
f = open(file_path)
data = json.load(f)
f.close()
return data[c.PLANT_IMAGE_RECT]
pg.init()
pg.display.set_caption(c.ORIGINAL_CAPTION)
SCREEN = pg.display.set_mode(c.SCREEN_SIZE)
GFX = load_all_gfx(os.path.join("resources","graphics"))
ZOMBIE_RECT = loadZombieImageRect()
PLANT_RECT = loadPlantImageRect()
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2893788/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python開發植物大戰殭屍遊戲Python遊戲
- Python 植物大戰殭屍程式碼實現(2):植物卡片選擇和種植Python
- Unity 植物大戰殭屍(一)Unity
- python實現超級瑪麗小遊戲(動圖演示+原始碼分享)Python遊戲原始碼
- python 植物大戰殭屍外掛輔助工具Python
- 《植物大戰殭屍》10歲了
- 植物大戰殭屍 雜交版
- 植物大戰殭屍-雜交版
- 植物大戰殭屍1.2.0.1073漢化版
- 使用Cheat Engine(CE)修改遊戲“植物大戰殭屍”之陽光篇遊戲
- 星鐵版植物大戰殭屍介紹
- 植物大戰殭屍,用QT注入程式碼,AT&T彙編語法QT
- 《植物大戰殭屍:綠意盎然的防禦戰》
- 植物大戰殭屍-雜交版-修改陽光
- 植物大戰殭屍 2.0中文版
- Win10植物大戰殭屍電腦存檔在哪_win10怎麼開啟植物大戰殭屍存檔位置Win10
- 這就是你們要的《植物大戰殭屍3》
- 《植物大戰殭屍 3》封閉測試開啟
- 植物大戰殭屍雜交版修改器22.09
- EA 與《植物大戰殭屍》如何對抗「熱點文化」現象
- 【轉載】《星鐵植物大戰殭屍》:策略遊戲的深度解析與未來展望遊戲
- 【BZOJ-1565】植物大戰殭屍 拓撲排序 + 最小割排序
- 植物大戰殭屍雜交版2.1下載連結
- 植物大戰殭屍雜交版2.2下載連結
- 植物大戰殭屍雜交版2.0下載連結
- 植物大戰殭屍win10開啟閃退怎麼解決_win10植物大戰殭屍開啟閃退解決步驟Win10
- “植物大戰殭屍之父”George Fan與他的設計之道
- 植物大戰殭屍雜交版2.0.88版下載連結
- 植物大戰殭屍融合版最新版下載和攻略
- c++製作的植物大戰殭屍,開源,一代二代結合遊戲C++遊戲
- 植物大戰殭屍雜交版 官方版(作者:b站潛艇偉偉迷) 植物大戰殭屍雜交版v2.0.88安裝程式.exe
- 馬斯洛“需求層次理論” 在《植物大戰殭屍》中的運用
- 【python遊戲製作】殭屍來襲 ~ 快來一起創造植物叭~Python遊戲
- 《植物大戰殭屍3》即將推出,你的青春結束了...
- 植物大戰殭屍雜交版 最新官方版(作者:b站潛艇偉偉迷) 植物大戰殭屍雜交版v2.4安裝程式.exe
- 《植物大戰殭屍》發售10週年 官方公佈設計原稿
- 超好玩的植物大戰殭屍中文版 for Mac(相容m1)Mac
- 麒麟作業系統 (kylinos) 從入門到精通 - 遊戲體驗 - 第四十二篇 植物大戰殭屍遊戲作業系統遊戲