pygame图像不会出现
问题描述:
app2图像应从右侧滑入,但不会出现。两幅图像都是1920x1080。我怎样才能解决这个问题?pygame图像不会出现
import pygame, sys
from pygame.locals import*
number=1
screen = pygame.display.set_mode((1920,1080))
app1=pygame.image.load("app1.jpg").convert()
app2=pygame.image.load("app2.jpg").convert()
clock = pygame.time.Clock()
app1x=0
app2x=1920
loop=True
while loop:
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type==KEYDOWN:
if event.key==K_RIGHT:
print ("right")
number=number+1
if event.type==pygame.QUIT:
print("l")
loop=False
pygame.quit()
quit()
screen.blit(app1, (app1x,0))
if number==1:
screen.blit(app1, (app1x,0))
if number==2:
if app2x==0:
print ("d")
else:
app2x=app2x-1
app1x=app1x-1
clock.tick(20)
pygame.display.update()
答
您只更新屏幕的一部分,以及时钟不在while循环内。下面是使用你的代码的例子:
while loop:
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type==KEYDOWN:
if event.key==K_RIGHT:
print ("right")
number=number+1
if event.type==pygame.QUIT:
print("l")
loop=False
screen.blit(app1, (app1x,0))
if number==1:
screen.blit(app1, (app1x,0))
if number==2:
if app2x==0:
print ("d")
else:
app2x=app2x-1
app1x=app1x-1
pygame.display.flip()
clock.tick(20)
pygame.quit()
另外,还要注意如何我整个while
循环之后移动pygame.quit()
。这使得当loop
为假并结束时,pygame也会退出。移动pygame.quit()
只是为了稍微清理代码,并且不会显着影响代码。
干杯!
尝试在'screen.blit(app1,(app1x,0))'后面执行'pygame.display.update()'。 –