-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
151 lines (113 loc) · 5.26 KB
/
test.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
import pyglet
from pyglet import shapes
from pyglet.window import key
from pyglet.window import mouse
import math
window = pyglet.window.Window()
window.set_size(400, 400)
Batch = pyglet.graphics.Batch()
class vector():
def __init__(self, x, y) -> None:
self.x = x
self.y = y
class pike():
def __init__(self, x, y, size) -> None:
self.position = vector(x, y)
self.size = size
self.pointer = vector(x + size, y)
self.velocity = vector(x=20,y=50)
self.previus_velocity = self.velocity
self.angle = 0
direction = vector(self.pointer.x - self.position.x, self.pointer.y - self.position.y)
to_perpendicular1 = [-direction.y * size / 30, direction.x * size / 30]
to_perpendicular2 = [direction.y * size / 30, -direction.x * size / 30]
self.center = shapes.Circle(x, y, radius=2, color=(50, 225, 30), batch=Batch)
self.towards_line = shapes.Line(
x=self.position.x, y=self.position.y,
x2=self.pointer.x, y2=self.pointer.y,
width=1, batch=Batch)
self.tail_line = shapes.Line(
x=self.position.x, y=self.position.y,
x2=self.position.x - direction.x / 3 , y2=self.position.y - direction.y / 3,
width=1, batch=Batch)
self.left_line = shapes.Line(
x=self.position.x, y=self.position.y,
x2=to_perpendicular1[0] + self.position.x, y2=to_perpendicular1[1] + self.position.y,
width=1, batch=Batch)
self.rigth_line = shapes.Line(
x=self.position.x, y=self.position.y,
x2=to_perpendicular2[0] + self.position.x, y2=to_perpendicular2[1] + self.position.y,
width=1, batch=Batch)
self.help_line1= shapes.Line(
x=self.rigth_line.x2, y=self.rigth_line.y2,
x2=self.towards_line.x2, y2=self.towards_line.y2,
width=1, batch=Batch)
self.help_line2= shapes.Line(
x=self.left_line.x2, y=self.left_line.y2,
x2=self.towards_line.x2, y2=self.towards_line.y2,
width=1, batch=Batch)
self.help_line3= shapes.Line(
x=self.tail_line.x2, y=self.tail_line.y2,
x2=self.towards_line.x2, y2=self.towards_line.y2,
width=1, batch=Batch)
self.help_line4= shapes.Line(
x=self.left_line.x2, y=self.left_line.y2,
x2=self.towards_line.x2, y2=self.towards_line.y2,
width=1, batch=Batch)
def set_position(self, line, x, y, x2, y2):
line.x = x
line.y = y
line.x2 = x2
line.y2 = y2
def update(self):
self.center.x = self.position.x + self.velocity.x
self.center.y = self.position.y + self.velocity.y
opp = self.velocity.y
adj = self.velocity.x
# print(f"opp : {opp} | adj: {adj}")
if adj == 0: adj += 1
tan = opp / adj
self.angle = math.atan(tan)
# print(self.angle)
self.previus_velocity = self.velocity
self.pointer = vector(
self.position.x + (self.size * math.cos(self.angle)),
self.position.y + (self.size * math.sin(self.angle))
)
direction = vector(self.towards_line.x2 - self.position.x, self.towards_line.y2 - self.position.y)
to_perpendicular1 = [-direction.y * self.size / 30, direction.x * self.size / 30]
to_perpendicular2 = [direction.y * self.size / 30, -direction.x * self.size / 30]
self.set_position(self.towards_line, self.position.x, self.position.y,
self.pointer.x, self.pointer.y)
self.set_position(self.tail_line, self.position.x, self.position.y,
self.position.x - direction.x / 3, self.position.y - direction.y / 3)
self.set_position(self.left_line, self.position.x, self.position.x,
self.position.x + to_perpendicular1[0], self.position.y + to_perpendicular1[1])
self.set_position(self.rigth_line, self.position.x, self.position.y,
self.position.x + to_perpendicular2[0], self.position.y + to_perpendicular2[1])
self.set_position(self.help_line1, self.left_line.x2, self.left_line.y2,
self.towards_line.x2, self.towards_line.y2)
self.set_position(self.help_line2, self.rigth_line.x2, self.rigth_line.y2,
self.towards_line.x2, self.towards_line.y2)
self.set_position(self.help_line3, self.tail_line.x2, self.tail_line.y2,
self.left_line.x2, self.left_line.y2)
self.set_position(self.help_line4, self.tail_line.x2, self.tail_line.y2,
self.rigth_line.x2, self.rigth_line.y2)
pike1 = pike(100, 100, 13)
@window.event
def on_mouse_press(x, y, button, modifiers):
pike1.velocity.x = x
pike1.velocity.y = y
@window.event
def on_draw():
window.clear()
Batch.draw()
def update(dt):
# pike1.angle += dt
# 😁
# pike1.angle = 90
print(pike1.angle)
pike1.update()
# towards_line.rotation += dt * 10
pyglet.clock.schedule_interval(update, 1/60)
pyglet.app.run()