-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar.py
100 lines (85 loc) · 2.86 KB
/
car.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
import pygame, sys, math
from pygame.locals import *
"""
Car class with following methods:
-draw
-getPos
-setSpeed
-setRotate
-move
-updateRight
-updateLeft
-updateUp
-updateDown
Initialise with starting coords
"""
class Car(pygame.sprite.Sprite):
#initialise car instance
def __init__(self, start_x, start_y):
pygame.sprite.Sprite.__init__(self)
self.x = start_x
self.y = start_y
self.right = 0
self.left = 0
self.up = 0
self.down = 0
self.speed = 0
self.maxSpeed = 20
self.maxReverse = -3
self.direction = 0
self.rad = 0
self.rotated = None
self.image = pygame.image.load("images/car.png")
self.rect = self.image.get_rect()
#Uses direction to rotate the image and send it to be drawn next update
#also update car mask to new rotation
def draw(self, screen, camera):
self.rotated = pygame.transform.rotate(self.image, self.direction)
self.rect = self.rotated.get_rect()
self.rect.center = self.getPos()
self.mask = pygame.mask.from_surface(self.rotated)
screen.blit(self.rotated, camera.apply(self))
def drawNav(self, screen, position, direction):
self.rotated = pygame.transform.rotate(self.image, direction)
car_rect = self.rotated.get_rect()
car_rect.center = position
screen.blit(self.rotated, car_rect)
def scale(self, scale_factor):
for i in range(0, scale_factor, 1):
self.image = pygame.transform.scale2x(self.image)
self.mask = pygame.mask.from_surface(self.image)
self.rect = self.image.get_rect()
#Return a tuple of coords
def getPos(self):
return (self.x, self.y)
#call all driving methods
def drive(self, bg):
self.setSpeed()
self.setRotate()
self.move(bg)
#Work out the speed, accelerate to cap
def setSpeed(self):
self.speed += (self.up+self.down)
if self.speed > self.maxSpeed: self.speed = self.maxSpeed
if self.speed < self.maxReverse: self.speed = self.maxReverse
#Work out the angle in radians
def setRotate(self):
self.direction += (self.right+self.left)
self.rad = self.direction * (math.pi/180)
#Take the angle and speed and move the car
def move(self, gameMap):
if not pygame.sprite.collide_mask(self, gameMap):
self.x += -self.speed * math.sin(self.rad)
self.y += -self.speed * math.cos(self.rad)
else:
self.x -= -self.speed * math.sin(self.rad)
self.y -= -self.speed * math.cos(self.rad)
self.speed = -0.1
def updateRight(self, r):
self.right = r
def updateLeft(self, l):
self.left = l
def updateUp(self, u):
self.up = u
def updateDown(self, d):
self.down = d