반응형
import pygame
import random
# 화면 크기 설정
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 640
# 색상 설정
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# 게임 관련 변수 설정
BALL_SPEED = 0.1
PAD_SPEED = 0.5
BRICK_WIDTH = 60
BRICK_HEIGHT = 20
BRICK_GAP = 10
BRICK_ROWS = 5
BRICK_COLS = 8
LEVELS = 3
LIVES = 3
# 게임 초기화
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("벽돌깨기")
# 폰트 설정
font = pygame.font.SysFont("malgungothic", 20)
# 게임 관련 함수
def draw_text(text, color, x, y):
"""화면에 텍스트를 그립니다."""
text_obj = font.render(text, True, color)
text_rect = text_obj.get_rect()
text_rect.centerx = x
text_rect.centery = y
screen.blit(text_obj, text_rect)
def draw_ball(ball_x, ball_y):
"""화면에 공을 그립니다."""
pygame.draw.circle(screen, WHITE, (ball_x, ball_y), 10)
def draw_pad(pad_x, pad_y):
"""화면에 패들을 그립니다."""
pygame.draw.rect(screen, WHITE, (pad_x, pad_y, 80, 10))
def draw_brick(brick_x, brick_y):
"""화면에 벽돌을 그립니다."""
pygame.draw.rect(screen, RED, (brick_x, brick_y, BRICK_WIDTH, BRICK_HEIGHT))
def init_game():
"""게임을 초기화합니다."""
global ball_x, ball_y, ball_dx, ball_dy, pad_x, pad_y, level, score, lives, bricks
ball_x = SCREEN_WIDTH // 2
ball_y = SCREEN_HEIGHT // 2
ball_dx = BALL_SPEED
ball_dy = BALL_SPEED
pad_x = SCREEN_WIDTH // 2 - 40
pad_y = SCREEN_HEIGHT - 50
level = 1
score = 0
lives = LIVES
# 벽돌 생성
bricks = []
for row in range(BRICK_ROWS):
for col in range(BRICK_COLS):
brick_x = BRICK_GAP + (BRICK_WIDTH + BRICK_GAP) * col
brick_y = BRICK_GAP + (BRICK_HEIGHT + BRICK_GAP) * row
bricks.append((brick_x, brick_y))
def play_game():
"""게임을 실행합니다."""
global ball_x, ball_y, ball_dx, ball_dy, pad_x, pad_y, level, score, lives, bricks
while True:
# 이벤트 처리
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# 입력 처리
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
pad_x -= PAD_SPEED
if keys[pygame.K_RIGHT]:
pad_x += PAD_SPEED
# 경계 처리
if pad_x < 0:
pad_x = 0
if pad_x > SCREEN_WIDTH - 80:
pad_x = SCREEN_WIDTH - 80
if ball_x < 10 or ball_x > SCREEN_WIDTH - 10:
ball_dx = -ball_dx
if ball_y < 10:
ball_dy = -ball_dy
if ball_y > SCREEN_HEIGHT - 10:
lives -= 1
if lives == 0:
init_game()
else:
ball_x = SCREEN_WIDTH // 2
ball_y = SCREEN_HEIGHT // 2
ball_dx = BALL_SPEED
ball_dy = BALL_SPEED
pad_x = SCREEN_WIDTH // 2 - 40
# 충돌 처리
pad_rect = pygame.Rect(pad_x, pad_y, 80, 10)
ball_rect = pygame.Rect(ball_x - 10, ball_y - 10, 20, 20)
if ball_rect.colliderect(pad_rect):
ball_dy = -ball_dy
for brick in bricks:
brick_rect = pygame.Rect(brick[0], brick[1], BRICK_WIDTH, BRICK_HEIGHT)
if ball_rect.colliderect(brick_rect):
bricks.remove(brick)
score += 10
ball_dy = -ball_dy
if not bricks:
level += 1
if level > LEVELS:
init_game()
else:
init_level()
# 이동 처리
ball_x += ball_dx
ball_y += ball_dy
# 화면 업데이트
screen.fill(BLACK)
draw_text("레벨: {}".format(level), WHITE, SCREEN_WIDTH - 50, 10)
draw_text("점수: {}".format(score), WHITE, SCREEN_WIDTH - 50, 30)
draw_text("목숨: {}".format(lives), WHITE, SCREEN_WIDTH - 50, 50)
draw_ball(ball_x, ball_y)
draw_pad(pad_x, pad_y)
for brick in bricks:
draw_brick(brick[0], brick[1])
pygame.display.update()
"""
def init_level():
#레벨을 초기화합니다.
global ball_x, ball_y, ball_dx, ball_dy, pad_x, pad_y, bricks
ball_x = SCREEN_WIDTH // 2
ball_y = SCREEN_HEIGHT // 2
ball_dx = BALL_SPEED
ball_dy = BALL_SPEED
pad_x = SCREEN_WIDTH // 2 - 40
# 벽돌 생성
bricks = []
for row in range(BRICK_ROWS):
for col in range(BRICK_COLS):
brick_x = BRICK_GAP + (BRICK_WIDTH + BRICK_GAP) * col
brick_y = BRICK_GAP + (BRICK_HEIGHT + BRICK_GAP) * row
bricks.append((brick_x, brick_y))
"""
# 게임 실행
init_game()
play_game()
약간 수정하였습니다. 공의 스피드가 너무 빨라서
# 게임 관련 변수 설정
BALL_SPEED = 0.1 # BALL_SPEED = 5
PAD_SPEED = 0.5 # BALL_SPEED = 5
원래는 공 스피드가 5였는데 --> 0.1
원래 패드 스피드는 5였는데 --> 0.5
로 하였더니 실행이 되네요.
벽돌을 다 깨고 나도 레벨업이 되지는 않습니다.