PythonGame/arkanoid(not the complete g...

165 lines
4.5 KiB
Plaintext

import pygame
import sys
import os
# Initialize Pygame
pygame.init()
pygame.mixer.init()
# Constants
WIDTH, HEIGHT = 800, 600
BALL_RADIUS = 15
PADDLE_WIDTH, PADDLE_HEIGHT = 100, 10
PADDLE_SPEED = 10
BRICK_WIDTH, BRICK_HEIGHT = 80, 20
BRICK_ROWS = 4
BRICK_COLS = 10
LINE_HEIGHT = 5
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (34, 139, 34)
RED = (255, 0, 0)
# Sounds
wall_sound = None
block_sound = None
death_sound = None
# Check if sound files exist before loading
if os.path.exists("wall.wav"):
wall_sound = pygame.mixer.Sound("wall.wav")
if os.path.exists("block.wav"):
block_sound = pygame.mixer.Sound("block.wav")
if os.path.exists("death.wav"):
death_sound = pygame.mixer.Sound("death.wav")
# Create the game window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Arkanoid")
# Create the paddle
paddle = pygame.Rect((WIDTH - PADDLE_WIDTH) // 2, HEIGHT - PADDLE_HEIGHT - 20, PADDLE_WIDTH, PADDLE_HEIGHT)
# Create the ball
ball = pygame.Rect(WIDTH // 2 - BALL_RADIUS, HEIGHT // 2 - BALL_RADIUS, BALL_RADIUS * 2, BALL_RADIUS * 2)
ball_speed = [5, 5] # [x, y]
# Create death line
line = pygame.Rect(0, HEIGHT - LINE_HEIGHT, WIDTH, LINE_HEIGHT)
# Create bricks
bricks = []
for row in range(BRICK_ROWS):
for col in range(BRICK_COLS):
brick = pygame.Rect(col * (BRICK_WIDTH + 5), row * (BRICK_HEIGHT + 5), BRICK_WIDTH, BRICK_HEIGHT)
bricks.append(brick)
# Scoring
score = 0
font = pygame.font.Font(None, 36)
# Game Over flag and text
game_over = False
game_started = False
game_over_text = font.render("Game Over", True, WHITE)
game_over_rect = game_over_text.get_rect(center=(WIDTH // 2, HEIGHT // 2))
# Render a simple "Press any key to start" message
start_message_font = pygame.font.Font(None, 36)
start_message = start_message_font.render("Press any key to start", True, WHITE)
start_message_rect = start_message.get_rect(center=(WIDTH // 2, HEIGHT // 2))
# Main game loop
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
game_started = True
if not game_started:
# Draw the start message and update the display
screen.fill(BLACK)
screen.blit(start_message, start_message_rect)
pygame.display.flip()
pygame.time.delay(100) # Adjust the delay as needed for smoother transition
continue # Skip the rest of the loop until the player presses a key to start the game
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and paddle.left > 0:
paddle.x -= PADDLE_SPEED
if keys[pygame.K_RIGHT] and paddle.right < WIDTH:
paddle.x += PADDLE_SPEED
if keys[pygame.K_a] and paddle.left > 0:
paddle.x -= PADDLE_SPEED
if keys[pygame.K_d] and paddle.right < WIDTH:
paddle.x += PADDLE_SPEED
# Update the ball position
ball.x += ball_speed[0]
ball.y += ball_speed[1]
# Ball collisions with walls
if ball.left <= 0 or ball.right >= WIDTH:
ball_speed[0] = -ball_speed[0]
if wall_sound:
wall_sound.play()
if ball.top <= 0:
ball_speed[1] = -ball_speed[1]
if wall_sound:
wall_sound.play()
# Game Over condition
if ball.bottom >= HEIGHT:
game_over = True
if death_sound:
death_sound.play()
# Ball collision with paddle
if ball.colliderect(paddle) and ball_speed[1] > 0:
ball_speed[1] = -ball_speed[1]
if wall_sound:
wall_sound.play()
# Ball collisions with bricks
for brick in bricks:
if ball.colliderect(brick):
bricks.remove(brick)
ball_speed[1] = -ball_speed[1]
score += 5
if block_sound:
block_sound.play()
# Draw everything
screen.fill(BLACK)
pygame.draw.rect(screen, WHITE, paddle)
pygame.draw.ellipse(screen, WHITE, ball)
pygame.draw.rect(screen, RED, line)
for brick in bricks:
pygame.draw.rect(screen, WHITE, brick)
# Draw score
score_text = font.render("Score: {}".format(score), True, WHITE)
screen.blit(score_text, (10, HEIGHT - 40 - score_text.get_height()))
# Update the display
pygame.display.flip()
# Cap the frame rate
pygame.time.Clock().tick(60)
# Display Game Over text
screen.fill(BLACK)
screen.blit(game_over_text, game_over_rect)
pygame.display.flip()
# Wait for a few seconds before quitting
pygame.time.delay(1500)
pygame.quit()
sys.exit()