Wall gibi python oyunu
import pygame
import sys
# Initialize the game
pygame.init()
# Define colors
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
blue = (0, 0, 255)
green = (0, 255, 0)
# Display dimensions
width, height = 800, 600
# Create the display
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Brick Breaker Game')
# Paddle dimensions
paddle_width, paddle_height = 100, 10
paddle_x, paddle_y = width // 2 - paddle_width // 2, height - 30
# Ball dimensions and speed
ball_radius = 10
ball_x, ball_y = width // 2, height // 2
ball_speed_x, ball_speed_y = 4, -4
# Brick dimensions
brick_width, brick_height = 75, 20
bricks = []
rows = 5
cols = 10
for row in range(rows):
for col in range(cols):
brick_x = col * (brick_width + 10) + 35
brick_y = row * (brick_height + 10) + 50
bricks.append(pygame.Rect(brick_x, brick_y, brick_width, brick_height))
def draw_objects():
# Draw paddle
pygame.draw.rect(screen, white, (paddle_x, paddle_y, paddle_width, paddle_height))
# Draw ball
pygame.draw.circle(screen, red, (ball_x, ball_y), ball_radius)
# Draw bricks
for brick in bricks:
pygame.draw.rect(screen, green, brick)
# Main game loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Handle paddle movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and paddle_x > 0:
paddle_x -= 6
if keys[pygame.K_RIGHT] and paddle_x < width - paddle_width:
paddle_x += 6
# Move the ball
ball_x += ball_speed_x
ball_y += ball_speed_y
# Ball collision with walls
if ball_x - ball_radius <= 0 or ball_x + ball_radius >= width:
ball_speed_x *= -1
if ball_y - ball_radius <= 0:
ball_speed_y *= -1
# Ball collision with paddle
if (paddle_y < ball_y + ball_radius < paddle_y + paddle_height and
paddle_x < ball_x < paddle_x + paddle_width):
ball_speed_y *= -1
# Ball collision with bricks
for brick in bricks[:]:
if brick.collidepoint(ball_x, ball_y):
bricks.remove(brick)
ball_speed_y *= -1
# Ball out of bounds
if ball_y + ball_radius >= height:
ball_x, ball_y = width // 2, height // 2
ball_speed_x, ball_speed_y = 4, -4
bricks = [pygame.Rect(col * (brick_width + 10) + 35, row * (brick_height + 10) + 50,
brick_width, brick_height) for row in range(rows) for col in range(cols)]
# Refresh screen
screen.fill(black)
draw_objects()
pygame.display.flip()
# Frame rate
pygame.time.Clock().tick(60)
No comments:
Post a Comment