Skip to content

Instantly share code, notes, and snippets.

@typosone
Created July 29, 2015 01:51
Show Gist options
  • Save typosone/a73537af4c5429f9f295 to your computer and use it in GitHub Desktop.
Save typosone/a73537af4c5429f9f295 to your computer and use it in GitHub Desktop.
Breakout
from tkinter import *
import random
import time
class Ball:
def __init__(self, canvas, paddle, speed, color):
self.canvas = canvas
self.paddle = paddle
self.speed = speed
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 245, 300)
self.x = 0
self.y = 0
self.canvas_height = self.canvas.winfo_height()
self.canvas_width = self.canvas.winfo_width()
self.hit_bottom = False
self.canvas.bind_all('<Button-1>', self.start)
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 = self.y * -1
if pos[0] <= 0:
self.x *= -1
if pos[2] >= self.canvas_width:
self.x *= -1
def start(self, evt):
self.x = -self.speed
self.y = self.speed
class Paddle:
def __init__(self, canvas, speed, color):
self.canvas = canvas
self.speed = speed
self.id = canvas.create_rectangle(0, 0, 100, 10, fill=color)
self.canvas.move(self.id, 200, 600)
self.x = 0
self.canvas_width = self.canvas.winfo_width()
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
def turn_left(self, evt):
self.x = -self.speed
def turn_right(self, evt):
self.x = self.speed
class Block:
def __init__(self, canvas, x, y, color):
self.canvas = canvas
self.pos_x = x
self.pos_y = y
self.id = canvas.create_rectangle(0, 0, 50, 20, fill=color)
self.canvas.move(self.id, 25 + self.pos_x * 50,
25 + self.pos_y * 20)
def delete(self):
self.canvas.delete(self.id)
class GameOverLabel:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_text(250, 200, text='GameOver!', fill=color,
font=('Times', 48), state='hidden')
def show(self):
self.canvas.itemconfig(self.id, state='normal')
#config
WIDTH = 500
HEIGHT = 700
FPS = 60
BALL_SPEED = 5
PADDLE_SPEED = 3
COLORS = ('cyan', 'green', 'gold', 'dark orange', 'magenta',)
#initialize
tk = Tk()
tk.title("Breakout")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=WIDTH, height=HEIGHT,
bd=0, highlightthickness=0)
canvas.pack()
tk.update()
blocks = []
for y in range(5):
for x in range(9):
blocks.append(Block(canvas, x, y, random.choice(COLORS)))
paddle = Paddle(canvas, PADDLE_SPEED, 'blue')
ball = Ball(canvas, paddle, BALL_SPEED, 'red')
#mainloop
while True:
if ball.hit_bottom == False:
ball.draw()
paddle.draw()
else:
break
tk.update_idletasks()
tk.update()
time.sleep(1/FPS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment