英文:
Why does Screen Fill not Remove Blit in my Pygame Program?
问题
我正在开发一个使用 Pygame 的 Python 程序。我无法完全清空屏幕,因为使用 screen.fill
仍然会导致文本残留。我的代码如下:
while True:
scene0()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
screen.fill("White")
pygame.display.flip()
scene0()
代码如下:
def scene0():
into_font = pygame.font.Font(None, 200)
intro_surf = into_font.render('游戏名称', False, '黑色').convert()
intro_rect = intro_surf.get_rect(center=(600, 300))
txt_font = pygame.font.Font(None, 90)
text_surf = txt_font.render('按空格键开始', False, "黑色").convert()
text_rect = text_surf.get_rect(center=(600,420))
screen.blit(intro_surf, intro_rect)
screen.blit(text_surf, text_rect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if exit_button.draw(screen):
run = exit()
场景 0:
Scene 0
使用 screen.fill
后:
After screen fill
英文:
I am developing a Python program which uses Pygame. I cannot clear the screen entirely as using screen.fill still results in lingering text. My code:
while True:
scene0()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
screen.fill("White")
pygame.display.flip()
The scene0()
code:
def scene0():
into_font = pygame.font.Font(None, 200)
intro_surf = into_font.render('Game Name', False, 'Black').convert()
intro_rect = intro_surf.get_rect(center=(600, 300))
txt_font = pygame.font.Font(None, 90)
text_surf = txt_font.render('Press Space to Start', False, "Black").convert()
text_rect = text_surf.get_rect(center=(600,420))
screen.blit(intro_surf, intro_rect)
screen.blit(text_surf, text_rect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if exit_button.draw(screen):
run = exit()
The scene 0:
Scene 0
After using screen fill:
After screen fill
答案1
得分: 1
Your code isn't working because screen.fill()
清空了屏幕,但在下一个 while
循环迭代中,scene0()
函数会再次在屏幕上绘制一切。您可以创建一个条件,然后调用 scene0()
函数。示例代码:
screen_status = "active"
while True:
if screen_status == "active":
scene0()
elif screen_status == "blank":
screen.fill("White")
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
screen_status = "blank";
在上面的代码中,最初将 screen_status
变量设置为 active
,因此调用 scene0()
函数。然而,当按下空格键时,screen_status
被设置为 blank
。因此,屏幕被清空。
如果您希望再次绘制所有内容,可以再次将 screen_status
设置为 active
。
英文:
Your code isn't working because, screen.fill()
clears the screen, but, in the next iteration of the while
loop, the scene0()
function blits everything on the screen again. You can create a condition and then call the scene0()
function. Example code:
screen_status = "active"
while True:
if screen_status == "active":
scene0()
elif screen_status == "blank":
screen.fill("White")
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
screen_status = "blank"
In the above code, initially the screen_status
variable is set to active
, so the scene0()
function is called. However, when the spacebar is pressed, screen_status
is set to blank
. So, the screen is cleared.
If you want to blit everything again, you can set screen_status
to active
again.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论