-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·408 lines (372 loc) · 15.7 KB
/
main.py
File metadata and controls
executable file
·408 lines (372 loc) · 15.7 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#!/usr/bin/env python3
import pygame
import sys
import json
import random
from math import sin, cos, tan, pi
def radians(hex_angle):return display_angle(hex_angle) / 128 * pi
def display_angle(hex_angle):return hex_angle*1.40625
def hex_angle(degrees):return degrees * 0.71111
def full_size(radius):return radius * 2 - 1
def flip_angle(angle, hflip, vflip):
if angle == -1:
if vflip:return 128
else:return -1
if hflip:
angle = (256 - angle) % 256
if vflip:
angle = (128 - angle) % 256
return angle
# animations
roll = 0
stand = 1
# modes
air = 0
floor = 1
left = 2
top = 3
right = 4
def swap(list, i1, i2):
circle = list[i1]
list[i1] = list[i2]
list[i2] = circle
def reverse(list):
for i in range(len(list) // 2):
swap(list, i, len(list) - 1 - i)
class Sonic:
def __init__(self):
self.animation = stand
self.mode = floor
self.acc = 0xC # acceleration
self.dec = 0x80 # deceleration
self.frc = 0xC # friction
self.top = 0x600 # top speed
self.slp = 0x20
self.slprollup = 0x14
self.slprolldown = 0x50
self.fall = 0x280 # tolerance ground speed for sticking to walls and ceilings
self.air = 0x18 # air acceleration
self.jmp = 0x680 # jump force
# self.jmp = 0x600 # for knuckles
self.grv = 0x38 # gravity
self.xpos = 0x1300 # 0x13 pixels
self.ypos = 0x1A00
self.body_width = 0x9 # for ground collision. Collision uses full pixels
self.body_height = 0x13
self.push_width = 0xA # for pushing things
self.push_height = 0xA
self.xsp = 0 # x speed
self.ysp = 0 # y speed
self.gsp = 0 # ground speed
self.slope = self.slp # current slope factor
self.ang = 0 # one byte angle. 0x00 right, 0x40 up, 0x80 left, 0xC0 down.
self.left = False
self.image = pygame.image.load('graphics/character/sonic/idle/1.png')
def draw(self, screen):
offset = 8 if self.ang == -1 else 0
error = (255, 0, 255)
width, height = self.image.get_size()
x = self.xpos // 256
y = self.ypos // 256
if self.xsp < 0:self.left = True
elif self.xsp > 0:self.left = False
image = pygame.transform.flip(self.image, True, False) if self.left else self.image
#print(self.mode)
if self.mode == left:
image = pygame.transform.rotate(image, 270)
push = [(x, y - self.push_width), (x, y + self.push_width)]
bleft = [(x - self.body_height, y - self.body_width), (x + self.body_height, y - self.body_width)]
bright = [(x - self.body_height, y + self.body_width), (x + self.body_height, y + self.body_width)]
elif self.mode == top:
image = pygame.transform.rotate(image, 180)
push = [(x - self.push_width, y), (x + self.push_width, y)]
bleft = [(x + self.body_width, y - self.body_height), (x + self.body_width, y + self.body_height)]
bright = [(x - self.body_width, y - self.body_height), (x - self.body_width, y + self.body_height)]
elif self.mode == right:
image = pygame.transform.rotate(image, 90)
push = [(x, y - self.push_width), (x, y + self.push_width)]
bleft = [(x - self.body_height, y + self.body_width), (x + self.body_height, y + self.body_width)]
bright = [(x - self.body_height, y - self.body_width), (x + self.body_height, y - self.body_width)]
else:
push = [(x - self.push_width, y + offset), (x + self.push_width, y + offset)]
bleft = [(x - self.body_width, y - self.body_height), (x - self.body_width, y + self.body_height)]
bright = [(x + self.body_width, y - self.body_height), (x + self.body_width, y + self.body_height)]
screen.blit(image, (x - width // 2, y - height // 2))
pygame.draw.line(screen, error, push[0], push[1])
pygame.draw.line(screen, error, bleft[0], bleft[1])
pygame.draw.line(screen, error, bright[0], bright[1])
pygame.draw.circle(screen, (random.randrange(256), random.randrange(256), random.randrange(256)), (self.xpos//256, self.ypos//256), 2)
def sensors(self, act):
if self.ang < 32:self.mode = floor
elif self.ang < 96:self.mode = right
elif self.ang < 160:self.mode = top
elif self.ang < 224:self.mode = left
else:self.mode = floor
#print(self.ang, self.mode)
if self.mode == air:pass # TODO
elif self.mode == floor:
y = self.ypos // 256
x = self.xpos // 256
if self.ang == -1:y += 8
if self.xsp > 0:
if act.solid(x + self.push_width, y):
offset = 0
while offset < 16:
if not act.solid(x + self.push_width - offset, y):break
offset += 1
self.xpos = (self.xpos // 256 - offset) * 256
self.gsp = 0
elif self.xsp < 0:
if act.solid(x - self.push_width, y):
offset = 0
while offset < 16:
if not act.solid(x - self.push_width + offset, y):break
offset += 1
self.xpos = (self.xpos // 256 + offset) * 256
self.gsp = 0
points = []
y = self.ypos // 256 + self.body_height
for x in (self.xpos // 256 - self.body_width, self.xpos // 256 + self.body_width):
if act.solid(x, y):
offset = -1
while offset > -16:
if not act.solid(x, y+offset):break
offset -= 1
else:
offset = 1
while offset < 16:
if act.solid(x, y+offset):break
offset += 1
offset -= 1
points.append((offset, act.tiles[(y+offset+1)//16][x//16].angle))
if points[0][0] < points[1][0]:
offset = points[0][0]
self.ang = points[0][1]
else:
offset = points[1][0]
self.ang = points[1][1]
self.ypos = (self.ypos // 256 + offset) * 256
elif self.mode == left:
y = self.ypos // 256
x = self.xpos // 256
if abs(self.gsp) < self.fall:self.mode = floor
if self.ysp > 0:
if act.solid(x, y + self.push_width):
offset = 0
while offset < 16:
if not act.solid(x, y + self.push_width - offset):break
offset += 1
self.ypos = (self.ypos // 256 - offset) * 256
self.gsp = 0
self.mode = floor
elif self.ysp < 0:
if act.solid(x, y - self.push_width, ):
offset = 0
while offset < 16:
if not act.solid(x, y - self.push_width + offset):break
offset += 1
self.ypos = (self.ypos // 256 + offset) * 256
self.gsp = 0
self.mode = floor
points = []
x = self.xpos // 256 - self.body_height
for y in (self.ypos // 256 - self.body_width, self.ypos // 256 + self.body_width):
if act.solid(x, y):
offset = -1
while offset > -16:
if not act.solid(x-offset, y):break
offset -= 1
else:
offset = 1
while offset < 16:
if act.solid(x-offset ,y):break
offset += 1
offset -= 1
points.append((offset, act.tiles[y//16][(x-offset-1)//16].angle))
if points[0][0] < points[1][0]:
offset = points[0][0]
self.ang = points[0][1]
else:
offset = points[1][0]
self.ang = points[1][1]
self.xpos = (self.xpos // 256 - offset) * 256
elif self.mode == top:
y = self.ypos // 256
x = self.xpos // 256
if self.xsp > 0:
if act.solid(x + self.push_width, y):
offset = 0
while offset < 16:
if not act.solid(x + self.push_width - offset, y):break
offset += 1
self.xpos = (self.xpos // 256 - offset) * 256
self.gsp = 0
elif self.xsp < 0:
if act.solid(x - self.push_width, y):
offset = 0
while offset < 16:
if not act.solid(x - self.push_width + offset, y):break
offset += 1
self.xpos = (self.xpos // 256 + offset) * 256
self.gsp = 0
points = []
y = self.ypos // 256 - self.body_height
for x in (self.xpos // 256 - self.body_width, self.xpos // 256 + self.body_width):
if act.solid(x, y):
offset = -1
while offset > -16:
if not act.solid(x, y-offset):break
offset -= 1
else:
offset = 1
while offset < 16:
if act.solid(x, y-offset):break
offset += 1
offset -= 1
points.append((offset, act.tiles[(y-offset-1)//16][x//16].angle))
if points[0][0] < points[1][0]:
offset = points[0][0]
self.ang = points[0][1]
else:
offset = points[1][0]
self.ang = points[1][1]
self.ypos = (self.ypos // 256 - offset) * 256
elif self.mode == right:
y = self.ypos // 256
x = self.xpos // 256
if abs(self.gsp) < self.fall:self.mode = floor
if self.ysp > 0:
if act.solid(x, y + self.push_width):
offset = 0
while offset < 16:
if not act.solid(x, y + self.push_width - offset):break
offset += 1
self.ypos = (self.ypos // 256 - offset) * 256
self.gsp = 0
self.mode = floor
elif self.ysp < 0:
if act.solid(x, y - self.push_width, ):
offset = 0
while offset < 16:
if not act.solid(x, y - self.push_width + offset):break
offset += 1
self.ypos = (self.ypos // 256 + offset) * 256
self.gsp = 0
self.mode = floor
points = []
x = self.xpos // 256 + self.body_height
for y in (self.ypos // 256 - self.body_width, self.ypos // 256 + self.body_width):
if act.solid(x, y):
offset = -1
while offset > -16:
if not act.solid(x+offset, y):break
offset -= 1
else:
offset = 1
while offset < 16:
if act.solid(x+offset ,y):break
offset += 1
offset -= 1
points.append((offset, act.tiles[y//16][(x+offset+1)//16].angle))
if points[0][0] < points[1][0]:
offset = points[0][0]
self.ang = points[0][1]
else:
offset = points[1][0]
self.ang = points[1][1]
self.xpos = (self.xpos // 256 + offset) * 256
def move(self, keys):
if self.mode == air:pass
elif self.mode == floor:
left = keys[pygame.K_LEFT]
right = keys[pygame.K_RIGHT]
if left and right:
left = False
right = False
if self.ang != -1:self.gsp += sin(radians(self.ang))
if self.gsp > 0:
if right:
if self.gsp < self.top:
self.gsp += self.acc
if self.gsp > self.top:self.gsp = self.top
elif left:self.gsp -= self.dec
else:
self.gsp -= self.frc
if self.gsp < 0:self.gsp = 0
elif self.gsp < 0:
if left:
if self.gsp > -self.top:
self.gsp -= self.acc
if self.gsp < -self.top:self.gsp = -self.top
elif right:self.gsp += self.dec
else:
self.gsp += self.frc
if self.gsp > 0:self.gsp = 0
else:
if right:self.gsp = self.acc
elif left:self.gsp = -self.acc
self.xsp = int(self.gsp * cos(radians(self.ang)))
self.ysp = int(self.gsp * sin(radians(self.ang)))
self.xpos += self.xsp
self.ypos += self.ysp
class Tile:
def __init__(self, tileset, number):
self.image = pygame.transform.flip(pygame.image.load('graphics/tileset/{}/{}.png'.format(tileset, number[0])), number[1], number[2])
with open('graphics/tileset/{}/{}.json'.format(tileset, number[0])) as f:data = json.load(f)
self.map = data[:-1]
self.angle = flip_angle(data[-1], number[1], number[2])
if number[1]:
for line in self.map:reverse(line)
if number[2]:reverse(self.map)
for line in self.map:
for index in range(len(line)):
line[index] &= number[3]
class Act:
def __init__(self, json):
self.name = json['name']
self.number = json['number']
alltiles = json['tileset']
self.tiles = json['tiles']
tileset = {}
for row in range(len(self.tiles)):
for point in range(len(self.tiles[row])):
if not tuple(self.tiles[row][point]) in tileset:
tileset[tuple(self.tiles[row][point])] = Tile(alltiles, self.tiles[row][point])
self.tiles[row][point] = tileset[tuple(self.tiles[row][point])]
def draw(self, screen): # static camera
for row in range(len(self.tiles)):
for tile in range(len(self.tiles[row])):
screen.blit(self.tiles[row][tile].image, (tile*16, row*16))
def solid(self, x, y):
xtile = x // 16
xpixel = x % 16
ytile = y // 16
ypixel = y % 16
return self.tiles[ytile][xtile].map[ypixel][xpixel] & collision
collision = 1
pygame.init()
screenX = 316
screenY = 224
scale = int(sys.argv[1]) if len(sys.argv) > 1 else 1
if scale == 1:
screen = pygame.display.set_mode((screenX, screenY))
else:
screen = pygame.Surface((screenX, screenY))
window = pygame.display.set_mode((screenX * scale, screenY * scale))
clock = pygame.time.Clock()
with open('test levels/zone/1.json') as f:act = Act(json.load(f))
player = Sonic()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:exit()
keys = pygame.key.get_pressed()
screen.fill((0, 0, 0))
act.draw(screen)
player.draw(screen)
player.move(keys)
player.sensors(act)
if scale != 1:pygame.transform.scale(screen, (screenX * scale, screenY * scale), window)
pygame.display.flip()
clock.tick(60)