-
Notifications
You must be signed in to change notification settings - Fork 56
/
tank.py
95 lines (74 loc) · 2.63 KB
/
tank.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
'''
Deflectouch
Copyright (C) 2012 Cyril Stoller
For comments, suggestions or other messages, contact me at:
This file is part of Deflectouch.
Deflectouch is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Deflectouch is distributed in the hope that it will be fun,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Deflectouch. If not, see <http://www.gnu.org/licenses/>.
'''
import kivy
kivy.require('1.0.9')
from kivy.properties import ObjectProperty
from kivy.graphics.transformation import Matrix
from kivy.uix.widget import Widget
from kivy.utils import boundary
from math import radians
from math import atan2
from math import pi
'''
####################################
##
## Tank Class
##
####################################
'''
class Tank(Widget):
tank_tower_scatter = ObjectProperty(None)
'''
####################################
##
## On Touch Down
##
####################################
'''
def on_touch_down(self, touch):
if not self.collide_point(*touch.pos):
return False
else:
touch.ud['tank_touch'] = True
return True
'''
####################################
##
## On Touch Move
##
####################################
'''
def on_touch_move(self, touch):
ud = touch.ud
if not 'tank_touch' in ud:
return False
if 'rotation_mode' in ud:
# if the current touch is already in the 'rotate' mode, rotate the tower.
dx = touch.x - self.center_x
dy = touch.y - self.center_y
angle = boundary(atan2(dy, dx) * 360 / 2 / pi, -60, 60)
angle_change = self.tank_tower_scatter.rotation - angle
rotation_matrix = Matrix().rotate(-radians(angle_change), 0, 0, 1)
self.tank_tower_scatter.apply_transform(rotation_matrix, post_multiply=True, anchor=(105, 15))
elif touch.x > self.right:
# if the finger moved too far to the right go into rotation mode
ud['rotation_mode'] = True
else:
# if the user wants only to drag the tank up and down, let him do it!
self.y += touch.dy
pass