nomorerest92259979
@nomorerest92259979
Reviews Written
2
Average Rating
5.0
Posts
Q&A
์ด์ ์๋์ ๊ฐ์ ์ค๋ฅ๊ฐ ๋จ๋๋ฐ KSP๊ฐ ๋ฌธ์ ์ธ๊ฑธ๊น์?
์ฝํ๋ฆฐ - ๊ทธ๋๋ค - ํ๋ฌ๊ทธ์ธ ๋ฒ์ ๋ง์ถ๋ ๊ฑธ๋ก ํด๊ฒฐ ํ์ต๋๋ค.
- 0
- 2
- 314
Q&A
์ ๊ทธ๋ฐ์ง ๋ชจ๋ฅด๊ฒ ์ง๋ง ๋ค๋ฅธ ๋ฒ๊ทธ๊ฐ ์์ต๋๋ค.
๋ค์๊ณผ ๊ฐ์ด ์์ฑํ์์ต๋๋ค. # 1. ๋ชจ๋ ๊ณต์ ์์ ๋ฉด ๊ฒ์ ์ข ๋ฃ (์ฑ๊ณต) # 2. ์บ๋ฆญํฐ๋ ๊ณต์ ๋ฟ์ผ๋ฉด ๊ฒ์ ์ข ๋ฃ (์คํจ) # 3. ์๊ฐ ์ ํ 99์ด ์ด๊ณผ ์ ๊ฒ์ ์ข ๋ฃ (์คํจ) 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("๊ฒ์ ์ด๋ฆ") # ๊ฒ์ ์ด๋ฆ # 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๊ฐ ํฌ๊ธฐ์ ๋ํด ๋ฐ๋ก ์ฒ๋ฆฌ) balloon_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"))] # ํ์ ํฌ๊ธฐ์ ๋ฐ๋ฅธ, - ์ต์ด ์๋ ์ค์ balloon_speed_y = [-12, -9, -6, -3] # index 0, 1, 2, 3์ ํด๋นํ๋ ๊ฐ # ํ์ ์ฒ๋ฆฌ balloons = [] balloons.append({ "pos_x" : 50, # ํ์ ์ x ์ขํ "pos_y" : 50, # ํ์ ์ y ์ขํ "img_idx" : 0, # ํ์ ์ ์ด๋ฏธ์ง ์ธ๋ฑ์ "to_x": 3, # x์ถ ์ด๋๋ฐฉํฅ, -3 ์ด๋ฉด ์ผ์ชฝ์ผ๋ก, 3์ด๋ฉด ์ค๋ฅธ์ชฝ์ผ๋ก "to_y": -6, # y์ถ ์ด๋๋ฐฉํฅ, "init_spe_y": balloon_speed_y[0]# y ์ต์ด ์๋ }) # ํ์ ๊ณผ ๋ฌด๊ธฐ ์ถฉ๋์ ์๋ฉธ์ํฌ ๊ฐ๊ฐ์ ์ ๋ณด ์ ์ฅ ๋ณ์ weapon_to_remove = -1 balloon_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 # ๊ฒ์์ด ์งํ์ค์ธ๊ฐ? - True while running: dt = clock.tick(60) # ๊ฒ์ํ๋ฉด์ ์ด๋น ํ๋ ์ ์๋ฅผ ์ค์ (dt = delta) #################################################################################################### # 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 # ๋ฌด๊ธฐ ์์น ์กฐ์ # 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 balloon_idx, balloon_val in enumerate(balloons): #ํ์ ์ index์ ๋ณด์ value๊ฐ์ ๊ฐ์ ธ์ฌ๋ ์ฌ์ฉํ๋ ํจ์ balloon_x_pos = balloon_val["pos_x"] balloon_y_pos = balloon_val["pos_y"] ball_img_idx = balloon_val["img_idx"] balloon_size = balloon_images[ball_img_idx].get_rect().size balloon_width = balloon_size[0] balloon_height = balloon_size[1] # ํ์ ์ด ๊ฐ๋ก๋ฒฝ์ ๋ฟ์์๋ ํ์ ์ ํ๊ธฐ๋ ๋ฐฉํ ๋ณ๊ฒฝ ์ฒ๋ฆฌ (ํ๊ฒจ ๋์ค๋ ํจ๊ณผ) if balloon_x_pos screen_width - balloon_width: balloon_val["to_x"] = balloon_val["to_x"] * -1 # ํ์ ์ด ๋ฌด๋์ ๋ฟ์์๋ ํ์ ์ ํ๊ธฐ๋ ๋ฐฉํ ๋ณ๊ฒฝ ์ฒ๋ฆฌ (ํ๊ฒจ ๋์ค๋ ํจ๊ณผ) if balloon_y_pos >= screen_height - stage_height - balloon_height: balloon_val["to_y"] = balloon_val["init_spe_y"] else: # ๊ทธ ์ธ ๋ชจ๋ ๋ชจ๋ ๊ฒฝ์ฐ์๋ ์๋๋ฅผ ์ฆ๊ฐ balloon_val["to_y"] += 0.5 balloon_val["pos_x"] += balloon_val["to_x"] balloon_val["pos_y"] += balloon_val["to_y"] # 4. ์ถฉ๋ ์ฒ๋ฆฌ # ์บ๋ฆญํฐ rect ์ ๋ณด ์ ๋ฐ์ดํธ character_rect = character.get_rect() character_rect.left = character_x_pos character_rect.top = character_y_pos for balloon_idx, balloon_val in enumerate(balloons): #ํ์ ์ index์ ๋ณด์ value๊ฐ์ ๊ฐ์ ธ์ฌ๋ ์ฌ์ฉํ๋ ํจ์ balloon_x_pos = balloon_val["pos_x"] balloon_y_pos = balloon_val["pos_y"] ball_img_idx = balloon_val["img_idx"] # ํ์ rect ์ ๋ณด ์ ๋ฐ์ดํธ balloon_rect = balloon_images[ball_img_idx].get_rect() balloon_rect.left = balloon_x_pos balloon_rect.top = balloon_y_pos # ํ์ ๊ณผ ์บ๋ฆญํฐ ์ถฉ๋ ์ฒ๋ฆฌ if character_rect.colliderect(balloon_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(balloon_rect): weapon_to_remove = weapon_idx # ํด๋น ๋ฌด๊ธฐ ์์ ๊ธฐ ์ํ ๊ฐ ์ค์ balloon_to_remove = balloon_idx # ํด์ฅ ํ์ ์์ ๊ธฐ ์ํ ๊ฐ ์ค์ # ๊ฐ์ฅ ์์ ํฌ๊ธฐ์ ํ์ ์ด ์๋๋ผ๋ฉด ๋ค์ ๋จ๊ณ์ ํ์ ์ผ๋ก ๋๋ ์ฃผ๊ธฐ if balloon_img_idx -1: del balloons[balloon_to_remove] balloon_to_remove = -1 if weapon_to_remove > -1: del weapons[weapon_to_remove] weapon_to_remove = -1 # ๋ชจ๋ ํ์ ์ ์์ค ๊ฒฝ์ฐ ๊ฒ์ ์ข ๋ฃ (Mission Complete) if len(balloons) == 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(balloons): balloon_x_pos = val["pos_x"] balloon_y_pos = val["pos_y"] balloon_img_idx = val["img_idx"] screen.blit(balloon_images[balloon_img_idx], (balloon_x_pos, balloon_y_pos)) 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(f"Time : {int(total_time - elapsed_time)}", True, (255,255,255)) screen.blit(timer, (10,10)) # ์๊ฐ ์ด๊ณผ์ธ ๊ฒฝ์ฐ if total_time - elapsed_time
- 0
- 4
- 397




