import pygame as pg
import sys
pg.init()
buttonFont = pg.font.SysFont("garamond", 25)
screenGray = pg.Color('gray80')
buttonGray2 = pg.Color('gray50')
textColour = pg.Color('navy')
screen = pg.display.set_mode((800, 600))
clock = pg.time.Clock()
class Button(pg.sprite.Sprite):
def __init__(self, text, x, y, width, height, colour):
super().__init__()
self.image = pg.Surface((width, height))
self.image.fill(colour)
self.rect = self.image.get_rect()
txt = buttonFont.render(text, True, textColour)
txtRect = txt.get_rect(center = self.rect.center)
self.image.blit(txt, txtRect)
self.rect.topleft = x, y
def isPressed(self, event):
if event.type == pg.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
return True
return False
def FrontPage():
screen.fill(screenGray)
Continue = Button('Continue', 105, 455, 120, 50, buttonGray2)
buttonsGroup1 = pg.sprite.Group(Continue)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
elif Continue.isPressed(event):
Menu()
buttonsGroup1.draw(screen)
pg.display.flip()
clock.tick(60)
def Menu():
screen.fill(screenGray)
Scytale = Button('Scytale', 105,105,140,65, buttonGray2)
Caesar = Button('Caesar', 330,105,140,65, buttonGray2)
Vigenere = Button('Vigenere', 555,105,140,65, buttonGray2)
Enigma = Button('Enigma', 105,430,140,65,buttonGray2)
PublicKey = Button('Public Key', 330,430,140,65, buttonGray2)
Rijndael = Button('Rijndael', 555,430,140,65, buttonGray2)
buttonsGroup2 = pg.sprite.Group(Scytale,Caesar,Vigenere,Enigma,PublicKey,Rijndael)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
buttonsGroup2.draw(screen)
clock.tick(60)
FrontPage()
Above is the stripped back code of my FrontPage, that has a button on it that, when clicked, should take the user to the menu screen where 6 more buttons are displayed to move onto the users chosen encryption method.
However, when I press the Continue button, nothing happens.
Is it because there is something wrong with the Button Class? Or is there something else that makes the button stop working?
Thanks in advance