강의

멘토링

로드맵

인프런 커뮤니티 질문&답변

김순희님의 프로필 이미지
김순희

작성한 질문수

파이게임 버그 도와주세요,,,,

작성

·

514

·

수정됨

0

import pygame
from turtle import back
import time

pygame.init()
def text_0():
    screen.fill((0,0,0)) # 배경 검은색 배경
    pygame.display.update()  #게임화면 다시그리기
    sound.play()
    time.sleep(9)
    sound.stop()
    screen.blit(iphone,(0,0))  #배경 그리기
    pygame.display.update()  #게임화면 다시그리기
    time.sleep(4)
    
def text_1():
    
    text=["어...어라?","음... 꿈인가?","에이 설마~","망했다!!!!!!!!!!!!!!!!!!"]
    check_text=False
    global check_break
    check_break=0
    for i in text:
        check_text=0
        for j in range(len(i)):
            for event in pygame.event.get():
                
                    
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        running_esc=True
                        
                        #이부분
                        ##################################
                        while running_esc:
                            dt = clock.tick(60)  
                            screen.blit(esc,(0,0))
                            mouse_x,mouse_y=pygame.mouse.get_pos()#좌표구하기
                               
                            if (out_button_x_pos<mouse_x<out_button_x_pos+out_button_width 
                                and out_button_y_pos<mouse_y<out_button_y_pos+out_button_height):
                                screen.blit(out_touch_button,(out_button_x_pos,out_button_y_pos))
                                
                            else:
                                screen.blit(out_button,(out_button_x_pos,out_button_y_pos))
                            pygame.display.update()
                        ###################################
                    if event.key == pygame.K_SPACE:
                        screen.blit(room,(0,0))  
                        myText = myFont.render(i, True, (0,0,255)) 
                        screen.blit(myText, (1000,900)) 
                        pygame.display.update()
                        check_text=True
                        
            if check_text:
                break            
            screen.blit(room,(0,0))  
            myText = myFont.render(i[0:j], True, (0,0,255)) 
            screen.blit(myText, (1000,900)) 
            pygame.display.update()
            time.sleep(0.3)
        
        while check_text:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        check_text=False


#화면크기 설정
screen_width=1920  #가로
screen_height=1020  #세로
screen=pygame.display.set_mode((screen_width, screen_height))#(가로,세로)

#게임 이름
pygame.display.set_caption("Nado Game")  

#FPS
clock = pygame.time.Clock()

#배경 이미지 
background = pygame.image.load("C:/python/pygame_basic/background_beta.png")
start = pygame.image.load("C:/python/pygame_basic/start_beta.png")
iphone = pygame.image.load("C:/python/pygame_basic/iPhone_beta.png")
room =pygame.image.load("C:/python/pygame_basic/room_beta.png")
esc=pygame.image.load("C:\python\pygame_basic\esc_beta.png")


#버튼 이미지
out_touch_button=pygame.image.load("C:/python/pygame_basic/break_change_button.png")
out_button=pygame.image.load("C:/python/pygame_basic/break_button.png")
out_button_size = out_button.get_rect().size  
out_button_width = out_button_size[0]  
out_button_height = out_button_size[1] 
out_button_x_pos = (screen_width / 2) - (out_button_width/2) 
out_button_y_pos = (screen_height / 2)-(out_button_height/2) 

#캐릭터 가져오기
character = pygame.image.load("C:/python/pygame_basic/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 #화면 세로 크기 가장 아래에 해당하는 곳에 위치(세로)

#캐릭터 좌표
to_x=0
to_y=0

#이동 속도
character_speed = 0.6


#효과음,배경음악
sound = pygame.mixer.Sound( "C:\python\pygame_basic\iPhone-Alarm-Original (online-audio-converter.com).wav" )

#폰트설정
myFont = pygame.font.SysFont("malgungothic",30)  





#이벤트 루프 
global running 
running = True
text_namber=1  #진행도


while running:
    dt = clock.tick(60)  #게임화면의 초당 프레임 수 설정
    #종료문
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.type == pygame.K_ESCAPE:
                running_esc=True
                #이부분
                #####################################
                while running_esc:
                    dt = clock.tick(60) 
                    screen.blit(esc,(0,0))
                    mouse_x,mouse_y=pygame.mouse.get_pos()#좌표구하기
                    print(mouse_x,mouse_y)   
                    if (out_button_x_pos<mouse_x<out_button_x_pos+out_button_width 
                        and out_button_y_pos<mouse_y<out_button_y_pos+out_button_height):
                        screen.blit(out_touch_button,(out_button_x_pos,out_button_y_pos))
                    else:
                        screen.blit(out_button,(out_button_x_pos,out_button_y_pos))
                    pygame.display.update()
                 #####################################
    if text_namber==0:
        text_0()
        text_namber=1
    if text_namber==1:
        text_1()
        text_namber=2
    
    
#pygame 종료
pygame.quit()

함수 안에선 마우스 좌표가 잘 구해져서 잘 실행 되는데 함수 안에선 마우스 좌표가 안구해져서 잘 실행이 안되요.....

답변 1

0

안녕하세요, 인프런 AI 인턴이에요.

제가 보았을 때, 여러 개의 for문과 if문이 중첩되어있는 구조입니다. 그래서 코드가 복잡해지고 버그가 발생할 가능성이 높아질 수 있어요.

버그의 원인과 디버깅을 도와드리기 위해 좀 더 자세한 정보를 제공해주시면 도움을 드릴 수 있을 것 같아요. 어떤 문제가 발생하는지, 어떤 에러 메시지가 나오는지 등을 알려주세요. 그리고 코드의 어느 부분에서 문제가 발생하는지도 알려주세요.

기본적으로 파이썬에서는 들여쓰기에 매우 민감하므로, 코드의 들여쓰기나 탭/공백 문자를 확인해 보시는 것도 좋을 것 같아요. 들여쓰기가 제대로 되어있지 않으면 코드가 의도한대로 동작하지 않을 수 있어요.

더 자세한 정보를 알려주시면 더 정확한 도움을 드릴 수 있을 것 같아요. 고맙습니다.

김순희님의 프로필 이미지
김순희

작성한 질문수

질문하기