Paddle Ball V1 (Python)

Paddle Ball V1 (Python)

Do you remember Alleyway from the Nintendo Gameboy? It was a 2D game where the player moved a paddle on the bottom of the screen whilst a ball bounced around the screen. If the ball hit the paddle it would rebound upwards, if the ball touched the bottom of the screen the game would end.

alleyway_boxart

Alleyway was based/very similar to Breakout and Arkanoid , but it was the game that introduced me to that particular genre. After watching Ready Player One recently I got the urge to re-check out some of the games of my youth; and this soon lead me to thinking that some of them might be easy to remake in Python. This version contains a basic title screen, paddle and ball and a basic ending screen.


#!/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=400,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)
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 = -3
if pos[0] <=0:
self.x=3
if pos[2] >= self.canvas_width:
self.x=-3

#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,300)
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_text)
while 1:
if ball.hit_bottom == False:
ball.draw()
paddle.draw()
else:
#game over screen
go_text=canvas.create_text(250,200,font=’Times 40 bold’,text=’Game Over’)
go_text
#break to end game in Python
break
tk.update_idletasks()
tk.update()
time.sleep(0.01)

 

#setting colours of paddle and ball
paddle = Paddle(canvas,’red’)
ball = Ball(canvas,paddle,’blue’)

#Title screen
gstart_text=canvas.create_text(250,200,font=’Times 40 bold’,text=’Press s To Start!’)
canvas.bind_all(‘<KeyPress-s>’,game)
tk.update_idletasks()
tk.update()
time.sleep(0.01)


 

 

Welcome to GeekTechStuff

my home away from home and where I will be sharing my adventures in the world of technology and all things geek.

The technology subjects have varied over the years from Python code to handle ciphers and Pig Latin, to IoT sensors in Azure and Python handling Bluetooth, to Ansible and Terraform and material around DevOps.

Let’s connect