0
我正在通过做一个小项目来练习我的pygame技能。其中,它会将背景图像投射到屏幕上。之后,它将使用一个名为士兵的列表,如果列表中的物品是1,它将打印一名士兵,如果它是0,则会跳过一个空格。然而,当我运行代码时,它将背景,然后是精灵,然后精灵消失。我想让我的精灵在for循环完成后留在屏幕上。下面是对循环部分:麻烦blitting图像pygame
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.blit(background_img, (0,0))
for i in soldiers:
if i == 1:
screen.blit(sprite_img,(x,y))
x = x + 50
time.sleep(0.5)
pygame.display.update()
elif i == 0:
x = x + 50
time.sleep(0.5)
pygame.display.update()
pygame.display.update()
这里是我的所有代码:
import sys, pygame, time
from pygame.locals import *
pygame.init()
soldiers = [0,1,1,1,1,0,0,1,1,0]
x = 0
y = 50
background_img = pygame.image.load("/home/myname/Desktop/Army Project/images/background.png")
sprite_img = pygame.image.load("/home/myname/Desktop/Army Project/images/sprite.png")
size = background_img.get_size()
rect = background_img.get_rect()
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Army Men")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.blit(background_img, (0,0))
for i in soldiers:
if i == 1:
screen.blit(sprite_img,(x,y))
x = x + 50
time.sleep(0.5)
pygame.display.update()
elif i == 0:
x = x + 50
time.sleep(0.5)
pygame.display.update()
pygame.display.update()
谢谢您的时间。
谢谢你,似乎除了图像的blit真快,几乎没有明显的向好工作。我可以使用“time.sleep()”函数什么替代方法? – user163505
@ user163505我不确定你的意思。当图像出现时,无论如何它都会停留在屏幕上。你能详细说明一下吗? –