如何清除pygame中的屏幕?

use*_*116 0 python pygame

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

小智 6

在游戏循环中,在绘制新框架之前,用背景颜色填充框架。

例子:

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()
Run Code Online (Sandbox Code Playgroud)

关键点是 mainSurface.fill ,它将清除前一帧。


Pyt*_*ice 5

首先,我建议您转到PyGame文档并阅读一些有关PyGame的信息。(链接

但是,为节省时间,您需要做的就是在屏幕上绘制一组新的形状/文字之前,必须使用函数screen.fill(#Your chosen colour)。那是PyGame中的功能,它摆脱了旧的屏幕,使您可以将新的项目绘制到清晰的屏幕上,而无需留下以前的图纸。

例:

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()
Run Code Online (Sandbox Code Playgroud)