Reputation: 183
I've been working with pygame and trying to learn it. I've been able to make a "button" if you want to call how I'm doing it that. I have main() which displays the main menu. Then pick_level() which will allow you to choose the level you would like to play. My issue is with getting the main() text off the screen once pick_level() executes. Below is my code. For some reason it is not coping over correctly..
#import games
import pygame
from pygame.locals import *
import sys
pygame.init()
DISPLAYSURF = pygame.display.set_mode((1024, 720), 0, 32)
pygame.display.set_caption('The End')
#Set color of main menu
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
DISPLAYSURF.fill(BLUE)
# Set up text to show.
myfont = pygame.font.SysFont("curier new", 45)
gameFont = pygame.font.SysFont("times new roman", 20)
def main():
""" Main Program """
# render text
lbltheEnd = myfont.render("The End", 1, WHITE)
lblchooseLevel = gameFont.render("Choose Level", 1, WHITE)
lblCredits = gameFont.render("Credits", 1, WHITE)
lblexit = gameFont.render("Exit", 1, WHITE)
DISPLAYSURF.blit(lbltheEnd, (100, 100))
#rectangle for button click
global btnChooseLevel
btnChooseLevel = pygame.draw.rect(DISPLAYSURF, BLACK, (98, 150, 111, 25))
DISPLAYSURF.blit(lblchooseLevel, (100, 150))
def pick_level():
""" Show pick Level """
lblLevel = myfont.render("Level 1", 1, WHITE)
DISPLAYSURF.blit(lblLevel, (100, 100))
# Run the program
main()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
# Clicks for make shift buttons
if event.type == MOUSEBUTTONUP:
print(str(event.pos[0]) + " " + str(event.pos[1]))
if(btnChooseLevel.collidepoint(event.pos)):
print("Choose Level Clicked")
pick_level()
pygame.display.update()
Upvotes: 0
Views: 691
Reputation: 32189
The quickest way to do this would be to clear the whole screen and redraw everything new that you want. Something like in pick_level()
you can add:
DISPLAYSURF.fill(BLUE) #if BLUE is your background color or whatever background color you want
followed by the rest of your pick_level()
code
To make it easier you should have a method called draw_background()
that draws the basic background for every menu and then you can draw your specific text/images on top of that.
Upvotes: 1