




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第基于Python實現經典植物大戰(zhàn)僵尸游戲目錄游戲截圖動態(tài)演示源碼分享state/tool.pystate/constants.pystate/main.py主執(zhí)行文件main.py
游戲截圖
動態(tài)演示
源碼分享
state/tool.py
importos
importjson
fromabcimportabstractmethod
importpygameaspg
from.importconstantsasc
classState():
def__init__(self):
self.start_time=0.0
self.current_time=0.0
self.done=False
self.next=None
self.persist={}
@abstractmethod
defstartup(self,current_time,persist):
'''abstractmethod'''
defcleanup(self):
self.done=False
returnself.persist
@abstractmethod
defupdate(self,surface,keys,current_time):
'''abstractmethod'''
classControl():
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:[leftmouseclick,rightmouseclick]
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}
defsetup_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)
defupdate(self):
self.current_time=pg.time.get_ticks()
ifself.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
defflip_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)
defevent_loop(self):
foreventinpg.event.get():
ifevent.type==pg.QUIT:
self.done=True
elifevent.type==pg.KEYDOWN:
self.keys=pg.key.get_pressed()
elifevent.type==pg.KEYUP:
self.keys=pg.key.get_pressed()
elifevent.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)
defmain(self):
whilenotself.done:
self.event_loop()
self.update()
pg.display.update()
self.clock.tick(self.fps)
print('gameover')
defget_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)))
returnimage
defload_image_frames(directory,image_name,colorkey,accept):
frame_list=[]
tmp={}
#image_nameis"Peashooter",picnameis'Peashooter_1',gettheindex1
index_start=len(image_name)+1
frame_num=0;
forpicinos.listdir(directory):
name,ext=os.path.splitext(pic)
ifext.lower()inaccept:
index=int(name[index_start:])
img=pg.image.load(os.path.join(directory,pic))
ifimg.get_alpha():
img=img.convert_alpha()
else:
img=img.convert()
img.set_colorkey(colorkey)
tmp[index]=img
frame_num+=1
foriinrange(frame_num):
frame_list.append(tmp[i])
returnframe_list
defload_all_gfx(directory,colorkey=c.WHITE,accept=('.png','.jpg','.bmp','.gif')):
graphics={}
forname1inos.listdir(directory):
#subfoldersunderthefolderresources\graphics
dir1=os.path.join(directory,name1)
ifos.path.isdir(dir1):
forname2inos.listdir(dir1):
dir2=os.path.join(dir1,name2)
ifos.path.isdir(dir2):
#e.g.subfoldersunderthefolderresources\graphics\Zombies
forname3inos.listdir(dir2):
dir3=os.path.join(dir2,name3)
#e.g.subfoldersorpicsunderthefolderresources\graphics\Zombies\ConeheadZombie
ifos.path.isdir(dir3):
#e.g.it'sthefolderresources\graphics\Zombies\ConeheadZombie\ConeheadZombieAttack
image_name,_=os.path.splitext(name3)
graphics[image_name]=load_image_frames(dir3,image_name,colorkey,accept)
else:
#e.g.picsunderthefolderresources\graphics\Plants\Peashooter
image_name,_=os.path.splitext(name2)
graphics[image_name]=load_image_frames(dir2,image_name,colorkey,accept)
break
else:
#e.g.picsunderthefolderresources\graphics\Screen
name,ext=os.path.splitext(name2)
ifext.lower()inaccept:
img=pg.image.load(dir2)
ifimg.get_alpha():
img=img.convert_alpha()
else:
img=img.convert()
img.set_colorkey(colorkey)
graphics[name]=img
returngraphics
defloadZombieImageRect():
file_path=os.path.join('source','data','entity','zombie.json')
f=open(file_path)
data=json.load(f)
f.close()
returndata[c.ZOMBIE_IMAGE_RECT]
defloadPlantImageRect():
file_path=os.path.join('source','data','entity','plant.json')
f=open(file_path)
data=json.load(f)
f.close()
returndata[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=loadZombi
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 教育行業(yè)數字化營銷與招生策略在在線教育平臺用戶行為分析中的應用報告
- 成本控制哲學
- 經驗成本管理
- 2025年重慶市事業(yè)單位招聘考試綜合類專業(yè)能力測試試卷(計算機類)解析
- 2025年中式面點師(高級)考試試卷:面點制作行業(yè)挑戰(zhàn)
- 2025年制冷空調特種作業(yè)操作證考試試卷:實操技能春季提升
- 2025年鑄造工(初級)鑄造生產現場安全管理與責任落實試題
- 2025年裝配鉗工(高級)考試試卷解析與高分試題
- 表面處理廢水處理與回收方案
- 復式層鋼木樓梯供貨安裝合同協(xié)議書范本
- 校服采購投標方案
- 病原生物與免疫學基礎
- 單個軍人隊列動作教案
- 柬埔寨鐵礦資源簡介
- 九年級上冊英語英語閱讀理解匯編題20套及解析
- GB/T 41837-2022溫泉服務溫泉水質要求
- 第二講東方管理學形成和發(fā)展
- GB/T 39604-2020社會責任管理體系要求及使用指南
- 《高等教育管理學》考試參考題庫(含答案)
- 物業(yè)公司經營分析工作匯報課件
- 中心靜脈導管評估觀察記錄表
評論
0/150
提交評論