Diagonal Movement of Sprites


twisted1230

Still Fresh
Joined
Mar 6, 2012
Messages
2
Hello, I'm currently in process of learning Pygame and Python and I'm trying to make a sprite move diagonally across the screen. Although it does work to move the sprite, this code ends up 'painting' the screen when I try to move it diagonally. Is there any way I can fix that?


import pygame, sys, time


from pygame.locals import *


pygame.init()


pygame.display.init()


screen=pygame.display.set_mode ((800,600))


clock=pygame.time.Clock()


image = 'image.png'


Image = pygame.image.load(image).convert_alpha()


x,y=0,0


movex, movey,=0,0


while True:


for event in pygame.event.get():


if event.type == QUIT:


pygame.quit()


sys.exit()


if event.type == KEYDOWN:


if event.key==K_LEFT:


movex=-1


elif event.key==K_RIGHT:


movex=1


elif event.key==K_UP:


movey=-1


elif event.key==K_DOWN:


movey=1


if event.type == KEYUP:


if event.key==K_LEFT:


movex=0


elif event.key==K_RIGHT:


movex=0


elif event.key==K_UP:


movey=0


elif event.key==K_DOWN:


movey=0


x = x+movex


y = y+movey


screen.blit(Image,(x,y))


pygame.display.update()
 
I think you need to put each movement in a loop and blit to screen on each loop otherwise it will just make a line appear.
 
Hey there, sadly I cannot help you with your code (no Pygame knowledge and from a logic point of view the snipped you posted seems sane), but especially with languages depending on indentation and whitespace (but also for any other) use the



Code:
[code][/ code] (remove the space in the closing tag)

tag (aka surround your code with this tag, looks like above) or when editing your post press the icon looking like <> which will open a small editor for you to paste your code in.



Alternatively, especially when it's a longer snippet (>50 lines), consider using http://pastebin.com/ or a similar service for the code.



That way it's much more readable and we are more likely to be able to help you :)



EDIT: As I said I don't know Pygame, but usually you have to clear the screen between two draw operations.

Drawing a bitmap on the screen kind of works like a stamp. So if you continuously "stamp" bitmaps on the screen they will overlap. Clearing then is like getting a new sheet of paper between each stamp (like how you only draw one frame per page in a flipbook).

The function probably is something like



Code:
pygame.display.clear()

or

screen.clear()

Google or the manual should give you the real name.
 
Last edited by a moderator:
You should clear the screen before blitting the sprite.


EDIT: aaand prehistorically ninja'd by foxblock. I should read more carefully.
 
Last edited by a moderator:
Back
Top