At the request of Furas, below are the segments of code in a Pygame Project that I'm currently working on. What I'm trying to get to happen is essentially like clearing everything visible on the previous window so I can start fresh with a new UI. When the code is run, and the "Start Game" button is pressed on the startup screen, the background colour updates, but everything that was previously rendered (the title and all of the buttons) remain on the screen. If anyone could offer any general pointers - or advice based off the code below - that would a great help!
The game_loop
function:
This first part is where I need the screen to be cleared/updated. The game_loop
contains more game logic, which is irrelevant to the current question, and what is pasted below is the only part of the function that interacts with the window.
new_surface = pygame.Surface((1,1))
def game_loop():
global clicked_game
global game_running
clicked_game = True
game_running = True
if clicked_game == True:
screen.fill( (0,0,0) )
screen.blit(new_surface, (0,0))
pygame.display.update()
User Interface Initialisation and Functions:
These are the rest of the functions which follow later on from the actual game logic in game_loop
:
def quit_game():
pygame.quit()
quit()
def update_options():
global clicked_options
clicked_options = True
def button(window, msg, x_coord, y_coord, width, height, initial_colour, highlight_colour, button_action):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x_coord + width > mouse[0] > x_coord and y_coord + height > mouse[1] > y_coord:
pygame.draw.rect(window, highlight_colour, (x_coord, y_coord, width, height))
if click[0] == 1:
button_action()
else:
pygame.draw.rect(window, initial_colour, (x_coord, y_coord, width, height))
button_Text = pygame.font.Font("freesansbold.ttf",20)
text_Surf, text_Rect = text_objects(msg, button_Text)
text_Rect.center = ((x_coord + (width / 2)), (y_coord + (height / 2)))
window.blit(text_Surf, text_Rect)
while play:
for event in pygame.event.get():
if event.type == pygame.QUIT:
play = False
button(screen, "Start Game", 231, 236, 250, 50, e_m_light, e_m_lighter, game_loop)
button(screen, "Options", 231, 336, 250, 50, e_m_light, e_m_lighter, update_options)
button(screen, "Exit", 231, 436, 250, 50, e_m_light, e_m_lighter, quit_game)
if clicked_options == True:
#update title colour for testing purposes
text_Surface = Font1.render('BlackJack Blast!', True, e_m_lighter)
else:
text_Surface = Font1.render('BlackJack Blast!', True, white)
screen.blit(textSurface,(106,100))
pygame.display.update()
Screenshots of the UI before and after pressing "Start Game":
I'm still a bit of a pygame novice, so if you're unsure of how to fix this specific problem, but recognise something else that could do with improving, or if you need any more information to develop a solution, please let me know!