~ OOP,Classes,Python,Tkinter ...recreating PONG


Step 2: Create a Ball Class

Code and Challenge

Copy and paste the code below. Run it! Read the task and see if you can do it before moving on to the next challenge

"""
==========Task===============
1. Create two more ball objects (ball2, and ball3) making them different colours and at different locations to ball1
HINT: In order to do this you need to
a) Put the line: self.canvas.move(self.id,0,0) #this moves the oval to the specified location in it's own method called "move"
b) The method must call the parameters 'self', 'x' and 'y' (x and y are the x and y positions)
c) When inside the move method simply use:   canvas.move(self.id,x,y)

2. Play around with the size and shape of the "ball" .....this is in the __init__ method
"""

from tkinter import *
import random
import time

class Ball: #create a ball class
    def __init__(self,canvas,color): #initiliased with the variables/attributes self, canvas, and color
        self.canvas=canvas #set the intiial values for the starting attributes
        self.id=canvas.create_oval(30,30,50,50,fill=color) #starting default values for the ball
        """ Note: x and y coordinates for top left corner and x and y coordinates for the bottom right corner, and finally the fill colour for the oval
        """
        self.canvas.move(self.id,0,0) #this moves the oval to the specified location

    def draw(self): #we have created the draw method but it doesn't do anything yet.
        pass 

tk=Tk()
tk.title("My 21st Century Pong Game")
tk.resizable(0,0)
tk.wm_attributes("-topmost",1)
canvas=Canvas(tk,bg="white",width=500,height=400,bd=0,highlightthickness=0)
canvas.pack()
tk.update()


ball1=Ball(canvas,'green') #here we are creating an object (green ball) of the class Ball




Code your solution here

Systems Life Cycle (in a nutshell): Analyse - Design - Create - Test - Evaluate. Designing something or writing out some pseudocode before you actually write code is always a good idea! Get in to the habit of doing so! You can draw your flowchart here and screenshot it.

A sample flow chart (design) for this particular challenge could look like:

Flowchart: Python program to get the Fibonacci series between 0 to 50
Each challenge section below provides an online drawing tool where you can dynamically create flowcharts. Screenshot them into your presentation for submission.

Solutions & Answers

Answers /Solutions in the "members area" drive under: "Solve and Learn >>SOLUTIONS"

Testing Table

You may want to read a little about Testing first. A teacher may go through some examples with you. Feel free to fill in the test table here, and screenshot it in to your powerpoint. Testing is absolutely essential once you have created a program!
Test No. Description Test Data(input) Expected Outcome Actual Outcome Further Action?
1
2
3
4
5
Coming soon!