mirror of
https://github.com/13hannes11/outyard-hackathon-rush-hour-co2.git
synced 2024-09-03 20:10:59 +02:00
add different diretion suport for car
Co-authored-by: Laura Januleviciute <januleviciute.laura@gmail.com>
This commit is contained in:
32
Car.py
32
Car.py
@@ -3,32 +3,48 @@ import random
|
|||||||
from Bubble import Bubble
|
from Bubble import Bubble
|
||||||
|
|
||||||
class Car:
|
class Car:
|
||||||
def __init__(self, posX, posY, height, width, game_objects, color=(0, 255,0), directionX=1):
|
def __init__(self, posX, posY, game_objects, color=(0, 255,0), directionX=1):
|
||||||
self.posX = posX
|
self.posX = posX
|
||||||
self.posY = posY
|
self.posY = posY
|
||||||
self.color = color
|
self.color = color
|
||||||
self.directionX = directionX
|
self.directionX = directionX
|
||||||
self.width = width
|
self.width = 173
|
||||||
self.height = height
|
self.height = 66
|
||||||
self.game_objects = game_objects
|
self.game_objects = game_objects
|
||||||
self.bubble_spawn_time_ms = 1000
|
self.bubble_spawn_time_ms = 1000
|
||||||
self.time_of_last_spawn = 0
|
self.time_of_last_spawn = 0
|
||||||
self.BUBBLE_IMAGE = pygame.image.load("sprites/bubble.png")
|
self.BUBBLE_IMAGE = pygame.image.load("sprites/bubble.png")
|
||||||
|
self.image = pygame.image.load("sprites/car.png")
|
||||||
|
|
||||||
|
if directionX < 0:
|
||||||
|
self.image = pygame.transform.flip(self.image, True, False)
|
||||||
|
|
||||||
def draw(self, screen):
|
def draw(self, screen):
|
||||||
pygame.draw.rect(screen, self.color, pygame.Rect(self.posX, self.posY, self.width, self.height))
|
screen.blit(self.image, (self.posX, self.posY))
|
||||||
|
|
||||||
|
|
||||||
def update(self, screen):
|
def update(self, screen):
|
||||||
self.posX += self.directionX
|
self.posX += self.directionX
|
||||||
screen_width, screen_height = screen.get_size()
|
screen_width, screen_height = screen.get_size()
|
||||||
if self.posX > screen_width:
|
if self.directionX > 0:
|
||||||
self.posX = -self.width
|
if self.posX > screen_width:
|
||||||
|
self.posX = -self.width
|
||||||
|
else:
|
||||||
|
if self.posX + self.width < 0:
|
||||||
|
self.posX = screen_width + self.width
|
||||||
|
|
||||||
|
|
||||||
if self.bubble_spawn_time_ms + self.time_of_last_spawn < pygame.time.get_ticks():
|
if self.bubble_spawn_time_ms + self.time_of_last_spawn < pygame.time.get_ticks():
|
||||||
bubble_y_direction = random.uniform(-1, 1)
|
bubble_y_direction = random.uniform(-1, 1)
|
||||||
bubble_x_speeddiff = random.uniform(0, 0.9)
|
|
||||||
bubble_radius = 16
|
bubble_radius = 16
|
||||||
self.game_objects.append(Bubble(self.posX, self.posY + self.height - bubble_radius,
|
if self.directionX > 0:
|
||||||
|
bubble_x_speeddiff = random.uniform(0, 0.9)
|
||||||
|
bubbleX = self.posX
|
||||||
|
else:
|
||||||
|
bubble_x_speeddiff = random.uniform(-0.9, 0)
|
||||||
|
bubbleX = self.posX + self.width
|
||||||
|
|
||||||
|
self.game_objects.append(Bubble(bubbleX, self.posY + self.height - bubble_radius,
|
||||||
self.BUBBLE_IMAGE,
|
self.BUBBLE_IMAGE,
|
||||||
radius=bubble_radius,
|
radius=bubble_radius,
|
||||||
direction=(-self.directionX * bubble_x_speeddiff,bubble_y_direction)))
|
direction=(-self.directionX * bubble_x_speeddiff,bubble_y_direction)))
|
||||||
|
|||||||
7
main.py
7
main.py
@@ -16,6 +16,7 @@ FONT = pygame.font.SysFont('Comic Sans MS', 128)
|
|||||||
TEXT_SCREEN = FONT.render('Game over!', False, (255, 255, 255))
|
TEXT_SCREEN = FONT.render('Game over!', False, (255, 255, 255))
|
||||||
TEXT_RECT = TEXT_SCREEN.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
|
TEXT_RECT = TEXT_SCREEN.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
|
||||||
|
|
||||||
|
|
||||||
BUBBLE_LIMIT = 100
|
BUBBLE_LIMIT = 100
|
||||||
|
|
||||||
SCREEN = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
|
SCREEN = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
|
||||||
@@ -25,8 +26,10 @@ CLOCK = pygame.time.Clock()
|
|||||||
|
|
||||||
game_objects = []
|
game_objects = []
|
||||||
bubble_objects = []
|
bubble_objects = []
|
||||||
for i in range(10):
|
for i in range(4):
|
||||||
game_objects.append(Car(random.uniform(0, 200), i * 50, 30, 50, bubble_objects, directionX=random.uniform(1, 5)))
|
game_objects.append(Car(random.uniform(0, SCREEN_WIDTH), i * 80 + 10, bubble_objects, directionX=random.uniform(1, 5)))
|
||||||
|
for i in range(4, 7):
|
||||||
|
game_objects.append(Car(random.uniform(0, SCREEN_WIDTH), i * 80 + 10, bubble_objects, directionX= - random.uniform(1, 5)))
|
||||||
|
|
||||||
game_objects.append(Vacuum(bubble_objects))
|
game_objects.append(Vacuum(bubble_objects))
|
||||||
|
|
||||||
|
|||||||
BIN
sprites/car.png
Normal file
BIN
sprites/car.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
Reference in New Issue
Block a user