Last active
November 28, 2023 13:41
-
-
Save Cistress/6bfc7b3eee77eab2892e828f00dba842 to your computer and use it in GitHub Desktop.
def increase_score from Scoreboard does not work as intended as the turtle leveled up (at line 40 main script and line 22 scoreboard script)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"] | |
STARTING_MOVE_DISTANCE = 5 | |
MOVE_INCREMENT = 10 | |
game_is_on = True | |
from turtle import Turtle | |
import random | |
class CarManager(): | |
def __init__(self): | |
self.auto_list = [] | |
def auto_generation(self): | |
random_number = random.randint(1,6) | |
if random_number == 1: | |
new_car = Turtle() | |
new_car.color(random.choice(COLORS)) | |
y = random.randint(-250,250) | |
# Size and shape | |
new_car.shape("square") | |
new_car.shapesize(stretch_wid=1, stretch_len=2) | |
# car color | |
new_car.penup() | |
new_car.goto(290, y) | |
self.auto_list.append(new_car) | |
# this will only create one car that keeps going to different position in the while loop | |
def movement(self): | |
for new_car in self.auto_list: | |
new_car.backward(STARTING_MOVE_DISTANCE) | |
def speed_up(self): | |
x = STARTING_MOVE_DISTANCE | |
y = x + MOVE_INCREMENT | |
x = y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from turtle import Screen | |
from player import Player | |
from car_manager import CarManager | |
from scoreboard import Scoreboard | |
screen = Screen() | |
screen.setup(width=600, height=600) | |
screen.tracer(0) | |
screen.listen() | |
# creating objects | |
player = Player() | |
car = CarManager() | |
scoreboard = Scoreboard() #### | |
# Game on | |
game_is_on = True | |
while game_is_on: | |
time.sleep(0.1) | |
screen.update() | |
# cars | |
car.auto_generation() | |
car.movement() | |
# collision | |
for cars in car.auto_list: | |
if player.distance(cars) < 20: | |
scoreboard.game_over() | |
game_is_on = False | |
# speed up and level up | |
if player.level_up(): | |
car.speed_up() | |
# increasing score | |
scoreboard.increase_score() | |
# if player.ycor() < 280: | |
screen.onkey(player.move,"Up") | |
screen.exitonclick() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
STARTING_POSITION = (0, -280) | |
MOVE_DISTANCE = 10 | |
FINISH_LINE_Y = 280 | |
from turtle import Turtle | |
class Player(Turtle): | |
def __init__(self): | |
super().__init__() | |
# Turtle setup | |
self.penup() | |
self.setheading(90) | |
self.setpos(STARTING_POSITION) | |
# Turtle features | |
self.shape("turtle") | |
self.color("Black") | |
self.shapesize(1,1) | |
def move(self): | |
new_y = self.ycor() + MOVE_DISTANCE | |
self.goto(0, new_y) | |
def level_up(self): | |
if self.ycor() > FINISH_LINE_Y: | |
self.setpos(0, -300) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FONT = ("Courier", 12, "normal") | |
from turtle import Turtle | |
class Scoreboard(Turtle): | |
def __init__(self): | |
super().__init__() | |
self.penup() | |
self.hideturtle() | |
self.score = 1 | |
self.goto(-200, 260) | |
self.update_scoreboard() | |
def update_scoreboard(self): | |
self.clear() | |
self.write(f"Current score: {self.score}", align="center", font=FONT) | |
def game_over(self): | |
self.goto(0, 20) | |
self.write("Game Over!", align="center", font=FONT) | |
def increase_score(self): | |
self.score += 1 | |
self.update_scoreboard() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I do not know what exactly was wrong with my code since I checked the solution side by side, yet the current score just would not update when the condition was met at line 37 main script