-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.py
198 lines (173 loc) · 6.27 KB
/
entity.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# 엔티티
from pygame.locals import *
import pygame
import random
import asset
from settings import settings
screen_width = 480
screen_height = 640
class Entity():
def __init__(self, image: str, scale: float, screen: pygame.Surface):
self.image:pygame.Surface = pygame.image.load(image)
self.image_str:str = image
self.before_size: list = self.image.get_rect().size
self.scale: tuple(float, float) = (self.before_size[0] * scale, self.before_size[1] * scale)
self.image = pygame.transform.scale(self.image, self.scale)
self.image_o = pygame.transform.scale(self.image, self.scale)
self.size:list = self.image.get_rect().size
self.width:float = self.size[0]
self.height:float = self.size[1]
self.x:float = 0
self.y:float = 0
self.speed:float = 0
self.rect:Rect = None
self.screen = screen
# 충돌 감지 비교 요소
def setRect(self):
self.rect = self.image.get_rect()
self.rect.left = self.x
self.rect.top = self.y
# 그리기
def draw(self):
self.screen.blit(self.image, (self.x, self.y))
# 숨기기
def hideEntity(self):
self.image = pygame.image.load(asset.empty_image)
# 보이기
def showEntity(self):
self.image = self.image_o
# 플레이어
class Player(Entity):
def __init__(self, image, screen: pygame.Surface):
super().__init__(image, 0.6, screen)
self.x = (screen_width / 2) - (self.width / 2)
self.y = float(screen_height) - self.height # 플레이어 y좌표
self.to_x = 0 # 플레이어 이동할 x좌표
self.speed = 0.2 # 플레이어 속도
self.score = 0 # 플레이어 점수
self.health = 100 # 플레이어 체력
# 화면 밖 나가기 제한
def limitPos(self):
if self.x < 0:
self.x = 0
elif self.x > screen_width - self.width:
self.x = screen_width - self.width
# 이동
def move(self, frame):
self.x += frame * self.to_x
# 체력 피해
def giveDamage(self, damage):
self.health -= damage
game_font = pygame.font.Font(asset.font["NeoDunggeunmo"], 20)
gotDamage = game_font.render("-" + str(damage), True, (255, 0, 0))
self.screen.blit(gotDamage, (self.x + 15, self.y - 15))
if self.health <= 0:
self.health = 0
# 체력 획득
def giveHeal(self, health):
self.health += health
game_font = pygame.font.Font(asset.font["NeoDunggeunmo"], 20)
gotHeal = game_font.render("+" + str(health), True, (0, 255, 0))
self.screen.blit(gotHeal, (self.x + 15, self.y - 15))
if self.health >= 100:
self.health = 100
# 초기화
def reset(self):
self.x = (screen_width / 2) - (self.width / 2)
self.y = screen_height - self.height
self.speed = 0.3
self.score = 0
self.health = 100
self.to_x = 0
# 피해 레벨 (0 ~ 3)
def checkLevel(self):
if 70 < self.health:
return 0
elif 50 < self.health <= 70:
return 1
elif 20 < self.health <= 50:
return 2
elif 0 < self.health <= 20:
return 3
else:
return 4
# 죽었는지 확인
def isDie(self):
if self.checkLevel() == 4:
return True
else:
return False
# 똥
class Poop(Entity):
def __init__(self, image, screen: pygame.Surface):
super().__init__(image, 0.8, screen)
self.x = random.randint(0, screen_width - int(round(self.width)))
self.y = 0 - self.height
self.speed = (random.randint(1, 4)) / 10
self.rect = None
self.damage = 0;
if settings["mode"].main == "MIXED":
if self.image_str == PoopImage[0]:
self.damage = PoopDamage[0]
elif self.image_str == PoopImage[1]:
self.damage = PoopDamage[1]
elif self.image_str == PoopImage[2]:
self.damage = PoopDamage[2]
else:
self.damage = PoopDamage
# 소멸되었을 때
def isDestroy(self):
return self.y >= (screen_height - self.height)
# return self.y > screen_height
# 이동
def move(self, frame):
self.y += frame * self.speed
# 초기화
def reset(self):
self.y = 0 - self.height
# 아이템
class Item(Entity):
def __init__(self, image, screen: pygame.Surface):
super().__init__(image, 0.8, screen)
self.isLand:bool = False
self.onLandTime:int = 0
self.nowTime:int = 0
self.destroyTime:int = 10
# 초기화
def reset(self):
self.y = 0 - self.height
self.isLand = False
# 랜딩했을 때, 소멸되었는가
def isOnLand(self, frame):
if self.isLand:
if self.onLandTime == 0:
self.onLandTime = pygame.time.get_ticks()
return True
elif self.onLandTime != 0:
self.nowTime = pygame.time.get_ticks()
return self.isDestroy(frame)
return True
# 소멸 조건
def isDestroy(self, frame):
return self.nowTime - self.onLandTime < self.destroyTime * frame
# 이동
def move(self, frame):
if not self.isLand:
self.y += frame * self.speed
if self.y >= screen_height - self.height:
self.y = screen_height - self.height
self.isLand = True
# 긴급 킷
class AidKit(Item):
def __init__(self, image, screen: pygame.Surface):
super().__init__(image, screen)
self.x = random.randint(0, screen_width - int(round(self.width)))
self.y = 0 - self.height
self.speed = (random.randint(1, 4)) / 10
self.heal = 5
PoopList = [] # 똥 인스턴스 생성 목록
PoopCount = 0 # 똥 갯수
PoopImage = asset.entity["poop"]
PoopDamage = 30
ItemList = [] # 아이템 인스턴스 생성 목록
ItemCount = 0 # 아이템 갯수