add different diretion suport for car

Co-authored-by: Laura Januleviciute <januleviciute.laura@gmail.com>
This commit is contained in:
2021-11-05 21:24:47 +01:00
parent aa96587f5e
commit 1df50a33a5
3 changed files with 29 additions and 10 deletions

28
Car.py
View File

@@ -3,32 +3,48 @@ import random
from Bubble import Bubble
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.posY = posY
self.color = color
self.directionX = directionX
self.width = width
self.height = height
self.width = 173
self.height = 66
self.game_objects = game_objects
self.bubble_spawn_time_ms = 1000
self.time_of_last_spawn = 0
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):
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):
self.posX += self.directionX
screen_width, screen_height = screen.get_size()
if self.directionX > 0:
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():
bubble_y_direction = random.uniform(-1, 1)
bubble_x_speeddiff = random.uniform(0, 0.9)
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,
radius=bubble_radius,
direction=(-self.directionX * bubble_x_speeddiff,bubble_y_direction)))

View File

@@ -16,6 +16,7 @@ FONT = pygame.font.SysFont('Comic Sans MS', 128)
TEXT_SCREEN = FONT.render('Game over!', False, (255, 255, 255))
TEXT_RECT = TEXT_SCREEN.get_rect(center=(SCREEN_WIDTH/2, SCREEN_HEIGHT/2))
BUBBLE_LIMIT = 100
SCREEN = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
@@ -25,8 +26,10 @@ CLOCK = pygame.time.Clock()
game_objects = []
bubble_objects = []
for i in range(10):
game_objects.append(Car(random.uniform(0, 200), i * 50, 30, 50, bubble_objects, directionX=random.uniform(1, 5)))
for i in range(4):
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))

BIN
sprites/car.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB