-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_player_old.py
75 lines (52 loc) · 2.44 KB
/
game_player_old.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
'''
Created on 2 okt. 2015
@author: danhe
'''
from game_sprite import Sprite
from math import copysign, sqrt
class Player(Sprite):
def __init__(self, id, position):
super(Player, self).__init__(source='graphics/player.png', pos=position)
self.id = id
self.velocity = [0, 0]
self.touch_position = position
self.map_position = position
self.map_offset = [0, 0]
return
def update(self):
print(self.children)
self.map_offset = self.parent.offset
if copysign(1, self.velocity[0]) * (self.touch_position[0] - self.map_position[0]) < 0:
self.velocity[0] = 0
if copysign(1, self.velocity[1]) * (self.touch_position[1] - self.map_position[1]) < 0:
self.velocity[1] = 0
if not self.parent.collide_point(self.map_position[0],self.map_position[1]):
self.map_position[0] -= self.velocity[0]
self.map_position[1] -= self.velocity[1]
self.velocity = [0,0]
for child in self.parent.children:
if child.id == self.id or child.id == 'sprite':
continue
if self.collide_widget(child):
self.map_position[0] -= self.velocity[0]
self.map_position[1] -= self.velocity[1]
self.velocity = [0,0]
print(self.id,child.id,self.collide_widget(child))
self.map_position[0] += self.velocity[0]
self.map_position[1] += self.velocity[1]
self.x = self.map_position[0] + self.map_offset[0]
self.y = self.map_position[1] + self.map_offset[1]
# print(self.parent.children[0].map_position)
# print(self.collide_widget(self.parent.children[0]))
return
def on_touch_down(self, touch):
if touch.button == 'left':
self.touch_position = list(touch.pos)
self.touch_position[0] -= self.map_offset[0]
self.touch_position[1] -= self.map_offset[1]
dx = self.map_position[0] - self.touch_position[0]
dy = self.map_position[1] - self.touch_position[1]
N = sqrt(dx ** 2 + dy ** 2)
self.velocity[0] = -5 * dx / N
self.velocity[1] = -5 * dy / N
return