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


Step 12: Create text "Game Over" feature in Python and Tkinter

Code and Challenge

Copy and paste the code below. Run it! See if you can play around with adding additional text labels to the start adn other parts of the game

from tkinter import *

import random
import time

class Game:
    def __init__(self, canvas): # Added method.
        self.canvas=canvas
    def game_loop(self):  # Removed canvas parameter.
        if hit_bottom==True:
            self.draw_text(self.canvas,300,200,text)  # Added self.canvas arg
    def draw_text(self,canvas,x,y,text,size='40'):
        font=('Helvetica',size)
        return self.canvas.create_text(x,y,text=text,font=font)

class Ball:
    def __init__(self,canvas,bat,color):
        self.canvas=canvas
        self.bat=bat
        self.id=canvas.create_oval(30,30,50,50,fill=color)
        self.canvas.move(self.id,100,200)
        starting_position=[-3,-2,-1,1,2,3]
        random.shuffle(starting_position)
        self.x = starting_position[0]
        self.y = -3
        self.canvas_height=self.canvas.winfo_height()
        self.canvas_width=self.canvas.winfo_width()
        self.hit_bottom=False 


    def hit_bat(self,pos):
        bat_pos=self.canvas.coords(self.bat.id)
        if pos[2] >=bat_pos[0] and pos[0] <=bat_pos[2]:
            if pos[3]>=bat_pos[1] and pos[3] <= bat_pos[3]:
                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=6
        if pos[3] >=self.canvas_height:
            self.hit_bottom = True


        if self.hit_bat(pos) ==True:
            self.y=-6
        if pos[0] <=0:
            self.x=6
        if pos[2]>=self.canvas_width:
            self.x=-6



class Pongbat():
    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('',self.left_turn)
        self.canvas.bind_all('',self.right_turn)


    def draw(self):
        self.canvas.move(self.id,self.x,0)
        pos=self.canvas.coords(self.id)
        if pos[0]<=0:
            self.x=0
        if pos[2]>=self.canvas_width:
            self.x=0

    def left_turn(self,evt):
        self.x=-10

    def right_turn(self,evt):
        self.x=10

def main():
    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()

    bat1=Pongbat(canvas,'red')
    ball1=Ball(canvas,bat1, 'green')

    while 1:
        if ball1.hit_bottom ==False: 
            ball1.draw()
            bat1.draw()
            tk.update()  # Allow screen to be updated (moved here after draw calls).
        else:
            draw1=Game(canvas)  # Added argument for new __init__() method.
            draw1.draw_text(canvas,250,200,'Game Over')  # Added canvas arg.
            tk.update()  # Allow screen to be updated.
            canvas.after(3000)  # Added. Pause for a few seconds.
            return  # Terminate.

#        time.sleep(0.02)  # Although you can do so, it is best not to call sleep() in tkinter app.
        canvas.after(2)  # Use universal 'after()` method instead. Time in millisecs.
main()



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!