如何在pygame中清除屏幕? [英] How to clear up screen in pygame?
问题描述
我和我的朋友正在 PyGame 中制作一个问答游戏,想知道如何当用户按下按钮时,他可以转到下一个问题(不留下之前的文本).
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).
推荐答案
首先,我建议您转到 PyGame 文档并阅读一些有关 PyGame 的信息.(链接)
First of all I would suggest that you go to the PyGame documentation and read up a little about PyGame. (Link)
然而,为了节省您的时间,您必须在屏幕上绘制一组新的形状/文字之前使用函数screen.fill(#Your selected colour)
.这就是 PyGame 中的功能,它摆脱了旧屏幕,并允许您在清晰的屏幕上绘制新项目,而不会留下以前的图纸.
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.
示例:
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()
这篇关于如何在pygame中清除屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!