-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayerobject.py
207 lines (157 loc) · 5.92 KB
/
playerobject.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
import math
from globalconst import *
from gameobjects import *
from time import time
class Player(GameObject):
def __init__(self, x, y, playerid):
GameObject.__init__(self, x, y)
self.playerid = playerid
self.playertile = playerid % 5 + 1
self.tile = str(self.playertile)
self.tick = 0
self.anim = 0
self.is_walking = True
self.status = "ALIVE"
self.death_time =0
self.old_x = 0
self.old_y = 0;
self.kick_mode = False
self.kick_angle = (0, 0)
def respawn(self):
self.x = self.spawnx
self.y = self.spawny
self.status = "ALIVE"
self.tick = 0
self.anim = 0
self.is_walking=True
self.kick_mode = False
def get_eaten(self):
self.status = "DEAD"
self.death_time = time()
def interact(self, gamestate, release=False):
if self.status == 'DEAD':
return
if not release:
self.kick_mode = True
# stop ball
self.calcKickAngle(gamestate)
if self.kick_angle != (0, 0):
gamestate.getBall().stop()
else: # release
if self.kick_mode:
self.kick_mode = False
if self.kick_angle != (0, 0):
ball = gamestate.getBall()
speed = 30
kickheight = (abs(self.kick_angle[0]) + abs(self.kick_angle[1])) * speed
ball.kick(self.kick_angle[0] * speed, self.kick_angle[1] * speed, kickheight)
self.kick_angle = (0, 0)
def calcKickAngle(self, gamestate):
ball = gamestate.getBall()
ballCenterX = ball.x + ball.width/2
ballCenterY = ball.y + ball.height/2
playerCenterX = self.x + TILE_W/2
playerCenterY = self.y + TILE_H - 3
diffX = ballCenterX - playerCenterX
diffY = ballCenterY - playerCenterY
distance = math.sqrt(pow(diffX, 2) + pow(diffY, 2))
if distance < 16 and distance > 0 and ball.z < 8:
if VARIABLE_KICK_POWER:
diffX /= 16
diffY /= 16
else:
diffX /= distance # normalise
diffY /= distance # normalise
self.kick_angle = (diffX, diffY)
else:
self.kick_angle = (0, 0)
def update(self, gamestate):
if self.status == "DEAD":
if self.death_time + PLAYER_RESPAWN_TIME < time():
self.respawn()
speedmod = 0.5 if self.kick_mode else 1
if REDUCE_DIAGONAL_SPEED:
if self.xdir != 0 and self.ydir != 0:
speedmod *= 0.75
newxdir = self.xdir * self.speed * speedmod
newydir = self.ydir * self.speed * speedmod
self.old_x = self.x
self.old_y = self.y
newx = self.x + newxdir
newy = self.y + newydir
x1 = int(newx / TILE_W)
x2 = int((newx + TILE_W - 1) / TILE_W)
y1 = int(newy / TILE_H)
y2 = int((newy + TILE_H - 1) / TILE_H)
colltile1 = gamestate.getLevel()[y1][x1] # upper left
colltile2 = gamestate.getLevel()[y1][x2] # upper right
colltile3 = gamestate.getLevel()[y2][x1] # lower left
colltile4 = gamestate.getLevel()[y2][x2] # lower right
if self.xdir < 0:
if colltile1 == "#" or colltile3 == "#":
newxdir = 0
elif self.xdir > 0:
if colltile2 == "#" or colltile4 == "#":
newxdir = 0
newx = self.x
newy = self.y + newydir
x1 = int(newx / TILE_W)
x2 = int((newx + TILE_W - 1) / TILE_W)
y1 = int(newy / TILE_H)
y2 = int((newy + TILE_H - 1) / TILE_H)
colltile1 = gamestate.getLevel()[y1][x1] # upper left
colltile2 = gamestate.getLevel()[y1][x2] # upper right
colltile3 = gamestate.getLevel()[y2][x1] # lower left
colltile4 = gamestate.getLevel()[y2][x2] # lower right
if self.ydir < 0:
if colltile1 == "#" or colltile2 == "#":
newydir = 0
elif self.ydir > 0:
if colltile3 == "#" or colltile4 == "#":
newydir = 0
newx = self.x + newxdir
newy = self.y + newydir
self.tick += 1
if self.is_walking:
if self.tick % 20 > 10:
self.anim = 2
else:
self.anim = 1
else:
self.anim = 0
# collision with screen bounds
if newx < 0:
newx = 0
elif newx > SCR_W - TILE_W:
newx = SCR_W - TILE_W
if newy < 0:
newy = 0
elif newy > SCR_H - TILE_H*2:
newy = SCR_H - TILE_H*2
self.is_walking = (self.xdir != 0) or (self.ydir != 0)
self.x = newx
self.y = newy
for currentWorm in gamestate.getWorms():
if currentWorm._collides_body(self):
self.x = self.old_x
self.y = self.old_y
# check again for EATEN status
self.status = 'ALIVE'
for currentWorm in gamestate.getWorms():
if currentWorm._collides_body(self):
self.status = 'EATEN'
# update kick angle
if self.kick_mode:
self.calcKickAngle(gamestate)
def draw(self, screen, tiles, gamestate):
if self.status == "DEAD":
return
if self.status == "EATEN":
screen.blit(tiles['ea'], (self.x, self.y - TILE_H))
else:
screen.blit(tiles[self.tile + str(self.anim)], (self.x, self.y - TILE_H))
if self.kick_mode and self.kick_angle != (0, 0):
ball = gamestate.getBall()
linesize = 16 if VARIABLE_KICK_POWER else 8
pygame.draw.line(screen, (0, 64, 0), (ball.x + ball.width/2, ball.y + ball.height/2),
(ball.x + ball.width/2 + self.kick_angle[0] * linesize, ball.y + ball.height/2 + self.kick_angle[1] * linesize))