【python遊戲製作】殭屍來襲 ~ 快來一起創造植物叭~
__author__ = 'marble_xu'
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-2900189/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python開發植物大戰殭屍遊戲Python遊戲
- 開發者訪談:《植物大戰殭屍》創意竟來自WAR3
- 《植物大戰殭屍》開發者聊《八爪怪》的製作過程
- 【轉載】《星鐵植物大戰殭屍》:策略遊戲的深度解析與未來展望遊戲
- Unity 植物大戰殭屍(一)Unity
- 分析《植物大戰殭屍》的遊戲平衡性遊戲
- c++製作的植物大戰殭屍,開源,一代二代結合遊戲C++遊戲
- 植物大戰殭屍 雜交版
- 植物大戰殭屍-雜交版
- python 植物大戰殭屍外掛輔助工具Python
- 如何實現H5小遊戲—植物大戰殭屍H5遊戲
- 星鐵版植物大戰殭屍介紹
- Chalubo殭屍網路來襲 IOT裝置或將受到DDoS攻擊
- “植物大戰殭屍”開發者分享的10個遊戲教程建議遊戲
- 植物大戰殭屍對戰版(Android)Android
- 植物大戰殭屍1.2.0.1073漢化版
- Python 植物大戰殭屍程式碼實現(2):植物卡片選擇和種植Python
- 用python實現植物大戰殭屍(遊戲截圖+動態演示+原始碼分享)Python遊戲原始碼
- 使用Cheat Engine(CE)修改遊戲“植物大戰殭屍”之陽光篇遊戲
- [譯] 殭屍幣時代即將到來?
- 植物大戰殭屍-雜交版-修改陽光
- 遊戲角色製作行業標準? 快來看看吧遊戲行業
- 一起來學習如何用 Node 來製作 CLI
- Win10植物大戰殭屍電腦存檔在哪_win10怎麼開啟植物大戰殭屍存檔位置Win10
- DDG殭屍網路“變種”來襲,騰訊安全提醒企業警惕伺服器安全伺服器
- 《植物大戰殭屍:綠意盎然的防禦戰》
- 使用DOTS製作一款第三人稱殭屍射擊遊戲遊戲
- 植物大戰殭屍雜交版2.1下載連結
- 植物大戰殭屍雜交版2.2下載連結
- 植物大戰殭屍雜交版2.0下載連結
- 網易遊戲:《植物大戰殭屍2》已過氣的四大理由遊戲
- 麒麟作業系統 (kylinos) 從入門到精通 - 遊戲體驗 - 第四十二篇 植物大戰殭屍遊戲作業系統遊戲
- 植物大戰殭屍win10開啟閃退怎麼解決_win10植物大戰殭屍開啟閃退解決步驟Win10
- 【BZOJ-1565】植物大戰殭屍 拓撲排序 + 最小割排序
- 植物大戰殭屍雜交版2.0.88版下載連結
- 植物大戰殭屍雜交版修改器22.09
- 國產單機遊戲的未來,只能是創意小製作嗎?遊戲
- 植物大戰殭屍雜交版 官方版(作者:b站潛艇偉偉迷) 植物大戰殭屍雜交版v2.0.88安裝程式.exe