๊ณ์ ์ฌ๋ผ๊ฐ์
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() ########################################################################################## #1 ์ฌ์ฉ์ ๊ฒ์ ์ด๊ธฐํ (๋ฐฐ๊ฒฝ ํ๋ฉด, ๊ฒ์ ์ด๋ฏธ์ง, ์ขํ, ์๋, ํฐํธ ๋ฑ) #๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ๋ถ๋ฌ์ค๊ธฐ current_path = os.path.dirname(__file__) #ํ์ฌํ์ผ ์์น ๋ฐํ image_path = os.path.join(current_path,"image") #image ํด๋ ์์น ๋ฐํ 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-character_width)/2 character_y_pos = screen_height-stage_height-character_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_image = [ pygame.image.load(os.path.join(image_path,"balloon_1.png")), pygame.image.load(os.path.join(image_path,"balloon_2.png")), pygame.image.load(os.path.join(image_path,"balloon_3.png")), pygame.image.load(os.path.join(image_path,"balloon_4.png")) ] #๊ณต ํฌ๊ธฐ์ ๋ฐ๋ฅธ ์ต์ด ์คํผ๋ ball_speed_y=[-18,-15,-12,-9] #๊ณต ์ฌ๋ฌ๊ฐ balls = [] #์ต์ด ๋ฐ์ํ๋ ํฐ๊ณต balls.append({ "pos_x" : 50, "pos_y" : 100, "img_idx" : 0, "to_x" : 3, "to_y" : -6, "init_spd_y" : ball_speed_y[0] }) #์ด๋ฒคํธ ๋ฃจํ runnung = True #๊ฒ์์ด ์งํ์ค์ธ๊ฐ? while runnung: dt = clock.tick(30) #๊ฒ์ ํ๋ฉด์ ์ด๋น ํ๋ ์ ์๋ฅผ ์ค์ #print("fps : "+ str(clock.get_fps())) # 2. ์ด๋ฒคํธ ์ฒ๋ฆฌ (ํค๋ณด๋, ๋ง์ฐ์ค ๋ฑ) for event in pygame.event.get(): #์ด๋ค ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์๋๊ฐ? if event.type == pygame.QUIT: #์ฐฝ์ด ๋ซํ๋ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ์๋๊ฐ? runnung = 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 #๋ฌด๊ธฐ ์์น ์กฐ์ 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_image[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 >= stage_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"] screen.blit(background,(0,0)) for weapon_x_pos, weapon_y_pos in weapons: screen.blit(weapon,(int(weapon_x_pos),int(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_image[ball_img_idx],(int(ball_pos_x),int(ball_pos_y))) screen.blit(stage,(0,screen_height-stage_height)) screen.blit(character,(int(character_x_pos),int(character_y_pos))) pygame.display.update() # ๊ฒ์ํ๋ฉด์ ๋ค์ ๊ทธ๋ฆฌ๊ธฐ #pygame ์ข
๋ฃ pygame.quit()