Thread: Pong speed

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0

    Pong speed


    So, I started out running Pong without using classes, just functions and such. But, when I switched to classes it got slow. Needless to say, it isn't done yet. But I'm trying to fix as I go. If you need more info just say so.
    Code:
    try:
    	from Tkinter import *
    except:
    	from tkinter import *
    
    import time
    from random import *
    
    class Pong(object):
            master = Tk()
            master.title("Pong")
    
            w = Canvas(master, width=500, height=500)
            w.pack()
    
            ball_left = 240
            ball_right = 260
            ball_up = randint(50, 450)
            ball_down = ball_up+20
    
            up_p1 = 200
            down_p1 = 300
    
            lr = randint(1, 2)
            angle = randint(1, 2)
    
            paddle_1_inst = w.create_rectangle(480, down_p1, 500, up_p1, fill="black")
            oval_instance = w.create_oval(ball_left, ball_down, ball_right, ball_up, fill="#00FF00")
    
            def __init__(self):    #main
                    self.master.bind_all("<Key-2>", self.move_down_p1)
                    self.master.bind_all("<Key-8>", self.move_up_p1)
                    self.move_ball()
                    
    
    
            def angle_set(self):
                    #global ball_up, ball_down, angle
                    if self.ball_up<2:
                            self.angle = 1
                    elif self.ball_down>500:
                            self.angle = 2
                    else:
                            self.angle = self.angle
                
    
            def move_ball(self):
                    #global ball_left, ball_right, ball_up, ball_down, lr, angle
                    self.master.after(1, self.move_ball)
                    self.angle_set()
                    if self.ball_right >= 500:
                            self.lr = 2
                    elif self.ball_right < 500:
                            if self.ball_left <= 0:
                                    self.lr = 1
                            else:
                                    self.lr = self.lr
    
        
    
                    if self.angle==1:
                            self.ball_up += 2
                            self.ball_down += 2
                    elif self.angle==2:
                            self.ball_up -= 2
                            self.ball_down -= 2
    
                    if self.lr==1:
                            self.ball_right += 4
                            self.ball_left += 4
                    elif self.lr==2:
                            self.ball_right -= 4
                            self.ball_left -= 4
        
                    self.w.coords(self.oval_instance, self.ball_left, self.ball_down, self.ball_right, self.ball_up)
    
            def move_down_p1(self, event):
                    #global up_p1, down_p1, paddle_1_inst
                    self.up_p1 += 5
                    self.down_p1 += 5
                    self.w.coords(self.paddle_1_inst, 480, self.down_p1, 500, self.up_p1)
    
            def move_up_p1(self, event):
                    #global up_p1, down_p1, paddle_1_inst
                    self.up_p1 -= 5
                    self.down_p1 -= 5
                    self.w.coords(self.paddle_1_inst, 480, self.down_p1, 500, self.up_p1)
                    
                           
    
    
    
    
    app=Pong()
    Thanks in advance for your help!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    507
    Rep Power
    33
    How fast the ball moves depends on the number passed to after() and the processor speed of the computer. This runs fine on my computer as far as the controls go. I did move the call to after() to the bottom since you should complete the move before you call the next move. Also, you want to use instance objects/variables instead of class varaibles, especially for the Tkinter main window, as you want this instance of Tkinter associated with this class not with all instances of this class.
    Code:
    try:
    	from Tkinter import *
    except:
    	from tkinter import *
    
    import time
    from random import *
    
    class Pong(object):
            def __init__(self):    #main
                    self.master = Tk()
                    self.master.title("Pong")
    
                    self.ball_left = 240
                    self.ball_right = 260
                    self.ball_up = randint(50, 450)
                    self.ball_down = self.ball_up+20
    
                    self.up_p1 = 200
                    self.down_p1 = 300
    
                    self.lr = randint(1, 2)
                    self.angle = randint(1, 2)
    
                    self.w = Canvas(self.master, width=500, height=500)
                    self.w.pack()
    
                    self.paddle_1_inst = self.w.create_rectangle(480, 
                                    self.down_p1, 500, self.up_p1, 
                                    fill="black")
                    self.oval_instance = self.w.create_oval(self.ball_left, 
                                    self.ball_down, self.ball_right, 
                                    self.ball_up, fill="#00FF00")
    
                    self.master.bind_all("<Key-2>", self.move_down_p1)
                    self.master.bind_all("<Key-8>", self.move_up_p1)
                    self.move_ball()
                    
                    self.master.mainloop()
    
            def angle_set(self):
                    ## instance variables can be seen throughout the class
                    ## but not outside the class (globally)
                    #instance variable ball_up, ball_down, angle
                    if self.ball_up<2:
                            self.angle = 1
                    elif self.ball_down>500:
                            self.angle = 2
                    else:
                            self.angle = self.angle
                
    
            def move_ball(self):
                    #instance ball_left, ball_right, ball_up, ball_down, lr, angle
                    self.angle_set()
                    if self.ball_right >= 500:
                            self.lr = 2
                    elif self.ball_right < 500:
                            if self.ball_left <= 0:
                                    self.lr = 1
                            else:
                                    self.lr = self.lr
    
        
    
                    if self.angle==1:
                            self.ball_up += 2
                            self.ball_down += 2
                    elif self.angle==2:
                            self.ball_up -= 2
                            self.ball_down -= 2
    
                    if self.lr==1:
                            self.ball_right += 4
                            self.ball_left += 4
                    elif self.lr==2:
                            self.ball_right -= 4
                            self.ball_left -= 4
        
                    self.w.coords(self.oval_instance, self.ball_left, self.ball_down, self.ball_right, self.ball_up)
                    self.master.after(1, self.move_ball)
    
            def move_down_p1(self, event):
                    #instance variable up_p1, down_p1, paddle_1_inst
                    self.up_p1 += 5
                    self.down_p1 += 5
                    self.w.coords(self.paddle_1_inst, 480, self.down_p1, 500, self.up_p1)
    
            def move_up_p1(self, event):
                    #instance variable up_p1, down_p1, paddle_1_inst
                    self.up_p1 -= 5
                    self.down_p1 -= 5
                    self.w.coords(self.paddle_1_inst, 480, self.down_p1, 500, self.up_p1)
                    
    
    app=Pong()
    Last edited by dwblas; August 19th, 2013 at 11:57 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by dwblas
    How fast the ball moves depends on the number passed to after() and the processor speed of the computer. This runs fine on my computer as far as the controls go. I did move the call to after() to the bottom since you should complete the move before you call the next move. Also, you want to use instance objects/variables instead of class varaibles, especially for the Tkinter main window, as you want this instance of Tkinter associated with this class not with all instances of this class.
    Code:
    try:
    	from Tkinter import *
    except:
    	from tkinter import *
    
    import time
    from random import *
    
    class Pong(object):
            def __init__(self):    #main
                    self.master = Tk()
                    self.master.title("Pong")
    
                    self.ball_left = 240
                    self.ball_right = 260
                    self.ball_up = randint(50, 450)
                    self.ball_down = self.ball_up+20
    
                    self.up_p1 = 200
                    self.down_p1 = 300
    
                    self.lr = randint(1, 2)
                    self.angle = randint(1, 2)
    
                    self.w = Canvas(self.master, width=500, height=500)
                    self.w.pack()
    
                    self.paddle_1_inst = self.w.create_rectangle(480, 
                                    self.down_p1, 500, self.up_p1, 
                                    fill="black")
                    self.oval_instance = self.w.create_oval(self.ball_left, 
                                    self.ball_down, self.ball_right, 
                                    self.ball_up, fill="#00FF00")
    
                    self.master.bind_all("<Key-2>", self.move_down_p1)
                    self.master.bind_all("<Key-8>", self.move_up_p1)
                    self.move_ball()
                    
                    self.master.mainloop()
    
            def angle_set(self):
                    ## instance variables can be seen throughout the class
                    ## but not outside the class (globally)
                    #instance variable ball_up, ball_down, angle
                    if self.ball_up<2:
                            self.angle = 1
                    elif self.ball_down>500:
                            self.angle = 2
                    else:
                            self.angle = self.angle
                
    
            def move_ball(self):
                    #instance ball_left, ball_right, ball_up, ball_down, lr, angle
                    self.angle_set()
                    if self.ball_right >= 500:
                            self.lr = 2
                    elif self.ball_right < 500:
                            if self.ball_left <= 0:
                                    self.lr = 1
                            else:
                                    self.lr = self.lr
    
        
    
                    if self.angle==1:
                            self.ball_up += 2
                            self.ball_down += 2
                    elif self.angle==2:
                            self.ball_up -= 2
                            self.ball_down -= 2
    
                    if self.lr==1:
                            self.ball_right += 4
                            self.ball_left += 4
                    elif self.lr==2:
                            self.ball_right -= 4
                            self.ball_left -= 4
        
                    self.w.coords(self.oval_instance, self.ball_left, self.ball_down, self.ball_right, self.ball_up)
                    self.master.after(1, self.move_ball)
    
            def move_down_p1(self, event):
                    #instance variable up_p1, down_p1, paddle_1_inst
                    self.up_p1 += 5
                    self.down_p1 += 5
                    self.w.coords(self.paddle_1_inst, 480, self.down_p1, 500, self.up_p1)
    
            def move_up_p1(self, event):
                    #instance variable up_p1, down_p1, paddle_1_inst
                    self.up_p1 -= 5
                    self.down_p1 -= 5
                    self.w.coords(self.paddle_1_inst, 480, self.down_p1, 500, self.up_p1)
                    
    
    app=Pong()

    Thank you!! It does exactly what I want it to!

IMN logo majestic logo threadwatch logo seochat tools logo