๐Ÿ“‚ My Project

ํŒŒ์ด์ฌ(Python) - pygame ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•ด ์ถ”์–ต์˜ [ํ”ผ์นด์ธ„ ๋ฐฐ๊ตฌ๊ฒŒ์ž„] ๋งŒ๋“ค๊ธฐ

kongmi 2023. 2. 4. 16:55

์•ˆ๋…•ํ•˜์„ธ์š”?

์ผ์ฃผ์ผ์ด ๋˜ ์ˆœ์‹๊ฐ„์— ์ง€๋‚˜๊ฐ€๊ณ  ์ฃผ๋ง์ด ์™”์Šต๋‹ˆ๋‹ค.

 

์ฃผ๋ง์—” ๋ญ๋‹ค?

์ด๊ฒƒ์ €๊ฒƒ ํ˜ผ์ž์„œ ์šฐ๋‹นํƒ•ํƒ• ๋ญ๋ผ๋„ ๋งŒ๋“œ๋Š” ์‹œ๊ฐ„์ด๋‹ค.๐Ÿค“

 

์ €๋ฒˆ ์ฃผ์— ์œ ํŠœ๋ฒ„ ๋‚˜๋„์ฝ”๋”ฉ๋‹˜์˜ ์˜์ƒ์„ ๋ณด๊ณ  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()

 

์ฟก...

๋‹ค์Œ์—๋Š”...์ ํ”„๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค....

๊ตฟ..๋ฒ ์ด..