這段代碼很好,我的圖片翻轉了,但是當我沒有按下某個鍵時,我的圖像就消失了......我知道這與在每個'if'語句中調用screen.blit方法時有關在更新功能,但我不知道如何解決這個問題?爲什麼我不按下按鈕時圖像消失?
import pygame, sys, glob
from pygame import *
h=400
w=800
screen = pygame.display.set_mode((w,h))
clock = pygame.time.Clock()
class player:
def __init__(self):
self.x = 200
self.y = 300
self.ani_speed_init = 8
self.ani_speed=self.ani_speed_init
self.Lani_speed_init = 8
self.Lani_speed=self.Lani_speed_init
self.ani = glob.glob("D:\Projects\pygame\TestGame\sprite*.png")
self.Lani = glob.glob("D:\Projects\pygame\TestGame\Lsprite*.png")
self.ani.sort()
self.ani_pos=0
self.Lani.sort()
self.Lani_pos=0
#takes length of the amount of images and subtracts 1 since count starts at 0
self.ani_max=len(self.ani) -1
self.Lani_max=len(self.Lani) -1
self.img = pygame.image.load(self.ani[0])
self.Limg = pygame.image.load(self.Lani[0])
self.update(0, 0)
def update(self, pos, posL):
if pos != 0:
#subtracts 1 from 10
self.ani_speed-=1
#adds self.x to itself
self.x+=pos
if self.ani_speed==0:
self.img = pygame.image.load(self.ani[self.ani_pos])
self.ani_speed = self.ani_speed_init
if self.ani_pos == self.ani_max:
self.ani_pos = 0
else:
self.ani_pos+=1
screen.blit(self.img,(self.x,self.y))
if posL != 0:
#subtracts 1 from 10
self.Lani_speed-=1
#adds self.x to itself
self.x+=posL
if self.Lani_speed==0:
self.Limg = pygame.image.load(self.Lani[self.Lani_pos])
self.Lani_speed = self.Lani_speed_init
if self.Lani_pos == self.Lani_max:
self.Lani_pos = 0
else:
self.Lani_pos+=1
screen.blit(self.Limg,(self.x,self.y))
player1 = player()
pos = 0
posL = 0
while 1:
screen.fill((255,255,255))
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.quit()
elif event.type == KEYDOWN and event.key == K_RIGHT:
pos = 1
elif event.type == KEYUP and event.key == K_RIGHT:
pos = 0
elif event.type == KEYDOWN and event.key == K_LEFT:
posL = -1
elif event.type == KEYUP and event.key == K_LEFT:
posL = 0
player1.update(pos, posL)
pygame.display.update()