-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
225 lines (187 loc) · 7 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import pygame
from pygame import display
import sys
from Box2D.b2 import world
from objimporter import ObjImporter
from Box2D import b2Vec2
from textsystem import Palette, TextSystem
from mapgrid import MapGrid
from contactlistener import ContactListener
from car import TDCar
import math
PPM = 1
TILE_SIZE = 6
TDC_LEFT = int('0001', 2)
TDC_RIGHT = int('0010', 2)
TDC_UP = int('0100', 2)
TDC_DOWN = int('1000', 2)
FUD_CAR_TIRE = 0
FUD_GROUND_AREA = 1
global breaking
breaking = False
def Step(car, m_controlState):
global breaking
direction = car.GetDirection()
if ((controlState & TDC_UP and direction == -1) or
(controlState & TDC_DOWN and direction == 1)):
breaking = True
car.update(m_controlState, breaking)
pygame.init()
clock = pygame.time.Clock()
world = world(gravity=(0, 0), doSleep=True)
textSystem = TextSystem()
car = TDCar(world)
world.contactListener = ContactListener(textSystem, car)
objects = []
pickups = []
otherCars = []
data = ObjImporter.readFile("objdefines.json")
for o in data:
if o.name == "building":
body = o.CreateBody(world, TILE_SIZE)
objects.append(body)
elif o.name == "pickup":
body = o.CreateBody(world, TILE_SIZE)
pickups.append(body)
elif o.name == "car":
pos = b2Vec2(o.pos.x * TILE_SIZE, -o.pos.y * TILE_SIZE)
dim = b2Vec2(o.dim.w * TILE_SIZE, o.dim.h * TILE_SIZE)
otherCar = TDCar(world, pos)
angle = 0
if (o.dim.h == 1):
angle = math.pi
else:
angle = -o.dim.w * math.pi / 2.0
otherCar.m_body.__SetTransform(pos, angle)
otherCars.append(otherCar)
prev = pickups[len(pickups) - 1].userData
for body in pickups:
body.userData.dest = prev
prev = body.userData
controlState = 0
size = (160, 144)
scale = 2
scaledSize = (size[0] * scale, size[1] * scale)
window = display.set_mode(scaledSize)
gbScreen = pygame.Surface(size)
display.set_caption('CabLife')
display.init()
mapGrid = MapGrid()
mapGrid.createGrid(data)
grid = mapGrid.grid
sprites = mapGrid.getUsedSpritesDict()
for key in sprites.keys():
sprite_dir = "./sprite/"
sprite_dir += key
sprites[key] = pygame.image.load(sprite_dir).convert()
#car.m_body.position = b2Vec2(25 * TILE_SIZE, -25 * TILE_SIZE)
while True:
diff = (1000.0 / 59.7)
gbScreen.fill(Palette.DARK)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
pygame.quit()
sys.exit()
elif event.key == pygame.K_a:
controlState |= TDC_LEFT
elif event.key == pygame.K_d:
controlState |= TDC_RIGHT
elif event.key == pygame.K_w:
controlState |= TDC_UP
elif event.key == pygame.K_s:
controlState |= TDC_DOWN
elif event.type == pygame.KEYUP:
if event.key == pygame.K_a:
controlState &= ~TDC_LEFT
elif event.key == pygame.K_d:
controlState &= ~TDC_RIGHT
elif event.key == pygame.K_w:
controlState &= ~TDC_UP
breaking = False
elif event.key == pygame.K_s:
controlState &= ~TDC_DOWN
breaking = False
Step(car, controlState)
world.Step(diff, 10, 10)
position = car.m_body.worldCenter
offset = [-position.x * PPM, position.y * PPM]
offset[0] += size[0] / 2
offset[1] += size[1] / 2 - 13
topLeft = [0, 0]
topLeft[0] = int(position.x / TILE_SIZE - 15)
topLeft[1] = int(-position.y / TILE_SIZE - 15)
for x in xrange(topLeft[0], topLeft[0] + 30):
for y in xrange(topLeft[1], topLeft[1] + 27):
if (y < 0 or x < 0 or len(grid) <= y or len(grid[y]) <= x):
continue
if isinstance(grid[y][x], str):
continue
sprite = pygame.sprite.Sprite()
sprite.image = sprites[grid[y][x].getSpriteName()]
drawPos = [x * TILE_SIZE * PPM, y * TILE_SIZE * PPM,
TILE_SIZE * PPM, TILE_SIZE * PPM]
drawPos[0] += offset[0]
drawPos[1] += offset[1]
sprite.rect = sprite.image.get_rect()
sprite.rect.topleft = [drawPos[0], drawPos[1]]
gbScreen.blit(sprite.image, sprite.rect)
#sprite = pygame.sprite.Sprite()
#sprite.image = sprites["car.png"]
#sprite.rect = sprite.image.get_rect()
#sprite.rect.topleft = [position[0] + offset[0] - 12, -position[1] + offset[1] - 15]
#gbScreen.blit(sprite.image, sprite.rect)
for drawCar in ([car] + otherCars):
for body in (drawCar.GetAllBodies()):
# The body gives us the position and angle of its shapes
for fixture in body.fixtures:
# The fixture holds information like density and friction,
# and also the shape.
shape = fixture.shape
# Naively assume that this is a polygon shape. (not good normally!)
# We take the body's transform and multiply it with each
# vertex, and then convert from meters to pixels with the scale
# factor.
vertices = [(body.transform * v) * PPM for v in shape.vertices]
## But wait! It's upside-down! Pygame and Box2D orient their
# axes in different ways. Box2D is just like how you learned
# in high school, with positive x and y directions going
# right and up. Pygame, on the other hand, increases in the
# right and downward directions. This means we must flip
# the y components.
vertices = [
(v[0] + offset[0],
- v[1] + offset[1]) for v in vertices]
pygame.draw.polygon(gbScreen, Palette.NORM, vertices)
if (textSystem.state == "driving"):
vec = textSystem.passenger.dest.m_body.worldCenter - car.m_body.worldCenter
vec.Normalize()
vec.x *= 15
vec.y *= -15
pos = (int(vec.x) + size[0] / 2, int(vec.y) + 118 / 2)
pygame.draw.circle(gbScreen, Palette.BRIGHT, pos, 2)
for body in pickups:
if (
(textSystem.state == "find" and
textSystem.ignore != body.userData and
body.userData.done is False) or
(textSystem.state == "driving" and
textSystem.passenger.dest == body.userData)):
pos = body.worldCenter.copy()
pos.x *= PPM
pos.y *= -PPM
pos.x += offset[0]
pos.y += offset[1]
pygame.draw.circle(
gbScreen,
Palette.BRIGHT,
(int(pos.x), int(pos.y)),
int(body.fixtures[0].shape.radius * PPM))
textSystem.update(diff)
textSystem.drawText(gbScreen)
pygame.transform.scale(gbScreen, scaledSize, window)
display.flip()
clock.tick(59.7)