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...
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.
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! 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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.