6

My friends and I are making a quiz game in PyGame and would like to know how, when the user presses a button, he can go to next question (without leaving the previous text behind).

Roamer-1888
  • 19,138
  • 5
  • 33
  • 44
user3219116
  • 77
  • 1
  • 1
  • 2

2 Answers2

8

First of all I would suggest that you go to the PyGame documentation and read up a little about PyGame. (Link)

However to save you time what you have to do is before you draw your new set of shapes/writings on the screen you have to use the function screen.fill(#Your chosen colour). That is the function in PyGame that gets rid of the old screen and allows you to draw new items on to a clear screen without the pervious drawings left on there.

Example:

import pygame
import sys
from pygame.locals import *

white = (255,255,255)
black = (0,0,0)
red = (255, 0, 0)

class Pane(object):
    def __init__(self):
        pygame.init()
        self.font = pygame.font.SysFont('Arial', 25)
        pygame.display.set_caption('Box Test')
        self.screen = pygame.display.set_mode((600,400), 0, 32)
        self.screen.fill((white))
        pygame.display.update()


    def addRect(self):
        self.rect = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
        pygame.display.update()

    def addText(self):
        self.screen.blit(self.font.render('Hello!', True, black), (200, 100))
        pygame.display.update()

    def addText2(self):
        self.screen.blit(self.font.render('Hello!', True, red), (200, 100))
        pygame.display.update()


    def functionApp(self):
        if __name__ == '__main__':
            self.addRect()
            self.addText()
            while True:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit(); sys.exit();

                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_ESCAPE:
                            self.screen.fill(white)
                            self.addRect()
                            self.addText2() #i made it so it only changes colour once.



display = Pane()
display.functionApp()
PythonNovice
  • 475
  • 2
  • 5
  • 21
6

In your game loop, before drawing the new frame, fill the frame with the background color.

Example:

ball = pygame.Rect(0,0,10,10)
while True:
    mainSurface.fill((0,0,0))
    pygame.draw.circle(display,(255,255,255),ball.center,5)
    ball.move_ip(1,1)
    pygame.display.update()

The key point is the mainSurface.fill which will clear the previous frame.

pathunstrom
  • 123
  • 1
  • 7