๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/Python

ํŒŒ์ด์ฌ ํ™œ์šฉํŽธ #1(๊ฒŒ์ž„ ๊ฐœ๋ฐœ)

by - ์˜คํŠธ - 2020. 8. 31.

www.youtube.com/watch?v=Dkx8Pl6QKW0&t=3s

* ์œ„ ๋งํฌ์— ์žˆ๋Š” ์˜์ƒ์„ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•˜๋ฉฐ ๊ธ€์„ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค

 

 

ํŒŒ์ด๊ฒŒ์ž„ ์„ค์น˜ํ•˜๊ธฐ : ์ฝ˜์†”์— pip install pygame ์ž…๋ ฅ

 

์•„๋ž˜ ์ฝ”๋“œ ์‹คํ–‰ ์‹œ ์ž ๊น ์ฐฝ์ด ๋–ด๋‹ค๊ฐ€ ์‚ฌ๋ผ์ง

import pygame



pygame.init() # ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•„์š”)



# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •

screen_width = 480 # ๊ฐ€๋กœ ํฌ๊ธฐ

screen_height = 640 # ์„ธ๋กœ ํฌ๊ธฐ

screen = pygame.display.set_mode((screen_width, screen_height))



# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •

pygame.display.set_caption("Nado Game") # ๊ฒŒ์ž„ ์ด๋ฆ„

 

ํ™”๋ฉด์ด ๋ฐ”๋กœ ๊บผ์ง€์ง€ ์•Š๋„๋ก(๊ธฐ๋ณธ์ ์ธ ๋ผˆ๋Œ€ ์ž‘์„ฑ)

import pygame

pygame.init() # ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•„์š”)

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 480 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 640 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption("Nado Game") # ๊ฒŒ์ž„ ์ด๋ฆ„

# ์ด๋ฒคํŠธ ๋ฃจํ”„
running = True # ๊ฒŒ์ž„์ด ์ง„ํ–‰์ค‘์ธ๊ฐ€?
while running:
    for event in pygame.event.get(): # ์–ด๋–ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋Š”๊ฐ€?
        if event.type == pygame.QUIT: # ์ฐฝ์ด ๋‹ซํžˆ๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋Š”๊ฐ€?
            running  = False # ๊ฒŒ์ž„์ด ์ง„ํ–‰์ค‘์ด ์•„๋‹˜


# pygame ์ข…๋ฃŒ
pygame.quit()

 

๋ฐฐ๊ฒฝ ์ถ”๊ฐ€ํ•˜๊ธฐ

import pygame

pygame.init() # ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•„์š”)

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 480 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 640 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption("Nado Game") # ๊ฒŒ์ž„ ์ด๋ฆ„

# ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
# background = pygame.image.load("C:\\Users\\jang\\Desktop\\PythonWorkspace\\pygame_basic\\background.png")
background = pygame.image.load("C:/Users/jang/Desktop/PythonWorkspace/pygame_basic/background.png")

# ์ด๋ฒคํŠธ ๋ฃจํ”„
running = True # ๊ฒŒ์ž„์ด ์ง„ํ–‰์ค‘์ธ๊ฐ€?
while running:
    for event in pygame.event.get(): # ์–ด๋–ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋Š”๊ฐ€?
        if event.type == pygame.QUIT: # ์ฐฝ์ด ๋‹ซํžˆ๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋Š”๊ฐ€?
            running  = False # ๊ฒŒ์ž„์ด ์ง„ํ–‰์ค‘์ด ์•„๋‹˜
    
    #screen.fill((0, 0, 255))
    screen.blit(background, (0, 0)) # ๋ฐฐ๊ฒฝ ๊ทธ๋ฆฌ๊ธฐ

    pygame.display.update() # ๊ฒŒ์ž„ํ™”๋ฉด์„ ๋‹ค์‹œ ๊ทธ๋ฆฌ๊ธฐ!
    


# pygame ์ข…๋ฃŒ
pygame.quit()

 

์บ๋ฆญํ„ฐ ์ƒ์„ฑ

import pygame

pygame.init() # ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•„์š”)

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 480 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 640 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption("Nado Game") # ๊ฒŒ์ž„ ์ด๋ฆ„

# ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
background = pygame.image.load("C:/Users/jang/Desktop/PythonWorkspace/pygame_basic/background.png")

# ์บ๋ฆญํ„ฐ(์Šคํ”„๋ผ์ดํŠธ) ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
character = pygame.image.load("C:/Users/jang/Desktop/PythonWorkspace/pygame_basic/character.png")
character_size = character.get_rect().size # ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•ด์˜ด
character_width = character_size[0] # ์บ๋ฆญํ„ฐ์˜ ๊ฐ€๋กœ ํฌ๊ธฐ
character_height = character_size[1] # ์บ๋ฆญํ„ฐ์˜ ์„ธ๋กœ ํฌ๊ธฐ
character_x_pos = (screen_width / 2) - (character_width / 2)  # ํ™”๋ฉด ๊ฐ€๋กœ์˜ ์ ˆ๋ฐ˜ ํฌ๊ธฐ์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์— ์œ„์น˜ (๊ฐ€๋กœ)
character_y_pos = screen_height - character_height # ํ™”๋ฉด ์„ธ๋กœ ํฌ๊ธฐ ๊ฐ€์žฅ ์•„๋ž˜์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์— ์œ„์น˜ (์„ธ๋กœ)


# ์ด๋ฒคํŠธ ๋ฃจํ”„
running = True # ๊ฒŒ์ž„์ด ์ง„ํ–‰์ค‘์ธ๊ฐ€?
while running:
    for event in pygame.event.get(): # ์–ด๋–ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋Š”๊ฐ€?
        if event.type == pygame.QUIT: # ์ฐฝ์ด ๋‹ซํžˆ๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋Š”๊ฐ€?
            running  = False # ๊ฒŒ์ž„์ด ์ง„ํ–‰์ค‘์ด ์•„๋‹˜
    
    
    screen.blit(background, (0, 0)) # ๋ฐฐ๊ฒฝ ๊ทธ๋ฆฌ๊ธฐ

    screen.blit(character, (character_x_pos, character_y_pos)) # ์บ๋ฆญํ„ฐ ๊ทธ๋ฆฌ๊ธฐ

    pygame.display.update() # ๊ฒŒ์ž„ํ™”๋ฉด์„ ๋‹ค์‹œ ๊ทธ๋ฆฌ๊ธฐ!
    


# pygame ์ข…๋ฃŒ
pygame.quit()

 

ํ‚ค๋ณด๋“œ๋กœ ์›€์ง์ด๊ฒŒ ๋งŒ๋“ค๊ธฐ(+ํ™”๋ฉด ๋ฒ—์–ด๋‚˜์ง€ ์•Š๋„๋ก)

import pygame

pygame.init() # ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•„์š”)

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 480 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 640 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption("Nado Game") # ๊ฒŒ์ž„ ์ด๋ฆ„

# ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
background = pygame.image.load("C:/Users/jang/Desktop/PythonWorkspace/pygame_basic/background.png")

# ์บ๋ฆญํ„ฐ(์Šคํ”„๋ผ์ดํŠธ) ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
character = pygame.image.load("C:/Users/jang/Desktop/PythonWorkspace/pygame_basic/character.png")
character_size = character.get_rect().size # ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•ด์˜ด
character_width = character_size[0] # ์บ๋ฆญํ„ฐ์˜ ๊ฐ€๋กœ ํฌ๊ธฐ
character_height = character_size[1] # ์บ๋ฆญํ„ฐ์˜ ์„ธ๋กœ ํฌ๊ธฐ
character_x_pos = (screen_width / 2) - (character_width / 2)  # ํ™”๋ฉด ๊ฐ€๋กœ์˜ ์ ˆ๋ฐ˜ ํฌ๊ธฐ์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์— ์œ„์น˜ (๊ฐ€๋กœ)
character_y_pos = screen_height - character_height # ํ™”๋ฉด ์„ธ๋กœ ํฌ๊ธฐ ๊ฐ€์žฅ ์•„๋ž˜์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์— ์œ„์น˜ (์„ธ๋กœ)

# ์ด๋™ํ•  ์ขŒํ‘œ
to_x = 0
to_y = 0

# ์ด๋ฒคํŠธ ๋ฃจํ”„
running = True # ๊ฒŒ์ž„์ด ์ง„ํ–‰์ค‘์ธ๊ฐ€?
while running:
    for event in pygame.event.get(): # ์–ด๋–ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋Š”๊ฐ€?
        if event.type == pygame.QUIT: # ์ฐฝ์ด ๋‹ซํžˆ๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋Š”๊ฐ€?
            running  = False # ๊ฒŒ์ž„์ด ์ง„ํ–‰์ค‘์ด ์•„๋‹˜
    

        if event.type == pygame.KEYDOWN: # ํ‚ค๊ฐ€ ๋ˆŒ๋Ÿฌ์กŒ๋Š”์ง€ ํ™•์ธ
            if event.key == pygame.K_LEFT: # ์บ๋ฆญํ„ฐ๋ฅผ ์™ผ์ชฝ์œผ๋กœ
                to_x -= 5 # to_x = to_x - 5
            elif event.key == pygame.K_RIGHT: # ์บ๋ฆญํ„ฐ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ
                to_x += 5
            elif event.key == pygame.K_UP: # ์บ๋ฆญํ„ฐ๋ฅผ ์œ„๋กœ
                to_y -= 5
            elif event.key == pygame.K_DOWN: # ์บ๋ฆญํ„ฐ๋ฅผ ์•„๋ž˜๋กœ
                to_y += 5


        if event.type == pygame.KEYUP: # ๋ฐฉํ–ฅํ‚ค๋ฅผ ๋–ผ๋ฉด ๋ฉˆ์ถค
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                to_x = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                to_y = 0


    character_x_pos += to_x
    character_y_pos += to_y

    # ๊ฐ€๋กœ ๊ฒฝ๊ณ„๊ฐ’ ์ฒ˜๋ฆฌ
    if character_x_pos < 0:
        character_x_pos = 0
    elif character_x_pos > screen_width - character_width:
        character_x_pos = screen_width - character_width

    # ์„ธ๋กœ ๊ฒฝ๊ณ„๊ฐ’ ์ฒ˜๋ฆฌ
    if character_y_pos < 0:
        character_y_pos = 0
    elif character_y_pos > screen_height - character_height:
        character_y_pos = screen_height - character_height



    screen.blit(background, (0, 0)) # ๋ฐฐ๊ฒฝ ๊ทธ๋ฆฌ๊ธฐ

    screen.blit(character, (character_x_pos, character_y_pos)) # ์บ๋ฆญํ„ฐ ๊ทธ๋ฆฌ๊ธฐ

    pygame.display.update() # ๊ฒŒ์ž„ํ™”๋ฉด์„ ๋‹ค์‹œ ๊ทธ๋ฆฌ๊ธฐ!
    


# pygame ์ข…๋ฃŒ
pygame.quit()

40:00"


FPS(frame per second)

import pygame

pygame.init() # ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•„์š”)

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 480 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 640 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption("Nado Game") # ๊ฒŒ์ž„ ์ด๋ฆ„

# FPS
clock = pygame.time.Clock()

# ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
background = pygame.image.load("C:/Users/jang/Desktop/PythonWorkspace/pygame_basic/background.png")

# ์บ๋ฆญํ„ฐ(์Šคํ”„๋ผ์ดํŠธ) ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
character = pygame.image.load("C:/Users/jang/Desktop/PythonWorkspace/pygame_basic/character.png")
character_size = character.get_rect().size # ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•ด์˜ด
character_width = character_size[0] # ์บ๋ฆญํ„ฐ์˜ ๊ฐ€๋กœ ํฌ๊ธฐ
character_height = character_size[1] # ์บ๋ฆญํ„ฐ์˜ ์„ธ๋กœ ํฌ๊ธฐ
character_x_pos = (screen_width / 2) - (character_width / 2)  # ํ™”๋ฉด ๊ฐ€๋กœ์˜ ์ ˆ๋ฐ˜ ํฌ๊ธฐ์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์— ์œ„์น˜ (๊ฐ€๋กœ)
character_y_pos = screen_height - character_height # ํ™”๋ฉด ์„ธ๋กœ ํฌ๊ธฐ ๊ฐ€์žฅ ์•„๋ž˜์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์— ์œ„์น˜ (์„ธ๋กœ)

# ์ด๋™ํ•  ์ขŒํ‘œ
to_x = 0
to_y = 0

# ์ด๋™ ์†๋„
character_speed = 0.6

# ์ด๋ฒคํŠธ ๋ฃจํ”„
running = True # ๊ฒŒ์ž„์ด ์ง„ํ–‰์ค‘์ธ๊ฐ€?
while running:
    dt = clock.tick(10) # ๊ฒŒ์ž„ํ™”๋ฉด์˜ ์ดˆ๋‹น ํ”„๋ ˆ์ž„ ์ˆ˜๋ฅผ ์„ค์ •

# ์บ๋ฆญํ„ฐ๊ฐ€ 100 ๋งŒํผ ์ด๋™์„ ํ•ด์•ผํ•จ
# 10 fps : 1์ดˆ ๋™์•ˆ์— 10๋ฒˆ ๋™์ž‘ -> 1๋ฒˆ์— ๋ช‡ ๋งŒํผ ์ด๋™? 10๋งŒํผ! 10 * 10 = 100
# 20 fps : 1์ดˆ ๋™์•ˆ์— 20๋ฒˆ ๋™์ž‘ -> 1๋ฒˆ์— 5๋งŒํผ! 5 * 20 = 100

    # print("fps : " + str(clock.get_fps()))

    for event in pygame.event.get(): # ์–ด๋–ค ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋Š”๊ฐ€?
        if event.type == pygame.QUIT: # ์ฐฝ์ด ๋‹ซํžˆ๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋Š”๊ฐ€?
            running  = False # ๊ฒŒ์ž„์ด ์ง„ํ–‰์ค‘์ด ์•„๋‹˜
    

        if event.type == pygame.KEYDOWN: # ํ‚ค๊ฐ€ ๋ˆŒ๋Ÿฌ์กŒ๋Š”์ง€ ํ™•์ธ
            if event.key == pygame.K_LEFT: # ์บ๋ฆญํ„ฐ๋ฅผ ์™ผ์ชฝ์œผ๋กœ
                to_x -= character_speed # to_x = to_x - 5
            elif event.key == pygame.K_RIGHT: # ์บ๋ฆญํ„ฐ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ
                to_x += character_speed
            elif event.key == pygame.K_UP: # ์บ๋ฆญํ„ฐ๋ฅผ ์œ„๋กœ
                to_y -= character_speed
            elif event.key == pygame.K_DOWN: # ์บ๋ฆญํ„ฐ๋ฅผ ์•„๋ž˜๋กœ
                to_y += character_speed


        if event.type == pygame.KEYUP: # ๋ฐฉํ–ฅํ‚ค๋ฅผ ๋–ผ๋ฉด ๋ฉˆ์ถค
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                to_x = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                to_y = 0


    character_x_pos += to_x * dt
    character_y_pos += to_y * dt

    # ๊ฐ€๋กœ ๊ฒฝ๊ณ„๊ฐ’ ์ฒ˜๋ฆฌ
    if character_x_pos < 0:
        character_x_pos = 0
    elif character_x_pos > screen_width - character_width:
        character_x_pos = screen_width - character_width

    # ์„ธ๋กœ ๊ฒฝ๊ณ„๊ฐ’ ์ฒ˜๋ฆฌ
    if character_y_pos < 0:
        character_y_pos = 0
    elif character_y_pos > screen_height - character_height:
        character_y_pos = screen_height - character_height



    screen.blit(background, (0, 0)) # ๋ฐฐ๊ฒฝ ๊ทธ๋ฆฌ๊ธฐ

    screen.blit(character, (character_x_pos, character_y_pos)) # ์บ๋ฆญํ„ฐ ๊ทธ๋ฆฌ๊ธฐ

    pygame.display.update() # ๊ฒŒ์ž„ํ™”๋ฉด์„ ๋‹ค์‹œ ๊ทธ๋ฆฌ๊ธฐ!
    


# pygame ์ข…๋ฃŒ
pygame.quit()

 

์ถฉ๋Œ์ฒ˜๋ฆฌ

import pygame

pygame.init() # ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•„์š”)

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 480 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 640 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption("Nado Game") # ๊ฒŒ์ž„ ์ด๋ฆ„

# FPS
clock = pygame.time.Clock()

# ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
background = pygame.image.load("C:/Users/jang/Desktop/PythonWorkspace/pygame_basic/background.png")

# ์บ๋ฆญํ„ฐ(์Šคํ”„๋ผ์ดํŠธ) ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
character = pygame.image.load("C:/Users/jang/Desktop/PythonWorkspace/pygame_basic/character.png")
character_size = character.get_rect().size # ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•ด์˜ด
character_width = character_size[0] # ์บ๋ฆญํ„ฐ์˜ ๊ฐ€๋กœ ํฌ๊ธฐ
character_height = character_size[1] # ์บ๋ฆญํ„ฐ์˜ ์„ธ๋กœ ํฌ๊ธฐ
character_x_pos = (screen_width / 2) - (character_width / 2)  # ํ™”๋ฉด ๊ฐ€๋กœ์˜ ์ ˆ๋ฐ˜ ํฌ๊ธฐ์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์— ์œ„์น˜ (๊ฐ€๋กœ)
character_y_pos = screen_height - character_height # ํ™”๋ฉด ์„ธ๋กœ ํฌ๊ธฐ ๊ฐ€์žฅ ์•„๋ž˜์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์— ์œ„์น˜ (์„ธ๋กœ)

# ์ด๋™ํ•  ์ขŒํ‘œ
to_x = 0
to_y = 0

# ์ด๋™ ์†๋„
character_speed = 0.6

# ์  enemy ์บ๋ฆญํ„ฐ
enemy = pygame.image.load("C:/Users/jang/Desktop/PythonWorkspace/pygame_basic/enemy.png")
enemy_size = enemy.get_rect().size # ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•ด์˜ด
enemy_width = enemy_size[0] # ์บ๋ฆญํ„ฐ์˜ ๊ฐ€๋กœ ํฌ๊ธฐ
enemy_height = enemy_size[1] # ์บ๋ฆญํ„ฐ์˜ ์„ธ๋กœ ํฌ๊ธฐ
enemy_x_pos = (screen_width / 2) - (enemy_width / 2)  # ํ™”๋ฉด ๊ฐ€๋กœ์˜ ์ ˆ๋ฐ˜ ํฌ๊ธฐ์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์— ์œ„์น˜ (๊ฐ€๋กœ)
enemy_y_pos = (screen_height / 2) - (enemy_height / 2) # ํ™”๋ฉด ์„ธ๋กœ ํฌ๊ธฐ ๊ฐ€์žฅ ์•„๋ž˜์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์— ์œ„์น˜ (์„ธ๋กœ)



# ์ด๋ฒคํŠธ ๋ฃจํ”„
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_LEFT: # ์บ๋ฆญํ„ฐ๋ฅผ ์™ผ์ชฝ์œผ๋กœ
                to_x -= character_speed # to_x = to_x - 5
            elif event.key == pygame.K_RIGHT: # ์บ๋ฆญํ„ฐ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ
                to_x += character_speed
            elif event.key == pygame.K_UP: # ์บ๋ฆญํ„ฐ๋ฅผ ์œ„๋กœ
                to_y -= character_speed
            elif event.key == pygame.K_DOWN: # ์บ๋ฆญํ„ฐ๋ฅผ ์•„๋ž˜๋กœ
                to_y += character_speed


        if event.type == pygame.KEYUP: # ๋ฐฉํ–ฅํ‚ค๋ฅผ ๋–ผ๋ฉด ๋ฉˆ์ถค
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                to_x = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                to_y = 0


    character_x_pos += to_x * dt
    character_y_pos += to_y * dt

    # ๊ฐ€๋กœ ๊ฒฝ๊ณ„๊ฐ’ ์ฒ˜๋ฆฌ
    if character_x_pos < 0:
        character_x_pos = 0
    elif character_x_pos > screen_width - character_width:
        character_x_pos = screen_width - character_width

    # ์„ธ๋กœ ๊ฒฝ๊ณ„๊ฐ’ ์ฒ˜๋ฆฌ
    if character_y_pos < 0:
        character_y_pos = 0
    elif character_y_pos > screen_height - character_height:
        character_y_pos = screen_height - character_height

    # ์ถฉ๋Œ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ rect ์ •๋ณด ์—…๋ฐ์ดํŠธ
    character_rect = character.get_rect()
    character_rect.left = character_x_pos
    character_rect.top = character_y_pos

    enemy_rect = enemy.get_rect()
    enemy_rect.left = enemy_x_pos
    enemy_rect.top = enemy_y_pos

    # ์ถฉ๋Œ ์ฒดํฌ
    if character_rect.colliderect(enemy_rect):
        print("์ถฉ๋Œํ–ˆ์–ด์š”")
        running = False


    screen.blit(background, (0, 0)) # ๋ฐฐ๊ฒฝ ๊ทธ๋ฆฌ๊ธฐ
    screen.blit(character, (character_x_pos, character_y_pos)) # ์บ๋ฆญํ„ฐ ๊ทธ๋ฆฌ๊ธฐ
    screen.blit(enemy, (enemy_x_pos, enemy_y_pos)) # ์  ๊ทธ๋ฆฌ๊ธฐ

    pygame.display.update() # ๊ฒŒ์ž„ํ™”๋ฉด์„ ๋‹ค์‹œ ๊ทธ๋ฆฌ๊ธฐ!
    


# pygame ์ข…๋ฃŒ
pygame.quit()

 

๊ธ€์ž ๋„ฃ์–ด๋ณด๊ธฐ(์‹œ๊ฐ„ ์ œํ•œ)

import pygame

pygame.init() # ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•„์š”)

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 480 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 640 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption("Nado Game") # ๊ฒŒ์ž„ ์ด๋ฆ„

# FPS
clock = pygame.time.Clock()

# ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
background = pygame.image.load("C:/Users/jang/Desktop/PythonWorkspace/pygame_basic/background.png")

# ์บ๋ฆญํ„ฐ(์Šคํ”„๋ผ์ดํŠธ) ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
character = pygame.image.load("C:/Users/jang/Desktop/PythonWorkspace/pygame_basic/character.png")
character_size = character.get_rect().size # ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•ด์˜ด
character_width = character_size[0] # ์บ๋ฆญํ„ฐ์˜ ๊ฐ€๋กœ ํฌ๊ธฐ
character_height = character_size[1] # ์บ๋ฆญํ„ฐ์˜ ์„ธ๋กœ ํฌ๊ธฐ
character_x_pos = (screen_width / 2) - (character_width / 2)  # ํ™”๋ฉด ๊ฐ€๋กœ์˜ ์ ˆ๋ฐ˜ ํฌ๊ธฐ์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์— ์œ„์น˜ (๊ฐ€๋กœ)
character_y_pos = screen_height - character_height # ํ™”๋ฉด ์„ธ๋กœ ํฌ๊ธฐ ๊ฐ€์žฅ ์•„๋ž˜์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์— ์œ„์น˜ (์„ธ๋กœ)

# ์ด๋™ํ•  ์ขŒํ‘œ
to_x = 0
to_y = 0

# ์ด๋™ ์†๋„
character_speed = 0.6

# ์  enemy ์บ๋ฆญํ„ฐ
enemy = pygame.image.load("C:/Users/jang/Desktop/PythonWorkspace/pygame_basic/enemy.png")
enemy_size = enemy.get_rect().size # ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ๋ฅผ ๊ตฌํ•ด์˜ด
enemy_width = enemy_size[0] # ์บ๋ฆญํ„ฐ์˜ ๊ฐ€๋กœ ํฌ๊ธฐ
enemy_height = enemy_size[1] # ์บ๋ฆญํ„ฐ์˜ ์„ธ๋กœ ํฌ๊ธฐ
enemy_x_pos = (screen_width / 2) - (enemy_width / 2)  # ํ™”๋ฉด ๊ฐ€๋กœ์˜ ์ ˆ๋ฐ˜ ํฌ๊ธฐ์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์— ์œ„์น˜ (๊ฐ€๋กœ)
enemy_y_pos = (screen_height / 2) - (enemy_height / 2) # ํ™”๋ฉด ์„ธ๋กœ ํฌ๊ธฐ ๊ฐ€์žฅ ์•„๋ž˜์— ํ•ด๋‹นํ•˜๋Š” ๊ณณ์— ์œ„์น˜ (์„ธ๋กœ)


# ํฐํŠธ ์ •์˜
game_font = pygame.font.Font(None, 40) # ํฐํŠธ ๊ฐ์ฒด ์ƒ์„ฑ (ํฐํŠธ, ํฌ๊ธฐ)

# ์ด ์‹œ๊ฐ„
total_time = 10

# ์‹œ์ž‘ ์‹œ๊ฐ„ 
start_ticks = pygame.time.get_ticks() # ์‹œ์ž‘ tick ์„ ๋ฐ›์•„์˜ด

# ์ด๋ฒคํŠธ ๋ฃจํ”„
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_LEFT: # ์บ๋ฆญํ„ฐ๋ฅผ ์™ผ์ชฝ์œผ๋กœ
                to_x -= character_speed # to_x = to_x - 5
            elif event.key == pygame.K_RIGHT: # ์บ๋ฆญํ„ฐ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ
                to_x += character_speed
            elif event.key == pygame.K_UP: # ์บ๋ฆญํ„ฐ๋ฅผ ์œ„๋กœ
                to_y -= character_speed
            elif event.key == pygame.K_DOWN: # ์บ๋ฆญํ„ฐ๋ฅผ ์•„๋ž˜๋กœ
                to_y += character_speed


        if event.type == pygame.KEYUP: # ๋ฐฉํ–ฅํ‚ค๋ฅผ ๋–ผ๋ฉด ๋ฉˆ์ถค
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                to_x = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                to_y = 0


    character_x_pos += to_x * dt
    character_y_pos += to_y * dt

    # ๊ฐ€๋กœ ๊ฒฝ๊ณ„๊ฐ’ ์ฒ˜๋ฆฌ
    if character_x_pos < 0:
        character_x_pos = 0
    elif character_x_pos > screen_width - character_width:
        character_x_pos = screen_width - character_width

    # ์„ธ๋กœ ๊ฒฝ๊ณ„๊ฐ’ ์ฒ˜๋ฆฌ
    if character_y_pos < 0:
        character_y_pos = 0
    elif character_y_pos > screen_height - character_height:
        character_y_pos = screen_height - character_height

    # ์ถฉ๋Œ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ rect ์ •๋ณด ์—…๋ฐ์ดํŠธ
    character_rect = character.get_rect()
    character_rect.left = character_x_pos
    character_rect.top = character_y_pos

    enemy_rect = enemy.get_rect()
    enemy_rect.left = enemy_x_pos
    enemy_rect.top = enemy_y_pos

    # ์ถฉ๋Œ ์ฒดํฌ
    if character_rect.colliderect(enemy_rect):
        print("์ถฉ๋Œํ–ˆ์–ด์š”")
        running = False


    screen.blit(background, (0, 0)) # ๋ฐฐ๊ฒฝ ๊ทธ๋ฆฌ๊ธฐ
    screen.blit(character, (character_x_pos, character_y_pos)) # ์บ๋ฆญํ„ฐ ๊ทธ๋ฆฌ๊ธฐ
    screen.blit(enemy, (enemy_x_pos, enemy_y_pos)) # ์  ๊ทธ๋ฆฌ๊ธฐ

    # ํƒ€์ด๋จธ ์ง‘์–ด ๋„ฃ๊ธฐ
    # ๊ฒฝ๊ณผ ์‹œ๊ฐ„ ๊ณ„์‚ฐ
    elapsed_time = (pygame.time.get_ticks() - start_ticks) / 1000
    # ๊ฒฝ๊ณผ ์‹œ๊ฐ„(ms)์„ 1000์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์ดˆ(s) ๋‹จ์œ„๋กœ ํ‘œ์‹œ

    timer = game_font.render(str(int(total_time - elapsed_time)), True, (255, 255, 255))
    # ์ถœ๋ ฅํ•  ๊ธ€์ž, True, ๊ธ€์ž ์ƒ‰์ƒ
    screen.blit(timer, (10, 10))

    # ๋งŒ์•ฝ ์‹œ๊ฐ„์ด 0 ์ดํ•˜์ด๋ฉด ๊ฒŒ์ž„ ์ข…๋ฃŒ
    if total_time - elapsed_time <= 0:
        print("ํƒ€์ž„์•„์›ƒ")
        running = False


    pygame.display.update() # ๊ฒŒ์ž„ํ™”๋ฉด์„ ๋‹ค์‹œ ๊ทธ๋ฆฌ๊ธฐ!
    
# ์ž ์‹œ ๋Œ€๊ธฐ
pygame.time.delay(2000) # 2์ดˆ ์ •๋„ ๋Œ€๊ธฐ (ms)

# pygame ์ข…๋ฃŒ
pygame.quit()

 

๊ฒŒ์ž„ ๊ฐœ๋ฐœ ํ”„๋ ˆ์ž„

import pygame
#######################################################################
# ๊ธฐ๋ณธ ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค)
pygame.init() 

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 480 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 640 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption("๊ฒŒ์ž„ ์ด๋ฆ„")

# FPS
clock = pygame.time.Clock()
#######################################################################

# 1. ์‚ฌ์šฉ์ž ๊ฒŒ์ž„ ์ดˆ๊ธฐํ™” (๋ฐฐ๊ฒฝ ํ™”๋ฉด, ๊ฒŒ์ž„ ์ด๋ฏธ์ง€, ์ขŒํ‘œ, ์†๋„, ํฐํŠธ ๋“ฑ)

running = True 
while running:
    dt = clock.tick(30)

    # 2. ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ (ํ‚ค๋ณด๋“œ, ๋งˆ์šฐ์Šค ๋“ฑ)
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            running  = False 
    
    # 3. ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ ์œ„์น˜ ์ •์˜
    
    # 4. ์ถฉ๋Œ ์ฒ˜๋ฆฌ

    # 5. ํ™”๋ฉด์— ๊ทธ๋ฆฌ๊ธฐ


    pygame.display.update() 
    
    
pygame.quit()

 

ํ€ด์ฆˆ

import random
import pygame
#######################################################################
# ๊ธฐ๋ณธ ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค)
pygame.init() 

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 480 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 640 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption("Quiz")

# FPS
clock = pygame.time.Clock()
#######################################################################

# 1. ์‚ฌ์šฉ์ž ๊ฒŒ์ž„ ์ดˆ๊ธฐํ™” (๋ฐฐ๊ฒฝ ํ™”๋ฉด, ๊ฒŒ์ž„ ์ด๋ฏธ์ง€, ์ขŒํ‘œ, ์†๋„, ํฐํŠธ ๋“ฑ)
# ๋ฐฐ๊ฒฝ ๋งŒ๋“ค๊ธฐ
background = pygame.image.load("C:\\Users\\jang\\Desktop\\PythonWorkspace\\pygame_basic\\background.png")

# ์บ๋ฆญํ„ฐ ๋งŒ๋“ค๊ธฐ
character = pygame.image.load("C:\\Users\\jang\\Desktop\\PythonWorkspace\\pygame_basic\\character.png")
character_size = character.get_rect().size
character_width = character_size[0]
character_height = character_size[1]
character_x_pos = (screen_width / 2) - (character_width / 2)
character_y_pos = screen_height - character_height

# ์ด๋™ ์œ„์น˜
to_x = 0
character_speed = 10

# ๋˜ฅ ๋งŒ๋“ค๊ธฐ
ddong = pygame.image.load("C:\\Users\\jang\\Desktop\\PythonWorkspace\\pygame_basic\\enemy.png")
ddong_size = ddong.get_rect().size
ddong_width = ddong_size[0]
ddong_height = ddong_size[1]
ddong_x_pos = random.randint(0, screen_width - ddong_width)
ddong_y_pos = 0
ddong_speed = 10


running = True 
while running:
    dt = clock.tick(30)

    # 2. ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ (ํ‚ค๋ณด๋“œ, ๋งˆ์šฐ์Šค ๋“ฑ)
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            running  = False 
    
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                to_x -= character_speed
            elif event.key == pygame.K_RIGHT:
                to_x += character_speed

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                to_x = 0


    # 3. ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ ์œ„์น˜ ์ •์˜
    character_x_pos += to_x

    if character_x_pos < 0:
        character_x_pos = 0
    elif character_x_pos > screen_width - character_width:
        character_x_pos = screen_width - character_width

    ddong_y_pos += ddong_speed

    if ddong_y_pos > screen_height:
        ddong_y_pos = 0
        ddong_x_pos = random.randint(0, screen_width - ddong_width)

    # 4. ์ถฉ๋Œ ์ฒ˜๋ฆฌ
    character_rect = character.get_rect()
    character_rect.left = character_x_pos
    character_rect.top = character_y_pos

    ddong_rect = ddong.get_rect()
    ddong_rect.left = ddong_x_pos
    ddong_rect.top = ddong_y_pos

    if character_rect.colliderect(ddong_rect):
        print("์ถฉ๋Œํ–ˆ์–ด์š”")
        running = False


    # 5. ํ™”๋ฉด์— ๊ทธ๋ฆฌ๊ธฐ
    screen.blit(background, (0, 0))
    screen.blit(character, (character_x_pos, character_y_pos))
    screen.blit(ddong, (ddong_x_pos, ddong_y_pos))


    pygame.display.update() 
    
    
pygame.quit()

 

1:22:30"


ํ”„๋กœ์ ํŠธ

1_frame_background_stage_character.py

(ํ”„๋ ˆ์ž„, ๋ฐฐ๊ฒฝ๊ณผ ๋ฌด๋Œ€, ์บ๋ฆญํ„ฐ ์„ค์ •)

import os
import pygame
#######################################################################
# ๊ธฐ๋ณธ ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค)
pygame.init() 

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 640 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 480 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption("Nado Pang")

# FPS
clock = pygame.time.Clock()
#######################################################################

# 1. ์‚ฌ์šฉ์ž ๊ฒŒ์ž„ ์ดˆ๊ธฐํ™” (๋ฐฐ๊ฒฝ ํ™”๋ฉด, ๊ฒŒ์ž„ ์ด๋ฏธ์ง€, ์ขŒํ‘œ, ์†๋„, ํฐํŠธ ๋“ฑ)
current_path = os.path.dirname(__file__) # ํ˜„์žฌ ํŒŒ์ผ์˜ ์œ„์น˜ ๋ฐ˜ํ™˜
image_path = os.path.join(current_path, "images") # images ํด๋” ์œ„์น˜ ๋ฐ˜ํ™˜

# ๋ฐฐ๊ฒฝ ๋งŒ๋“ค๊ธฐ
background = pygame.image.load(os.path.join(image_path, "background.png"))

# ์Šคํ…Œ์ด์ง€ ๋งŒ๋“ค๊ธฐ
stage = pygame.image.load(os.path.join(image_path, "stage.png"))
stage_size = stage.get_rect().size
stage_height = stage_size[1] # ์Šคํ…Œ์ด์ง€ ๋†’์ด ์œ„์— ์บ๋ฆญํ„ฐ๋ฅผ ๋‘๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

# ์บ๋ฆญํ„ฐ ๋งŒ๋“ค๊ธฐ
character = pygame.image.load(os.path.join(image_path, "character.png"))
character_size = character.get_rect().size
character_width = character_size[0]
character_height = character_size[1]
character_x_pos = (screen_width / 2) - (character_width / 2)
character_y_pos = screen_height - character_height - stage_height

running = True 
while running:
    dt = clock.tick(30)

    # 2. ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ (ํ‚ค๋ณด๋“œ, ๋งˆ์šฐ์Šค ๋“ฑ)
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            running  = False 
    
    # 3. ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ ์œ„์น˜ ์ •์˜
    
    # 4. ์ถฉ๋Œ ์ฒ˜๋ฆฌ

    # 5. ํ™”๋ฉด์— ๊ทธ๋ฆฌ๊ธฐ
    screen.blit(background, (0, 0))
    screen.blit(stage, (0, screen_height - stage_height))
    screen.blit(character, (character_x_pos, character_y_pos))

    pygame.display.update() 
    
    
pygame.quit()

 

2_weapon_keyevent.py

(๋ฌด๊ธฐ ์ƒ์„ฑ)

import os
import pygame
#######################################################################
# ๊ธฐ๋ณธ ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค)
pygame.init() 

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 640 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 480 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption("Nado Pang")

# FPS
clock = pygame.time.Clock()
#######################################################################

# 1. ์‚ฌ์šฉ์ž ๊ฒŒ์ž„ ์ดˆ๊ธฐํ™” (๋ฐฐ๊ฒฝ ํ™”๋ฉด, ๊ฒŒ์ž„ ์ด๋ฏธ์ง€, ์ขŒํ‘œ, ์†๋„, ํฐํŠธ ๋“ฑ)
current_path = os.path.dirname(__file__) # ํ˜„์žฌ ํŒŒ์ผ์˜ ์œ„์น˜ ๋ฐ˜ํ™˜
image_path = os.path.join(current_path, "images") # images ํด๋” ์œ„์น˜ ๋ฐ˜ํ™˜

# ๋ฐฐ๊ฒฝ ๋งŒ๋“ค๊ธฐ
background = pygame.image.load(os.path.join(image_path, "background.png"))

# ์Šคํ…Œ์ด์ง€ ๋งŒ๋“ค๊ธฐ
stage = pygame.image.load(os.path.join(image_path, "stage.png"))
stage_size = stage.get_rect().size
stage_height = stage_size[1] # ์Šคํ…Œ์ด์ง€ ๋†’์ด ์œ„์— ์บ๋ฆญํ„ฐ๋ฅผ ๋‘๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

# ์บ๋ฆญํ„ฐ ๋งŒ๋“ค๊ธฐ
character = pygame.image.load(os.path.join(image_path, "character.png"))
character_size = character.get_rect().size
character_width = character_size[0]
character_height = character_size[1]
character_x_pos = (screen_width / 2) - (character_width / 2)
character_y_pos = screen_height - character_height - stage_height

# ์บ๋ฆญํ„ฐ ์ด๋™ ๋ฐฉํ–ฅ
character_to_x = 0

# ์บ๋ฆญํ„ฐ ์ด๋™ ์†๋„
character_speed = 5

# ๋ฌด๊ธฐ ๋งŒ๋“ค๊ธฐ
weapon = pygame.image.load(os.path.join(image_path, "weapon.png"))
weapon_size = weapon.get_rect().size
weapon_width = weapon_size[0]

# ๋ฌด๊ธฐ๋Š” ํ•œ ๋ฒˆ์— ์—ฌ๋Ÿฌ ๋ฐœ ๋ฐœ์‚ฌ ๊ฐ€๋Šฅ
weapons = []

# ๋ฌด๊ธฐ ์ด๋™ ์†๋„
weapon_speed = 10

running = True 
while running:
    dt = clock.tick(30)

    # 2. ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ (ํ‚ค๋ณด๋“œ, ๋งˆ์šฐ์Šค ๋“ฑ)
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            running  = False 
        
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT: # ์บ๋ฆญํ„ฐ๋ฅผ ์™ผ์ชฝ์œผ๋กœ
                character_to_x -= character_speed
            elif event.key == pygame.K_RIGHT: # ์บ๋ฆญํ„ฐ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ
                character_to_x += character_speed
            elif event.key == pygame.K_SPACE: # ๋ฌด๊ธฐ ๋ฐœ์‚ฌ
                weapon_x_pos = character_x_pos + (character_width / 2) - (weapon_width / 2)
                weapon_y_pos = character_y_pos
                weapons.append([weapon_x_pos, weapon_y_pos])

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                character_to_x = 0
    
    # 3. ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ ์œ„์น˜ ์ •์˜
    character_x_pos += character_to_x

    if character_x_pos < 0:
        character_x_pos = 0
    elif character_x_pos > screen_width - character_width:
        character_x_pos = screen_width - character_width

    # ๋ฌด๊ธฐ ์œ„์น˜ ์กฐ์ •
    # 100, 200 -> 180, 160, 140, ...
    # 500, 200 -> 180, 160, 140, ...
    weapons = [ [w[0], w[1] - weapon_speed] for w in weapons] # ๋ฌด๊ธฐ ์œ„์น˜๋ฅผ ์œ„๋กœ 

    # ์ฒœ์žฅ์— ๋‹ฟ์€ ๋ฌด๊ธฐ ์—†์• ๊ธฐ
    weapons = [ [w[0], w[1]] for w in weapons if w[1] > 0]
    
    
    # 4. ์ถฉ๋Œ ์ฒ˜๋ฆฌ

    # 5. ํ™”๋ฉด์— ๊ทธ๋ฆฌ๊ธฐ
    screen.blit(background, (0, 0))

    for weapon_x_pos, weapon_y_pos in weapons:
        screen.blit(weapon, (weapon_x_pos, weapon_y_pos))

    screen.blit(stage, (0, screen_height - stage_height))
    screen.blit(character, (character_x_pos, character_y_pos))

    pygame.display.update() 
    
    
pygame.quit()

 

3_ball_movement.py

(๊ณต์˜ ์›€์ง์ž„ ์„ค์ •)

import os
import pygame
#######################################################################
# ๊ธฐ๋ณธ ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค)
pygame.init() 

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 640 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 480 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption("Nado Pang")

# FPS
clock = pygame.time.Clock()
#######################################################################

# 1. ์‚ฌ์šฉ์ž ๊ฒŒ์ž„ ์ดˆ๊ธฐํ™” (๋ฐฐ๊ฒฝ ํ™”๋ฉด, ๊ฒŒ์ž„ ์ด๋ฏธ์ง€, ์ขŒํ‘œ, ์†๋„, ํฐํŠธ ๋“ฑ)
current_path = os.path.dirname(__file__) # ํ˜„์žฌ ํŒŒ์ผ์˜ ์œ„์น˜ ๋ฐ˜ํ™˜
image_path = os.path.join(current_path, "images") # images ํด๋” ์œ„์น˜ ๋ฐ˜ํ™˜

# ๋ฐฐ๊ฒฝ ๋งŒ๋“ค๊ธฐ
background = pygame.image.load(os.path.join(image_path, "background.png"))

# ์Šคํ…Œ์ด์ง€ ๋งŒ๋“ค๊ธฐ
stage = pygame.image.load(os.path.join(image_path, "stage.png"))
stage_size = stage.get_rect().size
stage_height = stage_size[1] # ์Šคํ…Œ์ด์ง€ ๋†’์ด ์œ„์— ์บ๋ฆญํ„ฐ๋ฅผ ๋‘๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

# ์บ๋ฆญํ„ฐ ๋งŒ๋“ค๊ธฐ
character = pygame.image.load(os.path.join(image_path, "character.png"))
character_size = character.get_rect().size
character_width = character_size[0]
character_height = character_size[1]
character_x_pos = (screen_width / 2) - (character_width / 2)
character_y_pos = screen_height - character_height - stage_height

# ์บ๋ฆญํ„ฐ ์ด๋™ ๋ฐฉํ–ฅ
character_to_x = 0

# ์บ๋ฆญํ„ฐ ์ด๋™ ์†๋„
character_speed = 5

# ๋ฌด๊ธฐ ๋งŒ๋“ค๊ธฐ
weapon = pygame.image.load(os.path.join(image_path, "weapon.png"))
weapon_size = weapon.get_rect().size
weapon_width = weapon_size[0]

# ๋ฌด๊ธฐ๋Š” ํ•œ ๋ฒˆ์— ์—ฌ๋Ÿฌ ๋ฐœ ๋ฐœ์‚ฌ ๊ฐ€๋Šฅ
weapons = []

# ๋ฌด๊ธฐ ์ด๋™ ์†๋„
weapon_speed = 10

# ๊ณต ๋งŒ๋“ค๊ธฐ (4๊ฐœ ํฌ๊ธฐ์— ๋Œ€ํ•ด ๋”ฐ๋กœ ์ฒ˜๋ฆฌ)
ball_images = [
    pygame.image.load(os.path.join(image_path, "balloon1.png")),
    pygame.image.load(os.path.join(image_path, "balloon2.png")),
    pygame.image.load(os.path.join(image_path, "balloon3.png")),
    pygame.image.load(os.path.join(image_path, "balloon4.png"))]

# ๊ณต ํฌ๊ธฐ์— ๋”ฐ๋ฅธ ์ตœ์ดˆ ์Šคํ”ผ๋“œ
ball_speed_y = [-18, -15, -12, -9] # index 0, 1, 2, 3 ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’

# ๊ณต๋“ค
balls = []
 
# ์ตœ์ดˆ ๋ฐœ์ƒํ•˜๋Š” ํฐ ๊ณต ์ถ”๊ฐ€
balls.append({
    "pos_x" : 50, # ๊ณต์˜ x ์ขŒํ‘œ
    "pos_y" : 50, # ๊ณต์˜ y ์ขŒํ‘œ
    "img_idx" : 0, # ๊ณต์˜ ์ด๋ฏธ์ง€ ์ธ๋ฑ์Šค
    "to_x" : 3, # x์ถ• ์ด๋™๋ฐฉํ–ฅ, -3 ์ด๋ฉด ์™ผ์ชฝ์œผ๋กœ, 3 ์ด๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ
    "to_y" : -6, # y์ถ• ์ด๋™๋ฐฉํ–ฅ,
    "init_spd_y" : ball_speed_y[0]}) # y ์ตœ์ดˆ ์†๋„


running = True 
while running:
    dt = clock.tick(30)

    # 2. ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ (ํ‚ค๋ณด๋“œ, ๋งˆ์šฐ์Šค ๋“ฑ)
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            running  = False 
        
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT: # ์บ๋ฆญํ„ฐ๋ฅผ ์™ผ์ชฝ์œผ๋กœ
                character_to_x -= character_speed
            elif event.key == pygame.K_RIGHT: # ์บ๋ฆญํ„ฐ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ
                character_to_x += character_speed
            elif event.key == pygame.K_SPACE: # ๋ฌด๊ธฐ ๋ฐœ์‚ฌ
                weapon_x_pos = character_x_pos + (character_width / 2) - (weapon_width / 2)
                weapon_y_pos = character_y_pos
                weapons.append([weapon_x_pos, weapon_y_pos])

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                character_to_x = 0
    
    # 3. ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ ์œ„์น˜ ์ •์˜
    character_x_pos += character_to_x

    if character_x_pos < 0:
        character_x_pos = 0
    elif character_x_pos > screen_width - character_width:
        character_x_pos = screen_width - character_width

    # ๋ฌด๊ธฐ ์œ„์น˜ ์กฐ์ •
    # 100, 200 -> 180, 160, 140, ...
    # 500, 200 -> 180, 160, 140, ...
    weapons = [ [w[0], w[1] - weapon_speed] for w in weapons] # ๋ฌด๊ธฐ ์œ„์น˜๋ฅผ ์œ„๋กœ 

    # ์ฒœ์žฅ์— ๋‹ฟ์€ ๋ฌด๊ธฐ ์—†์• ๊ธฐ
    weapons = [ [w[0], w[1]] for w in weapons if w[1] > 0]
    
    # ๊ณต ์œ„์น˜ ์ •์˜
    for ball_idx, ball_val in enumerate(balls):
        ball_pos_x = ball_val["pos_x"]
        ball_pos_y = ball_val["pos_y"]
        ball_img_idx = ball_val["img_idx"]

        ball_size = ball_images[ball_img_idx].get_rect().size
        ball_width = ball_size[0]
        ball_height = ball_size[1]


        # ๊ฐ€๋กœ๋ฒฝ์— ๋‹ฟ์•˜์„ ๋•Œ ๊ณต ์ด๋™ ์œ„์น˜ ๋ณ€๊ฒฝ (ํŠ•๊ฒจ ๋‚˜์˜ค๋Š” ํšจ๊ณผ)
        if ball_pos_x < 0 or ball_pos_x > screen_width - ball_width:
            ball_val["to_x"] = ball_val["to_x"] * -1

        # ์„ธ๋กœ ์œ„์น˜
        # ์Šคํ…Œ์ด์ง€์— ํŠ•๊ฒจ์„œ ์˜ฌ๋ผ๊ฐ€๋Š” ์ฒ˜๋ฆฌ
        if ball_pos_y >= screen_height - stage_height - ball_height:
            ball_val["to_y"] = ball_val["init_spd_y"]
        else: # ๊ทธ ์™ธ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์—๋Š” ์†๋„๋ฅผ ์ฆ๊ฐ€
            ball_val["to_y"] += 0.5
        
        ball_val["pos_x"] += ball_val["to_x"]
        ball_val["pos_y"] += ball_val["to_y"]
     
    # 4. ์ถฉ๋Œ ์ฒ˜๋ฆฌ

    # 5. ํ™”๋ฉด์— ๊ทธ๋ฆฌ๊ธฐ
    screen.blit(background, (0, 0))

    for weapon_x_pos, weapon_y_pos in weapons:
        screen.blit(weapon, (weapon_x_pos, weapon_y_pos))

    for idx, val in enumerate(balls):
        ball_pos_x = val["pos_x"]
        ball_pos_y = val["pos_y"]
        ball_img_idx = val["img_idx"]
        screen.blit(ball_images[ball_img_idx], (ball_pos_x, ball_pos_y))

    screen.blit(stage, (0, screen_height - stage_height))
    screen.blit(character, (character_x_pos, character_y_pos))

    pygame.display.update() 
    
    
pygame.quit()

 

4_collision.py

(์ถฉ๋Œ ์ฒ˜๋ฆฌ)

import os
import pygame
#######################################################################
# ๊ธฐ๋ณธ ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค)
pygame.init() 

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 640 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 480 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption("Nado Pang")

# FPS
clock = pygame.time.Clock()
#######################################################################

# 1. ์‚ฌ์šฉ์ž ๊ฒŒ์ž„ ์ดˆ๊ธฐํ™” (๋ฐฐ๊ฒฝ ํ™”๋ฉด, ๊ฒŒ์ž„ ์ด๋ฏธ์ง€, ์ขŒํ‘œ, ์†๋„, ํฐํŠธ ๋“ฑ)
current_path = os.path.dirname(__file__) # ํ˜„์žฌ ํŒŒ์ผ์˜ ์œ„์น˜ ๋ฐ˜ํ™˜
image_path = os.path.join(current_path, "images") # images ํด๋” ์œ„์น˜ ๋ฐ˜ํ™˜

# ๋ฐฐ๊ฒฝ ๋งŒ๋“ค๊ธฐ
background = pygame.image.load(os.path.join(image_path, "background.png"))

# ์Šคํ…Œ์ด์ง€ ๋งŒ๋“ค๊ธฐ
stage = pygame.image.load(os.path.join(image_path, "stage.png"))
stage_size = stage.get_rect().size
stage_height = stage_size[1] # ์Šคํ…Œ์ด์ง€ ๋†’์ด ์œ„์— ์บ๋ฆญํ„ฐ๋ฅผ ๋‘๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

# ์บ๋ฆญํ„ฐ ๋งŒ๋“ค๊ธฐ
character = pygame.image.load(os.path.join(image_path, "character.png"))
character_size = character.get_rect().size
character_width = character_size[0]
character_height = character_size[1]
character_x_pos = (screen_width / 2) - (character_width / 2)
character_y_pos = screen_height - character_height - stage_height

# ์บ๋ฆญํ„ฐ ์ด๋™ ๋ฐฉํ–ฅ
character_to_x = 0

# ์บ๋ฆญํ„ฐ ์ด๋™ ์†๋„
character_speed = 5

# ๋ฌด๊ธฐ ๋งŒ๋“ค๊ธฐ
weapon = pygame.image.load(os.path.join(image_path, "weapon.png"))
weapon_size = weapon.get_rect().size
weapon_width = weapon_size[0]

# ๋ฌด๊ธฐ๋Š” ํ•œ ๋ฒˆ์— ์—ฌ๋Ÿฌ ๋ฐœ ๋ฐœ์‚ฌ ๊ฐ€๋Šฅ
weapons = []

# ๋ฌด๊ธฐ ์ด๋™ ์†๋„
weapon_speed = 10

# ๊ณต ๋งŒ๋“ค๊ธฐ (4๊ฐœ ํฌ๊ธฐ์— ๋Œ€ํ•ด ๋”ฐ๋กœ ์ฒ˜๋ฆฌ)
ball_images = [
    pygame.image.load(os.path.join(image_path, "balloon1.png")),
    pygame.image.load(os.path.join(image_path, "balloon2.png")),
    pygame.image.load(os.path.join(image_path, "balloon3.png")),
    pygame.image.load(os.path.join(image_path, "balloon4.png"))]

# ๊ณต ํฌ๊ธฐ์— ๋”ฐ๋ฅธ ์ตœ์ดˆ ์Šคํ”ผ๋“œ
ball_speed_y = [-18, -15, -12, -9] # index 0, 1, 2, 3 ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’

# ๊ณต๋“ค
balls = []
 
# ์ตœ์ดˆ ๋ฐœ์ƒํ•˜๋Š” ํฐ ๊ณต ์ถ”๊ฐ€
balls.append({
    "pos_x" : 50, # ๊ณต์˜ x ์ขŒํ‘œ
    "pos_y" : 50, # ๊ณต์˜ y ์ขŒํ‘œ
    "img_idx" : 0, # ๊ณต์˜ ์ด๋ฏธ์ง€ ์ธ๋ฑ์Šค
    "to_x" : 3, # x์ถ• ์ด๋™๋ฐฉํ–ฅ, -3 ์ด๋ฉด ์™ผ์ชฝ์œผ๋กœ, 3 ์ด๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ
    "to_y" : -6, # y์ถ• ์ด๋™๋ฐฉํ–ฅ,
    "init_spd_y" : ball_speed_y[0]}) # y ์ตœ์ดˆ ์†๋„

# ์‚ฌ๋ผ์งˆ ๋ฌด๊ธฐ, ๊ณต ์ •๋ณด ์ €์žฅ ๋ณ€์ˆ˜
weapon_to_remove = -1
ball_to_remove = -1


running = True 
while running:
    dt = clock.tick(30)

    # 2. ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ (ํ‚ค๋ณด๋“œ, ๋งˆ์šฐ์Šค ๋“ฑ)
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            running  = False 
        
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT: # ์บ๋ฆญํ„ฐ๋ฅผ ์™ผ์ชฝ์œผ๋กœ
                character_to_x -= character_speed
            elif event.key == pygame.K_RIGHT: # ์บ๋ฆญํ„ฐ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ
                character_to_x += character_speed
            elif event.key == pygame.K_SPACE: # ๋ฌด๊ธฐ ๋ฐœ์‚ฌ
                weapon_x_pos = character_x_pos + (character_width / 2) - (weapon_width / 2)
                weapon_y_pos = character_y_pos
                weapons.append([weapon_x_pos, weapon_y_pos])

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                character_to_x = 0
    
    # 3. ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ ์œ„์น˜ ์ •์˜
    character_x_pos += character_to_x

    if character_x_pos < 0:
        character_x_pos = 0
    elif character_x_pos > screen_width - character_width:
        character_x_pos = screen_width - character_width

    # ๋ฌด๊ธฐ ์œ„์น˜ ์กฐ์ •
    # 100, 200 -> 180, 160, 140, ...
    # 500, 200 -> 180, 160, 140, ...
    weapons = [ [w[0], w[1] - weapon_speed] for w in weapons] # ๋ฌด๊ธฐ ์œ„์น˜๋ฅผ ์œ„๋กœ 

    # ์ฒœ์žฅ์— ๋‹ฟ์€ ๋ฌด๊ธฐ ์—†์• ๊ธฐ
    weapons = [ [w[0], w[1]] for w in weapons if w[1] > 0]
    
    # ๊ณต ์œ„์น˜ ์ •์˜
    for ball_idx, ball_val in enumerate(balls):
        ball_pos_x = ball_val["pos_x"]
        ball_pos_y = ball_val["pos_y"]
        ball_img_idx = ball_val["img_idx"]

        ball_size = ball_images[ball_img_idx].get_rect().size
        ball_width = ball_size[0]
        ball_height = ball_size[1]


        # ๊ฐ€๋กœ๋ฒฝ์— ๋‹ฟ์•˜์„ ๋•Œ ๊ณต ์ด๋™ ์œ„์น˜ ๋ณ€๊ฒฝ (ํŠ•๊ฒจ ๋‚˜์˜ค๋Š” ํšจ๊ณผ)
        if ball_pos_x < 0 or ball_pos_x > screen_width - ball_width:
            ball_val["to_x"] = ball_val["to_x"] * -1

        # ์„ธ๋กœ ์œ„์น˜
        # ์Šคํ…Œ์ด์ง€์— ํŠ•๊ฒจ์„œ ์˜ฌ๋ผ๊ฐ€๋Š” ์ฒ˜๋ฆฌ
        if ball_pos_y >= screen_height - stage_height - ball_height:
            ball_val["to_y"] = ball_val["init_spd_y"]
        else: # ๊ทธ ์™ธ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์—๋Š” ์†๋„๋ฅผ ์ฆ๊ฐ€
            ball_val["to_y"] += 0.5
        
        ball_val["pos_x"] += ball_val["to_x"]
        ball_val["pos_y"] += ball_val["to_y"]
     
    # 4. ์ถฉ๋Œ ์ฒ˜๋ฆฌ

    # ์บ๋ฆญํ„ฐ rect ์ •๋ณด ์—…๋ฐ์ดํŠธ
    character_rect = character.get_rect()
    character_rect.left = character_x_pos
    character_rect.right = character_y_pos

    for ball_idx, ball_val in enumerate(balls):
        ball_pos_x = ball_val["pos_x"]
        ball_pos_y = ball_val["pos_y"]
        ball_img_idx = ball_val["img_idx"]

        # ๊ณต rect ์ •๋ณด ์—…๋ฐ์ดํŠธ
        ball_rect = ball_images[ball_img_idx].get_rect()
        ball_rect.left = ball_pos_x
        ball_rect.top = ball_pos_y

        # ๊ณต๊ณผ ์บ๋ฆญํ„ฐ ์ถฉ๋Œ ์ฒ˜๋ฆฌ
        if character_rect.colliderect(ball_rect):
            running = False
            break

        # ๊ณต๊ณผ ๋ฌด๊ธฐ๋“ค ์ถฉ๋Œ ์ฒ˜๋ฆฌ
        for weapon_idx, weapon_val in enumerate(weapons):
            weapon_pos_x = weapon_val[0]
            weapon_pos_y = weapon_val[1]

            # ๋ฌด๊ธฐ rect ์ •๋ณด ์—…๋ฐ์ดํŠธ
            weapon_rect = weapon.get_rect()
            weapon_rect.left = weapon_pos_x
            weapon_rect.top = weapon_pos_y

            # ์ถฉ๋Œ ์ฒดํฌ
            if weapon_rect.colliderect(ball_rect):
                weapon_to_remove = weapon_idx # ํ•ด๋‹น ๋ฌด๊ธฐ ์—†์• ๊ธฐ ์œ„ํ•œ ๊ฐ’ ์„ค์ •
                ball_to_remove = ball_idx # ํ•ด๋‹น ๊ณต ์—†์• ๊ธฐ ์œ„ํ•œ ๊ฐ’ ์„ค์ •
                break

    # ์ถฉ๋Œ๋œ ๊ณต or ๋ฌด๊ธฐ ์—†์• ๊ธฐ
    if ball_to_remove > -1:
        del balls[ball_to_remove]
        ball_to_remove = -1

    if weapon_to_remove > -1:
        del weapons[weapon_to_remove]
        weapon_to_remove = -1



    # 5. ํ™”๋ฉด์— ๊ทธ๋ฆฌ๊ธฐ
    screen.blit(background, (0, 0))

    for weapon_x_pos, weapon_y_pos in weapons:
        screen.blit(weapon, (weapon_x_pos, weapon_y_pos))

    for idx, val in enumerate(balls):
        ball_pos_x = val["pos_x"]
        ball_pos_y = val["pos_y"]
        ball_img_idx = val["img_idx"]
        screen.blit(ball_images[ball_img_idx], (ball_pos_x, ball_pos_y))

    screen.blit(stage, (0, screen_height - stage_height))
    screen.blit(character, (character_x_pos, character_y_pos))

    pygame.display.update() 
    
    
pygame.quit()

 

5_ball_division.py

(๊ณต์„ ๋งž์ท„์„ ๋•Œ ๊ณต์˜ ํฌ๊ธฐ๋ฅผ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ)

import os
import pygame
#######################################################################
# ๊ธฐ๋ณธ ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค)
pygame.init() 

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 640 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 480 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption("Nado Pang")

# FPS
clock = pygame.time.Clock()
#######################################################################

# 1. ์‚ฌ์šฉ์ž ๊ฒŒ์ž„ ์ดˆ๊ธฐํ™” (๋ฐฐ๊ฒฝ ํ™”๋ฉด, ๊ฒŒ์ž„ ์ด๋ฏธ์ง€, ์ขŒํ‘œ, ์†๋„, ํฐํŠธ ๋“ฑ)
current_path = os.path.dirname(__file__) # ํ˜„์žฌ ํŒŒ์ผ์˜ ์œ„์น˜ ๋ฐ˜ํ™˜
image_path = os.path.join(current_path, "images") # images ํด๋” ์œ„์น˜ ๋ฐ˜ํ™˜

# ๋ฐฐ๊ฒฝ ๋งŒ๋“ค๊ธฐ
background = pygame.image.load(os.path.join(image_path, "background.png"))

# ์Šคํ…Œ์ด์ง€ ๋งŒ๋“ค๊ธฐ
stage = pygame.image.load(os.path.join(image_path, "stage.png"))
stage_size = stage.get_rect().size
stage_height = stage_size[1] # ์Šคํ…Œ์ด์ง€ ๋†’์ด ์œ„์— ์บ๋ฆญํ„ฐ๋ฅผ ๋‘๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

# ์บ๋ฆญํ„ฐ ๋งŒ๋“ค๊ธฐ
character = pygame.image.load(os.path.join(image_path, "character.png"))
character_size = character.get_rect().size
character_width = character_size[0]
character_height = character_size[1]
character_x_pos = (screen_width / 2) - (character_width / 2)
character_y_pos = screen_height - character_height - stage_height

# ์บ๋ฆญํ„ฐ ์ด๋™ ๋ฐฉํ–ฅ
character_to_x = 0

# ์บ๋ฆญํ„ฐ ์ด๋™ ์†๋„
character_speed = 5

# ๋ฌด๊ธฐ ๋งŒ๋“ค๊ธฐ
weapon = pygame.image.load(os.path.join(image_path, "weapon.png"))
weapon_size = weapon.get_rect().size
weapon_width = weapon_size[0]

# ๋ฌด๊ธฐ๋Š” ํ•œ ๋ฒˆ์— ์—ฌ๋Ÿฌ ๋ฐœ ๋ฐœ์‚ฌ ๊ฐ€๋Šฅ
weapons = []

# ๋ฌด๊ธฐ ์ด๋™ ์†๋„
weapon_speed = 10

# ๊ณต ๋งŒ๋“ค๊ธฐ (4๊ฐœ ํฌ๊ธฐ์— ๋Œ€ํ•ด ๋”ฐ๋กœ ์ฒ˜๋ฆฌ)
ball_images = [
    pygame.image.load(os.path.join(image_path, "balloon1.png")),
    pygame.image.load(os.path.join(image_path, "balloon2.png")),
    pygame.image.load(os.path.join(image_path, "balloon3.png")),
    pygame.image.load(os.path.join(image_path, "balloon4.png"))]

# ๊ณต ํฌ๊ธฐ์— ๋”ฐ๋ฅธ ์ตœ์ดˆ ์Šคํ”ผ๋“œ
ball_speed_y = [-18, -15, -12, -9] # index 0, 1, 2, 3 ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’

# ๊ณต๋“ค
balls = []
 
# ์ตœ์ดˆ ๋ฐœ์ƒํ•˜๋Š” ํฐ ๊ณต ์ถ”๊ฐ€
balls.append({
    "pos_x" : 50, # ๊ณต์˜ x ์ขŒํ‘œ
    "pos_y" : 50, # ๊ณต์˜ y ์ขŒํ‘œ
    "img_idx" : 0, # ๊ณต์˜ ์ด๋ฏธ์ง€ ์ธ๋ฑ์Šค
    "to_x" : 3, # x์ถ• ์ด๋™๋ฐฉํ–ฅ, -3 ์ด๋ฉด ์™ผ์ชฝ์œผ๋กœ, 3 ์ด๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ
    "to_y" : -6, # y์ถ• ์ด๋™๋ฐฉํ–ฅ,
    "init_spd_y" : ball_speed_y[0]}) # y ์ตœ์ดˆ ์†๋„

# ์‚ฌ๋ผ์งˆ ๋ฌด๊ธฐ, ๊ณต ์ •๋ณด ์ €์žฅ ๋ณ€์ˆ˜
weapon_to_remove = -1
ball_to_remove = -1


running = True 
while running:
    dt = clock.tick(30)

    # 2. ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ (ํ‚ค๋ณด๋“œ, ๋งˆ์šฐ์Šค ๋“ฑ)
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            running  = False 
        
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT: # ์บ๋ฆญํ„ฐ๋ฅผ ์™ผ์ชฝ์œผ๋กœ
                character_to_x -= character_speed
            elif event.key == pygame.K_RIGHT: # ์บ๋ฆญํ„ฐ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ
                character_to_x += character_speed
            elif event.key == pygame.K_SPACE: # ๋ฌด๊ธฐ ๋ฐœ์‚ฌ
                weapon_x_pos = character_x_pos + (character_width / 2) - (weapon_width / 2)
                weapon_y_pos = character_y_pos
                weapons.append([weapon_x_pos, weapon_y_pos])

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                character_to_x = 0
    
    # 3. ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ ์œ„์น˜ ์ •์˜
    character_x_pos += character_to_x

    if character_x_pos < 0:
        character_x_pos = 0
    elif character_x_pos > screen_width - character_width:
        character_x_pos = screen_width - character_width

    # ๋ฌด๊ธฐ ์œ„์น˜ ์กฐ์ •
    # 100, 200 -> 180, 160, 140, ...
    # 500, 200 -> 180, 160, 140, ...
    weapons = [ [w[0], w[1] - weapon_speed] for w in weapons] # ๋ฌด๊ธฐ ์œ„์น˜๋ฅผ ์œ„๋กœ 

    # ์ฒœ์žฅ์— ๋‹ฟ์€ ๋ฌด๊ธฐ ์—†์• ๊ธฐ
    weapons = [ [w[0], w[1]] for w in weapons if w[1] > 0]
    
    # ๊ณต ์œ„์น˜ ์ •์˜
    for ball_idx, ball_val in enumerate(balls):
        ball_pos_x = ball_val["pos_x"]
        ball_pos_y = ball_val["pos_y"]
        ball_img_idx = ball_val["img_idx"]

        ball_size = ball_images[ball_img_idx].get_rect().size
        ball_width = ball_size[0]
        ball_height = ball_size[1]


        # ๊ฐ€๋กœ๋ฒฝ์— ๋‹ฟ์•˜์„ ๋•Œ ๊ณต ์ด๋™ ์œ„์น˜ ๋ณ€๊ฒฝ (ํŠ•๊ฒจ ๋‚˜์˜ค๋Š” ํšจ๊ณผ)
        if ball_pos_x < 0 or ball_pos_x > screen_width - ball_width:
            ball_val["to_x"] = ball_val["to_x"] * -1

        # ์„ธ๋กœ ์œ„์น˜
        # ์Šคํ…Œ์ด์ง€์— ํŠ•๊ฒจ์„œ ์˜ฌ๋ผ๊ฐ€๋Š” ์ฒ˜๋ฆฌ
        if ball_pos_y >= screen_height - stage_height - ball_height:
            ball_val["to_y"] = ball_val["init_spd_y"]
        else: # ๊ทธ ์™ธ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์—๋Š” ์†๋„๋ฅผ ์ฆ๊ฐ€
            ball_val["to_y"] += 0.5
        
        ball_val["pos_x"] += ball_val["to_x"]
        ball_val["pos_y"] += ball_val["to_y"]
     
    # 4. ์ถฉ๋Œ ์ฒ˜๋ฆฌ

    # ์บ๋ฆญํ„ฐ rect ์ •๋ณด ์—…๋ฐ์ดํŠธ
    character_rect = character.get_rect()
    character_rect.left = character_x_pos
    character_rect.right = character_y_pos

    for ball_idx, ball_val in enumerate(balls):
        ball_pos_x = ball_val["pos_x"]
        ball_pos_y = ball_val["pos_y"]
        ball_img_idx = ball_val["img_idx"]

        # ๊ณต rect ์ •๋ณด ์—…๋ฐ์ดํŠธ
        ball_rect = ball_images[ball_img_idx].get_rect()
        ball_rect.left = ball_pos_x
        ball_rect.top = ball_pos_y

        # ๊ณต๊ณผ ์บ๋ฆญํ„ฐ ์ถฉ๋Œ ์ฒ˜๋ฆฌ
        if character_rect.colliderect(ball_rect):
            running = False
            break

        # ๊ณต๊ณผ ๋ฌด๊ธฐ๋“ค ์ถฉ๋Œ ์ฒ˜๋ฆฌ
        for weapon_idx, weapon_val in enumerate(weapons):
            weapon_pos_x = weapon_val[0]
            weapon_pos_y = weapon_val[1]

            # ๋ฌด๊ธฐ rect ์ •๋ณด ์—…๋ฐ์ดํŠธ
            weapon_rect = weapon.get_rect()
            weapon_rect.left = weapon_pos_x
            weapon_rect.top = weapon_pos_y

            # ์ถฉ๋Œ ์ฒดํฌ
            if weapon_rect.colliderect(ball_rect):
                weapon_to_remove = weapon_idx # ํ•ด๋‹น ๋ฌด๊ธฐ ์—†์• ๊ธฐ ์œ„ํ•œ ๊ฐ’ ์„ค์ •
                ball_to_remove = ball_idx # ํ•ด๋‹น ๊ณต ์—†์• ๊ธฐ ์œ„ํ•œ ๊ฐ’ ์„ค์ •

                # ๊ฐ€์žฅ ์ž‘์€ ํฌ๊ธฐ์˜ ๊ณต์ด ์•„๋‹ˆ๋ผ๋ฉด ๋‹ค์Œ ๋‹จ๊ณ„์˜ ๊ณต์œผ๋กœ ๋‚˜๋ˆ ์ฃผ๊ธฐ
                if ball_img_idx < 3:
                    # ํ˜„์žฌ ๊ณต ํฌ๊ธฐ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ด
                    ball_width = ball_rect.size[0]
                    ball_height = ball_rect.size[1]

                    # ๋‚˜๋ˆ ์ง„ ๊ณต ์ •๋ณด
                    small_ball_rect = ball_images[ball_img_idx + 1].get_rect()
                    small_ball_width = small_ball_rect.size[0]
                    small_ball_height = small_ball_rect.size[1]

                    # ์™ผ์ชฝ์œผ๋กœ ํŠ•๊ฒจ๋‚˜๊ฐ€๋Š” ์ž‘์€ ๊ณต
                    balls.append({
                        "pos_x" : ball_pos_x + (ball_width / 2) - (small_ball_width / 2), # ๊ณต์˜ x ์ขŒํ‘œ
                        "pos_y" : ball_pos_x + (ball_height / 2) - (small_ball_height / 2), # ๊ณต์˜ y ์ขŒํ‘œ
                        "img_idx" : ball_img_idx + 1, # ๊ณต์˜ ์ด๋ฏธ์ง€ ์ธ๋ฑ์Šค
                        "to_x" : -3, # x์ถ• ์ด๋™๋ฐฉํ–ฅ, -3 ์ด๋ฉด ์™ผ์ชฝ์œผ๋กœ, 3 ์ด๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ
                        "to_y" : -6, # y์ถ• ์ด๋™๋ฐฉํ–ฅ,
                        "init_spd_y" : ball_speed_y[ball_img_idx + 1]}) # y ์ตœ์ดˆ ์†๋„

                    # ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํŠ•๊ฒจ๋‚˜๊ฐ€๋Š” ์ž‘์€ ๊ณต
                    balls.append({
                        "pos_x" : ball_pos_x + (ball_width / 2) - (small_ball_width / 2), # ๊ณต์˜ x ์ขŒํ‘œ
                        "pos_y" : ball_pos_x + (ball_height / 2) - (small_ball_height / 2), # ๊ณต์˜ y ์ขŒํ‘œ
                        "img_idx" : ball_img_idx + 1, # ๊ณต์˜ ์ด๋ฏธ์ง€ ์ธ๋ฑ์Šค
                        "to_x" : 3, # x์ถ• ์ด๋™๋ฐฉํ–ฅ, -3 ์ด๋ฉด ์™ผ์ชฝ์œผ๋กœ, 3 ์ด๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ
                        "to_y" : -6, # y์ถ• ์ด๋™๋ฐฉํ–ฅ,
                        "init_spd_y" : ball_speed_y[ball_img_idx + 1]}) # y ์ตœ์ดˆ ์†๋„
                break

    # ์ถฉ๋Œ๋œ ๊ณต or ๋ฌด๊ธฐ ์—†์• ๊ธฐ
    if ball_to_remove > -1:
        del balls[ball_to_remove]
        ball_to_remove = -1

    if weapon_to_remove > -1:
        del weapons[weapon_to_remove]
        weapon_to_remove = -1



    # 5. ํ™”๋ฉด์— ๊ทธ๋ฆฌ๊ธฐ
    screen.blit(background, (0, 0))

    for weapon_x_pos, weapon_y_pos in weapons:
        screen.blit(weapon, (weapon_x_pos, weapon_y_pos))

    for idx, val in enumerate(balls):
        ball_pos_x = val["pos_x"]
        ball_pos_y = val["pos_y"]
        ball_img_idx = val["img_idx"]
        screen.blit(ball_images[ball_img_idx], (ball_pos_x, ball_pos_y))

    screen.blit(stage, (0, screen_height - stage_height))
    screen.blit(character, (character_x_pos, character_y_pos))

    pygame.display.update() 
    
    
pygame.quit()

 

6_game_over.py

(* ์—ฌ๊ธฐ์„œ ์›์ธ ๋ชจ๋ฅผ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์„œ ๊ฐ•์‚ฌ๋‹˜์˜ ์ฝ”๋“œ๋ฅผ ๋ฐ‘์— ์ฒจ๋ถ€ํ–ˆ์Šต๋‹ˆ๋‹ค)

์™„์„ฑ ์ฝ”๋“œ
import os
import pygame
##############################################################
# ๊ธฐ๋ณธ ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค)
pygame.init()

# ํ™”๋ฉด ํฌ๊ธฐ ์„ค์ •
screen_width = 640 # ๊ฐ€๋กœ ํฌ๊ธฐ
screen_height = 480 # ์„ธ๋กœ ํฌ๊ธฐ
screen = pygame.display.set_mode((screen_width, screen_height))

# ํ™”๋ฉด ํƒ€์ดํ‹€ ์„ค์ •
pygame.display.set_caption("Nado Pang")

# FPS
clock = pygame.time.Clock()
##############################################################

# 1. ์‚ฌ์šฉ์ž ๊ฒŒ์ž„ ์ดˆ๊ธฐํ™” (๋ฐฐ๊ฒฝ ํ™”๋ฉด, ๊ฒŒ์ž„ ์ด๋ฏธ์ง€, ์ขŒํ‘œ, ์†๋„, ํฐํŠธ ๋“ฑ)
current_path = os.path.dirname(__file__) # ํ˜„์žฌ ํŒŒ์ผ์˜ ์œ„์น˜ ๋ฐ˜ํ™˜
image_path = os.path.join(current_path, "images") # images ํด๋” ์œ„์น˜ ๋ฐ˜ํ™˜

# ๋ฐฐ๊ฒฝ ๋งŒ๋“ค๊ธฐ
background = pygame.image.load(os.path.join(image_path, "background.png"))

# ์Šคํ…Œ์ด์ง€ ๋งŒ๋“ค๊ธฐ
stage = pygame.image.load(os.path.join(image_path, "stage.png"))
stage_size = stage.get_rect().size
stage_height = stage_size[1] # ์Šคํ…Œ์ด์ง€์˜ ๋†’์ด ์œ„์— ์บ๋ฆญํ„ฐ๋ฅผ ๋‘๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

# ์บ๋ฆญํ„ฐ ๋งŒ๋“ค๊ธฐ
character = pygame.image.load(os.path.join(image_path, "character.png"))
character_size = character.get_rect().size
character_width = character_size[0]
character_height = character_size[1]
character_x_pos = (screen_width / 2) - (character_width / 2)
character_y_pos = screen_height - character_height - stage_height

# ์บ๋ฆญํ„ฐ ์ด๋™ ๋ฐฉํ–ฅ
character_to_x = 0

# ์บ๋ฆญํ„ฐ ์ด๋™ ์†๋„
character_speed = 5

# ๋ฌด๊ธฐ ๋งŒ๋“ค๊ธฐ
weapon = pygame.image.load(os.path.join(image_path, "weapon.png"))
weapon_size = weapon.get_rect().size
weapon_width = weapon_size[0]

# ๋ฌด๊ธฐ๋Š” ํ•œ ๋ฒˆ์— ์—ฌ๋Ÿฌ ๋ฐœ ๋ฐœ์‚ฌ ๊ฐ€๋Šฅ
weapons = []

# ๋ฌด๊ธฐ ์ด๋™ ์†๋„
weapon_speed = 10

# ๊ณต ๋งŒ๋“ค๊ธฐ (4๊ฐœ ํฌ๊ธฐ์— ๋Œ€ํ•ด ๋”ฐ๋กœ ์ฒ˜๋ฆฌ)
ball_images = [
    pygame.image.load(os.path.join(image_path, "balloon1.png")),
    pygame.image.load(os.path.join(image_path, "balloon2.png")),
    pygame.image.load(os.path.join(image_path, "balloon3.png")),
    pygame.image.load(os.path.join(image_path, "balloon4.png"))]

# ๊ณต ํฌ๊ธฐ์— ๋”ฐ๋ฅธ ์ตœ์ดˆ ์Šคํ”ผ๋“œ
ball_speed_y = [-18, -15, -12, -9] # index 0, 1, 2, 3 ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’

# ๊ณต๋“ค
balls = []

# ์ตœ์ดˆ ๋ฐœ์ƒํ•˜๋Š” ํฐ ๊ณต ์ถ”๊ฐ€
balls.append({
    "pos_x" : 50, # ๊ณต์˜ x ์ขŒํ‘œ
    "pos_y" : 50, # ๊ณต์˜ y ์ขŒํ‘œ
    "img_idx" : 0, # ๊ณต์˜ ์ด๋ฏธ์ง€ ์ธ๋ฑ์Šค
    "to_x": 3, # x์ถ• ์ด๋™๋ฐฉํ–ฅ, -3 ์ด๋ฉด ์™ผ์ชฝ์œผ๋กœ, 3 ์ด๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ
    "to_y": -6, # y์ถ• ์ด๋™๋ฐฉํ–ฅ,
    "init_spd_y": ball_speed_y[0]})# y ์ตœ์ดˆ ์†๋„

# ์‚ฌ๋ผ์งˆ ๋ฌด๊ธฐ, ๊ณต ์ •๋ณด ์ €์žฅ ๋ณ€์ˆ˜
weapon_to_remove = -1
ball_to_remove = -1

# Font ์ •์˜
game_font = pygame.font.Font(None, 40)
total_time = 100
start_ticks = pygame.time.get_ticks() # ์‹œ์ž‘ ์‹œ๊ฐ„ ์ •์˜

# ๊ฒŒ์ž„ ์ข…๋ฃŒ ๋ฉ”์‹œ์ง€ 
# Time Over(์‹œ๊ฐ„ ์ดˆ๊ณผ ์‹คํŒจ)
# Mission Complete(์„ฑ๊ณต)
# Game Over (์บ๋ฆญํ„ฐ ๊ณต์— ๋งž์Œ, ์‹คํŒจ)
game_result = "Game Over"

running = True
while running:
    dt = clock.tick(30)
    
    # 2. ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ (ํ‚ค๋ณด๋“œ, ๋งˆ์šฐ์Šค ๋“ฑ)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False 

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT: # ์บ๋ฆญํ„ฐ๋ฅผ ์™ผ์ชฝ์œผ๋กœ
                character_to_x -= character_speed
            elif event.key == pygame.K_RIGHT: # ์บ๋ฆญํ„ฐ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ
                character_to_x += character_speed
            elif event.key == pygame.K_SPACE: # ๋ฌด๊ธฐ ๋ฐœ์‚ฌ
                weapon_x_pos = character_x_pos + (character_width / 2) - (weapon_width / 2)
                weapon_y_pos = character_y_pos
                weapons.append([weapon_x_pos, weapon_y_pos])
        
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                character_to_x = 0

    # 3. ๊ฒŒ์ž„ ์บ๋ฆญํ„ฐ ์œ„์น˜ ์ •์˜
    character_x_pos += character_to_x

    if character_x_pos < 0:
        character_x_pos = 0
    elif character_x_pos > screen_width - character_width:
        character_x_pos = screen_width - character_width

    # ๋ฌด๊ธฐ ์œ„์น˜ ์กฐ์ •
    # 100, 200 -> 180, 160, 140, ...
    # 500, 200 -> 180, 160, 140, ...
    weapons = [ [w[0], w[1] - weapon_speed] for w in weapons] # ๋ฌด๊ธฐ ์œ„์น˜๋ฅผ ์œ„๋กœ

    # ์ฒœ์žฅ์— ๋‹ฟ์€ ๋ฌด๊ธฐ ์—†์• ๊ธฐ
    weapons = [ [w[0], w[1]] for w in weapons if w[1] > 0]
    
    # ๊ณต ์œ„์น˜ ์ •์˜
    for ball_idx, ball_val in enumerate(balls):
        ball_pos_x = ball_val["pos_x"]
        ball_pos_y = ball_val["pos_y"]
        ball_img_idx = ball_val["img_idx"]

        ball_size = ball_images[ball_img_idx].get_rect().size
        ball_width = ball_size[0]
        ball_height = ball_size[1]

        # ๊ฐ€๋กœ๋ฒฝ์— ๋‹ฟ์•˜์„ ๋•Œ ๊ณต ์ด๋™ ์œ„์น˜ ๋ณ€๊ฒฝ (ํŠ•๊ฒจ ๋‚˜์˜ค๋Š” ํšจ๊ณผ)
        if ball_pos_x < 0 or ball_pos_x > screen_width - ball_width:
            ball_val["to_x"] = ball_val["to_x"] * -1

        # ์„ธ๋กœ ์œ„์น˜
        # ์Šคํ…Œ์ด์ง€์— ํŠ•๊ฒจ์„œ ์˜ฌ๋ผ๊ฐ€๋Š” ์ฒ˜๋ฆฌ
        if ball_pos_y >= screen_height - stage_height - ball_height:
            ball_val["to_y"] = ball_val["init_spd_y"]
        else: # ๊ทธ ์™ธ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์—๋Š” ์†๋„๋ฅผ ์ฆ๊ฐ€
            ball_val["to_y"] += 0.5

        ball_val["pos_x"] += ball_val["to_x"]
        ball_val["pos_y"] += ball_val["to_y"]

    # 4. ์ถฉ๋Œ ์ฒ˜๋ฆฌ

    # ์บ๋ฆญํ„ฐ rect ์ •๋ณด ์—…๋ฐ์ดํŠธ
    character_rect = character.get_rect()
    character_rect.left = character_x_pos
    character_rect.top = character_y_pos

    for ball_idx, ball_val in enumerate(balls):
        ball_pos_x = ball_val["pos_x"]
        ball_pos_y = ball_val["pos_y"]
        ball_img_idx = ball_val["img_idx"]

        # ๊ณต rect ์ •๋ณด ์—…๋ฐ์ดํŠธ
        ball_rect = ball_images[ball_img_idx].get_rect()
        ball_rect.left = ball_pos_x
        ball_rect.top = ball_pos_y

        # ๊ณต๊ณผ ์บ๋ฆญํ„ฐ ์ถฉ๋Œ ์ฒดํฌ
        if character_rect.colliderect(ball_rect):
            running = False
            break

        # ๊ณต๊ณผ ๋ฌด๊ธฐ๋“ค ์ถฉ๋Œ ์ฒ˜๋ฆฌ
        for weapon_idx, weapon_val in enumerate(weapons):
            weapon_pos_x = weapon_val[0]
            weapon_pos_y = weapon_val[1]

            # ๋ฌด๊ธฐ rect ์ •๋ณด ์—…๋ฐ์ดํŠธ
            weapon_rect = weapon.get_rect()
            weapon_rect.left = weapon_pos_x
            weapon_rect.top = weapon_pos_y

            # ์ถฉ๋Œ ์ฒดํฌ
            if weapon_rect.colliderect(ball_rect):
                weapon_to_remove = weapon_idx # ํ•ด๋‹น ๋ฌด๊ธฐ ์—†์• ๊ธฐ ์œ„ํ•œ ๊ฐ’ ์„ค์ •
                ball_to_remove = ball_idx # ํ•ด๋‹น ๊ณต ์—†์• ๊ธฐ ์œ„ํ•œ ๊ฐ’ ์„ค์ •

                # ๊ฐ€์žฅ ์ž‘์€ ํฌ๊ธฐ์˜ ๊ณต์ด ์•„๋‹ˆ๋ผ๋ฉด ๋‹ค์Œ ๋‹จ๊ณ„์˜ ๊ณต์œผ๋กœ ๋‚˜๋ˆ ์ฃผ๊ธฐ
                if ball_img_idx < 3:
                    # ํ˜„์žฌ ๊ณต ํฌ๊ธฐ ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ด
                    ball_width = ball_rect.size[0]
                    ball_height = ball_rect.size[1]

                    # ๋‚˜๋ˆ ์ง„ ๊ณต ์ •๋ณด
                    small_ball_rect = ball_images[ball_img_idx + 1].get_rect()
                    small_ball_width = small_ball_rect.size[0]
                    small_ball_height = small_ball_rect.size[1]

                    # ์™ผ์ชฝ์œผ๋กœ ํŠ•๊ฒจ๋‚˜๊ฐ€๋Š” ์ž‘์€ ๊ณต
                    balls.append({
                        "pos_x" : ball_pos_x + (ball_width / 2) - (small_ball_width / 2), # ๊ณต์˜ x ์ขŒํ‘œ
                        "pos_y" : ball_pos_y + (ball_height / 2) - (small_ball_height / 2), # ๊ณต์˜ y ์ขŒํ‘œ
                        "img_idx" : ball_img_idx + 1, # ๊ณต์˜ ์ด๋ฏธ์ง€ ์ธ๋ฑ์Šค
                        "to_x": -3, # x์ถ• ์ด๋™๋ฐฉํ–ฅ, -3 ์ด๋ฉด ์™ผ์ชฝ์œผ๋กœ, 3 ์ด๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ
                        "to_y": -6, # y์ถ• ์ด๋™๋ฐฉํ–ฅ,
                        "init_spd_y": ball_speed_y[ball_img_idx + 1]})# y ์ตœ์ดˆ ์†๋„

                    # ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํŠ•๊ฒจ๋‚˜๊ฐ€๋Š” ์ž‘์€ ๊ณต
                    balls.append({
                        "pos_x" : ball_pos_x + (ball_width / 2) - (small_ball_width / 2), # ๊ณต์˜ x ์ขŒํ‘œ
                        "pos_y" : ball_pos_y + (ball_height / 2) - (small_ball_height / 2), # ๊ณต์˜ y ์ขŒํ‘œ
                        "img_idx" : ball_img_idx + 1, # ๊ณต์˜ ์ด๋ฏธ์ง€ ์ธ๋ฑ์Šค
                        "to_x": 3, # x์ถ• ์ด๋™๋ฐฉํ–ฅ, -3 ์ด๋ฉด ์™ผ์ชฝ์œผ๋กœ, 3 ์ด๋ฉด ์˜ค๋ฅธ์ชฝ์œผ๋กœ
                        "to_y": -6, # y์ถ• ์ด๋™๋ฐฉํ–ฅ,
                        "init_spd_y": ball_speed_y[ball_img_idx + 1]})# y ์ตœ์ดˆ ์†๋„

                break
        else: # ๊ณ„์† ๊ฒŒ์ž„์„ ์ง„ํ–‰
            continue # ์•ˆ์ชฝ for ๋ฌธ ์กฐ๊ฑด์ด ๋งž์ง€ ์•Š์œผ๋ฉด continue. ๋ฐ”๊นฅ for ๋ฌธ ๊ณ„์† ์ˆ˜ํ–‰
        break # ์•ˆ์ชฝ for ๋ฌธ์—์„œ break ๋ฅผ ๋งŒ๋‚˜๋ฉด ์—ฌ๊ธฐ๋กœ ์ง„์ž… ๊ฐ€๋Šฅ. 2์ค‘ for ๋ฌธ์„ ํ•œ๋ฒˆ์— ํƒˆ์ถœ

    # for ๋ฐ”๊นฅ์กฐ๊ฑด:
    #     ๋ฐ”๊นฅ๋™์ž‘
    #     for ์•ˆ์ชฝ์กฐ๊ฑด:
    #         ์•ˆ์ชฝ๋™์ž‘
    #         if ์ถฉ๋Œํ•˜๋ฉด:
    #             break
    #     else:
    #         continue
    #     break

    # ์ถฉ๋Œ๋œ ๊ณต or ๋ฌด๊ธฐ ์—†์• ๊ธฐ
    if ball_to_remove > -1:
        del balls[ball_to_remove]
        ball_to_remove = -1

    if weapon_to_remove > -1:
        del weapons[weapon_to_remove]
        weapon_to_remove = -1

    # ๋ชจ๋“  ๊ณต์„ ์—†์•ค ๊ฒฝ์šฐ ๊ฒŒ์ž„ ์ข…๋ฃŒ (์„ฑ๊ณต)
    if len(balls) == 0:
        game_result = "Mission Complete"
        running = False

    # 5. ํ™”๋ฉด์— ๊ทธ๋ฆฌ๊ธฐ
    screen.blit(background, (0, 0))
    
    for weapon_x_pos, weapon_y_pos in weapons:
        screen.blit(weapon, (weapon_x_pos, weapon_y_pos))

    for idx, val in enumerate(balls):
        ball_pos_x = val["pos_x"]
        ball_pos_y = val["pos_y"]
        ball_img_idx = val["img_idx"]
        screen.blit(ball_images[ball_img_idx], (ball_pos_x, ball_pos_y))

    screen.blit(stage, (0, screen_height - stage_height))
    screen.blit(character, (character_x_pos, character_y_pos))
    
    # ๊ฒฝ๊ณผ ์‹œ๊ฐ„ ๊ณ„์‚ฐ
    elapsed_time = (pygame.time.get_ticks() - start_ticks) / 1000 # ms -> s
    timer = game_font.render("Time : {}".format(int(total_time - elapsed_time)), True, (255, 255, 255))
    screen.blit(timer, (10, 10))

    # ์‹œ๊ฐ„ ์ดˆ๊ณผํ–ˆ๋‹ค๋ฉด
    if total_time - elapsed_time <= 0:
        game_result = "Time Over"
        running = False

    pygame.display.update()

# ๊ฒŒ์ž„ ์˜ค๋ฒ„ ๋ฉ”์‹œ์ง€
msg = game_font.render(game_result, True, (255, 255, 0)) # ๋…ธ๋ž€์ƒ‰
msg_rect = msg.get_rect(center=(int(screen_width / 2), int(screen_height / 2)))
screen.blit(msg, msg_rect)
pygame.display.update()

# 2์ดˆ ๋Œ€๊ธฐ
pygame.time.delay(2000)

pygame.quit()