Python_9. pygame_1

소고기는레어·2021년 6월 9일
0

Python

목록 보기
9/10
import pygame
from random import *
from pygame.constants import K_LEFT, K_RIGHT



#################################################################################
# 0. 필수 선행(기본 초기화)
# 초기화
pygame.mixer.pre_init(44100, -16, 2, 512)
pygame.mixer.init()
pygame.init() 


# 화면 크기 설정 480*640
screen_width = 480
screen_height = 640
screen = pygame.display.set_mode((screen_width, screen_height))


# 화면 타이틀 설정
pygame.display.set_caption("meteor")


# FPS
clock = pygame.time.Clock() 
#################################################################################



# 1. 사용자 게임 초기화 (배경 화면, 게임 이미지, 좌표, 속도, 폰트 등)
background = pygame.image.load("/Users/song/Documents/Python workspace/pygame_basic/background2.png")
character = pygame.image.load("/Users/song/Documents/Python workspace/pygame_basic/character2.png")
meteor = pygame.image.load("/Users/song/Documents/Python workspace/pygame_basic/enemy.png")
meteor2 = pygame.image.load("/Users/song/Documents/Python workspace/pygame_basic/enemy.png")
weapon = pygame.image.load("/Users/song/Documents/Python workspace/pygame_basic/weapon.png")
life_image = pygame.image.load("/Users/song/Documents/Python workspace/pygame_basic/life_image.png")
life_item = pygame.image.load("/Users/song/Documents/Python workspace/pygame_basic/life_image2.png")
nice = pygame.mixer.Sound("/Users/song/Documents/Python workspace/pygame_basic/nice.wav")
bad = pygame.mixer.Sound("/Users/song/Documents/Python workspace/pygame_basic/damaged.wav")
level_sound = pygame.mixer.Sound("/Users/song/Documents/Python workspace/pygame_basic/levelup.wav")
get_item = pygame.mixer.Sound("/Users/song/Documents/Python workspace/pygame_basic/item.mp3")
countdown = pygame.mixer.Sound("/Users/song/Documents/Python workspace/pygame_basic/countdown.wav")
over_sound = pygame.mixer.Sound("/Users/song/Documents/Python workspace/pygame_basic/over.wav")
over_sound2 = pygame.mixer.Sound("/Users/song/Documents/Python workspace/pygame_basic/over2.wav")
pygame.mixer.music.load("/Users/song/Documents/Python workspace/pygame_basic/bgm.mp3")
pygame.mixer.music.play(-1)
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 - 20

life_size = life_image.get_rect().size
life_width = life_size[0]
life_height = life_size[1]

meteor_size = meteor.get_rect().size
meteor_width = character_size[0]
meteor_height = character_size[1]
meteor_y_pos = -1000
meteor_x_pos = randint(0, screen_width - meteor_width)
meteor_speed = 0.6

meteor2_size = meteor2.get_rect().size
meteor2_width = character_size[0]
meteor2_height = character_size[1]
meteor2_y_pos = -1000
meteor2_x_pos = randint(0, screen_width - meteor2_width)
meteor2_speed = 0.3

weapon_size = weapon.get_rect().size
weapon_width = weapon_size[0]
weapon_height = weapon_size[1]
weapon_speed = 2
weapon_to_remove = -1
weapons = []

life_item_size = life_item.get_rect().size
life_item_width = life_item_size[0]
life_item_height = life_item_size[1]
life_item_y_pos = 0 - life_item_height
life_item_x_pos = randint(0, screen_width - life_item_width)
life_item_speed = 0
life_item_spawn = 10

score = 0
life = 3
level_up = 10
level = 1

to_x = 0

character_speed = 0.5

start_tick = pygame.time.get_ticks()

game_font = pygame.font.Font(None, 40)
level_font = pygame.font.Font(None, 100)
game_result = "Game Over!"


total_time = 60
start_ticks = pygame.time.get_ticks()


running = True
while running:
    dt = clock.tick(60)
    # 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 -= character_speed
            elif event.key == pygame.K_RIGHT:
                to_x += character_speed
            elif event.key == pygame.K_SPACE:
                    pygame.mixer.Sound.play(bad)
                    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:
                to_x += character_speed
            elif event.key == pygame.K_RIGHT:
                to_x -= character_speed
            elif event.key == pygame.K_SPACE:
                pass

    # 3. 게임 캐릭터 위치 정의
    character_x_pos += to_x * 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


    weapons = [ [w[0], w[1] - weapon_speed * dt] for w in weapons ]
    weapons = [ [w[0], w[1]] for w in weapons if w[1] > 0]

    meteor_y_pos += meteor_speed * dt
    meteor2_y_pos += meteor2_speed * dt
    life_item_y_pos += life_item_speed * dt

    
    


   


    # 4. 충돌 처리
    character_rect = character.get_rect()
    character_rect.left = character_x_pos
    character_rect.top = character_y_pos

    meteor_rect = meteor.get_rect()
    meteor_rect.left = meteor_x_pos
    meteor_rect.top = meteor_y_pos

    meteor2_rect = meteor2.get_rect()
    meteor2_rect.left = meteor2_x_pos
    meteor2_rect.top = meteor2_y_pos

    life_item_rect = life_item.get_rect()
    life_item_rect.left = life_item_x_pos
    life_item_rect.top = life_item_y_pos



    if character_rect.colliderect(meteor_rect):
        pygame.mixer.Sound.play(bad)
        meteor_y_pos = 0 - meteor_height
        meteor_x_pos = randint(0, screen_width - meteor_width)
        score -= 5
        life -= 1   
    elif meteor_y_pos >= screen_height:
        pygame.mixer.Sound.play(nice)
        meteor_y_pos = 0 - meteor_height
        meteor_x_pos = randint(0, screen_width - meteor_width)
        score += 1    

    if character_rect.colliderect(meteor2_rect):
        pygame.mixer.Sound.play(bad)
        meteor2_y_pos = 0 - meteor2_height
        meteor2_x_pos = randint(0, screen_width - meteor2_width)
        score -= 5
        life -= 1   
    elif meteor2_y_pos >= screen_height:
        pygame.mixer.Sound.play(nice)
        meteor2_y_pos = 0 - meteor2_height
        meteor2_x_pos = randint(0, screen_width - meteor2_width)
        score += 1    

    for weapon_idx, weapon_val in enumerate(weapons):
        weapon_x_pos = weapon_val[0]
        weapon_y_pos = weapon_val[1]
        weapon_rect = weapon.get_rect()
        weapon_rect.left = weapon_x_pos
        weapon_rect.top = weapon_y_pos

        if weapon_rect.colliderect(meteor_rect) or weapon_rect.colliderect(meteor2_rect):
            if weapon_rect.colliderect(meteor_rect):
                pygame.mixer.Sound.play(nice)
                meteor_y_pos = 0 - meteor_height
                meteor_x_pos = randint(0, screen_width - meteor_width)
                weapon_to_remove = weapon_idx
                score += 5
            elif weapon_rect.colliderect(meteor2_rect):
                pygame.mixer.Sound.play(nice)
                meteor2_y_pos = 0 - meteor2_height
                meteor2_x_pos = randint(0, screen_width - meteor2_width)
                weapon_to_remove = weapon_idx
                score += 5

        if weapon_to_remove > -1:
            del weapons[weapon_to_remove]
            weapon_to_remove = -1
        
        break


    elapsed_time = (pygame.time.get_ticks() - start_ticks) / 1000
    # if elapsed_time >= life_item_spawn :
    #     if life < 3:
    #         life_item_speed = 0.3
    #         life_item_spawn += 20
    #     else:
    #         life_item_spawn += 20

    if elapsed_time >= life_item_spawn :
            life_item_speed = 0.3
            life_item_spawn += 10

        

    
    if character_rect.colliderect(life_item_rect):
        pygame.mixer.Sound.play(get_item)
        life_item_speed = 0
        life_item_y_pos = 0 - life_item_height
        life_item_x_pos = randint(0, screen_width - life_item_width)
        if life < 3:
            life += 1
            total_time += 15
        else:
            score += 20
            total_time += 15
    elif life_item_y_pos >= screen_height:
        life_item_speed = 0
        life_item_y_pos = 0 - life_item_height
        life_item_x_pos = randint(0, screen_width - life_item_width)
    

    if elapsed_time > level_up + 1:
        pygame.mixer.Sound.play(level_sound)
        meteor_speed += 0.1
        meteor2_speed += 0.1
        level_up += 10
        level += 1

    # if int(total_time - elapsed_time) == 3:
    #     pygame.mixer.Sound.play(countdown)
    # elif int(total_time - elapsed_time) == 2:
    #     pygame.mixer.Sound.play(countdown)
    # elif int(total_time - elapsed_time) == 1:
    #     pygame.mixer.Sound.play(countdown)




    # 5. 화면에 출력하기
    screen.blit(background, (0, 0))
    level_card = level_font.render("STAGE " + str(level), True, (95, 97, 98))
    level_card_width = level_card.get_rect().size[0]
    level_card_height = level_card.get_rect().size[1]
    level_rect = level_card.get_rect(center=(int(screen_width / 2), int(screen_height / 2)))
    for weapon_x_pos, weapon_y_pos in weapons:
        screen.blit(weapon, (weapon_x_pos, weapon_y_pos))
    screen.blit(level_card, level_rect)
    screen.blit(character, (character_x_pos, character_y_pos))
    screen.blit(meteor, (meteor_x_pos, meteor_y_pos))
    screen.blit(meteor2, (meteor2_x_pos, meteor2_y_pos))
    screen.blit(life_item, (life_item_x_pos, life_item_y_pos))



    
    timer = game_font.render("Time : " + str(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

    score_card = game_font.render("Score : " + str(score), True, (255, 255, 255))
    # score_card_width = score_card.get_rect().size[0]
    # score_card_height = score_card.get_rect().size[1]
    # score_rect = score_card.get_rect(center=(int(screen_width - score_card_width), 20))
    screen.blit(score_card, (330, 10))
    
    
    
    # if life >= 3:
    #     screen.blit(life_image, (screen_width / 2 - life_width, 0))
    #     screen.blit(life_image, (screen_width / 2, 0))
    # elif life == 2:
    #     screen.blit(life_image, (screen_width / 2 - life_width, 0))
    # elif life <= 0:
    #     screen.blit(over_text, (50, screen_height / 2))
    #     running = False
    if life >= 3:
        screen.blit(life_image, (5, screen_height - life_height))
        screen.blit(life_image, (5 + life_width, screen_height - life_height))
    elif life == 2:
        screen.blit(life_image, (5, screen_height - life_height))
    elif life <= 0:
        game_result = "Game Over!"
        running = False


    pygame.display.update()

pygame.mixer.Sound.play(over_sound2)
msg = game_font.render(game_result, True, (255, 255, 255))
# result1 = game_font.render("Stage : " + str(level), True, (255, 255, 255))
result2 = game_font.render("Score : " + str(score), True, (255, 255, 255))
msg_rect = msg.get_rect(center=(int(screen_width / 2), int(screen_height / 2 - 50)))
# result1_rect = result1.get_rect(center=(int(screen_width / 2), int(screen_height / 2)))
result2_rect = result2.get_rect(center=(int(screen_width / 2), int(screen_height / 2 + 50)))
level_card = False
screen.blit(msg, msg_rect)
# screen.blit(result1, result1_rect)
screen.blit(result2, result2_rect)
pygame.display.update()
pygame.time.delay(5000)


pygame.quit()
profile
https://www.rarebeef.co.kr/

0개의 댓글