-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (57 loc) · 1.64 KB
/
main.py
File metadata and controls
76 lines (57 loc) · 1.64 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
import sys
import pygame
from car import Car
class Game:
"""
Game class
Arguments:
size {tuple} -- window size
"""
def __init__(self, size):
self.width = size[0]
self.height = size[1]
self.win = pygame.display.set_mode(size)
self.display = pygame.Surface((5000, 5000))
pygame.display.set_caption("Racing Game")
self.background = (255, 255, 255)
self.p = Car((self.width / 2, self.height / 2))
def input(self, keys):
rot_speed = 4
acc = 2
if keys[pygame.K_LEFT]:
self.p.hdg -= rot_speed
if keys[pygame.K_RIGHT]:
self.p.hdg += rot_speed
if keys[pygame.K_UP]:
self.p.accelerating = True
self.p.speed += acc
elif keys[pygame.K_DOWN]:
self.p.accelerating = True
self.p.speed -= acc
else:
self.p.accelerating = False
def logic(self, delta):
self.p.apply_friction(0.96)
self.p.update(delta)
def render(self, window, disp):
self.display.fill(self.background)
window.fill(self.background)
self.p.render(disp)
window.blit(disp, (0, 0))
pygame.display.update()
def main():
pygame.init()
g = Game((1200, 800))
clock = pygame.time.Clock()
fps = 60
while True:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
g.input(pygame.key.get_pressed())
g.logic(clock.get_time() / 1000)
g.render(g.win, g.display)
if __name__ == "__main__":
main()
sys.exit()