0

I have a global variable, 'is_game_on' set to 'False' to start with. I have a turtle which responds to .ondrag() function. My program works perfectly fine if I change the 'is_game_on' variable to 'True' (The main program runs in a while loop when 'is_game_on' is 'True').

In the same Screen I have created turtle (a text- 'Click to start') in the top right of the screen, which I want to return 'is_game_on' = 'True' when the mouse is clicked on it so that the rest of my program starts working there after. However, my screen gets closed when I click the mouse. I think this is because of the command screen.exitonclick() at the end. Appreciate any suggestions how to avoid this problem.

Below is my code. I want to start with 'is_game_on == False' and with the output a static display. Then when I click the mouse on 'Click to Start', a mechanism to trigger 'is_game_on" as True and then the ball starts bouncing up and down.

from turtle import Screen, Turtle
import time


# is_game_on = False
is_game_on = True

def click(i, j):
    global is_game_on
    if i >= 250 and j >= 300:
        is_game_on = True
        print(is_game_on)
    return is_game_on

class Ball(Turtle):
    def __init__(self):
        super().__init__()
        self.shape('circle')
        self.color('black')
        self.shapesize(stretch_wid=2, stretch_len=2)
        self.penup()
        self.speed(6)
        self.goto(0, -355)
        self.x_move = 0
        self.y_move = 1
        self.move_speed = 10

    def move(self):
        xcor_new = self.xcor() + self.x_move
        ycor_new = self.ycor() + self.y_move
        self.goto(xcor_new, ycor_new)

    def bounce_y(self):
        self.y_move *= -1

class Paddle(Turtle):
    def __init__(self):
        super().__init__()
        self.shape('square')
        self.penup()
        self.goto(0, -380)
        self.color('blue')
        self.shapesize(stretch_wid=.5, stretch_len=10)

    def move(self,i, j):
        self.goto(i,  -380)

class Start(Turtle):
    def __init__(self):
        super().__init__()
        self.penup()
        self.goto(250, 300)
        self.color('blue')
        self.shapesize(stretch_wid=4, stretch_len=10)
        self.hideturtle()
        self.write('Click to Start', font=('Arial', 35, 'bold'))

screen = Screen()
screen.colormode(255)
screen.bgcolor('white')
screen.setup(1200, 800)

screen.tracer(0)

paddle = Paddle()
ball = Ball()

screen.listen()
paddle.ondrag(paddle.move)
screen.onclick(click)

start = Start()


while is_game_on:
    time.sleep(0)
    screen.update()
    ball.move()
    if ball.ycor() >= 375:
        ball.bounce_y()
    if (abs(ball.xcor() - paddle.xcor()) < 120) and ball.ycor() == -355:
        ball.bounce_y()

screen.update()

screen.exitonclick()
Srinivas
  • 568
  • 1
  • 4
  • 21

1 Answers1

0

After lot of trial and errors and lot of web searches, I found the solution.

The screen closing problem can be simply avoided by importing turtle and adding turtle.done() command just before screen.exitonclick() command. The complete code will be.

from turtle import Screen, Turtle
import time
import turtle


is_game_on = False
# is_game_on = True

def click(i, j):
    global is_game_on
    if i >= 250 and j >= 300:
        is_game_on = True
        print(is_game_on)
    return is_game_on

class Ball(Turtle):
    def __init__(self):
        super().__init__()
        self.shape('circle')
        self.color('black')
        self.shapesize(stretch_wid=2, stretch_len=2)
        self.penup()
        self.speed(6)
        self.goto(0, -355)
        self.x_move = 0
        self.y_move = 1
        self.move_speed = 10

    def move(self):
        xcor_new = self.xcor() + self.x_move
        ycor_new = self.ycor() + self.y_move
        self.goto(xcor_new, ycor_new)

    def bounce_y(self):
        self.y_move *= -1

class Paddle(Turtle):
    def __init__(self):
        super().__init__()
        self.shape('square')
        self.penup()
        self.goto(0, -380)
        self.color('blue')
        self.shapesize(stretch_wid=.5, stretch_len=10)

    def move(self,i, j):
        self.goto(i,  -380)

class Start(Turtle):
    def __init__(self):
        super().__init__()
        self.penup()
        self.goto(250, 300)
        self.color('blue')
        self.shapesize(stretch_wid=4, stretch_len=10)
        self.hideturtle()
        self.write('Click to Start', font=('Arial', 35, 'bold'))

screen = Screen()
screen.colormode(255)
screen.bgcolor('white')
screen.setup(1200, 800)

screen.tracer(0)

paddle = Paddle()
ball = Ball()

screen.listen()
paddle.ondrag(paddle.move)
screen.onclick(click)

start = Start()


while is_game_on:
    time.sleep(0)
    screen.update()
    ball.move()
    if ball.ycor() >= 375:
        ball.bounce_y()
    if (abs(ball.xcor() - paddle.xcor()) < 120) and ball.ycor() == -355:
        ball.bounce_y()

screen.update()
turtle.done()
screen.exitonclick()

The animation can work by moving the while loop into the click() function. The code will be as follows.

from turtle import Screen, Turtle
import time
import turtle


is_game_on = False
# is_game_on = True

def click(i, j):
    global is_game_on
    if i >= 250 and j >= 300:
        is_game_on = True
        print(is_game_on)
        while is_game_on:
            time.sleep(0)
            screen.update()
            ball.move()
            if ball.ycor() >= 375:
                ball.bounce_y()
            if (abs(ball.xcor() - paddle.xcor()) < 120) and ball.ycor() == -355:
                ball.bounce_y()
    return is_game_on

class Ball(Turtle):
    def __init__(self):
        super().__init__()
        self.shape('circle')
        self.color('black')
        self.shapesize(stretch_wid=2, stretch_len=2)
        self.penup()
        self.speed(6)
        self.goto(0, -355)
        self.x_move = 0
        self.y_move = 1
        self.move_speed = 10

    def move(self):
        xcor_new = self.xcor() + self.x_move
        ycor_new = self.ycor() + self.y_move
        self.goto(xcor_new, ycor_new)

    def bounce_y(self):
        self.y_move *= -1

class Paddle(Turtle):
    def __init__(self):
        super().__init__()
        self.shape('square')
        self.penup()
        self.goto(0, -380)
        self.color('blue')
        self.shapesize(stretch_wid=.5, stretch_len=10)

    def move(self,i, j):
        self.goto(i,  -380)

class Start(Turtle):
    def __init__(self):
        super().__init__()
        self.penup()
        self.goto(250, 300)
        self.color('blue')
        self.shapesize(stretch_wid=4, stretch_len=10)
        self.hideturtle()
        self.write('Click to Start', font=('Arial', 35, 'bold'))

screen = Screen()
screen.colormode(255)
screen.bgcolor('white')
screen.setup(1200, 800)

screen.tracer(0)

paddle = Paddle()
ball = Ball()

screen.listen()
paddle.ondrag(paddle.move)
screen.onclick(click)

start = Start()


screen.update()
turtle.done()
screen.exitonclick()
Srinivas
  • 568
  • 1
  • 4
  • 21