[英]How to clear up screen in pygame?
我和我的朋友們正在PyGame中做一個問答游戲,想知道如何,當用戶按下一個按鈕時,他可以轉到下一個問題(而不會留下前一個文本)。
首先,我建議您轉到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()
在游戲循環中,在繪制新框架之前,請用背景色填充框架。
例:
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()
關鍵點是mainSurface.fill,它將清除前一幀。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.