I'm trying to develop a simple game while I'm learning python. Game is not complex, random cars (which are squares) are spawning at right of the screen and they are going to left. We are a turtle, trying to avoid them and make it to the top of the screen.
The problem is my code below doesn't spawn two objects at the same time, it spawns just one.
from turtle import Screen
from turt import Turt
from spawnpoint import SpawnPoint
from cars import Car
import random
screen = Screen()
screen.setup(1000, 700)
screen.title("Cars and Turtle")
screen.bgcolor("gray")
turt = Turt()
is_game_on = True
screen.listen()
screen.onkey(turt.move_up, "Up")
spawn_points_ycords = [300, 200, 100, 0, -100, -200]
spawn_1 = SpawnPoint(spawn_points_ycords[0])
spawn_2 = SpawnPoint(spawn_points_ycords[1])
spawn_3 = SpawnPoint(spawn_points_ycords[2])
spawn_4 = SpawnPoint(spawn_points_ycords[3])
spawn_5 = SpawnPoint(spawn_points_ycords[4])
spawn_6 = SpawnPoint(spawn_points_ycords[5])
spawn_points = [spawn_1, spawn_2, spawn_3, spawn_4, spawn_5, spawn_6]
while is_game_on:
for n in range(60):
if n == 59:
random_spawn = spawn_points[random.randint(0, len(spawn_points)-1)]
random_spawn_2 = spawn_points[random.randint(0, len(spawn_points)-1)]
while random_spawn_2 == random_spawn:
random_spawn_2 = spawn_points[random.randint(0, len(spawn_points) - 1)]
random_spawn_car = Car(random_spawn.spawn.ycor())
random_spawn_2_car = Car(random_spawn_2.spawn.ycor())
screen.exitonclick()
My spawn points class code:
from turtle import Turtle
class SpawnPoint:
def __init__(self, ycor):
self.spawn = Turtle()
self.spawn.hideturtle()
self.spawn.speed(0)
self.spawn.penup()
self.spawn.goto(600, ycor)
self.spawn.showturtle()
self.new_car = None
and my car class code:
from turtle import Turtle
import random
class Car:
def __init__(self, ycor):
self.body = Turtle()
self.body.hideturtle()
self.body.penup()
self.body.shape("square")
self.colors = ["black", "red", "orange", "blue", "green", "yellow"]
self.body.color(self.colors[random.randint(0, len(self.colors)-1)])
self.body.shapesize(1, 5, 0)
self.body.speed(2)
self.body.goto(700, ycor)
self.body.showturtle()
self.body.goto(-700, ycor)
I can't figure it out to solve this bug. I'm using Turtle module.