Uh oh!
There was an error while loading. Please reload this page.
forked from RyanKruse/Python-RTS
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.py
More file actions
Latest commit
130 lines (108 loc) · 4.61 KB
/
Copy pathunit.py
File metadata and controls
130 lines (108 loc) · 4.61 KB
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
importpygameaspg
frompygame.spriteimportSprite
fromsettingsimport*
# =============================================== Owner Classes ================================================== #
classUnit(Sprite):
"""This class handles knights."""
def__init__(self, game):
self.groups=game.g.all_sprites, game.g.collision_sprites, game.g.units
pg.sprite.Sprite.__init__(self, self.groups)
self.game=game
# Image information
self.image_deselected=pg.image.load(UNIT_DESELECTED)
self.image_selected=pg.image.load(UNIT_DESELECTED)
self.image=self.image_deselected
# Location information.
self.size=self.image.get_rect().size
self.rect=self.image.get_rect()
self.rect.x=pg.mouse.get_pos()[0] - (self.size[0] /2) -self.game.camera.x
self.rect.y=pg.mouse.get_pos()[1] - (self.size[1] /2) -self.game.camera.y
# Movement slope information.
self.move_x=0
self.move_y=0
self.move_slope=0
self.move_quadrant=0
self.move_counter=0
self.speed=KNIGHT_SPEED
self.health=KNIGHT_HP
# Boolean logic.
self.is_selected=False
self.is_moving=False
self.is_hotkeyed=False
defupdate(self):
"""This fires every single frame."""
if (self.game.mouse.is_commandingandself.is_selected) orself.is_moving:
self.set_move_slope()
self.collision_detection()
defset_move_location(self):
"""Defines new movement location for selected units."""
self.move_counter=0
self.is_moving=True
self.move_x=pg.mouse.get_pos()[0] -self.game.camera.x
self.move_y=pg.mouse.get_pos()[1] -self.game.camera.y
defset_move_slope(self):
"""This calculates the movement to a given location."""
self.game.slope_movement_logic(self)
defcollision_detection(self):
"""If collision with another sprite occurs, this bumps us away."""
forotherinpg.sprite.spritecollide(self, self.game.g.collision_sprites, False):
self.game.collision_bump(self, other)
defset_selected(self):
"""Adds unit into the selected units group."""
pg.sprite.Group.add(self.game.g.selected_units, self)
self.is_selected=True
self.image=self.image_selected
defset_deselected(self):
"""Removes unit from the selected units group."""
pg.sprite.Group.remove(self.game.g.selected_units, self)
self.is_selected=False
self.image=self.image_deselected
defdelete(self):
"""Deletes self."""
pg.sprite.spritecollide(self, self.game.g.units, True)
classKnight(Unit):
def__init__(self, game):
super().__init__(game)
# Image information.
self.image_deselected=pg.image.load(KNIGHT_DESELECTED)
self.image_selected=pg.image.load(KNIGHT_SELECTED)
self.image=pg.image.load(KNIGHT_DESELECTED)
self.health=KNIGHT_HP
# =============================================== Enemy Classes ================================================== #
classEnemyUnit(Sprite):
"""This class handles enemy units."""
def__init__(self, game):
self.groups=game.g.all_sprites, game.g.collision_sprites, game.g.enemy_units
pg.sprite.Sprite.__init__(self, self.groups)
self.game=game
# Image information
self.image=pg.image.load(UNIT_DESELECTED)
self.size=self.image.get_rect().size
self.rect=self.image.get_rect()
self.rect.x=pg.mouse.get_pos()[0] - (self.size[0] /2) -self.game.camera.x
self.rect.y=pg.mouse.get_pos()[1] - (self.size[1] /2) -self.game.camera.y
# Movement slope information.
self.move_x=0
self.move_y=0
self.move_slope=0
self.move_quadrant=0
self.move_counter=0
# Health information
self.health=KNIGHT_HP
defchild_init(self):
pass
defupdate(self):
"""I need an AI class one day."""
self.collision_detection()
defcollision_detection(self):
"""If collision with another sprite occurs, this bumps us away."""
forotherinpg.sprite.spritecollide(self, self.game.g.collision_sprites, False):
self.game.collision_bump(self, other)
defdelete(self):
"""Deletes self."""
pg.sprite.spritecollide(self, self.game.g.enemy_units, True)
classEnemyKnight(EnemyUnit):
def__init__(self, game):
super().__init__(game)
# Image information.
self.image=pg.image.load(ENEMY_KNIGHT)