-
Notifications
You must be signed in to change notification settings - Fork 0
/
worm.py
248 lines (175 loc) · 6.97 KB
/
worm.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import math
import gameobjects
from time import time
from globalconst import *
from playerobject import Player
from ball import Ball
class rect():
def __init__(self,x,y,width,height):
self.x=x
self.y=y
self.width=width
self.height = height
class Worm(gameobjects.GameObject):
def __init__(self,x,y):
super(Worm, self).__init__(x,y)
#self.x = x
#self.y = y
self.head =[x,y]
self.body=[]
self.tiles=["H","B"]
#worm move timer
self.last_move_time= 0
#worm body addition timer
self.step_counter = 0
self.facedir=LEFT
self.facedir_old=LEFT
self.move_dir=[0,0]
self.debugList =[]
self._MOVEMENTS = [
[-1,0],
[1,0],
[0,-1],
[0,1],
]
self._dir_ops=[2,1,4,3]
self.state ="ALIVE" #possibl values here are ALIVE,RESPAWNING
self.time_of_death = 0
self.grow_counter = WORM_INITIAL_GROW
def getSprite(self,sprite,tiles):
return tiles[sprite]
def update(self, gamestate):
self.debugList = []
if self.state=="DEAD":
if self.time_of_death + WORM_RESPAWN_TIME < time():
self._respawn()
else:
return False
#when we should move
if self.last_move_time + WORM_MOVE_TIME < time() :
self.facedir_old=self.facedir
self.xdir = self._MOVEMENTS[self.facedir][0]
self.ydir = self._MOVEMENTS[self.facedir][1]
#if we should move
#did a dir change occure
if True:
#check future collision with self
if self._collides_body(rect((self.x +self.xdir)* self.width, (self.y+self.ydir )* self.height, self.width, self.height)) == True:
#print("Collided with self!!")
self.state="DEAD"
self.time_of_death = time()
self.head=[-1,-1]
self.y=-1
self.x=-1
self.body=[]
return False
# here reset the worm parts
#check collision with map
if gamestate.getLevel()[self.y+self.ydir][self.x+self.xdir] == "#" :
#print("Collided with a wall D:")
self.state = "DEAD"
self.time_of_death = time()
self.head=[-1,-1]
self.y=-1
self.x=-1
self.body=[]
return False
#update all the bodys first
if len(self.body)>0:
for part_idx in range(len(self.body),0,-1):
#part_tmp = self.body[part_idx-1]
if part_idx-1 ==0:
self.body[part_idx-1]=[*self.head]
else:
self.body[part_idx-1]=[*self.body[part_idx-2]]
#add a body part
if self.step_counter >= WORM_STEPS_TIL_ADDITION:
if self.grow_counter > 0:
self.step_counter = 0
self.body.insert(0,[*self.head])
self.grow_counter -= 1
self.last_move_time = time()
self.step_counter+=1
#update the position
self.head[0] += self.xdir
self.head[1] += self.ydir
self.x += self.xdir
self.y += self.ydir
for game_object in gamestate.objects.values():
if self.collide_head(game_object):
if type(game_object) is Player:
print("collided with player ~")
#game_object.get_eaten()
#if len(self.body) > 6:
# self.body = self.body[:len(self.body)-4]
elif type(game_object) is Ball:
print("collided with ball ~")
#game_object.get_eaten()
self.grow_counter = 8
def _respawn(self):
self.head = [self.spawnx, self.spawny]
self.x = self.head[0]
self.y = self.head[1]
self.body=[]
self.state="ALIVE"
self.facedir =LEFT
self.grow_counter = WORM_INITIAL_GROW
def _gen_collide(self,obj_a,obj_b):
if obj_a[0] < obj_b.x + obj_b.width and \
obj_a[0] + self.width > obj_b.x and \
obj_a[1] < obj_b.y + obj_b.height and \
obj_a[1] + self.height > obj_b.y:
self.debugList.append([obj_a[0], obj_a[1],self.width,self.height])
self.debugList.append([obj_b.x, obj_b.y,obj_b.width,obj_b.height])
return True
return False
def _collides_body(self,game_object):
collides = False
for part in self.body:
part_coords=[part[0]*self.width,part[1]*self.height]
if self._gen_collide(part_coords,game_object) == True:
return True
return collides
def collide_head(self,game_object):
if self.head[0]*self.width < game_object.x + game_object.width and \
self.head[0]*self.width + self.width > game_object.x and \
self.head[1]*self.height < game_object.y + game_object.height and \
self.head[1]*self.height + self.height > game_object.y:
#debugList.append([self.x, self.y])
#debugList.append([game_object.x, game_object.y])
return True
else:
return False
def collides(self, game_object):
if self.head[0]*self.width < game_object.x + game_object.width and \
self.head[0]*self.width + self.width > game_object.x and \
self.head[1]*self.height < game_object.y + game_object.height and \
self.head[1]*self.height + self.height > game_object.y:
#debugList.append([self.x, self.y])
#debugList.append([game_object.x, game_object.y])
return True
else:
col=self._collides_body(game_object)
return col
return False
def draw(self,screen,tiles,gamestate):
#self.getSprite("H",tiles)
#draw the head
if self.state== "DEAD":
return False
screen.blit(tiles[self.tiles[0]], (self.head[0] * self.width, self.head[1] * self.height))
#draw the body
for part in self.body:
screen.blit(tiles["B"], (part[0] * self.width, part[1] * self.height))
def moveLeft(self):
if self.facedir_old != RIGHT:
self.facedir =LEFT
def moveRight(self):
if self.facedir_old != LEFT:
self.facedir =RIGHT
def moveUp(self):
if self.facedir_old != DOWN:
self.facedir = UP
def moveDown(self):
if self.facedir_old != UP:
self.facedir = DOWN