简体   繁体   English

如何在pygame中将一个表面(图像)转换为另一个表面?

[英]How do I convert a surface(image) into another surface in pygame?

How do I convert an image to another image in pygame without using sprite class? 如何在不使用Sprite类的情况下将图像转换为pygame中的另一个图像? Also how can I remove the previous image after I convert it to another one? 另外,将前一张图像转换为另一张图像后,如何删除?

Converting one image to another is as simple as reassigning the variable 将一个图像转换为另一个图像就像重新分配变量一样简单

firstImage = pygame.image.load("firstImage.png")
secondImage = pygame.image.load("secondImage.png")

firstImage = secondImage

del secondImage

I'm not sure what exactly you mean by removing the image. 我不确定删除图像的确切含义。 You could use "del secondImage" to delete the reference in your code and send it to garbage collection. 您可以使用“ del secondImage”来删除代码中的引用,并将其发送到垃圾回收。 Once you clear the screen and blit the updated image there should no longer be any sign of the outdated image. 清除屏幕并变白更新的图像后,就不应再有过时图像的迹象。

I wrote a small program today that shows how I switch an objects image(it may help/answer your question). 我今天写了一个小程序,展示了我如何切换对象图像(它可以帮助/回答您的问题)。 It has notes for most of the code's use so it is easier to understand how and why it works(for all I know, anyone could have started programming yesterday). 它为大多数代码的使用提供了注释,因此更容易理解它的工作方式和原因(据我所知,任何人昨天都可以开始编程)。

Anyway, here is the code: 无论如何,这是代码:

import pygame, sys

#initializes pygame
pygame.init()

#sets pygame display width and height
screen = pygame.display.set_mode((600, 600))

#loads images
background = pygame.image.load("background.png").convert_alpha()

firstImage = pygame.image.load("firstImage.png").convert_alpha()

secondImage = pygame.image.load("secondImage.png").convert_alpha()

#object
class Player:
    def __init__(self):

        #add images to the object
        self.image1 = firstImage
        self.image2 = secondImage

#instance of Player
p = Player()

#variable for the image switch
image = 1

#x and y coords for the images
x = 150
y = 150

#main program loop
while True:

    #places background
    screen.blit(background, (0, 0))

    #places the image selected
    if image == 1:
        screen.blit(p.image1, (x, y))
    elif image == 2:
        screen.blit(p.image2, (x, y))

    #checks if you do something
    for event in pygame.event.get():

        #checks if that something you do is press a button
        if event.type == pygame.KEYDOWN:

            #quits program when escape key pressed
            if event.key == pygame.K_ESCAPE:
                sys.exit()

            #checks if down arrow pressed
            if event.key == pygame.K_DOWN:

                #checks which image is active
                if image == 1:

                    #switches to image not active
                    image = 2

                elif image == 2:

                    image = 1

    #updates the screen
    pygame.display.update()

I am not sure how your code is set up or if this is what you need (I don't entirely understand classes either so it might be a sprite class), but I hope this helps! 我不确定您的代码是如何设置的,或者这是否是您需要的(我也不完全理解类,因此它可能是sprite类),但是我希望这会有所帮助!

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

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