So here's my code. What I want is for the player to move down, and while it's moving down, to cycle through images in the sequence (0, 1, 0, -1, 0...) but it doesn't do that.

import pygame, time
pygame.init()
Screen = pygame.display.set_mode((400, 400), 0, 32)
pygame.display.set_caption('Walk around')
GREEN = (0, 255, 0)
Screen.fill(GREEN)
MOVESPEED = 2
moveDown = False
moveUp = False
moveLeft = False
moveRight = False
direction = 'down'
moveCount = 1
imageset = 0
playerimagedown0 = pygame.image.load('C:\Users\Lauren\Documents\RPG dev\down0.gif')
playerimagedown1 = pygame.image.load('C:\Users\Lauren\Documents\RPG dev\down1.gif')
playerimagedownminus1 = pygame.image.load('C:\Users\Lauren\Documents\RPG dev\down-1.gif')
player = pygame.Rect(100, 100, 38, 92)
while True:
	for event in pygame.event.get():
		if event.type == pygame.KEYDOWN:
			if event.key == pygame.K_DOWN:
				moveDown = True
				direction = 'down'
				if imageset == 0:
					Screen.fill(GREEN)
					Screen.blit(playerimagedown0, player)
					pygame.display.update()
					imageset = moveCount
					print '0'
					print moveCount
					print imageset
				elif imageset == -1:
					Screen.fill(GREEN)
					Screen.blit(playerimagedownminus1, player)
					pygame.display.update()
					moveCount = -moveCount
					imageset = 0
					print 'Neg 1'
					print moveCount
					print imageset
				elif imageset == 1:
					Screen.fill(GREEN)
					Screen.blit(playerimagedown1, player)
					pygame.display.update()
					moveCount = -moveCount
					imageset = 0
					print '1'
					print moveCount
					print imageset
		if event.type == pygame.KEYUP:
			if event.key == pygame.K_DOWN:
				moveDown = False
				imageset = 0
			if event.key == pygame.K_ESCAPE:
				pygame.quit()
			if direction == 'down':
				Screen.fill(GREEN)
				Screen.blit(playerimagedown0, player)
				pygame.display.update()
	if moveDown == True and player.bottom < 400:
		player.top += MOVESPEED	
	time.sleep(0.02)

I think the problem might have to do with the second and third images not loading properly, because all I see is playerimagedown0. Also, the movement is not smooth.

This code you must keep hitting the down arrow key to change images

import pygame

pygame.init()

GREEN = (0,255,0)

def main():
	Screen = pygame.display.set_mode((400,400))
	# pygame framerate control
	clock = pygame.time.Clock()
	
	# converting images is changing pixel layout.
	# it also lets transparent work
	# it speed up the drawing process
	playerimagedown0 = pygame.image.load('C:\Users\Lauren\Documents\RPG dev\down0.gif')
	playerimagedown0 = playerimagedown0.convert()
	playerimagedown1 = pygame.image.load('C:\Users\Lauren\Documents\RPG dev\down1.gif')
	playerimagedown1 = playerimagedown1.convert()
	playerimagedownminus1 = pygame.image.load('C:\Users\Lauren\Documents\RPG dev\down-1.gif')
	playerimagedownminus1 = playerimagedownminus1.convert()
	
	downsequence = (0,1,0,-1)
	sequencepos = 0
	
	drawimage = playerimagedown0 
	
	Run = True
	while Run:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				Run = False
			elif event.type == pygame.KEYDOWN:
				if event.key == pygame.K_ESCAPE:
					Run = False
				elif event.key == pygame.K_DOWN:
					sequencepos += 1
					sequencepos %= len(downsequence)
					drawimage = [playerimagedownminus1,playerimagedown0,playerimagedown1][downsequence[sequencepos] + 1]
		
		Screen.fill(Green)
		Screen.blit(drawimage,(150,150))
		pygame.display.flip()
		clock.tick(60)
	pygame.quit()
	
main()

Edited 5 Years Ago by DrakeMagi: n/a

This article has been dead for over six months. Start a new discussion instead.