-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.py
119 lines (91 loc) · 3.02 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from framework import *
import math
PlaneShape = [
( 2.0, -4.0),
( 0.0, 4.0),
( -2.4, -4.0),
]
class Ship(object):
vertices = [( 1.5, 0.0),
( 3.0, 5.0),
( 2.8, 11.0),
( 1.0,20.0),
(-1.0,20.0),
(-2.8, 11.0),
(-3.0, 5.0),
(-1.5, 0.0),
]
LINEAR_SPEED = 50
ANGULAR_SPEED = 0.1
ANGULAR_MAX_IMPULSE = 1.5
def __init__(self, world, vertices=None, density=0.1, position=(0, 0)):
self.linear_speed_sqr = 0
if vertices is None: vertices = Ship.vertices
self.body = world.CreateDynamicBody(position=position)
self.body.CreatePolygonFixture(vertices=vertices, density=density)
self.body.angularDamping = 1.1
self.body.linearDamping = 1.1
def update_linear(self, throttle):
direction = self.body.GetWorldVector((0, 1))
self.body.ApplyForceToCenter(self.LINEAR_SPEED * throttle * direction, True)
self.linear_speed_sqr = self.body.linearVelocity.lengthSquared
def update_angular(self, turn):
angular_impulse = self.ANGULAR_SPEED * self.linear_speed_sqr
if angular_impulse > self.ANGULAR_MAX_IMPULSE: angular_impulse = self.ANGULAR_MAX_IMPULSE
self.body.ApplyAngularImpulse( angular_impulse * turn, True )
def update(self, keys):
throttle = 0
if 'up' in keys: throttle += 1
if 'down' in keys: throttle -= 1
self.update_linear(throttle)
turn = 0
if 'left' in keys: turn += 1
if 'right' in keys: turn -= 1
self.update_angular(turn)
class ShipGame (Framework):
name="Ship Game"
description="Keys: accel = w, reverse = s, left = a, right = d"
def __init__(self):
super(ShipGame, self).__init__()
# Top-down -- no gravity in the screen plane
self.world.gravity = (0, 0)
self.key_map = {Keys.K_w: 'up', Keys.K_s: 'down', Keys.K_a: 'left', Keys.K_d: 'right', }
# Keep track of the pressed keys
self.pressed_keys = set()
# The walls
boundary = self.world.CreateStaticBody(position=(0, 20))
boundary.CreateEdgeChain([(-120,-120),
(-120, 120),
( 120, 120),
( 120,-120),
(-120,-120)]
)
# A couple regions of differing traction
self.car = Ship(self.world)
gnd1 = self.world.CreateStaticBody()
fixture = gnd1.CreatePolygonFixture(box=(9, 7, (-20, 15), math.radians(20)))
gnd2 = self.world.CreateStaticBody()
fixture = gnd2.CreatePolygonFixture(box=(4, 8, (5, 40), math.radians(-40)))
# Kill me
plane_test = self.world.CreateStaticBody(position=(10,0))
fixture = plane_test.CreatePolygonFixture(vertices=PlaneShape)
def Keyboard(self, key):
key_map = self.key_map
if key in key_map:
self.pressed_keys.add(key_map[key])
else:
super(ShipGame, self).Keyboard(key)
def KeyboardUp(self, key):
key_map = self.key_map
if key in key_map:
self.pressed_keys.remove(key_map[key])
else:
super(ShipGame, self).KeyboardUp(key)
def Step(self, settings):
self.car.update(self.pressed_keys)
super(ShipGame, self).Step(settings)
self.Print('Linear speed sqr: %s' % self.car.linear_speed_sqr)
if __name__=="__main__":
main(ShipGame)