[英]Why does my image disappear when I'm not pressing a button?
The code funs fine and my images flip as they should, but when I am not pressing a key, my image disappears...I know this has to do with when the screen.blit method is called during each 'if' statement in the update function, but I don't know how to get around this... 代码很好用,我的图像按原样翻转,但是当我不按任何键时,我的图像就会消失...我知道这与在屏幕中的每个“ 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()
This is a suggestion rather than an answer as it's difficult to tell what you're trying to do but I need to format code so a comment isn't really useful. 这是一个建议,而不是一个答案,因为很难告诉您要执行的操作,但是我需要格式化代码,因此注释并不是真正有用。 Would it work to extend the update function to have a default behaviour as follows:
将扩展功能扩展为具有以下默认行为是否可行:
def update(self, pos, posL):
updated = False
if pos != 0:
(logic removed)
screen.blit(self.img,(self.x,self.y))
updated = True
if posL != 0:
(logic removed)
screen.blit(self.Limg,(self.x,self.y))
updated = True
if not updated:
screen.blit(something sensible for arguments of 0,0)
You could avoid the 'updated' variable if you just tested for pos == 0 and posL == 0 but the updated test allows the logic above to possibly not do a screen.blit and the final test will ensure that something always gets written to the screen. 如果您只测试了pos == 0和posL == 0,则可以避免使用'updated'变量,但是更新后的测试允许上面的逻辑不做screen.blit,最终测试将确保始终将某些内容写入屏幕。
Rather than allowing the update function to blit player to the screen I would add player to the default pygame sprite group and draw it via the while loop! 与其允许更新功能将玩家带到屏幕上,不如将玩家添加到默认的pygame sprite组中并通过while循环进行绘制! I would also avoid loading the image each update (due to overhead) and rather load all images at once and then blit them when needed!
我还避免每次更新都加载图像(由于开销),而是一次加载所有图像,然后在需要时将其blit!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.