๐ฆ ๋ด์ผ๋ฐฐ์์บ ํ/์ฌ์ ๊ณผ์
[๋ด์ผ๋ฐฐ์์บ ํ] ์ฌ์ ๊ณผ์ - ์ถ์ต์ ์ค๋ฝ์ค ๊ฒ์ ๋ง๋ค๊ธฐ
- -
github : https://github.com/DHL68/github-DHL68/tree/main/22_04_08_arcade%20games
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
'๐ฆ ๋ด์ผ๋ฐฐ์์บ ํ > ์ฌ์ ๊ณผ์ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ด์ผ๋ฐฐ์์บ ํ] ์ฌ์ ๊ณผ์ - ๊ณ์ฐ๊ธฐ ๋ง๋ค๊ธฐ (0) | 2023.01.02 |
---|---|
[๋ด์ผ๋ฐฐ์์บ ํ] ์ฌ์ ๊ณผ์ - ์ถ์ต์ ์ค๋ฝ์ค ๊ฒ์ ๋ง๋ค๊ธฐ (0) | 2023.01.02 |
Contents
์์คํ ๊ณต๊ฐ ๊ฐ์ฌํฉ๋๋ค