/
player.py
117 lines (90 loc) · 2.98 KB
/
player.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
from euclid import *
from camera import *
from bullet import *
class Player:
def __init__(self):
self.camera = Camera()
self.bullets = []
def update(self, dt):
for b in self.bullets:
b.update(dt)
if b.position.z > 10 or b.position.z < -10 or \
b.position.x > 10 or b.position.x < -10:
self.bullets.remove(b)
#if collide(b, target):
# self.bullets.remove(b)
#self.draw_cross()
self.camera.update()
def paint(self):
for b in self.bullets:
b.paint()
def draw_cross(self):
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
gluOrtho2D(0, 800, 0, 600)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
#glPushAttrib(GL_LIST_BIT)
#glRasterPos2i(0, 0)
glDisable(GL_LIGHTING)
glColor4f(1, 0, 0, 1)
glBegin(GL_TRIANGLES)
glVertex3f(-1, 0, 1)
glVertex3f(1, 0, 1)
glVertex3f(0, 1, 1)
glEnd()
glEnable(GL_LIGHTING)
#glPopAttrib()
glPopMatrix()
glMatrixMode(GL_PROJECTION)
glPopMatrix()
def fire(self):
position = Vector3(self.camera.position.x, self.camera.position.y, self.camera.position.z)
direction = Vector3(self.camera.focus.x, self.camera.focus.y, self.camera.focus.z)
bullet = Bullet(position, direction)
self.bullets.append(bullet)
def walk_forward(self, dt):
speed = 5
cam = self.camera
v = cam.focus - cam.position
v[1] = 0
theta = math.atan2(v[2], v[0])
cam.focus[0] += dt * speed * math.cos(theta)
cam.focus[2] += dt * speed * math.sin(theta)
cam.position[0] += dt * speed * math.cos(theta)
cam.position[2] += dt * speed * math.sin(theta)
def walk_backward(self, dt):
speed = 5
cam = self.camera
v = cam.focus - cam.position
v[1] = 0
theta = math.atan2(v[2], v[0])
cam.focus[0] -= dt * speed * math.cos(theta)
cam.focus[2] -= dt * speed * math.sin(theta)
cam.position[0] -= dt * speed * math.cos(theta)
cam.position[2] -= dt * speed * math.sin(theta)
def walk_left(self, dt):
speed = 5
cam = self.camera
v = cam.focus - cam.position
v[1] = 0
q = Quaternion.new_rotate_axis(-math.pi/2, Vector3(0, 1, 0))
v = q * v
theta = math.atan2(v[2], v[0])
cam.focus[0] -= dt * speed * math.cos(theta)
cam.focus[2] -= dt * speed * math.sin(theta)
cam.position[0] -= dt * speed * math.cos(theta)
cam.position[2] -= dt * speed * math.sin(theta)
def walk_right(self, dt):
speed = 5
cam = self.camera
v = cam.focus - cam.position
v[1] = 0
q = Quaternion.new_rotate_axis(math.pi/2, Vector3(0, 1, 0))
v = q * v
theta = math.atan2(v[2], v[0])
cam.focus[0] -= dt * speed * math.cos(theta)
cam.focus[2] -= dt * speed * math.sin(theta)
cam.position[0] -= dt * speed * math.cos(theta)
cam.position[2] -= dt * speed * math.sin(theta)