Block breaker game
A classic! Here's a simple implementation of a block breaker game in the console:
Game Rules:
- The game starts with a grid of blocks, represented by
#
characters. - The player controls a paddle, represented by
-
characters, that can move left and right using the arrow keys. - The player's goal is to break all the blocks by hitting them with a ball, represented by
*
characters. - The ball will bounce off the walls and the paddle, and will break any blocks it hits.
- If the ball hits the bottom of the screen, the game is over.
Game Code:
import random
# Initialize the game grid
grid = [['#' for _ in range(10)] for _ in range(10)]
# Initialize the ball and paddle positions
ball_x = 5
ball_y = 0
paddle_x = 5
paddle_y = 9
# Game loop
while True:
# Print the current game state
for y in range(10):
for x in range(10):
if x == ball_x and y == ball_y:
print('*', end='')
elif x == paddle_x and y == paddle_y:
print('-', end='')
else:
print(grid[y][x], end='')
print()
# Get user input
user_input = input('Enter arrow key (left/right): ')
# Move the paddle
if user_input == 'a':
paddle_x -= 1
elif user_input == 'd':
paddle_x += 1
# Move the ball
ball_y += 1
if ball_y >= 10:
print('Game Over!')
break
# Check for collisions
if ball_x == paddle_x and ball_y == paddle_y:
# Ball hits paddle, bounce it back
ball_y = 0
elif ball_x == 0 or ball_x == 9:
# Ball hits wall, bounce it back
ball_x = 5
elif ball_y == 9:
# Ball hits bottom, game over
print('Game Over!')
break
# Check for block breaks
if grid[ball_y][ball_x] == '#':
# Block broken, remove it
grid[ball_y][ball_x] = ' '
# Check for win condition
if all(all(cell == ' ' for cell in row) for row in grid):
print('You win!')
break
How to Play:
- Run the code in your console or terminal.
- Use the arrow keys to move the paddle left and right.
- The ball will automatically move down the screen.
- Try to hit the blocks with the ball to break them.
- If the ball hits the bottom of the screen, the game is over.
- If you break all the blocks, you win!
Note: This is a very basic implementation, and you can add features like scoring, power-ups, and smoother animations to make the game more engaging.