Reputation: 11
I'm working on a school project at the moment in which you answer maths questions of varying difficulty in order to win. I'm using Pygame to create this alongside Python and have run into an issue that I don't see the problem with, I was wondering if anyone could shed some light!
The issue is with the game loops I'm using. When the game is started a title screen loads, with music and a set of buttons on screen. If these buttons are hovered over they change to different colours to signify this. Currently I am messing around with the play button, and want that to take you to a new screen. Currently I had this set up so that it would break you out of the title screen loop and put you onto simply a Teal background with nothing else yet. However, although the button is clearly being registered as clicked, the game seems to keep the Intro loop intact and not change over to the new loop which should be satisfied, "Play Clicked"
My code is this:
import pygame, sys
from pygame.locals import *
pygame.init()
#Initialises the music mixer and then loads the music
#file from the directory of the executable, sets the volume
#and sets it to infinite loop
pygame.mixer.init()
pygame.mixer.music.load("Wepa.mp3")
pygame.mixer.music.set_volume(0.4)
pygame.mixer.music.play(-1,0.0)
#-1 Causes an infinite loop.
#The track starts at the beginning, 0.0.
#Sets the cursor to be visible
pygame.mouse.set_visible
#Sets the FPS
FPS = 60
fpsClock = pygame.time.Clock()
#Creates a display surface, the main window for the game, and sets a title.
DISPLAYSURF = pygame.display.set_mode((800, 600))
#Resolution of 800x600.
pygame.display.set_caption("Maths Mania")
#Creates a background colour, I used teal simply to test if it's working
#as the base colour of the window is black anyway.
TEAL = (0, 128, 128)
#Gets the title screen image from the same directory as the executable
#and sets the coordinates of the top left corner, in this case, the top left
#of DISPLAYSURF, therefore 0,0.
TitleScreen = pygame.image.load('TitleScreenButtonless.png')
Titlex=0
Titley=0
#Creates sprites for the individual buttons on the title screen and sets their top left
#corner.
PlayButton=pygame.image.load('PlayButton.png')
PlayButtonx=284
PlayButtony=235
PlayButtonHovered=pygame.image.load('PlayButtonHovered.png')
PlayButtonHoveredx=284
PlayButtonHoveredy=235
OptionsButton=pygame.image.load('OptionsButton.png')
OptionsButtonx=284
OptionsButtony=329
OptionsButtonHovered=pygame.image.load('OptionsButtonHovered.png')
OptionsButtonHoveredx=284
OptionsButtonHoveredy=329
QuitButton=pygame.image.load('QuitButton.png')
QuitButtonx=284
QuitButtony=425
QuitButtonHovered=pygame.image.load('QuitButtonHovered.png')
QuitButtonHoveredx=284
QuitButtonHoveredy=425
Intro=True
PlayClicked=False
def PlayButtonClicker(PlayButtonx,PlayButtony,width,height,action=None):
cursor=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
if (PlayButtonx+width)>cursor[0]>PlayButtonx and (PlayButtony+height)>cursor[1]>PlayButtony:
DISPLAYSURF.blit(PlayButtonHovered, (PlayButtonx, PlayButtony))
if click[0]==1 and action!=None:
if action=="quit":
pygame.quit()
quit()
elif action=="Playbutton":
print("1")
PlayClicked==True
Intro==False
def OptionsButtonClicker(OptionsButtonx,OptionsButtony,width,height,action=None):
cursor=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
if (OptionsButtonx+width)>cursor[0]>OptionsButtonx and (OptionsButtony+height)>cursor[1]>OptionsButtony:
DISPLAYSURF.blit(OptionsButtonHovered, (OptionsButtonx, OptionsButtony))
if click[0]==1 and action!=None:
if action=="quit":
pygame.quit()
quit()
def QuitButtonClicker(QuitButtonx,QuitButtony,width,height,action=None):
cursor=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
if (QuitButtonx+width)>cursor[0]>QuitButtonx and (QuitButtony+height)>cursor[1]>QuitButtony:
DISPLAYSURF.blit(QuitButtonHovered, (QuitButtonx, QuitButtony))
if click[0]==1 and action!=None:
if action=="quit":
pygame.quit()
quit()
while Intro==True: #THIS IS THE MAIN GAME LOOP, EVERYTHING IN THIS LOOP IS THE GAME
DISPLAYSURF.fill(TEAL)
#Fills the display window with the background colour
DISPLAYSURF.blit(TitleScreen, (Titlex, Titley))
#Fills the display window with the TitleScreen image and tells it
#where to place the top left corner of said image.
#Places the buttons on the title screen
DISPLAYSURF.blit(PlayButton, (PlayButtonx, PlayButtony))
DISPLAYSURF.blit(OptionsButton, (OptionsButtonx, OptionsButtony))
DISPLAYSURF.blit(QuitButton, (QuitButtonx, QuitButtony))
PlayButtonClicker(284,235,231,64,action='Playbutton')
OptionsButtonClicker(284,329,231,64,action='Optionsbutton')
QuitButtonClicker(284,425,231,64,action='Quitbutton')
#The following lines check each event that happens in the game. If any of those
#events should be to quit, the game exits.
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
#These couple lines update the display and the FPS clock.
pygame.display.update()
fpsClock.tick(FPS)
while PlayClicked==True:
DISPLAYSURF.fill(TEAL)
Nonsense=pygame.image.load('Nonsense.png')
Nonsensex=0
Nonsensey=0
DISPLAYSURF.blit(Nonsene, (Nonsensex, Nonsensey))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
fpsClock.tick(FPS)
Upvotes: 1
Views: 195
Reputation: 20488
You need to declare the PlayClicked
and Intro
as global variables, and do what Astrolab told you and change the double ==
to =
.
However, global variables should usually be avoided, because they often make code harder to understand and more error prone, so it would be better to restructure the program.
def PlayButtonClicker(PlayButtonx,PlayButtony,width,height,action=None):
global PlayClicked
global Intro
cursor=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
if (PlayButtonx+width)>cursor[0]>PlayButtonx and (PlayButtony+height)>cursor[1]>PlayButtony:
DISPLAYSURF.blit(PlayButtonHovered, (PlayButtonx, PlayButtony))
if click[0]==1 and action!=None:
if action=="quit":
pygame.quit()
sys.exit()
elif action=="Playbutton":
print("1")
PlayClicked = True
Intro = False
Upvotes: 0
Reputation: 171
elif action=="Playbutton":
print("1")
PlayClicked==True
Intro==False
Change this so that you're using a single =
sign instead of ==
for PlayClicked and Intro. Presumably you're trying to set the value of those variable instead of checking for equality :P
Upvotes: 1