Skip to content

Commit b1ee714

Browse files
committed
collision handling methods moved, partially fixed
1 parent 7196048 commit b1ee714

File tree

4 files changed

+217
-109
lines changed

4 files changed

+217
-109
lines changed

car.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import pygame as pg
22
from vector2d import Vector2D
3+
from map import Map
34
from math import sin, cos, radians
45

56
WHITE = (255, 255, 255)
67

78

89
class Car(pg.sprite.Sprite):
9-
def __init__(self, id, position, speed, direction, rotation, engine):
10+
def __init__(self, id, position, speed, direction, rotation, engine, curr_map: Map):
1011
pg.sprite.Sprite.__init__(self)
1112
self.__image = pg.image.load("./data/supra_new.png").convert_alpha()
1213
self.image = self.__image.copy()
1314
# self.image.fill((0, 0, 0))
1415
self.image.set_colorkey(WHITE) # this will make the img ignore all the white pixels
1516
self.rect = self.image.get_rect()
16-
self.rect.center = (position.x, position.y)
17+
1718
self.name = "Car"
1819

1920
self.id = id
@@ -22,8 +23,24 @@ def __init__(self, id, position, speed, direction, rotation, engine):
2223
self.direction = direction
2324
self.rotation = rotation
2425
self.engine = engine
26+
self.map = curr_map
2527

2628
def update(self, dt):
29+
collision_test_result = self.map.handle_collision_with_walls(self, 0)
30+
31+
if not collision_test_result[0]:
32+
self.position.add(self.speed * cos(radians(self.direction)), self.speed * sin(radians(self.direction)))
33+
else:
34+
self.speed = -self.speed * 0.3
35+
self.position = collision_test_result[1]
36+
37+
self.rect = self.image.get_rect()
38+
self.rect.x, self.rect.y = self.position.x, self.position.y
39+
# self.rect.centerx = self.position.x
40+
# self.rect.centery = self.position.y
41+
42+
def move(self, dt):
43+
2744
pressed = pg.key.get_pressed()
2845
if pressed[pg.K_UP]:
2946
self.accelerate(dt)
@@ -34,35 +51,43 @@ def update(self, dt):
3451
if pressed[pg.K_RIGHT]:
3552
self.rotate_right(dt)
3653

37-
def move(self, dt):
3854
if self.speed > 0:
39-
print(self.speed, self.speed * (0.1 + self.speed * 0.01))
55+
#print(self.speed, self.speed * (0.1 + self.speed * 0.01))
4056
self.speed -= self.speed * (0.1 + self.speed * 0.01) # v drogi i v*v powietrza
4157
elif self.speed < 0:
4258
self.speed += self.speed * (0.03 + self.speed * 0.05) # v drogi i v*v powietrza
43-
self.position.add(self.speed * cos(radians(self.direction)), self.speed * sin(radians(self.direction)))
44-
self.rect.x = self.position.x
45-
self.rect.y = self.position.y
4659

47-
# if self.rect.left > 400: #for now hardcoded
48-
# self.rect.right = 0
4960

5061
# def collision(self, wall):
5162
# self.direction += 180 - abs(self.direction - wall.get_facing)
5263

5364
def rotate_left(self, dt):
5465
if self.speed != 0:
55-
self.direction -= 3 * self.speed * self.rotation / (dt ** 2)
66+
self.direction = (self.direction - 4 * self.speed * self.rotation / (dt ** 2)) % 360
67+
5668
if self.direction < 0:
5769
self.direction += 360
70+
5871
self.image = pg.transform.rotate(self.__image, 360 - self.direction)
72+
self.rect = self.image.get_rect()
73+
self.rect.x, self.rect.y = self.position.x, self.position.y
74+
# self.rect.centerx = self.position.x
75+
# self.rect.centery = self.position.y
76+
# self.rect.x = self.position.x
77+
# self.rect.y = self.position.y
5978

6079
def rotate_right(self, dt):
6180
if self.speed != 0:
62-
self.direction += 3 * self.speed * self.rotation / (dt ** 2)
81+
self.direction = (self.direction + 4 * self.speed * self.rotation / (dt ** 2)) % 360
6382
if self.direction > 360:
6483
self.direction -= 360
84+
6585
self.image = pg.transform.rotate(self.__image, 360 - self.direction)
86+
self.rect = self.image.get_rect()
87+
self.rect.x, self.rect.y = self.position.x, self.position.y
88+
# self.rect.centery = self.position.y
89+
# self.rect.x = self.position.x
90+
# self.rect.y = self.position.y
6691

6792
def accelerate(self, dt):
6893
self.speed += self.engine / dt

engine.py

Lines changed: 28 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,7 @@
99
from stopwatch import Stopwatch
1010
from booster import Booster
1111
from boosterType import BoosterType
12-
13-
14-
def new_collision_place(x,y, wall: Wall, car : Car):
15-
options = []
16-
17-
distance_right_wall = abs(x - wall.rect.right)
18-
options.append((distance_right_wall, "distance_right_wall"))
19-
20-
distance_left_wall = abs(x - wall.rect.left)
21-
options.append((distance_left_wall, "distance_left_wall"))
22-
23-
distance_bottom_wall = abs(y - wall.rect.bottom)
24-
options.append((distance_bottom_wall, "distance_bottom_wall"))
25-
26-
distance_top_wall = abs(y - wall.rect.top)
27-
options.append((distance_top_wall, "distance_top_wall"))
28-
29-
best = (float('inf'), None)
30-
for dist in options:
31-
if best[0] > dist[0]:
32-
best = dist
33-
34-
#print(best)
35-
36-
if best[1] == "distance_right_wall": # OK
37-
#car.position.set_x(wall.rect.right)
38-
return Vector2D(wall.rect.right, y)
39-
40-
elif best[1] == "distance_left_wall":
41-
#car.position.set_x(wall.rect.left)
42-
return Vector2D(wall.rect.left - car.rect.w, y)
43-
44-
elif best[1] == "distance_bottom_wall": # OK
45-
#car.position.set_y(wall.rect.bottom)
46-
return Vector2D(x, wall.rect.bottom)
47-
48-
else:
49-
#car.position.set_y(wall.rect.top)
50-
return Vector2D(x, wall.rect.top - car.rect.h)
12+
from math import sin, cos, radians
5113

5214

5315
class Engine:
@@ -60,8 +22,8 @@ def __init__(self, refresh_rate):
6022
self.clock = pg.time.Clock()
6123

6224
#move to map
63-
self.all_walls = pg.sprite.Group()
64-
self.all_surfaces = pg.sprite.Group()
25+
# self.all_walls = pg.sprite.Group()
26+
# self.all_surfaces = pg.sprite.Group()
6527

6628
#it would be nice if booster stayed here
6729
self.all_boosters = pg.sprite.Group()
@@ -79,67 +41,55 @@ def start_timer(self):
7941
return stopwatch
8042

8143
def run(self):
82-
wall1 = Wall(Vector2D(250, 300), 60, 60, False)
83-
self.all_walls.add(wall1) # beginning of sprites
84-
wall2 = Wall(Vector2D(20, 150), 60, 20, True)
85-
self.all_walls.add(wall2) # beginning of sprites
86-
87-
surface1 = Surface(Vector2D(150, 20), 100, 50, SurfaceType.ASPHALT)
88-
self.all_surfaces.add(surface1)
89-
surface2 = Surface(Vector2D(400, 200), 100, 80, SurfaceType.ICE)
90-
self.all_surfaces.add(surface2)
91-
9244
#temporarily for boosters
93-
booster1 = Booster(Vector2D(700, 700), 30, "dt",BoosterType.SPEED, self.clock.tick(self.refresh))
94-
self.all_boosters.add(booster1)
95-
45+
# booster1 = Booster(Vector2D(700, 700), 30, "dt",BoosterType.SPEED, self.clock.tick(self.refresh))
46+
# self.all_boosters.add(booster1)
9647

9748
traction = 0.15
9849

99-
car = Car(0, Vector2D(10, 10), 0, 0, 10, 200)
10050
# car_img = pg.image.load("./data/car.png") #done temporarily inside the car class
51+
10152
x, y = self.screen.get_size()
102-
curr_map = Map(0, x, y, car, None, None)
53+
54+
curr_map = Map(0, x, y)
10355
map_img = pg.image.load("./data/grass.png")
56+
curr_map.place_objects()
57+
58+
car = Car(0, Vector2D(10, 10), 0, 0, 10, 50, curr_map)
59+
10460
run = True
10561
stopwatch = self.start_timer()
10662

10763
while run:
10864
dt = self.clock.tick(self.refresh)
10965
self.screen.blit(map_img, (0, 0))
11066

111-
self.all_surfaces.draw(self.screen)
112-
self.all_surfaces.update()
67+
curr_map.all_surfaces.draw(self.screen)
68+
curr_map.all_surfaces.update()
11369

11470
#self.screen.blit(car.image, (car.position.x, car.position.y))
11571
#self.all_cars.draw(self.screen)
11672
#added
11773

118-
self.all_walls.draw(self.screen)
119-
self.all_walls.update()
74+
curr_map.all_walls.draw(self.screen)
75+
curr_map.all_walls.update()
12076

121-
car.update(dt)
12277
car.move(dt) # maybe car also should be coded as a sprite???
78+
car.update(dt)
12379

12480
self.screen.blit(car.image, (car.position.x, car.position.y))
12581

126-
collisions = pg.sprite.spritecollide(car, self.all_walls, False)
127-
if collisions: # it's a list of objects/sprites that collided with the car
128-
for col in collisions:
129-
car.position = new_collision_place(car.position.x, car.position.y, col, car)
130-
car.speed = -car.speed * traction # well the setter is needed
131-
132-
slides = pg.sprite.spritecollide(car, self.all_surfaces, False)
133-
if slides:
134-
#print("surface here")
135-
for slide in slides:
136-
traction = slide.adjust_fraction()
137-
138-
#collisions with boosters
139-
pick_ups = pg.sprite.spritecollide(car, self.all_boosters, False) #maybe in this case it can be set to true
140-
if pick_ups:
141-
for boost in pick_ups:
142-
pass #activate booster!
82+
test1 = Surface(Vector2D(car.rect.x, car.rect.y), 10, 10, SurfaceType.ICE)
83+
self.screen.blit(test1.image, test1.rect.center)
84+
85+
test2 = Surface(Vector2D(car.rect.centerx, car.rect.centery), 10, 10, SurfaceType.GRASS)
86+
self.screen.blit(test2.image, (car.rect.centerx, car.rect.centery))
87+
88+
# test3 = Surface(Vector2D(car.rect.right, (car.rect.top + car.rect.bottom)/2), 10, 10, SurfaceType.SAND)
89+
# self.screen.blit(test3.image, (car.rect.right, (car.rect.top + car.rect.bottom)/2))
90+
91+
pg.draw.rect(self.screen, (0,0,0), car, 3)
92+
14393

14494

14595
#print(traction) no we have to include different surfaces in the movement of the car

0 commit comments

Comments
 (0)