samedi 22 décembre 2018

How to add a restart button in a bouncing ball game-tkinter python?

I created a bouncing ball game inside python using tkinter and I wanted to add a restart button for when the game ends but I cant get it to work. I created the function in the class game but it cmes up with a type error saying Game() takes no argumens. Can anybody help me please?

TypeError: Game() takes no arguments

Here is my code:

from tkinter import *
import random
import time

class Ball:
def __init__(self, canvas, paddle, score, color):
    self.canvas = canvas
    self.paddle = paddle
    self.score = score
    self.id = canvas.create_oval(10,10,25,25, fill=color)
    self.canvas.move(self.id, 245, 100)
    starts = [-3, -2, -2, 2, 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]:
            self.score.hit()
            return True
    return False

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_width:
        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

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):
    self.canvas.move(self.id, self.x, 0)
    pos = self.canvas.coords(self.id)
    if pos[0] <= 0:
        self.x = 0
    elif pos[2] >= self.canvas_width:
        self.x = 0

def turn_left(self, evt):
    self.x = -2

def turn_right(self, evt):
    self.x = 2

class Score:
    def __init__(self, canvas, color):
        self.score = 0
        self.canvas = canvas
        self.id = canvas.create_text(450, 10, text=self.score, fill=color)

def hit(self):
    self.score += 1
    self.canvas.itemconfig(self.id, text=self.score)

class Game:
    def add_restart(self):
    self.restart_button = Button(tk, text="Click to restart game", 
 command=self.restart)
    self.restart_button.pack()
    self.canvas.coords(self.paddle.id, 200, 300, 300, 310)
    self.canvas.coords(self.ball.id, 245,100,260,115)


tk = Tk()
tk.title("Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=500, height=400,bd=0, highlightthickness=0)
canvas.pack()
tk.update()


score = Score(canvas, 'green')
paddle = Paddle(canvas, 'blue')
ball= Ball(canvas, paddle, score, 'red')
game_over_text = canvas.create_text(250, 200, text='GAME OVER', state='hidden')
game = Game(canvas, paddle, ball, score, game_over_text)

def update_game():
    if ball.hit_bottom == False:
        ball.draw()
        paddle.draw()
    if ball.hit_bottom == True:
        canvas.itemconfig(game_over_text, state='normal')
        time.sleep(2)
    if game.restart_button is None:
        game.add_restart()

    tk.update_idletasks()
    tk.after(10, update_game)

tk.after(10, update_game)
tk.mainloop()




Aucun commentaire:

Enregistrer un commentaire