Bases: pygame.sprite.Sprite, Collidable
NPC
Source code in Tairitsu/NPCs.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 | def __init__(self, x, y, name):
"""
NPC
"""
# Initialize father classes
pygame.sprite.Sprite.__init__(self)
Collidable.__init__(self)
self.skinPath = GamePath.npc
self.image = pygame.image.load(GamePath.npc)
self.image = pygame.transform.scale(self.image, (NPCSettings.npcWidth, NPCSettings.npcHeight))
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
self.actualRect = self.rect.copy()
self.name = name
self.initialPosition = x # 记录初始位置
self.speed = NPCSettings.npcSpeed
self.direction = 1
self.patrollingRange = 70 # 巡逻范围
self.talking = False
self.talkCD = 0 # cooldown of talk
|
Attributes
actualRect
instance-attribute
actualRect = self.rect.copy()
direction
instance-attribute
image
instance-attribute
image = pygame.transform.scale(self.image, (NPCSettings.npcWidth, NPCSettings.npcHeight))
initialPosition
instance-attribute
patrollingRange
instance-attribute
rect
instance-attribute
rect = self.image.get_rect()
skinPath
instance-attribute
speed
instance-attribute
speed = NPCSettings.npcSpeed
talkCD
instance-attribute
talking
instance-attribute
Functions
can_talk
Source code in Tairitsu/NPCs.py
| def can_talk(self):
return self.talkCD == 0
|
draw
Source code in Tairitsu/NPCs.py
| def draw(self, window, dx=0, dy=0):
window.blit(self.image, (self.rect.x, self.rect.y))
|
reset_talkCD
Source code in Tairitsu/NPCs.py
| def reset_talkCD(self):
self.talkCD = NPCSettings.talkCD
|
update
Source code in Tairitsu/NPCs.py
34
35
36
37
38
39
40
41
42
43 | def update(self):
# raise NotImplementedError
if not self.talking:
self.actualRect.x += self.speed * self.direction
if abs(self.actualRect.x - self.initialPosition) > self.patrollingRange:
self.direction *= -1 # 反转方向
self.image = pygame.transform.flip(self.image, True, False)
if self.talkCD > 0:
self.talkCD -= 1
|