Skip to content

Commit 5838b10

Browse files
committed
boosters now spawn, and decrease timer activates and works
1 parent 88e66fa commit 5838b10

File tree

8 files changed

+94
-39
lines changed

8 files changed

+94
-39
lines changed

CSVParser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def draw_map(self, map: map):
4444
elif row[0] == "SURFACE":
4545
if row[5] == "ASPHALT":
4646
map.all_surfaces.add(Surface(position, width, height, SurfaceType.ASPHALT, rotation))
47+
map.places_for_boosters.append([position.x + 40, position.y + 40, position.x + width - 40, position.y + height - 40])
4748

4849
elif row[5] == "SNOW":
4950
map.all_surfaces.add(Surface(position, width, height, SurfaceType.SNOW, rotation))

booster.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77

88

99
class Booster (pg.sprite.Sprite):
10-
def __init__(self, position: Vector2D, change: int, image_name, type: BoosterType, dt): #change -> wartośc zmiany, jesli dotyczy zmian boolowskich to ==1
10+
def __init__(self, position: Vector2D, change: int, type: BoosterType, dt): #change -> wartośc zmiany, jesli dotyczy zmian boolowskich to ==1
1111
pg.sprite.Sprite.__init__(self)
12-
self.image = pg.image.load("./data/quick_racing_booster_"+image_name+".png").convert() # make booster images
12+
self.image = pg.image.load("./data/quick_racing_booster_"+type.value[1]+".png").convert_alpha() # make booster images
13+
self.image.set_colorkey((0,0,0))
1314
self.rect = self.image.get_rect()
1415
self.rect.x = position.x
1516
self.rect.y = position.y
@@ -24,6 +25,7 @@ def __init__(self, position: Vector2D, change: int, image_name, type: BoosterTyp
2425
self.mask = pg.mask.from_surface(self.image)
2526

2627
def activate(self, car: Car, stopwatch: Stopwatch):
28+
print("Booster is activating")
2729
if self.type == BoosterType.SPEED:
2830
car.speed += self.int_booster_value
2931
#sleep(3)
@@ -36,18 +38,19 @@ def activate(self, car: Car, stopwatch: Stopwatch):
3638
pass
3739

3840
elif self.type == BoosterType.DECREASE_TIMER: #the time of the lap is decreased
39-
stopwatch.decrease_timer(1000)
41+
print("deacreasing time")
42+
stopwatch.decrease_timer(4000)
4043

4144
elif self.type == BoosterType.FREEZE:
4245
car.speed = 0
4346

4447
else: #BoosterType.NO_TURNING -> self descriptive
4548
pass
4649

47-
# we gotta find a way to make it last only a short amount of time -> like 3 seconds or so
50+
# we gotta find a way to make it last only a short amount of time -> like 3 seconds or so
4851

49-
#we can use dt
50-
#we can also add "push" or something - it'd be like booster but with a single push (increase in speed in facing direction)
51-
# -maybe maybe
52+
#we can use dt
53+
#we can also add "push" or something - it'd be like booster but with a single push (increase in speed in facing direction)
54+
# -maybe maybe
5255

5356

boosterType.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
class BoosterType(Enum):
55
#both positive and negative
6-
SPEED = 1
7-
TURNING = 2
6+
SPEED = (1, (255, 255, 255))
7+
TURNING = (2, (255, 255, 255))
88

99
#only positive
10-
NO_COLLISIONS = 3
11-
DECREASE_TIMER = 4
10+
NO_COLLISIONS = (3, (255, 255, 255))
11+
DECREASE_TIMER = (4, "dt_resized")
1212

1313
#only negative
14-
NO_TURNING = 5
15-
FREEZE = 6
14+
NO_TURNING = (5, (255, 255, 255))
15+
FREEZE = (6, (255, 255, 255))

car.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def update(self, dt):
3939

4040
self.speed = -self.speed * 0.15
4141

42+
new_traction = self.map.handle_collision_with_sufraces(self)
43+
self.map.handle_collision_with_boosters(self)
44+
4245
self.rect = self.image.get_rect()
4346
self.rect.x, self.rect.y = self.position.x, self.position.y
4447

4.87 KB
Loading

engine.py

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
from __future__ import annotations
2+
3+
import asyncio
4+
import random
5+
26
import pygame as pg
7+
8+
import boosterType
39
from vector2d import Vector2D
410
from car import Car
511
from map import Map
6-
from wall import Wall
7-
from surface import Surface
8-
from surfaceType import SurfaceType
912
from stopwatch import Stopwatch
1013
from booster import Booster
1114
from boosterType import BoosterType
@@ -21,12 +24,6 @@ def __init__(self, refresh_rate):
2124
pg.display.set_caption("QUICK RACING")
2225
self.clock = pg.time.Clock()
2326

24-
#move to map
25-
# self.all_walls = pg.sprite.Group()
26-
# self.all_surfaces = pg.sprite.Group()
27-
28-
#it would be nice if booster stayed here
29-
self.all_boosters = pg.sprite.Group()
3027

3128
# btw that's how we can load the map -> read all walls size and location from CSV then create
3229
# them and add them all to sprite group,
@@ -35,6 +32,51 @@ def __init__(self, refresh_rate):
3532

3633
#thought exactly the same thing :) -> can be done today during labs
3734

35+
def spawn_booster(self, map: Map, dt):
36+
37+
if random.randrange(0, 256) != 8:
38+
return
39+
40+
place = random.randrange(0, len(map.places_for_boosters)-1)
41+
x_coordinate = random.randrange(map.places_for_boosters[place][0], map.places_for_boosters[place][2])
42+
y_coordinate = random.randrange(map.places_for_boosters[place][1], map.places_for_boosters[place][3])
43+
44+
45+
what_booster = random.randrange(0,5)
46+
new_booster_type = None
47+
change = None
48+
49+
#temp
50+
what_booster = 3
51+
52+
if what_booster == 0:
53+
new_booster_type = BoosterType.SPEED
54+
change = random.randrange(-100, 100)
55+
56+
elif what_booster == 1:
57+
new_booster_type = BoosterType.TURNING
58+
change = random.randrange(-3 , 3)
59+
60+
elif what_booster == 2:
61+
new_booster_type = BoosterType.NO_COLLISIONS
62+
change = 1
63+
64+
elif what_booster == 3:
65+
new_booster_type = BoosterType.DECREASE_TIMER
66+
change = 1
67+
68+
elif what_booster == 4:
69+
new_booster_type = BoosterType.NO_TURNING
70+
change = 1
71+
72+
elif what_booster == 5:
73+
new_booster_type = BoosterType.FREEZE
74+
change = 1
75+
76+
map.all_boosters.add(Booster(Vector2D(x_coordinate, y_coordinate), change, new_booster_type, dt))
77+
#print("Booster spawned!\n")
78+
79+
3880
def start_timer(self):
3981
stopwatch = Stopwatch(self.screen, self.clock, Vector2D(1300, 40))
4082
stopwatch.restart_timer(pg.time.get_ticks())
@@ -51,15 +93,15 @@ def run(self):
5193

5294
x, y = self.screen.get_size()
5395

54-
curr_map = Map(0, x, y)
96+
run = True
97+
stopwatch = self.start_timer()
98+
99+
curr_map = Map(0, x, y, stopwatch)
55100
map_img = pg.image.load("./data/grass.png")
56101
curr_map.place_objects()
57102

58103
car = Car(0, Vector2D(50, 100), 0, 0, 10, 50, curr_map)
59104

60-
run = True
61-
stopwatch = self.start_timer()
62-
63105
while run:
64106
dt = self.clock.tick(self.refresh)
65107
self.screen.blit(map_img, (0, 0))
@@ -74,6 +116,11 @@ def run(self):
74116
curr_map.all_walls.draw(self.screen)
75117
curr_map.all_walls.update()
76118

119+
self.spawn_booster(curr_map, dt)
120+
121+
curr_map.all_boosters.draw(self.screen)
122+
curr_map.all_boosters.update()
123+
77124
car.move(dt) # maybe car also should be coded as a sprite???
78125
car.update(dt)
79126

map.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@
22
import pygame as pg
33

44
from CSVParser import CSVParser
5-
from vector2d import Vector2D
6-
from wall import Wall
7-
from surface import Surface
8-
from surfaceType import SurfaceType
95

10-
from math import sin, cos, radians, sqrt
6+
from math import sin, cos, radians
117

128

139
class Map:
14-
def __init__(self, id, width, height):
10+
def __init__(self, id, width, height, stopwatch):
1511
self.id = id
1612
self.width = width
1713
self.height = height
1814
self.all_walls = pg.sprite.Group()
1915
self.all_surfaces = pg.sprite.Group()
20-
self.all_booster = pg.sprite.Group()
16+
self.all_boosters = pg.sprite.Group()
17+
self.places_for_boosters = []
2118
self.name = "Map"
19+
self.stopwatch = stopwatch
2220

2321
def place_objects(self):
2422
parser = CSVParser("./data/map1.csv", "../data/Leaderboard.csv")
2523

2624
parser.draw_map(self)
2725

26+
#print(self.places_for_boosters)
27+
2828
# surface1 = Surface(Vector2D(100, 100), 1500, 110, SurfaceType.ASPHALT)
2929
# self.all_surfaces.add(surface1)
3030

@@ -129,24 +129,24 @@ def place_objects(self):
129129
def handle_collision_with_walls(self, car):
130130
collisions = pg.sprite.spritecollide(car, self.all_walls, False, pg.sprite.collide_mask)
131131
if collisions: # it's a list of objects/sprites that collided with the car
132-
print("Collision with wall")
133132
for col in collisions:
134133
return True
135134

136135
return False
137136

138-
def handle_collision_with_sufraces(self, car, traction: float):
137+
def handle_collision_with_sufraces(self, car):
139138
slides = pg.sprite.spritecollide(car, self.all_surfaces, False, pg.sprite.collide_mask)
140139
if slides:
141140
for slide in slides:
142141
if slide.rect.x == car.rect.x + car.speed * cos(
143142
radians(car.direction)) and slide.rect.y == car.rect.y + car.speed * sin(
144143
radians(car.direction)):
145-
traction = slide.adjust_fraction() #do sth about it later on!
144+
return slide.adjust_fraction() #do sth about it later on!
146145

147146
def handle_collision_with_boosters(self, car):
148147
# collisions with boosters
149-
pick_ups = pg.sprite.spritecollide(car, self.all_boosters, False, pg.sprite.collide_mask) # maybe in this case it can be set to true
148+
pick_ups = pg.sprite.spritecollide(car, self.all_boosters, True, pg.sprite.collide_mask) # maybe in this case it can be set to true
150149
if pick_ups:
150+
print("Booster picked up!")
151151
for boost in pick_ups:
152-
pass # activate booster!
152+
boost.activate(car, self.stopwatch)

stopwatch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ def __init__(self, screen, clock, position: Vector2D):
1515
self.difference = 0
1616

1717
def display_timer(self, ticks):
18+
print("ticks: ", ticks, ", diff: ", self.difference, "t-d: ", ticks - self.difference)
1819
millis = (ticks - self.difference) % 1000
1920
seconds = int((ticks - self.difference) / 1000 % 60)
2021
minutes = int((ticks - self.difference) / 60000 % 24)
@@ -30,4 +31,4 @@ def stop_timer(self):
3031
pass
3132

3233
def decrease_timer(self, value: int):
33-
self.difference = value
34+
self.difference += value

0 commit comments

Comments
 (0)