简体   繁体   English

python-如何在pygame中反转“ window.blit”,或删除我已创建的对象(sprite)

[英]python - how to reverse “window.blit” in pygame, or deleting an object (sprite) i have made

i am trying to delete a sprite whenever I click on it, however, i can't get the oringinal item off of the screen. 我试图在每次单击它时删除一个精灵,但是,我无法从屏幕上删除原始项目。 it seems once i render, or 'draw' it there, it stays there permentaelly. 似乎一旦我渲染或“绘制”到那里,它就会一直呆在那里。 How can I change the following code to get rid of the 'blit' i produced in the class once it's clicked on? 一旦单击该类,如何更改以下代码以消除该类中产生的“污点”?

import pygame

pygame.init()

window = pygame.display.set_mode((1152,720))

background = pygame.image.load("images/emptyroom.jpg")
image=pygame.image.load("images/smallkey.png")
b = window.blit(pygame.image.load("images/emptyroom.jpg"), (0,0))
spritestay = True

# d = window.blit(pygame.image.load("images/leglamp.png"), (15,500))

class HiddenObject(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.i1 = pygame.image.load("images/smallkey.png")
        self.i2 = pygame.image.load("images/Clear.png")
        self.rect=self.i1.get_rect()

    def draw(self, x, y):
        while spritestay == True:
            d = window.blit(self.i1, (x,y))
#d.collidepoint(pygame.mouse.get_pos())
        while spritestay == False:
            d = window.blit(self.i1, (x,y))

pygame.display.set_caption("Eye Spy Game Test")

gameLoop = True

while gameLoop:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            spritestay = False

        if event.type == pygame.MOUSEBUTTONUP:
            spritestay = True

        if (event.type==pygame.QUIT):
            gameLoop=False

    yotestvar = HiddenObject()
    d = yotestvar.draw(100,100)


    pygame.display.flip()

pygame.quit()

There are quite a few issues with your code. 您的代码有很多问题。

Blitting should not be done before the main loop. 在主循环之前不应该进行blitting。

It would be better if the Hidden Object new its position. 如果“隐藏对象”重新设置位置会更好。 Change it by adding a position in the constructor, and use it in the draw method. 通过在构造函数中添加位置来更改它,并在draw方法中使用它。

def __init__(self,pos):
    ...
    self.pos = pos

def draw(self):
    window.blit(self.i1, self.pos)

Your draw method never finishes. 您的绘制方法永远不会完成。 You need to replace the while statements with if statements. 您需要将if语句替换为while语句。 Also you are not drawing the second image when spritestay is False. 当spritestay为False时,您也不会绘制第二张图像。 I guess you wanted to blit Clear.png. 我想您想对Clear.png进行blit。

if spritestay:
    window.blit(self.i1, (x,y))
else: 
    window.blit(self.i2, (x,y))

If Clear.png is just an empty image, there is no point of blitting it at all. 如果Clear.png只是一个空图像,则根本不会对其进行盲文处理。 So this becomes: 因此,它变为:

if spritestay:
   window.blit(self.i1, (x,y))

You do not return anything from the draw function, so after 您不从draw函数返回任何内容,因此

d = yotestvar.draw(100,100)

d is None. d为无。 You don't use d anywhere so you can remove that. 您不会在任何地方使用d,因此可以将其删除。

You create the HiddenObject on every frame. 您在每个框架上创建HiddenObject。 I think that it is much better to move it before the loop. 我认为最好在循环之前将其移动。

Last issue is the fact that you do not fill the screen with any color. 最后一个问题是您没有用任何颜色填充屏幕。 This might be the real issue you have been strugling with. 这可能是您一直在努力解决的真正问题。 Here is how blitting works. 这是发短信的方式。

You are a painter, and in front of you is a canvas. 您是画家,而在您面前是画布。 If you take your brush and draw something, how can you get rid of it? 如果您拿着笔刷画些东西,该如何摆脱呢? Well the only way is to draw something on top of it. 好吧,唯一的办法就是在上面画些东西。 In pygame we use the fill method to fill a whole surface with a certain color. 在pygame中,我们使用fill方法用某种颜色填充整个表面。

screen.fill((0,0,0)) # fills screen with black color

So in order to have something on the screen, you need to call screen.fill and then draw any other objects you wish to see. 因此,为了在屏幕上显示某些内容,您需要调用screen.fill ,然后绘制您希望看到的任何其他对象。

In pygame, once you blit to the screen you cannot simply delete chosen objects. 在pygame中,一旦打开屏幕,便无法简单地删除所选对象。 Instead you must clear your screen after every draw and then draw the new updated objects. 相反,您必须在每次绘制后清除屏幕,然后绘制新的更新对象。 If for example, you were making something more, you would have to draw it and then erase it, move it very slightly, draw it then erase it again and so on. 例如,如果要制作更多东西,则必须先绘制然后将其擦除,将其稍微移动,然后绘制然后再将其擦除,依此类推。

Therefore, it is recommended that you clear your screen at the end every time you loop through a while loop. 因此,建议您每当循环一次while循环时都在最后清除屏幕。 It would look something as follows for your case: 对于您的情况,它看起来如下所示:

yotestvar = HiddenObject()

while gameLoop:
    window.fill((255, 255, 255)) # (255, 255, 255) RGB value for WHITE
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            spritestay = False

        if event.type == pygame.MOUSEBUTTONUP:
            spritestay = True

        if (event.type==pygame.QUIT):
            gameLoop=False

    yotestvar.draw(100,100)
    pygame.display.update()

pygame.quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM