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

Python 如何在pygame中将一个曲面(图像)转换为另一个曲面?,python,python-3.x,pygame,pygame-surface,Python,Python 3.x,Pygame,Pygame Surface,如何在不使用sprite类的情况下将pygame中的一个图像转换为另一个图像?另外,在将上一个图像转换为另一个图像后,如何删除它?将一个图像转换为另一个图像与重新分配变量一样简单 firstImage = pygame.image.load("firstImage.png") secondImage = pygame.image.load("secondImage.png") firstImage = secondImage del secondImage 我不知道你删除图像的确切意思。您

如何在不使用sprite类的情况下将pygame中的一个图像转换为另一个图像?另外,在将上一个图像转换为另一个图像后,如何删除它?

将一个图像转换为另一个图像与重新分配变量一样简单

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

firstImage = secondImage

del secondImage

我不知道你删除图像的确切意思。您可以使用“del secondImage”删除代码中的引用并将其发送到垃圾收集。一旦您清除屏幕并blit更新的图像,就不会再有任何过时图像的迹象

将一个图像转换为另一个图像就像重新分配变量一样简单

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

firstImage = secondImage

del secondImage

我不知道你删除图像的确切意思。您可以使用“del secondImage”删除代码中的引用并将其发送到垃圾收集。一旦您清除屏幕并blit更新的图像,就不会再有任何过时图像的迹象

我今天写了一个小程序,演示如何切换对象图像(它可能有助于/回答您的问题)。它为大部分代码的使用提供了注释,因此更容易理解它是如何工作的以及为什么工作的(据我所知,任何人都可以从昨天开始编程)

无论如何,代码如下:

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()

我不确定您的代码是如何设置的,或者这是否是您所需要的(我也不完全理解类,所以它可能是一个sprite类),但我希望这有帮助

我今天写了一个小程序,演示如何切换对象图像(它可能有助于/回答您的问题)。它为大部分代码的使用提供了注释,因此更容易理解它是如何工作的以及为什么工作的(据我所知,任何人都可以从昨天开始编程)

无论如何,代码如下:

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()

我不确定您的代码是如何设置的,或者这是否是您所需要的(我也不完全理解类,所以它可能是一个sprite类),但我希望这有帮助

你到底想做什么?把一个曲面转换成另一个曲面是什么意思?更改为新格式?你能给我们举一个你尝试过的例子吗?也许更清楚。你到底想做什么?你把一个曲面转换成另一个曲面是什么意思?更改为新格式?你能给我们看一个你尝试过的代码示例吗?它可能更清晰。