Paddle Ball V2 (Python)

Added a scoring system, a newer title screen that accepts a mouse click, but not managed to change the ball physics yet (I want it to get faster the more it hits the paddle). Also adjusted the window size.


#!/bin/python3
#geektechstuff
#Python Paddle Ball

#libraries to import
from tkinter import *
import time, random

#create window
tk=Tk()
#create window title
tk.title(“Python Paddle Ball”)
#make window not resizable
tk.resizable(0,0)
#put window on top of other windows
tk.wm_attributes(“-topmost”,1)
#create canvas on window/define width and hight
canvas=Canvas(tk,width=500,height=800,bd=0,highlightthickness=0)
canvas.pack()
tk.update()

#create ball
class Ball:
def __init__(self,canvas,paddle,color):
self.canvas=canvas
self.paddle=paddle
self.id=canvas.create_oval(10,10,25,25,fill=color)
self.canvas.move(self.id,245,100)
starts=[-3,-2,-1,1,2,3]
random.shuffle(starts)
self.x=starts[0]
self.y=–3
self.canvas_height=self.canvas.winfo_height()
self.canvas_width=self.canvas.winfo_width()
self.hit_bottom = False

def hit_paddle(self, pos):
paddle_pos = self.canvas.coords(self.paddle.id)
if pos[2] >= paddle_pos[0] and pos[0] <= paddle_pos[2]:
if pos[3] >= paddle_pos[1] and pos[3] <= paddle_pos[3]:
return True
return False

def draw(self):
self.canvas.move(self.id,self.x,self.y)
pos=self.canvas.coords(self.id)
paddle_hit=0
if paddle_hit<5:
if pos[1] <= 0:
self.y = 1
if pos[1] <= 0:
self.y = 10
if pos[3] >= self.canvas_height:
self.hit_bottom=True
if self.hit_paddle(pos) == True:
self.y = -3
paddle_hit=paddle_hit+1
if pos[0] <=0:
self.x=3
if pos[2] >= self.canvas_width:
self.x=-3
else:
if pos[1] <= 0:
self.y = 1
if pos[3] >= self.canvas_height:
self.hit_bottom=True
if self.hit_paddle(pos) == True:
self.y = -30
paddle_hit=paddle_hit+1
if pos[0] <=0:
self.x=30
if pos[2] >= self.canvas_width:
self.x=-30

#create paddle
class Paddle:
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,700)
self.x=0
self.canvas_width=self.canvas.winfo_width()
#define controls
self.canvas.bind_all(‘<KeyPress-Left>’,self.turn_left)
self.canvas.bind_all(‘<KeyPress-Right>’,self.turn_right)

def draw(self):
self.canvas.move(self.id, self.x, 0)
pos = self.canvas.coords(self.id)
if pos[0] <= 0:
self.x = 0
elif pos[2] >= self.canvas_width:
self.x = 0
#speed of paddle going left
def turn_left(self,evt):
self.x=-4
#speed of paddle going right
def turn_right(self,evt):
self.x=4

#putting it all together in a game
def game(self):
canvas.delete(gstart_title)
canvas.delete(gstart_text)
#setting colours of paddle and ball
paddle = Paddle(canvas,’red’)
ball = Ball(canvas,paddle,’blue’)
score=0
while 1:
if ball.hit_bottom == False:
ball.draw()
paddle.draw()
score=score+1
else:
#game over screen
go_text=canvas.create_text(250,200,font=’Times 40 bold’,text=’Game Over’)
gscore_text=canvas.create_text(250,50,font=’Times 20 bold’,text=’Score:’)
gs_text=canvas.create_text(450,50,font=’Times 20 bold’,text=score)
#break to end game in Python
break
tk.update_idletasks()
tk.update()
time.sleep(0.01)

 

#Title screen
gstart_title=canvas.create_text(250,50,font=’Times 40 bold’,text=’Breakout Cl0ne’)
gstart_text=canvas.create_text(250,200,font=’Times 40 bold’,text=’Click to start’)
canvas.bind_all(‘<Button-1>’,game)
tk.update_idletasks()
tk.update()
time.sleep(0.01)