ํ์ด์ฌ(Python) - pygame ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ด์ฉํด ์ถ์ต์ [ํผ์นด์ธ ๋ฐฐ๊ตฌ๊ฒ์] ๋ง๋ค๊ธฐ
์๋ ํ์ธ์?
์ผ์ฃผ์ผ์ด ๋ ์์๊ฐ์ ์ง๋๊ฐ๊ณ ์ฃผ๋ง์ด ์์ต๋๋ค.
์ฃผ๋ง์ ๋ญ๋ค?
์ด๊ฒ์ ๊ฒ ํผ์์ ์ฐ๋นํํ ๋ญ๋ผ๋ ๋ง๋๋ ์๊ฐ์ด๋ค.๐ค
์ ๋ฒ ์ฃผ์ ์ ํ๋ฒ ๋๋์ฝ๋ฉ๋์ ์์์ ๋ณด๊ณ Pong ๊ฒ์์ ๋ง๋ค์์๋๋ฐ์.
๋ง๋ฏธ์ ๋ค์์ ํผ์นด์ธ ๋ฐฐ๊ตฌ๊ฒ์์ผ๋ก ๋์์ค๊ฒ ๋ค๊ณ ํ์์ฃ .
ํธ๊ธฐ๋กญ๊ฒ ๋งํ๊ณ ๋์ ๋ง์ ๊ตฌํํ๋ ค๋ ์ด๋ ค์์ ์กฐ๊ธ ์๊ฐ์ด ๊ฑธ๋ ธ์ต๋๋ค.
์ฌ์ง์ด ๊ต์ฅํ ์์ฑํฉ๋๋ค. ^^;;
์ฝ๋ ์ ๋ฆฌ๋ฅผ ์ข ๊น๋ํ๊ฒ ํด์ผ ํ๋๋ฐ ๊ทธ๋ฌ์ง ๋ชปํ๊ณ ์ฌ๋ฆฌ๊ฒ ๋์์ต๋๋ค.
ํน์ ๋ณด์๊ณ ์๊ฒฌ์ด ์์ผ์ ๋ถ์ ๋๊ธ ๋ฌ์์ฃผ์๋ฉด ๊ฐ์ฌ๋๋ฆฌ๊ฒ ์ต๋๋ค!!
๊ท์น
- ๊ณต์ด ์คํ ์ด์ง์ ๋ฟ์ผ๋ฉด ์๋ํธ ์ ์ ์ฆ๊ฐํ๊ณ , ์บ๋ฆญํฐ ์ ์ชฝ์์ ๋ฑ์ฅ
- ์ ์ 5 ๋๋ฉด ๊ฒ์ ์ข ๋ฃ
- ์ข/์ฐ ๋ก๋ง ์ด๋ ๊ฐ๋ฅ
๐ถ 1p : a(์ข) d(์ฐ) space(๊ฐ์)
๐ถ 2p : ←(์ข) →(์ฐ) enter(๊ฐ์)
๊ธฐ์ด ๊ณต์ฌ
- ์ด๊ธฐํ
- ๋ฐฐ๊ฒฝ, ์บ๋ฆญํฐ1, ์บ๋ฆญํฐ2, ์คํ ์ด์ง, ๊ธฐ๋ฅ ๊ทธ๋ฆฌ๊ธฐ
- ์บ๋ฆญํฐ1, ์บ๋ฆญํฐ2 ํค๋ณด๋ ์ด๋ฒคํธ
- ์บ๋ฆญํฐ1, ์บ๋ฆญํฐ2 ๊ฐ๋ก ๊ฒฝ๊ณ๊ฐ ์ฒ๋ฆฌ
- ์บ๋ฆญํฐ1, ์บ๋ฆญํฐ2 ์ธ๋ก ๊ฒฝ๊ณ๊ฐ ์ฒ๋ฆฌ
- ๊ณต ๊ทธ๋ฆฌ๊ธฐ
์ถฉ๋
- ๊ณต์ด ํ๋ฉด ์ข์ฐ์ ๋ง์ผ๋ฉด ์์ชฝ์ผ๋ก ํ๊ธฐ๋๋ก ํ๊ธฐ
- ๊ณต๊ณผ ์บ๋ฆญํฐ ์ถฉ๋ ์ ๊ณต ๋ฐ๋ ๋ฐฉํฅ์ผ๋ก ํ๊ฒจ ๋๊ฐ๋๋ก ํ๊ธฐ
- ๊ณต์ด ๋คํธ์ ๋ถ๋ชํ๋ฉด ์์ชฝ์ผ๋ก ํ๊ธฐ๊ธฐ
์ฌ์ด๋& ํฐํธ
- ๊ณต ํ๊ธธ ๋ ์๋ฆฌ
- ๊ฒ์ ๋๋ฌ์ ๋ ์๋ฆฌ
- ๊ฐ ์ ์ ํ๋ฉด ์์ ๋ฐฐ์น
- ์น๋ฆฌ ๋ฌธ๊ตฌ ๊ฐ์ด๋ฐ ๋ฐฐ์น
์ข ๋ฃ ์กฐ๊ฑด
- ์ผ์ชฝ ์นด์ดํธ 5 : 1P ์น๋ฆฌ
๋ฌธ๊ตฌ: Player1 WIN - ์ค๋ฅธ์ชฝ ์นด์ดํธ 5 : 2P ์น๋ฆฌ
๋ฌธ๊ตฌ: Player2 WIN
import pygame
import random
pygame.init() #์ด๊ธฐํ(๋ฐ๋์ ํ์)
# ํ๋ฉด ํฌ๊ธฐ ์ค์
screen_width = 800 # ๊ฐ๋ก ํฌ๊ธฐ
screen_height = 600 # ์ธ๋ก ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))
# ํ๋ฉด ํ์ดํ ์ค์
pygame.display.set_caption("ํผ์นด์ธ ๋ฐฐ๊ตฌ") # ๊ฒ์ ์ด๋ฆ
clock = pygame.time.Clock()
# ๊ฒ์ ์ฌ์ด๋
hit_sound = pygame.mixer.Sound("C:/Python/Python_Game_PikachuVolleyball/sound/hit.mp3")
win_sound = pygame.mixer.Sound("C:/Python/Python_Game_PikachuVolleyball/sound/win.mp3")
# <images ์ธํ
>
# ๋ฐฐ๊ฒฝ
background = pygame.image.load("C:/Python/Python_Game_PikachuVolleyball/images/background.jpg")
# ์คํ
์ด์ง
stage = pygame.image.load("C:/Python/Python_Game_PikachuVolleyball/images/stage.jpg")
stage_size = stage.get_rect().size
stage_height = stage_size[1]
# ๊ฐ์ด๋ฐ ๊ธฐ๋ฅ
stlit = pygame.image.load("C:/Python/Python_Game_PikachuVolleyball/images/stlit.png")
stlit_size = stlit.get_rect().size
stlit_width = stlit_size[0]
stlit_height = stlit_size[1]
stlit_x_pos = screen_width / 2 - stlit_width / 2
stlit_y_pos = screen_height - stlit_height - stage_height
# ์บ๋ฆญํฐ1
character1 = pygame.image.load("C:/Python/Python_Game_PikachuVolleyball/images/character1.png")
character1_size = character1.get_rect().size
character1_width = character1_size[0]
character1_height = character1_size[1]
character1_x_pos = screen_width / 4 - (character1_width * 2) + character1_width
character1_y_pos = screen_height - character1_height - stage_height
# ์บ๋ฆญํฐ2
character2 = pygame.image.load("C:/Python/Python_Game_PikachuVolleyball/images/character2.png")
character2_size = character2.get_rect().size
character2_width = character2_size[0]
character2_height = character2_size[1]
character2_x_pos = screen_width / 2 + character2_width * 2
character2_y_pos = screen_height - character2_height - stage_height
# ๊ณต1
ball = pygame.image.load("C:/Python/Python_Game_PikachuVolleyball/images/ball.png")
ball_size = ball.get_rect().size
ball_width = ball_size[0]
ball_height = ball_size[1]
ball_pos_x = random.randint(0, screen_width / 2 - ball_width)
ball_pos_y = 0
#๊ณต2
ball2 = pygame.image.load("C:/Python/Python_Game_PikachuVolleyball/images/ball2.png")
ball2_size = ball2.get_rect().size
ball2_width = ball2_size[0]
ball2_height = ball2_size[1]
ball2_pos_x = random.randint(stlit_x_pos, screen_width - ball2_width)
ball2_pos_y = 0
# <์ด๋ํ ์ขํ>
# ์บ๋ฆญํฐ1
character1_to_x = 0
character1_to_y = 0
# ์บ๋ฆญํฐ2
character2_to_x = 0
character2_to_y = 0
# ๊ณต
ball_to_x = 0
ball_to_y = 3
# ๊ณต2
ball2_to_x = 0
ball2_to_y = 3
# <์บ๋ฆญํฐ ์ด๋ ์๋>
character1_speed = 7
character2_speed = 7
ball_speed = 1
ball2_speed = 1
# <ํฐํธ>
game_font = pygame.font.Font(None, 100)
score1 = 0
score2 = 0
game_result = ""
# ์ด๋ฒคํธ ๋ฃจํ
running = True # ๊ฒ์ ์งํ์ค
while running:
dt = clock.tick(60)
for event in pygame.event.get(): # ์ด๋ค ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์๋๊ฐ?
if event.type == pygame.QUIT: # ์ฐฝ์ด ๋ซํ๋ ์ด๋ฒคํธ ๋ฐ์ํ์๋๊ฐ?
running = False # ๊ฒ์์ด ์งํ์ค์ด ์๋
# ํค๋ณด๋ ์ด๋ฒคํธ(๋๋ ์๋)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
character1_to_x -= character1_speed
elif event.key == pygame.K_d:
character1_to_x += character1_speed
elif event.key == pygame.K_SPACE:
character1_to_x += 20
if event.key == pygame.K_LEFT:
character2_to_x -= character2_speed
elif event.key == pygame.K_RIGHT:
character2_to_x += character2_speed
elif event.key == pygame.K_RETURN:
character2_to_x += 20
# ํค๋ณด๋ ์ด๋ฒคํธ(๋ผ์์๋)
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
character1_to_x = 0
elif event.key == pygame.K_SPACE:
character1_to_x = 0
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
character2_to_x = 0
elif event.key == pygame.K_RETURN:
character2_to_x = 0
character1_x_pos += character1_to_x
character1_y_pos += character1_to_y
character2_x_pos += character2_to_x
character2_y_pos += character2_to_y
ball_pos_y += ball_speed
ball2_pos_y += ball2_speed
# ๊ฐ๋ก ๊ฒฝ๊ณ๊ฐ
if character1_x_pos < 0:
character1_x_pos = 0
elif character1_x_pos > (screen_width / 2) - (stlit_width / 2) - character1_width:
character1_x_pos = (screen_width / 2) - (stlit_width / 2) - character1_width
if character2_x_pos > screen_width - character2_width:
character2_x_pos = screen_width - character2_width
elif character2_x_pos < screen_width / 2 + stlit_width / 2:
character2_x_pos = screen_width / 2 + stlit_width / 2
# ์ธ๋ก ๊ฒฝ๊ณ๊ฐ
if character1_y_pos < 0:
character1_y_pos = 0
elif character1_y_pos > screen_height - character1_height - stage_height:
character1_y_pos = screen_height - character1_height - stage_height
if character2_y_pos < 0:
character2_y_pos = 0
elif character2_y_pos > screen_height - character2_height - stage_height:
character2_y_pos = screen_height - character2_height - stage_height
if ball_pos_y > screen_height - ball_height - stage_height and ball_pos_x < screen_width / 2 - stlit_width / 2 :
ball_pos_y = screen_height - ball_height - stage_height
pygame.time.delay(2000)
score2 += 1
ball_pos_y = 0
ball_pos_x = screen_width / 4 - (character1_width * 2) + character1_width
ball_to_x = random.randint(0, 3)
elif ball_pos_y > screen_height - ball_height - stage_height and ball_pos_x > screen_width / 2 - stlit_width / 2 :
ball_pos_y = screen_height - ball_height - stage_height
pygame.time.delay(2000)
score1 += 1
ball_pos_y = 0
ball_pos_x = screen_width / 2 + character2_width * 2
ball_to_x = random.randint(0, 3)
if ball_pos_y < 0:
ball_pos_y = 0
ball_to_y = 5
if ball_pos_x < 0 or ball_pos_x > screen_width - ball_width or ball_pos_x > stlit_x_pos - stlit_width / 2:
ball_to_x = ball_to_x * -1
ball_pos_x += ball_to_x
if ball2_pos_x < 0 or ball2_pos_x > screen_width - ball2_width or ball2_pos_x < stlit_x_pos + stlit_width / 2:
ball2_to_x = ball2_to_x * -1
ball2_pos_x += ball2_to_x
if ball2_pos_y > screen_height - ball2_height - stage_height and ball2_pos_x < screen_width / 2 - stlit_width / 2 :
ball2_pos_y = screen_height - ball2_height - stage_height
pygame.time.delay(2000)
score2 += 1
ball2_pos_y = 0
ball2_pos_x = random.randint(0, screen_width - ball2_width)
ball2_to_x = random.randint(0, 3)
elif ball2_pos_y > screen_height - ball2_height - stage_height and ball2_pos_x > screen_width / 2 - stlit_width / 2 :
ball2_pos_y = screen_height - ball2_height - stage_height
pygame.time.delay(2000)
score1 += 1
ball2_pos_y = 0
ball2_pos_x = random.randint(0, screen_width - ball2_width)
ball2_to_x = random.randint(-3, 0)
if ball2_pos_y < 0:
ball2_pos_y = 0
ball2_to_y = 5
if score1 == 5:
game_result = "Player1 WIN"
win_sound.play()
running = False
elif score2 == 5:
game_result = "Player2 WIN"
win_sound.play()
running = False
# ์ถฉ๋ ์ฒ๋ฆฌ
character1_rect = character1.get_rect()
character1_rect.left = character1_x_pos
character1_rect.top = character1_y_pos
character2_rect = character2.get_rect()
character2_rect.left = character2_x_pos
character2_rect.top = character2_y_pos
ball_rect = ball.get_rect()
ball_rect.left = ball_pos_x
ball_rect.top = ball_pos_y
ball2_rect = ball2.get_rect()
ball2_rect.left = ball2_pos_x
ball2_rect.top = ball2_pos_y
if ball_rect.colliderect(character1_rect):
hit_sound.play()
ball_to_y = -18
ball_to_x = 6
if ball_rect.colliderect(character2_rect):
hit_sound.play()
ball_to_y = -18
ball_to_x = -6
if ball2_rect.colliderect(character1_rect):
hit_sound.play()
ball2_to_y = -25
ball2_to_x = 6
if ball2_rect.colliderect(character2_rect):
hit_sound.play()
ball2_to_y = -25
ball2_to_x = -6
ball_pos_x += ball_to_x
ball_pos_y += ball_to_y
ball2_pos_x += ball2_to_x
ball2_pos_y += ball2_to_y
game_score1 = game_font.render(str(score1), True, (0,0,255))
game_score2 = game_font.render(str(score2), True, (0,0,255))
screen.blit(background, (0, 0))
screen.blit(game_score1, (80,40))
screen.blit(game_score2, (700,40))
screen.blit(stlit, (stlit_x_pos, stlit_y_pos))
screen.blit(stage, (0,screen_height - stage_height))
screen.blit(character1, (character1_x_pos,character1_y_pos))
screen.blit(character2, (character2_x_pos,character2_y_pos))
screen.blit(ball, (ball_pos_x,ball_pos_y))
screen.blit(ball2, (ball2_pos_x,ball2_pos_y))
msg = game_font.render(game_result, True, (255,0,0))
msg_rect = msg.get_rect(center=(int(screen_width / 2), int(screen_height / 2)))
screen.blit(msg, msg_rect)
pygame.display.update()
pygame.time.delay(3000)
# pygame ์ข
๋ฃ
pygame.quit()
์ฟก...
๋ค์์๋...์ ํ๋ฅผ ๊ตฌํํ ์ ์๋๋ก ํด๋ณด๊ฒ ์ต๋๋ค....
๊ตฟ..๋ฒ ์ด..