1. cmd를 켠다
2. python --version 입력
3. pip --version 입력
4.pip install pygame 입력 후 pygame 설치
5. 그 후 아래에 있는 also-gpt2가 만들어준 코드를 복붙한다
import pygame
import sys
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Ping-Pong Game")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Paddle settings
PADDLE_WIDTH, PADDLE_HEIGHT = 15, 90
PADDLE_SPEED = 10
# Ball settings
BALL_SIZE = 20
BALL_SPEED_X, BALL_SPEED_Y = 5, 5
# Score
player1_score, player2_score = 0, 0
font = pygame.font.Font(None, 36)
# Paddles and ball positions
player1_pos = [50, HEIGHT // 2 - PADDLE_HEIGHT // 2]
player2_pos = [WIDTH - 50 - PADDLE_WIDTH, HEIGHT // 2 - PADDLE_HEIGHT // 2]
ball_pos = [WIDTH // 2 - BALL_SIZE // 2, HEIGHT // 2 - BALL_SIZE // 2]
ball_dir_x, ball_dir_y = BALL_SPEED_X, BALL_SPEED_Y
# Game loop
running = True
clock = pygame.time.Clock()
while running:
screen.fill(BLACK)
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
# Player 1 controls
if keys[pygame.K_w] and player1_pos[1] > 0:
player1_pos[1] -= PADDLE_SPEED
if keys[pygame.K_s] and player1_pos[1] < HEIGHT - PADDLE_HEIGHT:
player1_pos[1] += PADDLE_SPEED
# Player 2 controls
if keys[pygame.K_UP] and player2_pos[1] > 0:
player2_pos[1] -= PADDLE_SPEED
if keys[pygame.K_DOWN] and player2_pos[1] < HEIGHT - PADDLE_HEIGHT:
player2_pos[1] += PADDLE_SPEED
# Ball movement
ball_pos[0] += ball_dir_x
ball_pos[1] += ball_dir_y
# Ball collision with top and bottom
if ball_pos[1] <= 0 or ball_pos[1] >= HEIGHT - BALL_SIZE:
ball_dir_y = -ball_dir_y
# Ball collision with paddles
if (player1_pos[0] < ball_pos[0] < player1_pos[0] + PADDLE_WIDTH and
player1_pos[1] < ball_pos[1] < player1_pos[1] + PADDLE_HEIGHT) or \
(player2_pos[0] < ball_pos[0] + BALL_SIZE < player2_pos[0] + PADDLE_WIDTH and
player2_pos[1] < ball_pos[1] < player2_pos[1] + PADDLE_HEIGHT):
ball_dir_x = -ball_dir_x
# Ball out of bounds
if ball_pos[0] <= 0:
player2_score += 1
ball_pos = [WIDTH // 2 - BALL_SIZE // 2, HEIGHT // 2 - BALL_SIZE // 2]
ball_dir_x, ball_dir_y = BALL_SPEED_X, BALL_SPEED_Y
if ball_pos[0] >= WIDTH - BALL_SIZE:
player1_score += 1
ball_pos = [WIDTH // 2 - BALL_SIZE // 2, HEIGHT // 2 - BALL_SIZE // 2]
ball_dir_x, ball_dir_y = -BALL_SPEED_X, BALL_SPEED_Y
# Draw paddles and ball
pygame.draw.rect(screen, WHITE, (*player1_pos, PADDLE_WIDTH, PADDLE_HEIGHT))
pygame.draw.rect(screen, WHITE, (*player2_pos, PADDLE_WIDTH, PADDLE_HEIGHT))
pygame.draw.ellipse(screen, WHITE, (*ball_pos, BALL_SIZE, BALL_SIZE))
# Draw score
score_text = font.render(f"{player1_score} - {player2_score}", True, WHITE)
screen.blit(score_text, (WIDTH // 2 - score_text.get_width() // 2, 20))
# Refresh screen
pygame.display.flip()
clock.tick(60)
6. py 파일로 저장한다 (예: ping-pong)
7. 다시 cmd를 켜서 python ping-pong
8. 게임 시작
How to Play
- Player 1: Use
W
(up) and S
(down) keys. - Player 2: Use the arrow keys
UP
(up) and DOWN
(down).
재 밌 다!!
댓글 영역
획득법
① NFT 발행
작성한 게시물을 NFT로 발행하면 일주일 동안 사용할 수 있습니다. (최초 1회)
② NFT 구매
다른 이용자의 NFT를 구매하면 한 달 동안 사용할 수 있습니다. (구매 시마다 갱신)
사용법
디시콘에서지갑연결시 바로 사용 가능합니다.