




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第基于Python實現(xiàn)經(jīng)典植物大戰(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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 紡織品市場調(diào)查試題及答案
- 2024年紡織工程師心理素質(zhì)試題及答案
- 亮劍藍(lán)海試題及答案
- 2024年紡織工程師證書考試如何復(fù)習(xí)試題及答案
- 生理學(xué)試題及答案基礎(chǔ)
- 幼師答辯面試試題及答案
- 向?qū)I(yè)商業(yè)美術(shù)設(shè)計師考試的試題及答案看齊
- 2024年紡織品設(shè)計師的法律與倫理探索試題及答案
- 廣告設(shè)計師證書考試設(shè)計創(chuàng)新案例分析試題及答案
- 紡織品檢驗員考試相關(guān)標(biāo)準(zhǔn)解析試題及答案
- 冷庫制冷負(fù)荷計算表
- 迅雷網(wǎng)盤最最最全影視資源-持續(xù)更新7.26
- 管理者與員工溝通技巧
- GB/T 19510.213-2023光源控制裝置第2-13部分:LED模塊用直流或交流電子控制裝置的特殊要求
- 2024年橋式起重機(jī)司機(jī)(中級)職業(yè)技能考試題庫(職校培訓(xùn))
- 工程建設(shè)公司QC小組道路瀝青混凝土面層裂縫的控制成果匯報書
- 孕產(chǎn)婦妊娠風(fēng)險篩查與評估
- 電梯維保服務(wù)投標(biāo)方案
- 【承諾書】危險化學(xué)品承諾書
- C語言程序設(shè)計說課(共34張PPT)
- (完整版)初二地理會考模擬試卷(含答案)
評論
0/150
提交評論