from tkinter import * class Table: #### constructor def __init__ (self, window, colour="black", net_colour="green", width = 600, height=400, vertical_net =False, horizontal_net=False) : self.width = width self.height = height self.colour = colour self.canvas = Canvas(window, bg=self.colour, height=self.height, width=self.width) self.canvas.pack() if(vertical_net) : self.canvas.create_line(self.width/2, 0, self.width/2, self.height, width=2, fill=net_colour, dash=(15, 23)) if (horizontal_net) : self.canvas.create_line(0, self.height/2, self.width, self.height/2, width=2, fill=net_colour, dash=(15,23)) from tkinter import * import table window = Tk() window.title("MyPong") my_table = table.Table(window, net_colour="green", vertical_net=True window.mainloop() import table, random class Ball: #### constructor def __init__(self, table, width=14, height=14, colour="red", x_speed=6, y_speed=4, y_start=0, x_start=0) : self.width = width self.height = height self.x_posn = x_start self.y_posn = y_start self.colour = colour self.x_start = x_start self.y_start = y_start self.x_speed = x_speed sely.y_speed = y_speed self.table = table self.circle = self.table.draw_oval(self) #### methods def start_position(self) : self.x_posn = self.x_start self.y_posn = self.y_start def start_ball(self, x_speed, y_speed) : self.x_speed = -x_speed if random.randint(0,1) else x_speed self.y_speed = -y_speed if random.randint(0,1) else y_speed def move_next(self) : self.x_posn = self.x_posn = self.x_speed self.y_posn = self.y_posn = self.y_speed # if the ball hits the left wall: if(self.x_posn <= 3): self.x_posn = 3 self.x_speed = -self.x_speed if(self.x_posn >= (self.table.width - (self.width - 3))) : self.x_posn = 3 self.x_speed = -self.x_speed if(self.y_posn <= 3) : self.y_posn = 3 self.y_speed = -self.y_speed if(self.y_posn
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question