-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
106 lines (82 loc) · 3.08 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
#!/usr/bin/env python
import pygame
from pygame.locals import (QUIT, KEYDOWN, K_ESCAPE, K_RETURN)
import Box2D # The main library
# Box2D.b2 maps Box2D.b2Vec2 to vec2 (and so on)
from Box2D.b2 import (world, polygonShape, circleShape, staticBody, dynamicBody)
import run_simulation
import track_generator
import vehicle
# --- constants ---
# Box2D deals with meters, but we want to display pixels,
# so define a conversion factor:
PPM = 10.0 # pixels per meter
TARGET_FPS = 60 #60
TIME_STEP = 1.0 / TARGET_FPS
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
def start_game(track=None, car=None):
pygame.display.init()
global screen
# --- pygame setup ---
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32)
pygame.display.set_caption('Simple pygame example')
global clock
clock = pygame.time.Clock()
if not car:
car = vehicle.Car()
if not track:
track = track_generator.Track(200) #200 m length
global sim
sim = run_simulation.Simulation(track, car)
colors = {
staticBody: (255, 255, 255, 255),
dynamicBody: (127, 127, 127, 255),
}
def my_draw_polygon(polygon, body, fixture, shift=(0,0)):
vertices = [((body.transform * v) + shift) * PPM for v in polygon.vertices]
vertices = [(v[0], SCREEN_HEIGHT - v[1]) for v in vertices]
pygame.draw.polygon(screen, colors[body.type], vertices)
polygonShape.draw = my_draw_polygon
def my_draw_circle(circle, body, fixture, shift=(0,0)):
position = (body.transform * circle.pos + shift) * PPM
out_pos = ((body.transform * (circle.pos + (circle.radius, 0)) + shift) * PPM)
position = (position[0], SCREEN_HEIGHT - position[1])
out_pos = (out_pos[0], SCREEN_HEIGHT - out_pos[1])
pygame.draw.circle(screen, colors[body.type], [int(
x) for x in position], int(circle.radius * PPM))
pygame.draw.line(screen, (255,0,0,0), position, out_pos)
circleShape.draw = my_draw_circle
# --- main game loop ---
def drawing_func(world, shift=(0,0)):
#world = sim.sim_world
screen.fill((0, 0, 0, 0))
# Draw the world
for body in world.bodies:
for fixture in body.fixtures:
fixture.shape.draw(body, fixture, shift)
# Update the screen
pygame.display.flip()
def run(speed=1.):
drawing_func(sim.sim_world)
start = False
while not start:
event = pygame.event.wait()
if event.type == KEYDOWN and event.key == K_RETURN:
start = True
elif event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
pygame.display.quit()
return
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
# The user closed the window or pressed escape
running = False
dist, is_over, ii= sim.run(1, speed=speed)
print dist, is_over
tracker = sim.tracker.worldCenter
drawing_func(sim.sim_world, -tracker+(20,20))
clock.tick(TARGET_FPS)
if is_over:
running = False
pygame.display.quit()