Brick breaker game with ball
A classic! Let's create a simple Brick Breaker game with a ball. We'll use a text-based interface to keep things simple.
Game Rules:
- The game starts with a ball bouncing around the screen.
- The player controls a paddle at the bottom of the screen.
- The goal is to break bricks by hitting them with the ball.
- Each brick has a specific point value.
- The game ends when all bricks are broken or the ball hits the bottom of the screen.
Game Code:
Here's a simple implementation in Python:
import random
# Game variables
bricks = [
{"x": 0, "y": 0, "width": 4, "height": 1, "points": 10}, # Top brick
{"x": 4, "y": 0, "width": 4, "height": 1, "points": 20}, # Top brick
{"x": 8, "y": 0, "width": 4, "height": 1, "points": 30}, # Top brick
{"x": 0, "y": 1, "width": 4, "height": 1, "points": 10}, # Middle brick
{"x": 4, "y": 1, "width": 4, "height": 1, "points": 20}, # Middle brick
{"x": 8, "y": 1, "width": 4, "height": 1, "points": 30}, # Middle brick
{"x": 0, "y": 2, "width": 4, "height": 1, "points": 10}, # Bottom brick
{"x": 4, "y": 2, "width": 4, "height": 1, "points": 20}, # Bottom brick
{"x": 8, "y": 2, "width": 4, "height": 1, "points": 30}, # Bottom brick
]
paddle = {"x": 0, "y": 2, "width": 4, "height": 1}
ball = {"x": 0, "y": 0, "dx": 1, "dy": 1, "radius": 1}
score = 0
# Game loop
while True:
# Clear the screen
print("\n" * 20)
# Draw the bricks
for brick in bricks:
if brick["y"] == 0:
print(" " * brick["x"] + "#" * brick["width"] + " " * (20 - brick["x"] - brick["width"]))
elif brick["y"] == 1:
print(" " * brick["x"] + "#" * brick["width"] + " " * (20 - brick["x"] - brick["width"]))
elif brick["y"] == 2:
print(" " * brick["x"] + "#" * brick["width"] + " " * (20 - brick["x"] - brick["width"]))
# Draw the paddle
print(" " * paddle["x"] + "-" * paddle["width"] + " " * (20 - paddle["x"] - paddle["width"]))
# Draw the ball
print(" " * ball["x"] + "O" + " " * (20 - ball["x"] - 1))
# Get user input
user_input = input("Enter a direction (L/R): ")
# Move the paddle
if user_input == "L":
paddle["x"] -= 1
elif user_input == "R":
paddle["x"] += 1
# Move the ball
ball["x"] += ball["dx"]
ball["y"] += ball["dy"]
# Check for collisions
if ball["x"] < 0 or ball["x"] > 19:
ball["dx"] = -ball["dx"]
if ball["y"] < 0:
ball["dy"] = -ball["dy"]
if ball["y"] > 2:
print("Game Over!")
break
if paddle["x"] <= ball["x"] <= paddle["x"] + paddle["width"] and ball["y"] == paddle["y"]:
ball["dy"] = -ball["dy"]
for brick in bricks:
if brick["x"] <= ball["x"] <= brick["x"] + brick["width"] and brick["y"] <= ball["y"] <= brick["y"] + brick["height"]:
ball["dy"] = -ball["dy"]
score += brick["points"]
bricks.remove(brick)
# Update the score
print("Score:", score)
How to Play:
- Run the code in a Python interpreter or save it to a file and run it.
- Use the
L
andR
keys to move the paddle left and right. - The ball will bounce around the screen, and you need to hit the bricks with the paddle to break them.
- Each brick has a specific point value, and you'll earn points for breaking them.
- The game ends when all bricks are broken or the ball hits the bottom of the screen.
Note: This is a very basic implementation, and you can improve it by adding features like smoother animations, sound effects, and more levels.