파이썬 활용
클래스, 멤버변수, 메소드, 상속, 다중상속, 메소드, 오버라이딩, pass, super를 활용한 예시 프로젝트
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
from random import *
# 일반 유닛
class Unit:
def __init__(self, name, hp, speed):
self.name = name
self.hp = hp
self.speed = speed
print("{0} 유닛이 생성되었습니다".format(name))
def move(self, location):
print("{0} : {1} 방향으로 이동합니다[속도 {2}]".format(
self.name, location, self.speed))
def damaged(self, damage):
print("{0} : {1} 데미지를 입었습니다".format(self.name, damage))
self.hp -= damage
print("{0} : 현재 체력은 {1} 입니다".format(self.name, self.hp))
if self.hp <= 0:
print("{0} : 파괴되었습니다".format(self.name))
# 공격 유닛
class AttackUnit(Unit):
def __init__(self, name, hp, speed, damage):
Unit.__init__(self, name, hp, speed)
self.damage = damage
def attack(self, location):
print("{0} : {1} 방향으로 적군을 공격합니다 [공격력 {2}]".format(
self.name, location, self.damage))
# 마린
class Marine(AttackUnit):
def __init__(self):
AttackUnit.__init__(self, "마린", 40, 1, 5)
# 스팀팩
def stimpack(self):
if self.hp > 10:
self.hp -= 10
print("{0} : 스팀팩을 사용합니다 (HP 10 감소)".format(self.name))
else:
print("{0} : 체력이 부족하여 스팀팩을 사용하지 않습니다".format(self.name))
class Tank(AttackUnit):
# 시즈모드
seize_developed = False # 시즈 모드 개발 여부
def __init__(self):
AttackUnit.__init__(self, "탱크", 150, 1, 35)
self.seize_mode = False
def set_seize_mode(self):
if Tank.seize_developed == False:
return
# 현재 시즈모드가 아닐 때 -> 시즈모드
if self.seize_mode == False:
print("{0} : 시즈 모드로 전환합니다".format(self.name))
self.damage *= 2
self.seize_mode = True
# 현재 시즈모드 일 때 -> 해제
else:
print("{0} : 시즈 모드로 해제합니다".format(self.name))
self.damage /= 2
self.seize_mode = False
class Flyable:
def __init__(self, flying_speed):
self.flying_speed = flying_speed
def fly(self, name, location):
print("{0} : {1} 방향으로 날아갑니다 [속도 {2}]".format(
name, location, self.flying_speed))
class FlyableAttackUnit(AttackUnit, Flyable):
def __init__(self, name, hp, damage, flying_speed):
AttackUnit.__init__(self, name, hp, 0, damage) # 지상 스피드는 0
Flyable.__init__(self, flying_speed)
def move(self, location):
self.fly(self.name, location)
# 레이스
class Wraith(FlyableAttackUnit):
def __init__(self):
FlyableAttackUnit.__init__(self, "레이스", 80, 20, 5)
self.clocked = False # 클로킹 모드(해제 상태)
def clocking(self):
if self.clocked == True:
print("{0} : 클로킹 모드 해제 합니다".format(self.name))
self.clocked = False
else: # 클로킹 해제 -> 모드 설정
print("{0} : 클로킹 모드 설정 합니다".format(self.name))
self.clocked = True
def game_start():
print("[알림] 새로운 게임을 시작합니다")
def game_over():
print("Player : gg")
print("[Player] 님이 게임에서 퇴장하셨습니다")
# 실제 게임 시작
game_start()
m1 = Marine()
m2 = Marine()
m3 = Marine()
t1 = Tank()
t2 = Tank()
w1 = Wraith()
# 유닛 일괄 관리
attack_units = []
attack_units.append(m1)
attack_units.append(m2)
attack_units.append(m3)
attack_units.append(t1)
attack_units.append(t2)
attack_units.append(w1)
# 전군 이동
for unit in attack_units:
unit.move("1시")
# 탱크 시즈모드 개발
Tank.seize_developed = True
print("[알림] 탱크 시즈모드 개발 완료")
# 공격 모드준비 (마린 : 스팀팩, 탱크 : 시즈 모드 , 레이스 : 클로킹)
for unit in attack_units:
if isinstance(unit, Marine):
unit.stimpack()
elif isinstance(unit, Tank):
unit.set_seize_mode()
elif isinstance(unit, Wraith):
unit.clocking()
# 전국 공격
for unit in attack_units:
unit.attack("1시")
# 전군 피해
for unit in attack_units:
unit.damaged(randint(5, 20)) # 공격은 랜덤으로 받음(5~20)
# 게임 종료
game_over()
|
cs |
출처 유튜브 '나도코딩' [파이썬 코딩 무료강의 (기본편)]