-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuel_can.py
54 lines (42 loc) · 1.6 KB
/
fuel_can.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
import pygame
class FuelCan(pygame.sprite.Sprite):
"""
Fuel Can Class
This class represents a Fuel Can in the Turbo Racing 3000 game.
It derives from the Sprite class from pygame.
The fuel can is an object that the player can collect to replenish their car's fuel.
When the player collides with a fuel can, their car's fuel level increases, allowing them to race for a longer duration.
Attributes:
----------
speed: int
The speed at which the Fuel Can moves along the track.
Methods:
-------
moveForward(speed):
Moves the Fuel Can forward by the specified speed.
moveBackward(speed):
Moves the Fuel Can backward by the specified speed.
changeSpeed(speed):
Changes the Fuel Can's movement speed to the specified value.
create_mask():
Returns a Pygame mask object for collision detection.
"""
def __init__(self, speed):
# Call the parent class (Sprite) constructor
super().__init__()
# Set the fuel can image
self.image = pygame.image.load(f'assets/fuel_can.png')
#Initialise attributes of the sprite.
self.width = 50
self.height = 50
self.speed = speed
# Fetch the rectangle object that has the dimensions of the image.
self.rect = self.image.get_rect()
def moveForward(self, speed):
self.rect.y += speed * 2
def moveBackward(self, speed):
self.rect.y -= speed * 2
def changeSpeed(self, speed):
self.speed = speed
def create_mask(self):
return pygame.mask.from_surface(self.image)