나의 메모장/파이썬 나의 메모장

4월 26일 TIL (pygame)

khw7876 2022. 4. 26. 23:42

오늘 굉장히 많은 코드를 쓰고 오류가 나와서 지웠다 ㅋㅋㅋㅋ

1. 우선 오늘 추가했던 타이머

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

screen.fill((0, 0, 255))
elapsed_time = (pygame.time.get_ticks() - start_ticks) / 1000
timer = game_font.render(str(int(total_time - elapsed_time)), True, (255, 255, 255))


screen.blit(timer, (10, 10))

총 99초의 시간에서 1초마다 1씩 빠져나가게 타이머를 표시!

 

2. 텍스트를 pygame으로 표현하고 싶을 경우 기입

text_stage = font.render("STAGE: ", True, (255, 255, 255))
screen.blit(text_stage, (8,150))

등등을 종합해서 드디어 완성한 완성본..

import pygame
import random
import os, threading, time
from collections import deque


pygame.init()

pygame.mixer.init()
pygame.mixer.music.load('mypygame/music/music.mp3')
pygame.mixer.music.play(10)
shot_sound = pygame.mixer.Sound('mypygame/music/hit.ogg')

screen_width = 640
screen_height = 750
screen = pygame.display.set_mode((screen_width, screen_height))
game_font = pygame.font.Font(None, 40)
total_time = 99
start_ticks = pygame.time.get_ticks()

pygame.display.set_caption("pump")


clock = pygame.time.Clock()


current_path = os.path.dirname(__file__)
image_path = os.path.join(current_path, "mypygame/images")


background = pygame.image.load(os.path.join(image_path, "background2.png"))
side1 = pygame.image.load(os.path.join(image_path, "side_left.png"))
side2 = pygame.image.load(os.path.join(image_path, "side_right.png"))
field1 = pygame.image.load(os.path.join(image_path, "field.png"))
field2 = pygame.image.load(os.path.join(image_path, "field2.png"))

side_size = side1.get_rect().size
side_width = side_size[0]

field_size = field1.get_rect().size
field_width = field_size[0]
field_height = field_size[1]

#좌표값을 준다면, 그 좌표에
object1 = pygame.image.load(os.path.join(image_path, "object_left.png"))
object2 = pygame.image.load(os.path.join(image_path, "object_up.png"))
object3 = pygame.image.load(os.path.join(image_path, "objet_space.png"))
object4 = pygame.image.load(os.path.join(image_path, "object_down.png"))
object5 = pygame.image.load(os.path.join(image_path, "object_right.png"))

target1 = pygame.image.load(os.path.join(image_path, "target_left.png"))
target2 = pygame.image.load(os.path.join(image_path, "target_up.png"))
target3 = pygame.image.load(os.path.join(image_path, "target_space.png"))
target4 = pygame.image.load(os.path.join(image_path, "target_down.png"))
target5 = pygame.image.load(os.path.join(image_path, "target_right.png"))



targets = [
pygame.image.load(os.path.join(image_path, "target_left.png")),
pygame.image.load(os.path.join(image_path, "target2.png")),
pygame.image.load(os.path.join(image_path, "target3.png")),
pygame.image.load(os.path.join(image_path, "target4.png")),
pygame.image.load(os.path.join(image_path, "target5.png")),


]
# objects = deque([], maxlen=5)


object_x_pos = 0
object_y_pos = 5
object_speed = 10


object_size = object1.get_rect().size
object_width = object_size[0]
object_height = object_size[1]

target1_size = target1.get_rect().size
target_width = target1_size[0]
target_height = target1_size[1]
target1_x_pos = side_width + object_width * 0
target1_y_pos = screen_height - target_height - 5

target1_size = target2.get_rect().size
target2_x_pos = side_width + object_width * 1
target2_y_pos = screen_height - target_height - 5

target3_size = target3.get_rect().size
target3_x_pos = side_width + object_width * 2
target3_y_pos = screen_height - target_height - 5

target4_size = target4.get_rect().size
target4_x_pos = side_width + object_width * 3
target4_y_pos = screen_height - target_height - 5

target5_size = target5.get_rect().size
target5_x_pos = side_width + object_width * 4
target5_y_pos = screen_height - target_height - 5

object_to_remove = -1
targets = [
        [[side_width + object_width * 0], [screen_height - target_height - 5]],
        [[side_width + object_width * 1], [screen_height - target_height - 5]],
        [[side_width + object_width * 2], [screen_height - target_height - 5]],
        [[side_width + object_width * 3], [screen_height - target_height - 5]],
        [[side_width + object_width * 4], [screen_height - target_height - 5]]

    ]

#타이머 사용
objects1 = []
objects2 = []
objects3 = []
objects4 = []
objects5 = []

lifes = 3
penalty = 0
score = 0
count = 0
level = 15
running = True
while running:
    dt = clock.tick(60)


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            #왼쪽키 눌렀을 경우
            if event.key == pygame.K_LEFT:
                for object_idx, object_val in enumerate(objects1):
                    object_x_pox = object_val[0]
                    object_y_pox = object_val[1]

                    if object_y_pox > screen_height - target_height*1.5 :
                        print("왼쪽 키")
                        object_to_remove = object_idx
                        score += 1
                        print(score)

            if object_to_remove > -1:
                del objects1[object_to_remove]
                print(objects1)
                object_to_remove = -1


            #위 키 눌렀을 경우
            if event.key == pygame.K_UP :
                for object_idx, object_val in enumerate(objects2):
                    object_x_pox = object_val[0]
                    object_y_pox = object_val[1]

                    if object_y_pox > screen_height - target_height*2  :
                        score += 1
                        print(score)
                        object_to_remove = object_idx
            if object_to_remove > -1:
                del objects2[object_to_remove]
                object_to_remove = -1
            if event.key == pygame.K_SPACE:
                for object_idx, object_val in enumerate(objects3):
                    object_x_pox = object_val[0]
                    object_y_pox = object_val[1]

                    # img_scale = pygame.transform.scale(target_space, (1280, 720))


                    if object_y_pox > screen_height - target_height*2 :
                        print("스페이스바")

                        score += 1
                        print(score)
                        object_to_remove = object_idx
            if object_to_remove > -1:
                del objects3[object_to_remove]
                object_to_remove = -1
            if event.key == pygame.K_DOWN:
                for object_idx, object_val in enumerate(objects4):
                    object_x_pox = object_val[0]
                    object_y_pox = object_val[1]

                    if object_y_pox > screen_height - target_height * 2:
                        print("아래 키")
                        score += 1
                        print(score)
                        object_to_remove = object_idx
            if object_to_remove > -1:
                del objects4[object_to_remove]
                print(objects1)
                object_to_remove = -1
            if event.key == pygame.K_RIGHT:
                for object_idx, object_val in enumerate(objects5):
                    object_x_pox = object_val[0]
                    object_y_pox = object_val[1]

                    if object_y_pox > screen_height - target_height * 2:
                        print("오른쪽 키")

                        score += 1
                        print(score)
                        object_to_remove = object_idx


            if object_to_remove > -1:
                del objects5[object_to_remove]
                object_to_remove = -1

                target = targets[0]
                target_x_pos = target[0]
                target_y_pos = target[1]

    for object_idx, object_val in enumerate(objects1):
        object_x_pox = object_val[0]
        object_y_pox = object_val[1]


        if object_y_pox >= screen_height -10:
            print("놓쳤습니다")
            pygame.mixer.Sound.play(shot_sound)
            penalty += 1

    for object_idx, object_val in enumerate(objects2):
        object_x_pox = object_val[0]
        object_y_pox = object_val[1]


        if object_y_pox >= screen_height -10:
            print("놓쳤습니다")
            pygame.mixer.Sound.play(shot_sound)
            penalty += 1

    for object_idx, object_val in enumerate(objects3):
        object_x_pox = object_val[0]
        object_y_pox = object_val[1]


        if object_y_pox >= screen_height -10:
            print("놓쳤습니다")
            pygame.mixer.Sound.play(shot_sound)
            penalty += 1

    for object_idx, object_val in enumerate(objects4):
        object_x_pox = object_val[0]
        object_y_pox = object_val[1]


        if object_y_pox >= screen_height -10:
            print("놓쳤습니다")
            pygame.mixer.Sound.play(shot_sound)
            penalty += 1

    for object_idx, object_val in enumerate(objects5):
        object_x_pox = object_val[0]
        object_y_pox = object_val[1]


        if object_y_pox >= screen_height -10:
            print("놓쳤습니다")
            print(object_y_pox)
            penalty += 1
    if penalty > 3 :
        print("게임 오버")
        running = False
        pygame.time.delay(1400)
##!! objects에서 x값을 찾아야함


    #레벨에 따라서 카운터가 낮아지는 것으로 ㄱㄱ
    if count > level:
        # print("counter = " , count)
        object_x_pos = random.randrange(side_width + object_width * 0, side_width + object_width * 5, object_width)
        object_y_pos = 5
        if object_x_pos == side_width + object_width * 0:
            objects1.append([object_x_pos, object_y_pos])
        if object_x_pos == side_width + object_width * 1:
            objects2.append([object_x_pos, object_y_pos])
        if object_x_pos == side_width + object_width * 2:
            objects3.append([object_x_pos, object_y_pos])
        if object_x_pos == side_width + object_width * 3:
            objects4.append([object_x_pos, object_y_pos])
        if object_x_pos == side_width + object_width * 4:
            objects5.append([object_x_pos, object_y_pos])


        level = 15
        count = 0
    if object_to_remove > -1 :
        del objects1[object_to_remove]
        object_to_remove = -1

    if score < 20:
        object_speed = 10
        level = 15
        stage = 1
    elif score < 40:
        level = 13
        object_speed = 15
        stage = 2
    else:
        object_speed = 20
        stage = 3
        level = 10

    object_y_pos += object_speed
    # objects = [[w[0], w[1] + object_speed] for w in objects]
    # objects = [[w[0], w[1]]for w in objects if w[1] < screen_height]

    objects1 = [[w[0], w[1] + object_speed] for w in objects1]
    objects1 = [[w[0], w[1]] for w in objects1 if w[1] < screen_height]

    objects2 = [[w[0], w[1] + object_speed] for w in objects2]
    objects2 = [[w[0], w[1]] for w in objects2 if w[1] < screen_height]

    objects3 = [[w[0], w[1] + object_speed] for w in objects3]
    objects3 = [[w[0], w[1]] for w in objects3 if w[1] < screen_height]

    objects4 = [[w[0], w[1] + object_speed] for w in objects4]
    objects4 = [[w[0], w[1]] for w in objects4 if w[1] < screen_height]

    objects5 = [[w[0], w[1] + object_speed] for w in objects5]
    objects5 = [[w[0], w[1]] for w in objects5 if w[1] < screen_height]



    screen.fill((0, 0, 255))
    elapsed_time = (pygame.time.get_ticks() - start_ticks) / 1000

    timer = game_font.render(str(int(total_time - elapsed_time)), True, (255, 255, 255))
    scores = game_font.render(str(int(score)), True, (255,255,255))
    life = game_font.render(str(int(lifes- penalty)), True, (255,255,255))
    stage = game_font.render(str(int(stage)),True,(255,255,255))

    screen.blit(background,(0,0))
    screen.blit(side1, (0, 0))
    screen.blit(side2, (screen_width - side_width, 0))
    screen.blit(field1, (side_width, 0))
    screen.blit(field2, (side_width, screen_height - field_height))


    screen.blit(target1, (side_width + object_width * 0, screen_height - target_height - 5))
    screen.blit(target2, (side_width + object_width * 1, screen_height - target_height - 5))
    screen.blit(target3, (side_width + object_width * 2, screen_height - target_height - 5))
    screen.blit(target4, (side_width + object_width * 3, screen_height - target_height - 5))
    screen.blit(target5, (side_width + object_width * 4, screen_height - target_height - 5))



    for object_x_pos, object_y_pos in objects1:
        if object_x_pos == side_width + object_width * 0:
            screen.blit(object1, (object_x_pos, object_y_pos))
    for object_x_pos, object_y_pos in objects2:
        if object_x_pos == side_width + object_width * 1:
            screen.blit(object2, (object_x_pos, object_y_pos))
    for object_x_pos, object_y_pos in objects3:
        if object_x_pos == side_width + object_width * 2:
            screen.blit(object3, (object_x_pos, object_y_pos))
    for object_x_pos, object_y_pos in objects4:
        if  object_x_pos == side_width + object_width * 3:
            screen.blit(object4, (object_x_pos, object_y_pos))
    for object_x_pos, object_y_pos in objects5:
        if  object_x_pos == side_width + object_width * 4:
            screen.blit(object5, (object_x_pos, object_y_pos))

    font = pygame.font.SysFont("arial", 30, True, True)
    text_stage = font.render("STAGE: ", True, (255, 255, 255))
    text_life = font.render("LIFE : ", True, (255, 255, 255))
    text_score = font.render("SCORE : ",True, (255, 255, 255))

    screen.blit(object1, (side_width + object_width * 0, 5))
    screen.blit(object2, (side_width + object_width * 1, 5))
    screen.blit(object3, (side_width + object_width * 2, 5))
    screen.blit(object4, (side_width + object_width * 3, 5))
    screen.blit(object5, (side_width + object_width * 4, 5))
    screen.blit(timer, (10, 10))
    screen.blit(scores, (120,87))
    screen.blit(text_life, (10,116))
    screen.blit(life, (88, 122))
    screen.blit(text_stage, (8,150))
    screen.blit(stage, (100,155))
    screen.blit(text_score,(10,80))

    count += 1

    pygame.display.update()

pygame.quit()