Kenny Okoye
Kenny Okoye

Reputation: 27

what could i be doing wrong? am using turtle on my pycharm and my turtle.write method is not displaying on the screen

below are my scoreboard.py and main.py codes but after i click run, i can't see my score printed on the screen.

from turtle import Turtle
ALIGNMENT = "center"
FONT = ("Courier", 14, "normal")


class Scoreboard(Turtle):

    def __init__(self):
        super().__init__()
        self.score = 0
        self.color("white")
        self.penup()
        self.goto(0, 270)
        self.hideturtle()
        self.update_scoreboard()

    def update_scoreboard(self):
        self.write(f"Score: {self.score}", align=ALIGNMENT, font=FONT)

    def game_over(self):
        self.goto(0, 0)
        self.write("GAME OVER", align=ALIGNMENT, font=FONT)

    def increase_score(self):
        self.score += 1
        self.clear()

then my main.py

from turtle import Screen
from snake import Snake
from food import Food
import time
from scoreboard import Scoreboard

screen = Screen()
screen.setup(width=600, height=500)
screen.bgcolor("black")
screen.title("my snake game")
screen.tracer(0)

snake = Snake()
food = Food()
scoreboard = Scoreboard()

screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right, "Right")
game_is_on = True
while game_is_on:
    screen.update()
    time.sleep(0.1)
    snake.move()

so, based on the two files: scoreboard and main.py respectively, what am i ommiting that prevents my score from being shown on my screen?

Upvotes: 1

Views: 66

Answers (1)

furas
furas

Reputation: 142919

Window has height 500 (Y in range -250...250) and you write in position (0, 270) - so it writes outside window and you can't see it.

Use for example (0, 200) to write inside window.


Full working example - all in one file so everyone can simply copy and run it.

from turtle import Turtle
from turtle import Screen
import time

ALIGNMENT = "center"
FONT = ("Courier", 14, "normal")

class Scoreboard(Turtle):

    def __init__(self):
        super().__init__()
        self.score = 0
        self.color("white")
        self.penup()
        self.goto(0, 200)   # <-- HERE
        self.hideturtle()
        self.update_scoreboard()

    def update_scoreboard(self):
        self.write(f"Score: {self.score}", align=ALIGNMENT, font=FONT)

    def game_over(self):
        self.goto(0, 0)
        self.write("GAME OVER", align=ALIGNMENT, font=FONT)

    def increase_score(self):
        self.score += 1
        self.clear()
        

screen = Screen()
screen.setup(width=600, height=500)
screen.bgcolor("black")
screen.title("my snake game")
screen.tracer(0)

scoreboard = Scoreboard()

screen.listen()
game_is_on = True
while game_is_on:
    screen.update()
    time.sleep(0.1)

Upvotes: 1

Related Questions