
ํ์ด์ฌ์ ๋ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค ํ๋์ธ pygame์ ์ด์ฉํด Pong! ๊ฒ์์ ๋ง๋ค์ด๋ณด์์ต๋๋ค.
ํ์ด์ฌ์ผ๋ก ๋ง๋ค๊ธฐ ์ ์ C / C++ ๋ก ๊ตฌํํด๋ณด๋ ๋ฐฉ๋ฒ๋ ์ฐพ์๋ดค๋๋ฐ raylib ์ธํ ํ๋ค๊ฐ ๊ฒฐ๊ตญ ์๋์ด์ ์๊ฐ ๋ค ๋ ๋ ค๋จน๊ณ ..
๋น๊ต์ ๊ฐ๋จํ ํ์ด์ฌ์ผ๋ก ๊ตฌํํ๊ธฐ๋ก ๋ง์์ ํ์์ต๋๋ค. ํํซ..
ํผ์ ๋ง๋ค๊ธฐ์ ์ด๋ ค์์ ์ ํ๋ธ์ ํ์ ๋น๋ ธ์ต๋๋ค! ^^
'๋๋์ฝ๋ฉ' ๋์ ํ์ด์ฌ ๊ฒ์ ๋ง๋ค๊ธฐ ์์์ ์ฐธ์กฐํ์์ต๋๋ค.
๊ธฐ๋ณธ์ ์ธ ํ์ด์ฌ ์ง์ ํน์ ํ๋ก๊ทธ๋๋ฐ ์ง์์ด ์์ด๋ ์ถฉ๋ถํ ๋ฐ๋ผํ ์ ์์ ์ ๋๋ก ์ค๋ช ์ ๊ต์ฅํ ์์ธํ ์ํด์ฃผ์ญ๋๋ค.
๊ฐ์ธ์ ์ผ๋ก ์ฝ๋ ์์ฑํ ๊ฒ ๋ณด๋ค ์ด๋ฏธ์ง ์ฐพ๋ ์๊ฐ์ด ๋ ๊ฑธ๋ ธ์ด์..๐
์ ๋ ์ํผ๋ง๋ฆฌ์ค ์ด๋ฏธ์ง๋ก ๊ตฌํํด๋ณด์์ต๋๋ค.
์๋ฆฌ๋ ๋ฃ์ผ๋ ํจ์ฌ ๋ ๊ฒ์ ๊ฐ์์ง๋๋ผ๊ตฌ์. ์ข์ ์ ํ์ด์์ต๋๋ค.
# 1. ๋ชจ๋ ๊ณต์ ์์ ๋ฉด ๊ฒ์ ์ข
๋ฃ (์ฑ๊ณต)
# 2. ์บ๋ฆญํฐ๊ฐ ๊ณต์ ๋ง์์ ๋ (์คํจ)
# 3. ์๊ฐ ์ ํ 99์ด ์ด๊ณผ ์ ๊ฒ์ ์ข
๋ฃ (์คํจ)
import os
import pygame
import time
##############################################################
# ๊ธฐ๋ณธ ์ด๊ธฐํ (๋ฐ๋์ ํด์ผ ํ๋ ๊ฒ๋ค)
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()
# ๊ฒ์ ์์ ์ฌ์ด๋
start_sound = pygame.mixer.Sound("C:/Python/Python_2023_01_Game_Pong/sound/game-start-6104.mp3")
start_sound.play()
bgm_sound = pygame.mixer.Sound("C:/Python/Python_2023_01_Game_Pong/sound/bgm.mp3")
bgm_sound.play(-1)
##############################################################
# 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, "ball1.png")),
pygame.image.load(os.path.join(image_path, "ball2.png")),
pygame.image.load(os.path.join(image_path, "ball3.png")),
pygame.image.load(os.path.join(image_path, "ball4.png"))]
# ๊ณต ํฌ๊ธฐ์ ๋ฐ๋ฅธ ์ต์ด ์คํผ๋
ball_speed_y = [-18, -15, -12, -12] # 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
# ํฐํธ ์ ์
game_font = pygame.font.Font(None, 40)
total_time = 31
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):
bgm_sound.stop()
gameover_sound = pygame.mixer.Sound("C:/Python/Python_2023_01_Game_Pong/sound/gameover.mp3")
gameover_sound.play()
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):
hit_sound = pygame.mixer.Sound("C:/Python/Python_2023_01_Game_Pong/sound/hit.mp3")
hit_sound.play()
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๋ฌธ ํ๋ฒ์ ํ์ถ ๊ฐ๋ฅ
# ์ถฉ๋๋ ๊ณต 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:
bgm_sound.stop()
win_sound = pygame.mixer.Sound("C:/Python/Python_2023_01_Game_Pong/sound/win.mp3")
win_sound.play()
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
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:
bgm_sound.stop()
gameover_sound = pygame.mixer.Sound("C:/Python/Python_2023_01_Game_Pong/sound/gameover.mp3")
gameover_sound.play()
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()
pygame.time.delay(2500)
pygame.quit()
๋ฐฐํฌ ํ ์ค ๋ชฐ๋ผ์..๋์์์ผ๋ก ์ฌ๋ฆฌ๋ ๋๋ ์ฌ๋..
์ถํ ์์ฉํด์ ์ถ์ต์ ํผ์นด์ธ ๋ฐฐ๊ตฌst ๊ฒ์ ๋ง๋ค์ด ๋ณผ ์๊ฐ์ธ๋ฐ ๊ทธ ๋๋ ๋ฐฐํฌ๊น์ง ๋์ ํด๋ณด๊ฒ ์ต๋๋ค.
(๊ณผ์ฐ?
'๐ฉโ๐ป Language > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ํ์ด์ฌPython - ์น ํฌ๋กค๋ง ๋ฏธ๋ ํ๋ก์ ํธ(์์ ์์, ํ์ฌ ์๊ฐ,๋ ์จ, ์ฝ๋ก๋19 ํ์ง์ ํํฉ, ๋ด์ค ํค๋๋ผ์ธ) (2) | 2022.12.30 |
|---|