๐Ÿ‘ฆ ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„/์‚ฌ์ „๊ณผ์ œ

[๋‚ด์ผ๋ฐฐ์›€์บ ํ”„] ์‚ฌ์ „๊ณผ์ œ - ์ถ”์–ต์˜ ์˜ค๋ฝ์‹ค ๊ฒŒ์ž„ ๋งŒ๋“ค๊ธฐ

  • -

github : https://github.com/DHL68/github-DHL68/tree/main/22_04_08_arcade%20games

 

GitHub - DHL68/github-DHL68: github-DHL68-practice

github-DHL68-practice. Contribute to DHL68/github-DHL68 development by creating an account on GitHub.

github.com

 

1. ๋˜ฅ ํ”ผํ•˜๊ธฐ ๊ฒŒ์ž„(๊ธฐ์ดˆ)

 

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/Lee_DH/Desktop/python prac/22_04_08_arcade games/pygame_basic/background.png")

# ์บ๋ฆญํ„ฐ(์Šคํ”„๋ผ์ดํŠธ) ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
character = pygame.image.load("C:/Users/Lee_DH/Desktop/python prac/22_04_08_arcade games/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/Lee_DH/Desktop/python prac/22_04_08_arcade games/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) # ๊ฒŒ์ž„ํ™”๋ฉด์˜ ์ดˆ๋‹น ํ”„๋ ˆ์ž„ ์ˆ˜๋ฅผ ์„ค์ •

    # ์บ๋ฆญํ„ฐ๊ฐ€ 1์ดˆ ๋™์•ˆ์— 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

    # ์ถฉ๋Œ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ 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()

 

 

 

 

 

2. ๋˜ฅ ํ”ผํ•˜๊ธฐ ๊ฒŒ์ž„(์‹ฌํ™”)

 

# Quiz) ํ•˜๋Š˜์—์„œ ๋–จ์–ด์ง€๋Š” ๋˜ฅ ํ”ผํ•˜๊ธฐ ๊ฒŒ์ž„์„ ๋งŒ๋“œ์‹œ์˜ค

# [๊ฒŒ์ž„์กฐ๊ฑด]
# 1. ์บ๋ฆญํ„ฐ๋Š” ํ™”๋ฉด ๊ฐ€์žฅ ์•„๋ž˜์— ์œ„์น˜, ์ขŒ์šฐ๋กœ๋งŒ ์ด๋™ ๊ฐ€๋Šฅ
# 2. ๋˜ฅ์€ ํ™”๋ฉด ๊ฐ€์žฅ ์œ„์—์„œ ๋–จ์–ด์ง. x ์ขŒํ‘œ๋Š” ๋งค๋ฒˆ ๋žœ๋ค์œผ๋กœ ์„ค์ •
# 3. ์บ๋ฆญํ„ฐ๊ฐ€ ๋˜ฅ์„ ํ”ผํ•˜๋ฉด ๋‹ค์Œ ๋˜ฅ์ด ๋‹ค์‹œ ๋–จ์–ด์ง
# 4. ์บ๋ฆญํ„ฐ๊ฐ€ ๋˜ฅ๊ณผ ์ถฉ๋Œํ•˜๋ฉด ๊ฒŒ์ž„ ์ข…๋ฃŒ
# 5. FPS ๋Š” 30 ์œผ๋กœ ๊ณ ์ •

# [๊ฒŒ์ž„ ์ด๋ฏธ์ง€]
# 1. ๋ฐฐ๊ฒฝ : 640 * 480 (์„ธ๋กœ ๊ฐ€๋กœ) - background.png
# 2. ์บ๋ฆญํ„ฐ : 70 * 70 - character.png
# 3. ๋˜ฅ : 70 * 70 - enemy.png



from platform import python_branch
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\\Lee_DH\\Desktop\\python prac\\22_04_08_arcade games\\pygame_basic\\background.png")

# ์บ๋ฆญํ„ฐ ๋งŒ๋“ค๊ธฐ
character = pygame.image.load("C:\\Users\\Lee_DH\\Desktop\\python prac\\22_04_08_arcade games\\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
charcter_speed = 10

# ๋˜ฅ ๋งŒ๋“ค๊ธฐ
ddong = pygame.image.load("C:\\Users\\Lee_DH\\Desktop\\python prac\\22_04_08_arcade games\\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 -= charcter_speed
            elif event.key == pygame.K_RIGHT:
                to_x += charcter_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()

 

 

 

 

 

3. ๊ณต ํ„ฐํŠธ๋ฆฌ๊ธฐ ๊ฒŒ์ž„

 

# 1. ๋ชจ๋“  ๊ณต์„ ์—†์• ๋ฉด ๊ฒŒ์ž„ ์ข…๋ฃŒ (์„ฑ๊ณต)
# 2. ์บ๋ฆญํ„ฐ๋Š” ๊ณต์— ๋‹ฟ์œผ๋ฉด ๊ฒŒ์ž„ ์ข…๋ฃŒ (์‹คํŒจ)
# 3. ์‹œ๊ฐ„ ์ œํ•œ 99์ดˆ ์ดˆ๊ณผ ์‹œ ๊ฒŒ์ž„ ์ข…๋ฃŒ (์‹คํŒจ)

from ast import PyCF_ALLOW_TOP_LEVEL_AWAIT
from email.errors import InvalidMultipartContentTransferEncodingDefect
import pygame
import os
#############################################################################
# ๊ธฐ๋ณธ ์ดˆ๊ธฐํ™” (๋ฐ˜๋“œ์‹œ ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ๋“ค)
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_ima_idx = val["img_idx"]
        screen.blit(ball_images[ball_ima_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()

 

 

 

 

 

์ถœ์ฒ˜ : https://youtu.be/Dkx8Pl6QKW0

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.