2017-12-01 10 views
0

컴퓨터 과학 프로젝트의 옵션 화면 텍스트를 코드에 표시하려고합니다. 문제는 옵션 화면으로 바꿀 때 텍스트가 보이지 않는다는 것입니다. 나는 화면간에 신속하게 전환하는 것이 간혹 간혹 있음을 발견했다. 주 화면의 오른쪽 위에있는 설정을 클릭하거나 키패드에서 Enter 키를 누를 때 텍스트를 두 위치에 표시하는 코드를 넣었습니다.특정 화면이 표시 될 때 텍스트를 표시하는 파이 게임에 코드가 있지만 바로 사라집니다.

import pygame 
import sys 
import time 
from pygame.locals import * 
displaytext = False 
fadedout = False 
timechange = 0 
played = False 
musicUp = pygame.K_o 
musicDown = pygame.K_l 
pygame.init() # initialize pygame 
pygame.font.init() 
myfont = pygame.font.SysFont('Comic Sans MS', 30) 
pygame.mixer.music.set_volume(0.50) 
clock = pygame.time.Clock() 
screen = pygame.display.set_mode((1600,800)) 
pygame.mouse.set_cursor(*pygame.cursors.tri_left) 
currentBack = 'welcome' 

bg = pygame.image.load("welcome1600new.jpg") 

def callText(): 
    textsurface = myfont.render('Some Text', True, (10, 10, 10)) 
    textpos = textsurface.get_rect() 
    screen.blit(textsurface,textpos) 

def options(): 
    pausePos = play_time()/1000 
    pygame.mouse.set_cursor(*pygame.cursors.arrow) 
    bg = pygame.image.load("Optionsback.jpg") 

    displaytext = True 
    return bg,displaytext 

def play_time(): 
    playTime = pygame.mixer.music.get_pos() 
    return playTime 

while True: 
    clock.tick(60) 
    screen.blit(bg, (0,0)) 
    pygame.display.update() 
    for event in pygame.event.get(): 
     pygame.mixer.init() 
     if (pygame.mixer.music.get_busy() == False) and (fadedout == False): 
      pygame.mixer.music.load("dududududu.ogg") 
      pygame.mixer.music.play(-1,0.0) 
     if (displaytext) == True: 
      textsurface = myfont.render('Some Text', 1, (10, 10, 10)) 
      textpos = textsurface.get_rect() 
      screen.blit(textsurface,textpos) 

     if ((currentBack == 'welcome') and (event.type == 
pygame.MOUSEBUTTONUP) and (pygame.mouse.get_pos()[0] >= 1540) and 
(pygame.mouse.get_pos()[0] <= 1600) and (pygame.mouse.get_pos()[1] >= 0) and 
(pygame.mouse.get_pos()[1] <= 70)): 
      currentBack = 'options' 
      bg, displaytext = options() 


    if event.type == KEYDOWN: 
     if ((event.key == pygame.K_h) and (currentBack == 'welcome')): 
      pausePos = play_time()/1000 
      pygame.mouse.set_cursor(*pygame.cursors.arrow) 
      bg = pygame.image.load("Help1600.jpg") 
      currentBack = 'help' 
      #pygame.mixer.pause() 
      pygame.mixer.music.fadeout(1000) 
      pygame.display.update() 
      fadedout = True 
      displaytext = False 
     if event.key == musicUp: 
      if (pygame.mixer.music.get_volume()<=0.90) and 
timechange+0.25<time.time(): 
       timechange = time.time() 

pygame.mixer.music.set_volume(pygame.mixer.music.get_volume()+.10) 
      pygame.display.update() 
      displaytext = False 
     if event.key == musicDown: 
      if pygame.mixer.music.get_volume()>=.10 and 
timechange+0.25<time.time(): 
       timechange = time.time() 

pygame.mixer.music.set_volume(pygame.mixer.music.get_volume()-.10) 
      pygame.display.update() 
      displaytext = False 
     if (event.key == pygame.K_ESCAPE): 
      pygame.mouse.set_cursor(*pygame.cursors.tri_left) 
      bg = pygame.image.load("welcome1600new.jpg") 
      currentBack = 'welcome' 
      pygame.mixer.music.play(-1, pausePos) 
      pygame.display.update() 
      fadedout = False 
      displaytext = False 
     if event.key == (pygame.K_KP_ENTER): 
      callText() 
      bg, displaytext = options() 
      currentBack = 'options' 
+0

코드 루형 - 실제로는 최소한의 예가 아닙니다. 코드를 추가로 처리하기 전에 잠깐 기다리시겠습니까? [how-can-i-make-a-time-delay-in-python] (https://stackoverflow.com/questions/510348/how-can-i-make-a-time-delay-in-python)을 참조하십시오.) –

+0

이 순서대로해야합니다 : 배경 지우기/그리기, 요소 그리기, 화면 전송 (update()), 잠시 기다려주십시오 ('tick (60)'). 그리기 전에 화면에 보내어 잠시 기다려주십시오. – furas

+0

'for event' 루프를 짧게 만드는 함수에 코드를 넣어두면 문제가 생길 것입니다. – furas

답변

0

거기에 몇 가지 이상한 점이 있습니다. 불만을 제기하는 특정 동작의 원인은 이벤트를 확인하는 for 루프 내에서만 텍스트를 그립니다. 프레임에서 키 누르기 또는 마우스 이동과 같은 이벤트가 없으면 텍스트를 블 리치하는 블록이 전혀 실행되지 않습니다. 가까운 작업 일에 당신을 데려 수

또 다른 것은, pygame.display.update() 또는 각 프레임에 해당하는 .flip()하나 단일 통화를 만드는 것입니다.

주위에 코드를 적용하여 이해하는 것이 좋습니다. 위의 ...display.update()으로 전화 할 때마다 전체 화면을 다시 그려야했습니다. 그리고 거기에 있던 텍스트는 사라질 것입니다.

for event in pygame.event.get() 루프는 외부 공간의 마법 엔티티가 아닙니다. 그것은 단순히 각 프레임에서 발생하는 파이 게임 이벤트를 확인하는 방법입니다.

event 변수를 테스트하지 않는 모든 항목은 외출해야합니다.

또한 pygame.mixer.init()은 프로그램 시작시 한 번만 호출해야하며 각 프레임마다 여러 번 호출해야합니다. 믹서를 리셋해야 할 경우 음악을 다시 시작하려면 문서를 확인하여 다른 전화를 받으십시오.

전체적으로 코드는 다소 차이가 있습니다. 나는 그것을 실행하려고하지 않았지만, 나는 당신이 가지고있는 것들 중 일부를 엉망으로 만들었다.

import pygame 
import sys 
import time 
from pygame.locals import * 


def init(): 
    global clock, screen 

    pygame.init() # initialize pygame 
    screen = pygame.display.set_mode((1600,800)) 
    pygame.font.init() 
    pygame.mixer.init() 
    pygame.mixer.music.set_volume(0.50) 
    pygame.mouse.set_cursor(*pygame.cursors.tri_left) 
    clock = pygame.time.Clock() 

# put stuff inside a function. 
def main(): 

    myfont = pygame.font.SysFont('Comic Sans MS', 30) 
    displaytext = False 
    fadedout = False 
    timechange = 0 
    played = False 
    musicUp = pygame.K_o 
    musicDown = pygame.K_l 
    currentBack = 'welcome' 

    bg_image = pygame.image.load("Help1600.jpg") 
    bg = img_welcome = pygame.image.load("welcome1600new.jpg") 

    while True: 

     screen.blit(bg, (0,0)) 

     if (pygame.mixer.music.get_busy() == False) and (fadedout == False): 
      pygame.mixer.music.load("dududududu.ogg") 
      pygame.mixer.music.play(-1,0.0) 
     if (displaytext) == True: 
      textsurface = myfont.render('Some Text', 1, (10, 10, 10)) 
      textpos = textsurface.get_rect() 
      screen.blit(textsurface,textpos) 

     # Use a rectangle and Rect.collidepoint instead of this mess: 
     # https://www.pygame.org/docs/ref/rect.html#pygame.Rect.collidepoint 
     if ((currentBack == 'welcome') and 
      (event.type == pygame.MOUSEBUTTONUP) and 
      (pygame.mouse.get_pos()[0] >= 1540) and 
      (pygame.mouse.get_pos()[0] <= 1600) and 
      (pygame.mouse.get_pos()[1] >= 0) and 
      (pygame.mouse.get_pos()[1] <= 70) 
     ): 
      currentBack = 'options' 
      bg, displaytext = options() 

     for event in pygame.event.get(): 
      # just check for _events_ inside here. 

      if event.type == KEYDOWN: 
       if ((event.key == pygame.K_h) and (currentBack == 'welcome')): 
        pausePos = play_time()/1000 
        pygame.mouse.set_cursor(*pygame.cursors.arrow) 
        # DOn't load images with pygame.image.load inside the game loop!! 
        bg = bg_image 
        currentBack = 'help' 
        #pygame.mixer.pause() 
        pygame.mixer.music.fadeout(1000) 
        fadedout = True 
        displaytext = False 
       if event.key == musicUp: 
        if (pygame.mixer.music.get_volume()<=0.90) and timechange+0.25<time.time(): 
         timechange = time.time() 

        pygame.mixer.music.set_volume(pygame.mixer.music.get_volume()+.10) 
        displaytext = False 
       if event.key == musicDown: 
        if pygame.mixer.music.get_volume()>=.10 and timechange+0.25<time.time(): 
         timechange = time.time() 

        pygame.mixer.music.set_volume(pygame.mixer.music.get_volume()-.10) 
        displaytext = False 
       if (event.key == pygame.K_ESCAPE): 
        pygame.mouse.set_cursor(*pygame.cursors.tri_left) 
        bg = img_welcome 
        currentBack = 'welcome' 
        pygame.mixer.music.play(-1, pausePos) 
        fadedout = False 
        displaytext = False 
       if event.key == (pygame.K_KP_ENTER): 
        callText() 
        bg, displaytext = options() 
        currentBack = 'options' 


     # single call to update: 
     pygame.display.update() 
     # after you display the image, you can pause 
     # until the next frame: 
     clock.tick(60) 

init() 
main()