Python Tkinter bounce ball game restart

Question:

I ‘ve been reading from a book called “Python for kids” where it has step by step, how to create the bounce ball game.
The final code looks like this:

from Tkinter import *
import random

tk = Tk()
tk.title("Paddleball Game")
tk.resizable(0,0) # tk window cannot be resized in x or y
tk.wm_attributes("-topmost", 1)

canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0) #no borders around the canvas
canvas.pack()
tk.update()


class Ball:
    def __init__(self, canvas, paddle, color):
        self.canvas = canvas
        self.paddle = paddle
        self.id = canvas.create_oval(10, 10, 25, 25, fill = color)
        self.canvas.move(self.id, 245, 100)

        starts = [-3, -2, -1, 1, 2, 3]
        random.shuffle(starts)
        self.x = starts[0]
        self.y = -3
        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()

        self.hit_bottom = False

    def hit_paddle(self, pos):
        paddle_pos = self.canvas.coords(self.paddle.id)
        if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
            if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
                return True
        return False

    def draw(self):
        if self.hit_bottom == False:

            self.canvas.move(self.id, self.x, self.y)

            pos = self.canvas.coords(self.id)
            if pos[1] <= 0:
                self.y = 3
            if pos[3] >= self.canvas_height:
                self.y = -3

            if pos[3] >= self.canvas_height:
                self.hit_bottom = True

            if self.hit_paddle(pos) == True:
                self.y = -3

            if pos[0] <= 0:
                self.x = 3
            if pos[2] >= self.canvas_width:
                self.x = -3

            if ball.hit_bottom == False:
                self.canvas.after(10, self.draw) # miliseconds, function

class Paddle:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
        self.canvas.move(self.id, 200, 300)

        self.x = 0

        self.canvas_width = self.canvas.winfo_width()

        self.canvas.bind_all("<KeyPress-Left>", self.turn_left)
        self.canvas.bind_all("<KeyPress-Right>", self.turn_right)



    def draw(self):
        if ball.hit_bottom == False:

            self.canvas.move(self.id, self.x, 0)

            pos = self.canvas.coords(self.id)
            if pos[0] <= 0:
                self.x = 0
            if pos[2] >= self.canvas_width:
                self.x = 0

            self.canvas.after(10, self.draw)

    def turn_left(self, event):
        self.pos = self.canvas.coords(self.id)

        if self.pos[0] >= 1:
            self.x = -3

    def turn_right(self, event):
        self.pos = self.canvas.coords(self.id)

        if self.pos[2] <= self.canvas_width-1:
            self.x = 3

paddle = Paddle(canvas, "blue")
ball = Ball(canvas, paddle, "red")


def start_game(event):
    ball.draw()

canvas.bind_all("<Button-1>", start_game)
paddle.draw()

tk.mainloop()

Now after reading the book and understanding most parts, I tried to recreate the game alone and adding some extra stuff. 1) Display game over when I lose, 2) counting the score and displaying it, 3) Delaying the start (by 1 sec), 4) stopping both ball and paddle from moving after i lose.
Moreover, I made it so that when I lose, 5) if i click the window again game over text disappears, score goes to zero and paddle and ball start moving again.

However, I want the ball to start from it’s original place and not from the bottom and later, if possible, when i lose, display a button so that when I press it, the game restarts.

My code is this:

from Tkinter import *
import random
import time

root = Tk()
root.title("Testing... testing...")
root.resizable(0,0)
root.wm_attributes("-topmost", -1)

canvas = Canvas(root, width=500, height=400, bd=0,highlightthickness=0)
canvas.pack()

root.update()

count = 0
lost = False

class Ball:
    def __init__(self, canvas, paddle, color):
        self.canvas = canvas
        self.paddle = paddle
        self.id = canvas.create_oval(0, 0, 15, 15, fill=color)
        self.canvas.move(self.id, 245, 200)

        starts_x = [-3, -2, -1, 1, 2, 3]
        random.shuffle(starts_x)

        self.x = starts_x[0]
        self.y = -3

        self.canvas_height = self.canvas.winfo_height()
        self.canvas_width = self.canvas.winfo_width()


    def draw(self):
        self.canvas.move(self.id, self.x, self.y)

        pos = self.canvas.coords(self.id)

        if pos[1] <= 0:
            self.y = 3
        if pos[3] >= self.canvas_height:
            self.y = -3

        if pos[0] <= 0:
            self.x = 3
        if pos[2] >= self.canvas_width:
            self.x = -3

        self.paddle_pos = self.canvas.coords(self.paddle.id)


        if pos[2] >= self.paddle_pos[0] and pos[0] <= self.paddle_pos[2]:
            if pos[3] >= self.paddle_pos[1] and pos[3] <= self.paddle_pos[3]:
                self.y = -3
                global count
                count +=1
                score()


        if pos[3] <= self.canvas_height:
            self.canvas.after(10, self.draw)
        else:
            game_over()
            global lost
            lost = True


class Paddle:
    def __init__(self, canvas, color):
        self.canvas = canvas
        self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
        self.canvas.move(self.id, 200, 300)

        self.x = 0

        self.canvas_width = self.canvas.winfo_width()

        self.canvas.bind_all("<KeyPress-Left>", self.move_left)
        self.canvas.bind_all("<KeyPress-Right>", self.move_right)

    def draw(self):
        self.canvas.move(self.id, self.x, 0)

        self.pos = self.canvas.coords(self.id)

        if self.pos[0] <= 0:
            self.x = 0
        if self.pos[2] >= self.canvas_width:
            self.x = 0
        global lost
        if lost == False:
            self.canvas.after(10, self.draw)

    def move_left(self, event):
        if self.pos[0] >= 0:
            self.x = -3

    def move_right(self, event):
        if self.pos[2] <= self.canvas_width:
            self.x = 3


def start_game(event):
    global lost, count
    lost = False
    count = 0
    score()
    canvas.itemconfig(game, text=" ")

    time.sleep(1)
    paddle.draw()
    ball.draw()


def score():
    canvas.itemconfig(score_now, text="score: " + str(count))

def game_over():
    canvas.itemconfig(game, text="Game over!")


paddle = Paddle(canvas, "blue")
ball = Ball(canvas, paddle, "red")


score_now = canvas.create_text(430, 20, text="score: " + str(count), fill = "red", font=("Arial", 16))
game = canvas.create_text(250, 150, text=" ", fill="red", font=("Arial", 20))


canvas.bind_all("<Button-1>", start_game)

root.mainloop()

Anyone could help me?
Thanks.

EDIT: I think I need a way to un-draw the ball after I lose and re-draw it where it was when the game started! But am not sure how to do this…

I tried inserting ball = Ball(canvas, paddle, "red") inside the start_game function but this will draw a new ball and keep the old one on the bottom…

Asked By: midkin

||

Answers:

If anyone else tried to solve this and couldn’t, I found a solution.

I edited 2 things, as follows:

inside the draw() function of the Ball class, edited these lines:

if pos[3] <= self.canvas_height:
            self.canvas.after(10, self.draw)
        else:
            self.canvas.move(self.id, 245, 200) #added this line
            game_over()
            global lost
            lost = True

also edited the start_game() function as follows:

def start_game(event):
    global lost, count, ball #added ball here
    if lost == True: # added this if
        ball = Ball(canvas, paddle, "red")

    lost = False #and finally changed the lost var BEFORE drawing the paddle which has a check of lost var in order to move.
    paddle.draw()
    ball.draw()
    count = 0

    score()
    canvas.itemconfig(game, text=" ")

    time.sleep(1)

Any more help/answers welcome!

Answered By: midkin

Also, if you need to ‘undraw’ the ball, use canvas.delete(ball.id) and off the top of my head canvas.itemconfig(state=Tkinter.HIDDEN) should work

Answered By: CoderMuffin

I was in the same place as you , but then I start researching library Tkinter and few books a I figured out simple and quick solution that keeps you in main loop and let you resume.I basically after game is over , move the ball to fix point above the paddle and resume . sometimes you do not need to stop completely and restart all , just need to readjust end point to be in different coordinate, and then game will resume when you click mouse/ button and score will be reseted.Here is code I used:
I added new function is Score class to rest score value in memory and on screen in. top. right corner :
class Score:
def reset(self):
self.score = 0
self.canvas.itemconfig(self.id, text=self.score)
TotalScoreValue = self.score

Then outside any class I defined separate function that is used by "reset button" and clears all.
def clear():

canvas.coords(ball.id ,330.0, 130.0, 345.0, 145.0)
score.reset()
ball.hit_bottom = False
paddle.started = False
tk.update_idletasks()
tk.update()
time.sleep(0.01)

btn = Button(tk, text =’click me’,command=clear)
btn.pack()

Key here is "canvas.coords(ball.id ,330.0, 130.0, 345.0, 145.0)" as well as reseting the status of "hit bottom" and paddle_started
ball.hit_bottom = False
paddle.started = False

after this we need to on user demand manually ( outside main loop) rest the canvas by :
tk.update_idletasks()
tk.update()

and the ball appear above paddle in set position , score is reseted ( few other messages are hidden again) and then the same main loop awaits user to start a game again by pressing mouse button etc .
enter image description here

enter image description here

Answered By: DragonDraco
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.