MAC pygame์ ์ค๋ฅ๊ฐ ์๋ค๊ณ ๋์ค๋ค์.
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 Game") # ๊ฒ์์ด๋ฆ # 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 Out(์๊ฐ ๋ด ์ข
๋ฃ ๋ชปํจ) # 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 screen_width - character_width: character_x_pos = screen_width - character_width # ๋ฌด๊ธฐ ์์น ์กฐ์ 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 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 -1: del weapons[weapon_to_remove] weapon_to_remove = -1 # ์ถฉ๋๋ ๊ณต ์์ ๊ธฐ if ball_to_remove > -1: del balls[ball_to_remove] ball_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 int(total_time - elapsed_time)