Skip to content

Commit b4aceba

Browse files
Added car
1 parent fa962a3 commit b4aceba

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

.DS_Store

6 KB
Binary file not shown.

car.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
from math import cos, sin, radians
3+
import pygame
4+
5+
6+
class Car:
7+
"""
8+
Class for making a car.
9+
10+
Arguments:
11+
pos {tuple} -- starting position of cars
12+
"""
13+
14+
def __init__(self, pos):
15+
self.img = pygame.image.load(os.path.join("data", "racecar.png"))
16+
17+
self.x = pos[0]
18+
self.y = pos[1]
19+
self.x_vel = 0
20+
self.y_vel = 0
21+
22+
ratio_mult = 0.7
23+
self.size = (int(135 * ratio_mult), int(64 * ratio_mult))
24+
self.speed = 0 # It's really the acceleration of the car :p
25+
self.hdg = 0
26+
self.rect = self.img.get_rect()
27+
28+
def update(self, delta):
29+
self.x_vel += cos(radians(self.hdg)) * self.speed
30+
self.y_vel += sin(radians(self.hdg)) * self.speed
31+
32+
self.x += self.x_vel * delta
33+
self.y += self.y_vel * delta
34+
35+
def render(self, window):
36+
transformed = pygame.transform.rotate(
37+
pygame.transform.scale(self.img, self.size),
38+
-self.hdg
39+
)
40+
size = transformed.get_size()
41+
window.blit(transformed, (self.x - size[0] / 2, self.y - size[1] / 2))

data/racecar.png

58.1 KB
Loading

scripts/main.py renamed to main.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,45 @@
1-
import pygame
21
import sys
3-
pygame.init()
2+
from random import randint
3+
import pygame
4+
from car import Car
45

56

67
class Game:
8+
"""
9+
Game class
10+
11+
Arguments:
12+
size {tuple} -- window size
13+
"""
14+
715
def __init__(self, size):
816
self.width = size[0]
917
self.height = size[1]
1018
self.win = pygame.display.set_mode(size)
1119
pygame.display.set_caption("Racing Game")
12-
self.background = (0, 0, 0)
20+
self.background = (255, 255, 255)
21+
22+
self.p = Car((self.width / 2, self.height / 2))
23+
self.p.speed = 2
24+
self.p.hdg = randint(0, 360)
1325

1426
def input(self, keys):
1527
pass
1628

1729
def logic(self, delta):
18-
print(delta)
30+
self.p.update(delta)
1931

2032
def render(self, window):
2133
window.fill(self.background)
2234

23-
pygame.draw.rect(window, (255, 255, 255), (100, 100, 50, 50))
35+
self.p.render(window)
2436

2537
pygame.display.update()
2638

2739

2840
def main():
29-
g = Game((800, 600))
41+
pygame.init()
42+
g = Game((1024, 720))
3043
clock = pygame.time.Clock()
3144
fps = 60
3245

0 commit comments

Comments
 (0)