My original 2022 Python, running unchanged in your browser through Skulpt, a Python interpreter written in JavaScript. Skulpt’s turtle lacks two features the game uses (shapesize and onkeypress), so a few lines fill those in before the game starts. Press Start, then use ← and → (or the buttons) to move. The original has no game-over screen, so if the ball gets past the paddle, press Restart.
The code that’s running
main.py
import turtle as t
import time
from paddle import Paddle
from ball import Ball
from bricks import Bricks
screen = t.Screen()
screen.bgcolor("black")
screen.setup(width=780, height=610)
screen.title("Breakout")
screen.tracer(0)
paddle = Paddle()
ball = Ball()
bricks = Bricks()
speed = 0.1
screen.listen()
screen.onkeypress(paddle.move_right, "Right")
screen.onkeypress(paddle.move_left, "Left")
while len(bricks.bricks) != 0:
screen.update()
time.sleep(speed)
ball.move()
if ball.xcor() == 384 or ball.xcor() == -384:
ball.bounce_off_side()
if ball.ycor() >= 299:
ball.bounce_off_top()
if ball.ycor() == -241 and ball.distance(paddle) <= (1649 ** 0.5):
ball.bounce_off_paddle(paddle)
for brick in bricks.bricks:
x_dis = abs(brick.xcor() - ball.xcor())
y_dis = abs(brick.ycor() - ball.ycor())
if x_dis <= 18 and y_dis <= 18:
ball.bounce_off_brick(x_dis, y_dis)
brick.hideturtle()
bricks.bricks.remove(brick)
screen.exitonclick()
ball.py
import turtle as t
class Ball(t.Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.color("white")
self.shapesize(0.6, 0.6)
self.hideturtle()
self.penup()
self.goto((0, -241))
self.showturtle()
self.x_move = 12
self.y_move = 12
def move(self):
self.goto(self.xcor() + self.x_move, self.ycor() + self.y_move)
def bounce_off_side(self):
self.x_move *= -1
def bounce_off_brick(self, x_dis, y_dis):
self.y_move *= -1
def bounce_off_paddle(self, paddle):
self.y_move *= -1
if ((self.xcor() - paddle.xcor()) > 30 and self.x_move < 0)\
or ((self.xcor() - paddle.xcor()) < -30 and self.x_move > 0):
self.x_move *= -1
def bounce_off_top(self):
self.y_move *= -1
bricks.py
import turtle as t
class Bricks:
def __init__(self):
self.bricks = []
y_cor = 215
for color in ['#FF0000', '#FF7F00', '#FFFF00', '#00FF00', '#0000FF', '#4B0082', '#9400D3']:
x_cor = -364
for _ in range(16):
new_brick = t.Turtle()
new_brick.shape('square')
new_brick.color(color)
new_brick.shapesize(1.2, 2.4)
new_brick.hideturtle()
new_brick.penup()
new_brick.goto((x_cor, y_cor))
new_brick.showturtle()
self.bricks.append(new_brick)
x_cor += 48
y_cor -= 24
paddle.py
import turtle as t
class Paddle(t.Turtle):
def __init__(self):
super().__init__()
self.shape("square")
self.color("white")
self.shapesize(0.2, 4)
self.hideturtle()
self.penup()
self.goto((0, -250))
self.showturtle()
self.move = 20
def move_right(self):
if self.xcor() < 360:
self.goto(self.xcor() + self.move, self.ycor())
def move_left(self):
if self.xcor() > -360:
self.goto(self.xcor() - self.move, self.ycor())