좀비 게임을 간단하게 구현한 예제이다.
resource.xml
<stages>
<stage>
<name>Stage1</name>
<hp>40</hp>
<enemies>
<enemy type="basic">
<name>basic zombie</name>
<hp>150</hp>
<bite>20</bite>
<speed>30</speed>
<image>A</image>
<gen>3</gen>
</enemy>
<enemy type="basic">
<name>basic zombie</name>
<hp>150</hp>
<bite>20</bite>
<speed>30</speed>
<image>B</image>
<gen>7</gen>
</enemy>
</enemies>
</stage>
<stage>
<name>Stage2</name>
<hp>70</hp>
<enemies>
<enemy type="basic">
<name>basic zombie</name>
<hp>150</hp>
<bite>20</bite>
<speed>30</speed>
<image>A</image>
<gen>3</gen>
</enemy>
<enemy type="ax">
<name>ax zombie</name>
<hp>300</hp>
<bite>50</bite>
<speed>20</speed>
<image>C</image>
<gen>7</gen>
</enemy>
</enemies>
</stage>
</stages>
test1.py
import xml.etree.ElementTree as tree
import time
WIDTH = 320
class Zombie:
def __init__(self, name, hp, bite, speed, image, gen):
self.__name = name
self.__hp = hp
self.__bite = bite
self.__speed = speed
self.__image = image
self.__gen = gen
self.__position = 0
self.__visible = False
def __str__(self):
return str(self.__dict__)
def __repr__(self):
return str(self.__dict__)
@property # getter
def name(self):
return self.__name
@property
def hp(self):
return self.__hp
@hp.setter
def hp(self, hp):
self.__hp = hp
if self.__hp < 0:
self.__hp = 0
@property
def bite(self):
return self.__bite
@property
def speed(self):
return self.__speed
@property
def image(self):
return self.__image
@property
def gen(self):
return self.__gen
@property
def position(self):
return self.__position
@position.setter
def position(self, position):
self.__position = position
@property
def visible(self):
return self.__visible
@visible.setter
def visible(self, visible):
self.__visible = visible
def howl(self):
print("Grrr")
class AxZombie(Zombie): # Zombie를 상속
def __init__(self, name, hp, bite, speed, image, gen):
super().__init__(name, hp, bite, speed, image, gen)
def howl(self):
print("Ax!")
class Stage:
def __init__(self, name, hp, enemies):
self.__name = name
self.__hp = hp
self.__enemies = enemies
def __str__(self):
return str(self.__dict__)
def __repr__(self):
return str(self.__dict__)
@property
def enemies(self):
return self.__enemies
@property
def hp(self):
return self.__hp
@hp.setter
def hp(self, hp):
self.__hp = hp
if self.__hp < 0:
self.__hp = 0
def zombie_factory(ele_zombie):
# attribute
if ele_zombie.attrib.get("type") == "basic":
zombie = Zombie
elif ele_zombie.attrib.get("type") == "ax":
zombie = AxZombie
# Python에서는 함수도 객체로 취급
return zombie(ele_zombie.find("name").text, int(ele_zombie.find("hp").text),
int(ele_zombie.find("bite").text), int(ele_zombie.find("speed").text),
ele_zombie.find("image").text, int(ele_zombie.find("gen").text))
def load_data(resource):
root = tree.parse(resource)
obj_stages = list()
for i in root.findall("stage"):
enemies = list()
for j in i.find("enemies").findall("enemy"):
# if j == 'basic':
# enemies.append(Zombie)
# elif j == 'ax':
# enemies.append(AxZombie)
enemies.append(zombie_factory(j))
obj_stages.append(Stage(i.find("name").text, int(i.find("hp").text), enemies))
return obj_stages
def display_zombie(enemies):
map_image = "__|" * (WIDTH//10)
map_image_list = list(map_image)
for i in enemies:
if i.visible:
map_image_list[(i.position//10) * 3] = i.image
print("".join(map_image_list)) # list의 요소를 ""로 연결해 한 문자열로 출력
def display(stage):
print("user hp:", stage.hp)
for i in stage.enemies:
if i.visible:
print(i.name, " hp:", i.hp)
display_zombie(stage.enemies)
stages = load_data("resource.xml")
print(stages)
game_over = False
interval = 0.5
current_stage = 0
current_frame = 0
while not game_over:
print("------------------------------" + str(current_frame))
for zombie in stages[current_stage].enemies:
if zombie.visible:
zombie.position += zombie.speed
if zombie.gen == current_frame:
zombie.visible = True
zombie.howl()
if zombie.position >= WIDTH:
stages[current_stage].hp -= zombie.bite
zombie.visible = False
display(stages[current_stage])
current_frame += 1
if stages[current_stage].hp == 0:
current_stage += 1
current_frame = 0
if current_stage == len(stages):
game_over = True
time.sleep(interval)
출처: 코딩티타임
'파이썬 Python' 카테고리의 다른 글
[Python] 중급 2강 문자열 포매팅 (0) | 2021.02.25 |
---|---|
[Python] 중급 1 문자열 (0) | 2021.02.25 |
초급 10, 11 클래스와 상속 (0) | 2021.01.23 |
초급 9 XML (0) | 2021.01.15 |
초급 8 REST, JSON (0) | 2021.01.14 |