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'