Pygame:即使在while循环中,也只运行一次函数



在我的pygame脚本中,我有一个函数来处理我必须在while循环中使用的一些图像。问题是,我只想对每个图像运行一次函数。所以,我想运行一次self.convert_img(self.img_one),然后继续运行self.convert_img(self.img_two),以此类推,直到处理完所有图像。之后,我想让这个功能停止,这样我就可以改变场景了。

下面是我的代码的模拟:

import pygame
class Main:
def __init__(self):
pygame.init()
self.window = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
self.clock = pygame.time.Clock()
self.count = 5
# Load Images
self.img_one = pygame.image.load(os.path.join("images", "img_one.png"))
self.img_two = pygame.image.load(os.path.join("images", "img_two.png"))

def run(self):
while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()

img_one_transorm = self.convert_img(self.img_one)
img_two_transorm = self.convert_img(self.img_two)
## img three transform

self.clock.tick(30) 
def convert_img(self, arg1):    
self.window.blit(arg1, convert_img_rect)

pygame.display.update()
if self.count > 0:
## convert image function

创建一个计数,该计数增加1,然后使用一系列if语句如:

if count == 1:
img_one_transorm=self.convert_img(self.img_one)
count+=1
elif count == 2:
img_two_transorm=self.convert_img(self.img_two)
count+=1

我理解对了吗?

最新更新