简体   繁体   中英

How to clear up screen in pygame?

我和我的朋友们正在PyGame中做一个问答游戏,想知道如何,当用户按下一个按钮时,他可以转到下一个问题(而不会留下前一个文本)。

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM