0

I want the code to display text for an options screen on my computer science project. The problem is that when I change to the options screen the text is not visible. I have found that switching quickly between the screens shows it briefly sometimes. I have put code for the text to display in 2 places either when you click on the settings in the top right of the main screen or when you press enter on the keypad.

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'
  • 1
    Loooots of code - not really a minimal example :) do you just want to wait a bit before processing the code further? See [how-can-i-make-a-time-delay-in-python](https://stackoverflow.com/questions/510348/how-can-i-make-a-time-delay-in-python) – Patrick Artner Dec 01 '17 at 11:23
  • you have to do in this order: clear/draw background, draw elements, send on screen (`update()`), wait awhile (`tick(60)`). And you send on screen before you draw it and wait awhile – furas Dec 01 '17 at 11:54
  • you should put code in functions to make `for event` loop shorter - and then you would see problem. – furas Dec 01 '17 at 11:55
  • BTW: you can split code in separated "stages/pages" (ie. Intro, Options, Game) with own `white True` loops but it is easier with classes: https://i.imgur.com/MT7tZ4s.png – furas Dec 01 '17 at 12:00

1 Answers1

0

There are several strange things there. What causes the specific behavior you are complaining about is that you only draw your text inside the for loop that checks for events. If there are no events, such as keypresses or mouse movement in a frame, the block that blits your text is not run at all.

Another thing that might bring you closer to a working thing, is to make one and only single call to pygame.display.update() or the equivalent .flip() in each frame.

And work the code around it so that it makes sense. Each time you call ...display.update() above, you had redrawn the whole screen. And whatever text was there will be gone.

The for event in pygame.event.get() loop is not a magic entity from outer space. It is simply a way for you to check for the pygame events that happen at each frame.

Every single thing in there that is not testing the event variable have to go out.

Also pygame.mixer.init() should be called one single time, at the beginning of the program - while you put it being called multiple times each frame. If you have to reset the mixer, to restart the music, check the docs for another call to do that.

All in all, more or less the code bellow. I did not try to run it, but I just unmangled some of the stuff you have around.

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()
jsbueno
  • 99,910
  • 10
  • 151
  • 209