-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gun.py
65 lines (56 loc) · 2.71 KB
/
Gun.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
from Character import Character
import math
import keyboard
class Gun(Character):
def __init__(self, canvas):
super().__init__(canvas)
self.gun_end_x_axis = self.x_axis + (2 * self.R)
self.gun_end_y_axis = self.y_axis
def create(self):
self.item_id = self.canvas.create_line(self.x_axis, self.y_axis, self.x_axis + (2 * self.R),
self.y_axis, fill='red', width=30)
def move(self):
if keyboard.is_pressed('a'):
if self.x_axis > 2 + self.R:
self.x_axis -= self.speed
self.gun_end_x_axis -= self.speed
self.canvas.move(self.item_id, -self.speed, 0)
if keyboard.is_pressed('d'):
if self.x_axis < 800 - self.R:
self.x_axis += self.speed
self.gun_end_x_axis += self.speed
self.canvas.move(self.item_id, self.speed, 0)
def jump(self):
if keyboard.is_pressed('w'):
self.jump_position -= self.jump_speed
self.y_axis -= self.jump_position
self.gun_end_y_axis -= self.jump_position
self.canvas.move(self.item_id, 0, -self.jump_position)
if self.jump_position <= -(self.ground_position - self.jump_speed):
self.jump_position = self.ground_position
self.y_axis = 550
else:
if (self.jump_position >= -(self.ground_position - self.jump_speed * 2)
and self.jump_position != self.ground_position):
self.jump_position -= self.jump_speed
self.y_axis -= self.jump_position
self.gun_end_y_axis -= self.jump_position
self.canvas.move(self.item_id, 0, -self.jump_position)
else:
self.jump_position = self.ground_position
self.y_axis = 550
self.canvas.coords(self.item_id, self.x_axis, self.y_axis,
self.gun_end_x_axis, self.gun_end_y_axis)
def move_gun(self):
def motion(event):
if event.x > self.x_axis:
angle = math.atan((event.y - self.y_axis) / (event.x - self.x_axis))
elif event.x == self.x_axis:
angle = -math.pi / 2
else:
angle = math.atan((event.y - self.y_axis) / (event.x - self.x_axis)) + math.pi
self.gun_end_x_axis = self.x_axis + self.R * 2 * math.cos(angle)
self.gun_end_y_axis = self.y_axis + self.R * 2 * math.sin(angle)
self.canvas.coords(self.item_id, self.x_axis, self.y_axis,
self.gun_end_x_axis, self.gun_end_y_axis)
self.canvas.bind('<Motion>', motion)