0

So, yeah I'm a beginner to Tkinter Canvas Games and it's being too difficult for me to code than I thought. Okk, let's come to the chase, below is my code:

import time
from tkinter import *
from numpy import random
class Paddle:
    def __init__(self, Main_Frame):
        Main_Frame.title("Paddle")
        Main_Frame.geometry("300x300+630+150")
        self.Random_X = random.randint(270)
        self.Random_Y = random.randint(120)
        self.x = 3
        self.y = 3
        self.Y_Position = 288
        self.Can = Canvas(Main_Frame, height = 300, width = 300)
        self.Can.pack()
        self.paddle = self.Can.create_rectangle(0, 288, 90, 300, fill = "Aqua", outline = "Aqua")
        self.ball = self.Can.create_oval(self.Random_X, self.Random_Y, self.Random_X + 20, self.Random_Y + 20, outline = "Red", fill = "Red")
        self.Can.bind("<Motion>", self.Move_Paddle)
        self.Move_Ball()
    def Move_Ball(self):
        Ball_x1, Ball_y1, Ball_x2, Ball_y2 = self.Can.coords(self.ball)
        if Ball_x2 > 300:
            self.x = -self.x
        if Ball_y2 > 300:
            self.y = -self.y
        if Ball_x1 < 0:
            self.x = -self.x
        if Ball_y2 < 10:
            self.y = -self.y
        self.Can.moveto(self.ball, self.x, self.y)
        Window.after(1, self.Move_Ball)
    def Move_Paddle(self, event):
        self.X_Position = event.x
        self.Can.moveto(self.paddle, self.X_Position, self.Y_Position)
Window = Tk()
Class = Paddle(Window)
Window.mainloop()

So, my problem here is that even after calling the Move_Ball() function, it had no effect on the ball, (okk there is no error occurring though) when I tried to call the function under Move_Paddle() function (before adding the Window.after(1, self.Move_Ball) line), it worked well when I moved the mouse around the Canvas, well, I want it to automatically activate when the Paddle class is called. I was trying to check the problem in the code by placing the function call in Move_Paddel() (with the Window.after(1, self.Move_Ball) line as in the code), it increased the trouble and insanely increased the speed of the ball. If anyone could help me with these couple of problems, it would be highly appreciated. Thanks in advance!

3 Answers 3

1

Use move() instead of moveto() method. move() method moves each of the items given by tagOrId in the canvas coordinate space by adding X amount to the x-coordinate of each point associated with the item and y amount to the y-coordinate of each point associated with the item.

Then you may increase the delay in the after() method.

Here is the working code:

import time
from tkinter import *
from numpy import random
class Paddle:
    def __init__(self, Main_Frame):
        Main_Frame.title("Paddle")
        Main_Frame.geometry("300x300+630+150")
        self.Random_X = random.randint(270)
        self.Random_Y = random.randint(120)
        self.x = 3
        self.y = 3
        self.Y_Position = 288
        self.Can = Canvas(Main_Frame, height = 300, width = 300)
        self.Can.pack()
        self.paddle = self.Can.create_rectangle(0, 288, 90, 300, fill = "Aqua", outline = "Aqua")
        self.ball = self.Can.create_oval(self.Random_X, self.Random_Y, self.Random_X + 20, self.Random_Y + 20, outline = "Red", fill = "Red")
        self.Can.bind("<Motion>", self.Move_Paddle)
        self.Move_Ball()

    def Move_Ball(self):
        Ball_x1, Ball_y1, Ball_x2, Ball_y2 = self.Can.coords(self.ball)
        
        
        if Ball_x2 > 300:
            self.x = -self.x

        if Ball_y2 > 300:
            self.y = -self.y

        if Ball_x1 < 0:
            self.x = -self.x

        if Ball_y2 < 10:
            self.y = -self.y

        self.Can.move(self.ball, self.x, self.y)

        Window.after(50 ,self.Move_Ball)

    def Move_Paddle(self, event):
        self.X_Position = event.x
        self.Can.moveto(self.paddle, self.X_Position, self.Y_Position)
Window = Tk()
Class = Paddle(Window)
Window.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

0

I rewrote your code and now I think it works as you wished. Red ball slowly goes down:

import time
from tkinter import *
from numpy import random


class Paddle:
    def __init__(self, main_frame):
        self.root = main_frame
        random.seed(int(time.time()))
        self.x = random.randint(270)
        self.y = random.randint(120)
        self.Y_Position = 288
        self.Can = Canvas(self.root, height=300, width=300)
        self.Can.pack()
        self.paddle = self.Can.create_rectangle(0, 288, 90, 300, fill="Aqua", outline="Aqua")
        self.ball = self.Can.create_oval(self.x, self.y, self.x + 20, self.y + 20, outline="Red", fill="Red")
        self.Can.bind("<Motion>", self.move_paddle)
        self.move_ball()

    def move_ball(self):
        Ball_x1, Ball_y1, Ball_x2, Ball_y2 = self.Can.coords(self.ball)
        # if Ball_x2 > 300:
        #     self.x = -self.x
        # if Ball_y2 > 300:
        #     self.y = -self.y
        # if Ball_x2 < 0:
        #     self.x = 300
        if Ball_y2 > 300:
            self.y = 0
        self.y += 0.1
        self.Can.moveto(self.ball, self.x, self.y)
        self.root.after(1, self.move_ball)

    def move_paddle(self, event):
        self.X_Position = event.x
        self.Can.moveto(self.paddle, self.X_Position, self.Y_Position)


root = Tk()
root.title("Paddle")
root.geometry("300x300+630+150")
Class = Paddle(root)
root.mainloop()

And please read about PEP8

Comments

0

You should use move() instead of moveto() inside Move_Ball() and after(1, ...) is too frequent so the ball will be moved very fast. Try after(50, ...) instead:

def Move_Ball(self):
    ...
    self.Can.move(self.ball, self.x, self.y)
    Window.after(50, self.Move_Ball)

1 Comment

Okk, thanks, actually I corrected that thing just after posting the question but it didn't worked. But I'll try the second solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.