Reputation: 39
I'm attempting to build a menu based program where if I click on a button, it corresponds to a function; and it will carry out certain conditions. However, I am unable to get the button itself working. I wish to have it so that when I click on the button, the screen fills with another colour and so that I am able to have another screen with whatever function it has clicked on. I've added my code below; any help is so greatly appreciated!
This is the button that I have been having trouble with:
def arrayIntro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
mousePosition = pygame.mouse.get_pos()
screen.fill(MUTEDBLUE)
textChoice = pygame.font.SysFont('americantypewriter', 20)
mousePosition = pygame.mouse.get_pos()
mouseClick = pygame.mouse.get_pressed()
#Top Row Draw Rects
if 50+250 > mousePosition[0] > 50 and 50 + 100 > mousePosition[1] > 50:
pygame.draw.rect(screen, GRAY, (50, 50, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (50, 50, 250, 100))
enterInt = textChoice.render("enterInt", True, NAVYBLUE)
screen.blit(enterInt, (135, 90))
if mouseClick[0] ==1:
screen.fill(MUTEDBLUE)
However if necessary, here is all of my code:
#Import the necessary modules
import pygame
import sys
import os
#Initialize pygame
pygame.init()
# Set the size for the surface (screen)
screenSize = (900,750)
screen = pygame.display.set_mode((screenSize),0)
# Set the caption for the screen
pygame.display.set_caption("Array")
#Define Colours
WHITE = (255,255,255)
BLUE = (0,0,255)
BLACK = (0,0,0)
GRAY = (128, 128, 128)
MAROON = (128, 0, 0)
NAVYBLUE = (0, 0, 128)
OLIVE = (128, 128, 0)
PURPLE = (128, 0, 128)
TEAL = (0,128,128)
PINK = (226,132,164)
MUTEDBLUE = (155,182,203)
PLUM = (221,160,221)
def selectedFirst():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def enterInt():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def display():
runSecond = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def countInt():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def displayReverse():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def sumInt():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def average():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def findMaxORfindMin():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def search():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def writeToText():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def fillFromText():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def searchReplace():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def shuffleArray():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def wordCount():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def arrayIntro():
buttonenterInt = pygame.Rect(50, 50, 250, 100)
buttondisplay =pygame.Rect(325, 50, 250, 100)
buttoncountInt = pygame.Rect(600, 50, 250, 100)
buttondisplayReverse = pygame.Rect(50, 250, 250, 100)
buttonsumInt = pygame.Rect(325, 250, 250, 100)
buttonaverage = pygame.Rect(600, 250, 250, 100)
buttonfindMaxORfindMin = pygame.Rect(50, 450, 250, 100)
buttonsearch = pygame.Rect(325, 450, 250, 100)
buttonwriteToText= pygame.Rect(600, 450, 250, 100)
buttonfillFromText =pygame.Rect(50, 640, 250, 100)
buttonsearchReplace = pygame.Rect(325, 640, 250, 100)
buttonwordCount = pygame.Rect(600, 640, 250, 100)
intro = True
while intro:
mousePosition = pygame.mouse.get_pos()
screen.fill(MUTEDBLUE)
textChoice = pygame.font.SysFont('americantypewriter', 20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if (event.type == pygame.MOUSEBUTTONDOWN and event.button == 1):
clicked = pygame.mouse.get_pos()
if buttonenterInt.collidepoint(clicked):
selectedFirst()
elif buttondisplay.collidepoint(clicked):
display()
elif buttoncountInt.collidepoint(clicked):
countInt()
elif buttondisplayReverse.collidepoint(clicked):
displayReverse()
elif buttonsumInt.collidepoint(clicked):
sumInt()
elif buttonaverage.collidepoint(clicked):
average()
elif buttonfindMaxORfindMin.collidepoint(clicked):
findMaxORfindMin()
elif buttonsearch.collidepoint(clicked):
search()
elif buttonwriteToText.collidepoint(clicked):
writeToText()
elif buttonfillFromText.collidepoint(clicked):
fillFromText()
elif buttonsearchReplace.collidepoint(clicked):
searchReplace()
elif buttonshuffleArray.collidepoint(clicked):
shuffleArray()
elif buttonwordCount.collidepoint(clicked):
wordCount()
#########################################################################################
#Top Row Draw Rects
if 50+250 > mousePosition[0] > 50 and 50 + 100 > mousePosition[1] > 50:
pygame.draw.rect(screen, GRAY, (50, 50, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (50, 50, 250, 100))
enterInt = textChoice.render("enterInt", True, NAVYBLUE)
screen.blit(enterInt, (135, 90))
if 325+250 > mousePosition[0] > 325 and 50 + 100 > mousePosition[1] > 50:
pygame.draw.rect(screen, GRAY, (325, 50, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (325, 50, 250, 100))
display = textChoice.render("display", True, NAVYBLUE)
screen.blit(display, (414, 90))
if 600+250 > mousePosition[0] > 600 and 50 + 100 > mousePosition[1] > 50:
pygame.draw.rect(screen, GRAY, (600, 50, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (600, 50, 250, 100))
countInt = textChoice.render("countInt", True, NAVYBLUE)
screen.blit(countInt, (680, 90))
#########################################################################################
#Middle Row Draw Rects
if 50+250 > mousePosition[0] > 50 and 250 + 100 > mousePosition[1] > 250:
pygame.draw.rect(screen, GRAY, (50, 250, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (50, 250, 250, 100))
displayReverse = textChoice.render("displayReverse", True, NAVYBLUE)
screen.blit(displayReverse, (102, 287))
if 325+250 > mousePosition[0] > 325 and 250 + 100 > mousePosition[1] > 250:
pygame.draw.rect(screen, GRAY, (325, 250, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (325, 250, 250, 100))
sumInt = textChoice.render("sumInt", True, NAVYBLUE)
screen.blit(sumInt, (410, 287))
if 600+250 > mousePosition[0] > 600 and 250 + 100 > mousePosition[1] > 250:
pygame.draw.rect(screen, GRAY, (600, 250, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (600, 250, 250, 100))
average = textChoice.render("average", True, NAVYBLUE)
screen.blit(average, (685, 287))
#########################################################################################
#Middle Bottom Row Draw Rects
if 50+250 > mousePosition[0] > 50 and 450 + 100 > mousePosition[1] > 450:
pygame.draw.rect(screen, GRAY, (50, 450, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (50, 450, 250, 100))
findMaxORfindMin = textChoice.render("findMax/findMin", True, NAVYBLUE)
screen.blit(findMaxORfindMin, (95, 488))
if 325+250 > mousePosition[0] > 325 and 450 + 100 > mousePosition[1] > 450:
pygame.draw.rect(screen, GRAY, (325, 450, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (325, 450, 250, 100))
search = textChoice.render("search", True, NAVYBLUE)
screen.blit(search, (417, 488))
if 600+250 > mousePosition[0] > 600 and 450 + 100 > mousePosition[1] > 450:
pygame.draw.rect(screen, GRAY, (600, 450, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (600, 450, 250, 100))
writeToText = textChoice.render("writeToText", True, NAVYBLUE)
screen.blit(writeToText, (665, 488))
#########################################################################################
#Bottom Row Draw Rects
if 50+250 > mousePosition[0] > 50 and 640 + 100 > mousePosition[1] > 640:
pygame.draw.rect(screen, GRAY, (50, 640, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (50, 640, 250, 100))
fillFromText = textChoice.render("fillFromText", True, NAVYBLUE)
screen.blit(fillFromText, (115, 680))
if 325+250 > mousePosition[0] > 325 and 640 + 100 > mousePosition[1] > 640:
pygame.draw.rect(screen, GRAY, (325, 640, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (325, 640, 250, 100))
searchReplace = textChoice.render("searchReplace", True, NAVYBLUE)
screen.blit(searchReplace, (382, 680))
if 600+250 > mousePosition[0] > 600 and 640 + 100 > mousePosition[1] > 640:
pygame.draw.rect(screen, GRAY, (600, 640, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (600, 640, 250, 100))
wordCount = textChoice.render("wordCount", True, NAVYBLUE)
screen.blit(wordCount, (670, 680))
pygame.display.update()
#########################################################################################
eval(selected)
arrayIntro()
Upvotes: 0
Views: 1064
Reputation: 308
The while loop keeps on running indefinitely. In the current code, you detect the mouse click, which then makes the screen go blue but only for an instant and then it proceeds to the next function till it reaches the end and loop continues thereby totally what happened in the last iteration.
Look at the code below. I changed it a little
#Import the necessary modules
import pygame
import sys
import os
#Initialize pygame
pygame.init()
# Set the size for the surface (screen)
screenSize = (900,750)
screen = pygame.display.set_mode((screenSize),0)
# Set the caption for the screen
pygame.display.set_caption("Array")
#Define Colours
WHITE = (255,255,255)
BLUE = (0,0,255)
BLACK = (0,0,0)
GRAY = (128, 128, 128)
MAROON = (128, 0, 0)
NAVYBLUE = (0, 0, 128)
OLIVE = (128, 128, 0)
PURPLE = (128, 0, 128)
TEAL = (0,128,128)
PINK = (226,132,164)
MUTEDBLUE = (155,182,203)
PLUM = (221,160,221)
#function here
def selectedFirst():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
def arrayIntro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONUP:
mousePosition = pygame.mouse.get_pos()
screen.fill(MUTEDBLUE)
textChoice = pygame.font.SysFont('americantypewriter', 20)
mousePosition = pygame.mouse.get_pos()
mouseClick = pygame.mouse.get_pressed()
#Top Row Draw Rects
if 50+250 > mousePosition[0] > 50 and 50 + 100 > mousePosition[1] > 50:
pygame.draw.rect(screen, GRAY, (50, 50, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (50, 50, 250, 100))
enterInt = textChoice.render("enterInt", True, NAVYBLUE)
screen.blit(enterInt, (135, 90))
if mouseClick[0] ==1:
selectedFirst() #change here. Made a new function temporarily to be executed on the click
if 325+250 > mousePosition[0] > 325 and 50 + 100 > mousePosition[1] > 50:
pygame.draw.rect(screen, GRAY, (325, 50, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (325, 50, 250, 100))
display = textChoice.render("display", True, NAVYBLUE)
screen.blit(display, (414, 90))
if 600+250 > mousePosition[0] > 600 and 50 + 100 > mousePosition[1] > 50:
pygame.draw.rect(screen, GRAY, (600, 50, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (600, 50, 250, 100))
countInt = textChoice.render("countInt", True, NAVYBLUE)
screen.blit(countInt, (680, 90))
#Middle Row Draw Rects
if 50+250 > mousePosition[0] > 50 and 250 + 100 > mousePosition[1] > 250:
pygame.draw.rect(screen, GRAY, (50, 250, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (50, 250, 250, 100))
displayReverse = textChoice.render("displayReverse", True, NAVYBLUE)
screen.blit(displayReverse, (102, 287))
if 325+250 > mousePosition[0] > 325 and 250 + 100 > mousePosition[1] > 250:
pygame.draw.rect(screen, GRAY, (325, 250, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (325, 250, 250, 100))
sumInt = textChoice.render("sumInt", True, NAVYBLUE)
screen.blit(sumInt, (410, 287))
if 600+250 > mousePosition[0] > 600 and 250 + 100 > mousePosition[1] > 250:
pygame.draw.rect(screen, GRAY, (600, 250, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (600, 250, 250, 100))
average = textChoice.render("average", True, NAVYBLUE)
screen.blit(average, (685, 287))
#Middle Bottom Row Draw Rects
if 50+250 > mousePosition[0] > 50 and 450 + 100 > mousePosition[1] > 450:
pygame.draw.rect(screen, GRAY, (50, 450, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (50, 450, 250, 100))
findMaxORfindMin = textChoice.render("findMax/findMin", True, NAVYBLUE)
screen.blit(findMaxORfindMin, (95, 488))
if 325+250 > mousePosition[0] > 325 and 450 + 100 > mousePosition[1] > 450:
pygame.draw.rect(screen, GRAY, (325, 450, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (325, 450, 250, 100))
search = textChoice.render("search", True, NAVYBLUE)
screen.blit(search, (417, 488))
if 600+250 > mousePosition[0] > 600 and 450 + 100 > mousePosition[1] > 450:
pygame.draw.rect(screen, GRAY, (600, 450, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (600, 450, 250, 100))
writeToText = textChoice.render("writeToText", True, NAVYBLUE)
screen.blit(writeToText, (665, 488))
#Bottom Row Draw Rects
if 50+250 > mousePosition[0] > 50 and 640 + 100 > mousePosition[1] > 640:
pygame.draw.rect(screen, GRAY, (50, 640, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (50, 640, 250, 100))
fillFromText = textChoice.render("fillFromText", True, NAVYBLUE)
screen.blit(fillFromText, (115, 680))
if 325+250 > mousePosition[0] > 325 and 640 + 100 > mousePosition[1] > 640:
pygame.draw.rect(screen, GRAY, (325, 640, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (325, 640, 250, 100))
searchReplace = textChoice.render("searchReplace", True, NAVYBLUE)
screen.blit(searchReplace, (382, 680))
if 600+250 > mousePosition[0] > 600 and 640 + 100 > mousePosition[1] > 640:
pygame.draw.rect(screen, GRAY, (600, 640, 250, 100))
else:
pygame.draw.rect(screen, WHITE, (600, 640, 250, 100))
wordCount = textChoice.render("wordCount", True, NAVYBLUE)
screen.blit(wordCount, (670, 680))
pygame.display.update()
arrayIntro()
Its commented where I changed which is..
defined a new func
def selectedFirst():
runFirst = True
while runFirst:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill(MUTEDBLUE)
pygame.display.update()
And called this when clicked
if mouseClick[0] ==1:
selectedFirst()
Try it out.
https://i.sstatic.net/vy7Dk.jpg
EDIT: You don't need to write such a big code. It could be reduced by looping the blitting and mouse tracking in a single func
Upvotes: 1