如何使图像/图像在 pygame 中消失? [英] how to make image/images disappear in pygame?

查看:222
本文介绍了如何使图像/图像在 pygame 中消失?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一个问题,这个问题在这个网站上已经回答了很多次,但我从来没有找到一个明显的答案.如果游戏窗口中已经有背景图像,如何在 Pygame 中删除图像.在大多数答案中,他们说使用 screen.fill(color) 但它使该区域变黑.如果没有背景图像但有背景图像,它工作正常,当只有某个区域被涂成黑色时看起来很奇怪.如何我可以摆脱它吗?在这种特殊情况下,有没有其他方法可以删除图像.顺便说一下,我没有在此处添加任何特定代码,因为我在构建游戏时经常处理这个问题.

Hi i want to ask a question which has been answered a lot of times on this site but i never found an appreciable answer.How to remove the images in Pygame if there is already a background image in game window. In most of the answers,they say to use screen.fill(color) but it makes the area black.It works fine if there is no background image but with background image,it looks odd when only a certain region is colored black.How can i get rid of it?Is there an alternative way to remove the image in this particular situation.By the way I'm not adding any specific code here because i deal with this issue a lot of time while building games.

推荐答案

您不能删除图像.您必须在应用程序循环中重绘整个场景(包括背景).请注意,图像只是在背景顶部绘制的大量像素.图像下方"的背景被覆盖,信息丢失:

You can not remove an image. You have to redraw the entire scene (including the background) in the application loop. Note the images are just a buch of pixel drawn on top of the background. The background "under" the images is overwritten and the imformation is lost:

主应用程序循环必须:

最低申请是

import pygame

pygame.init()

window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

# main application loop
run = True
while run:
    clock.tick(60)

    # event loop
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # update game states and move objects
    # [...]

    # clear the display
    window.fill(0) # or `blit` the back ground image instead

    # draw the scene - draw all the objects
    # [...]

    # update the display
    pygame.display.flip()

这篇关于如何使图像/图像在 pygame 中消失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆