재밌는 공놀이 게임 하나 만들어줄게!
**"공 튀기기 챌린지"**라는 이름으로, 마우스로 공을 튀기면서 점수를 얻는 간단하면서도 중독성 있는 게임이야.
✅ 게임 설명
- 마우스로 공을 클릭하면 튕겨 올라감.
- 공이 화면 위쪽에 닿으면 점수 +1
- 공이 바닥에 떨어지면 게임 오버
- 점수가 높아질수록 공이 점점 빨라짐
✅ 실행 방법
아래 코드를 ball_game.py로 저장하고, 터미널에서 python ball_game.py 실행하면 바로 게임 시작!
✅ 파이썬 코드 (pygame 필요)
import pygame
import sys
import random
# 초기화
pygame.init()
# 화면 설정
WIDTH, HEIGHT = 400, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("공 튀기기 챌린지")
# 색상
WHITE = (255, 255, 255)
BLUE = (50, 150, 255)
RED = (255, 50, 50)
# 공 설정
ball_radius = 20
ball_x = WIDTH // 2
ball_y = HEIGHT // 2
ball_speed_y = 0
gravity = 0.3
bounce_strength = -10
speed_increase = 0.1
# 점수
score = 0
font = pygame.font.SysFont("malgungothic", 30)
# 게임 상태
game_over = False
# 게임 루프
clock = pygame.time.Clock()
def reset_game():
global ball_x, ball_y, ball_speed_y, score, game_over
ball_x = WIDTH // 2
ball_y = HEIGHT // 2
ball_speed_y = 0
score = 0
game_over = False
while True:
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN and not game_over:
# 마우스 클릭 위치와 공의 거리 계산
mouse_x, mouse_y = pygame.mouse.get_pos()
distance = ((mouse_x - ball_x) ** 2 + (mouse_y - ball_y) ** 2) ** 0.5
if distance < ball_radius * 2:
ball_speed_y = bounce_strength - (score * speed_increase)
if event.type == pygame.KEYDOWN and game_over:
if event.key == pygame.K_SPACE:
reset_game()
if not game_over:
# 물리 적용
ball_speed_y += gravity
ball_y += ball_speed_y
# 점수 체크
if ball_y < 0:
score += 1
ball_y = 0
ball_speed_y = 0
# 바닥 충돌
if ball_y > HEIGHT - ball_radius:
game_over = True
# 공 그리기
pygame.draw.circle(screen, BLUE if not game_over else RED, (int(ball_x), int(ball_y)), ball_radius)
# 점수 표시
score_text = font.render(f"점수: {score}", True, (0, 0, 0))
screen.blit(score_text, (10, 10))
if game_over:
game_over_text = font.render("게임 오버! 스페이스로 재시작", True, (0, 0, 0))
screen.blit(game_over_text, (20, HEIGHT // 2 - 20))
pygame.display.flip()
clock.tick(60)
✅ 필요한 라이브러리 설치
pip install pygame
✅ 팁
- 공을 클릭할 때 정확히 클릭해야 튕겨요!
- 점수가 높아질수록 공이 더 빨리 떨어지므로 반사 타이밍이 중요해요.
재밌게 즐겨줘! 혹시 더 어려운 버전이나 2인용으로 확장하고 싶으면 말해줘!