Skip to content

Commit 3cf0cb7

Browse files
committed
added fuel
1 parent 53494f3 commit 3cf0cb7

File tree

10 files changed

+30
-8
lines changed

10 files changed

+30
-8
lines changed

Car Racing 2d/Assets/coins/1.png

-530 Bytes
Loading

Car Racing 2d/Assets/coins/2.png

-552 Bytes
Loading

Car Racing 2d/Assets/coins/3.png

-384 Bytes
Loading

Car Racing 2d/Assets/coins/4.png

-250 Bytes
Loading

Car Racing 2d/Assets/coins/5.png

-373 Bytes
Loading

Car Racing 2d/Assets/coins/6.png

-562 Bytes
Loading

Car Racing 2d/Assets/fuel.png

2.65 KB
Loading
692 Bytes
Binary file not shown.

Car Racing 2d/main.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pygame
22
import random
33
from objects import Road, Player, Nitro, Tree, Button, \
4-
Obstacle, Coins
4+
Obstacle, Coins, Fuel
55

66
pygame.init()
77
SCREEN = WIDTH, HEIGHT = 288, 512
@@ -79,6 +79,7 @@ def center(image):
7979

8080
tree_group = pygame.sprite.Group()
8181
coin_group = pygame.sprite.Group()
82+
fuel_group = pygame.sprite.Group()
8283
obstacle_group = pygame.sprite.Group()
8384

8485
# VARIABLES *******************************************************************
@@ -179,19 +180,21 @@ def center(image):
179180
tree_group.add(tree)
180181

181182
if counter % 270 == 0:
182-
count = random.randint(1, 3)
183+
type = random.choices([1, 2], weights=[6, 4], k=1)[0]
183184
x = random.choice(lane_pos)+10
184-
for i in range(count):
185-
coin = Coins(x,-100 - (25 * i))
186-
coin_group.add(coin)
185+
if type == 1:
186+
count = random.randint(1, 3)
187+
for i in range(count):
188+
coin = Coins(x,-100 - (25 * i))
189+
coin_group.add(coin)
190+
elif type == 2:
191+
fuel = Fuel(x, -100)
192+
fuel_group.add(fuel)
187193
elif counter % 90 == 0:
188194
obs = random.choices([1, 2, 3], weights=[6,2,2], k=1)[0]
189195
obstacle = Obstacle(obs)
190196
obstacle_group.add(obstacle)
191197

192-
if counter >= 10000:
193-
counter = 0
194-
195198
if nitro_on and nitro.gas > 0:
196199
x, y = p.rect.centerx - 8, p.rect.bottom - 10
197200
win.blit(nitro_frames[nitro_counter], (x, y))
@@ -214,6 +217,8 @@ def center(image):
214217
tree_group.draw(win)
215218
coin_group.update(speed)
216219
coin_group.draw(win)
220+
fuel_group.update(speed)
221+
fuel_group.draw(win)
217222

218223
p.update(move_left, move_right)
219224
p.draw(win)

Car Racing 2d/objects.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,23 @@ def update(self, speed):
142142
def draw(self, win):
143143
win.blit(self.image, self.rect)
144144

145+
class Fuel(pygame.sprite.Sprite):
146+
def __init__(self, x, y):
147+
super(Fuel, self).__init__()
148+
149+
self.image = pygame.image.load('Assets/fuel.png')
150+
self.rect = self.image.get_rect()
151+
self.rect.x = x
152+
self.rect.y = y
153+
154+
def update(self, speed):
155+
self.rect.y += speed
156+
if self.rect.top >= HEIGHT:
157+
self.kill()
158+
159+
def draw(self, win):
160+
win.blit(self.image, self.rect)
161+
145162
class Coins(pygame.sprite.Sprite):
146163
def __init__(self, x, y):
147164
super(Coins, self).__init__()

0 commit comments

Comments
 (0)