Jump to content

Does anyone know how to move an object in pygame? When I hit the left or right arrow keys all it does is fill in to the right or left of where the image started from. So basically I am creating a line that goes both left and right instead of moving a solid object to the right or left of it's current position when hitting the arrow keys. Sorry, did that make any sense? This is my code -

main.txt

Link to comment
https://linustechtips.com/topic/114813-pygame-moving-an-object/
Share on other sites

Link to post
Share on other sites

So I looked up the documentation and it looks like you are using the wrong function to move the rectangle.  You should be using move instead of move_ip.  By the looks of things move_ip moves the rectangle in place (not quite sure of the meaning, but given that there is move I would bank on that being the correct call).
 
Also in the future please use code tags like below...and if it is a long set of code that you don't want visible just put the hides around them...like below (with the different functions called)  (You just type in [ spoiler] and [/spoiler ] without the spaces to hide it)
 

import pygamefrom pygame import *pygame.init()screen = pygame.display.set_mode((800,800))clock = pygame.time.Clock()class backgroundClass():        def __init__(self):        self.backgroundImage = (255,255,255)        screen.fill(self.backgroundImage)        class playerClass(pygame.sprite.Sprite):    def __init__(self):        pygame.sprite.Sprite.__init__(self)                            self.imageFile = 'firstPlayerImage.png'        self.image = pygame.image.load(self.imageFile)                self.rect = pygame.Rect(60,455,60,60) firstPlayer_Object = playerClass()   sprites = pygame.sprite.Group()sprites.add(firstPlayer_Object)backgroundClass()    def mainLoop():    gameLoop = True    while gameLoop == True:                for event in pygame.event.get():            if event.type == QUIT:                gameLoop = False                        if event.type == KEYDOWN:                                    if event.key == K_LEFT:                    firstPlayer_Object.rect.move(-8,0)                                                        if event.key == K_RIGHT:                    firstPlayer_Object.rect.move(8,0)                                                                    sprites.draw(screen)        sprites.update()        clock.tick(30)        pygame.display.update()mainLoop()

0b10111010 10101101 11110000 00001101

Link to comment
https://linustechtips.com/topic/114813-pygame-moving-an-object/#findComment-1534974
Share on other sites

Link to post
Share on other sites

 

So I looked up the documentation and it looks like you are using the wrong function to move the rectangle.  You should be using move instead of move_ip.  By the looks of things move_ip moves the rectangle in place (not quite sure of the meaning, but given that there is move I would bank on that being the correct call).

 

Also in the future please use code tags like below...and if it is a long set of code that you don't want visible just put the hides around them...like below (with the different functions called)  (You just type in [ spoiler] and [/spoiler ] without the spaces to hide it)

 

import pygamefrom pygame import *pygame.init()screen = pygame.display.set_mode((800,800))clock = pygame.time.Clock()class backgroundClass():        def __init__(self):        self.backgroundImage = (255,255,255)        screen.fill(self.backgroundImage)        class playerClass(pygame.sprite.Sprite):    def __init__(self):        pygame.sprite.Sprite.__init__(self)                            self.imageFile = 'firstPlayerImage.png'        self.image = pygame.image.load(self.imageFile)                self.rect = pygame.Rect(60,455,60,60) firstPlayer_Object = playerClass()   sprites = pygame.sprite.Group()sprites.add(firstPlayer_Object)backgroundClass()    def mainLoop():    gameLoop = True    while gameLoop == True:                for event in pygame.event.get():            if event.type == QUIT:                gameLoop = False                        if event.type == KEYDOWN:                                    if event.key == K_LEFT:                    firstPlayer_Object.rect.move(-8,0)                                                        if event.key == K_RIGHT:                    firstPlayer_Object.rect.move(8,0)                                                                    sprites.draw(screen)        sprites.update()        clock.tick(30)        pygame.display.update()mainLoop()
&

nbsp;Thanks but that din't work. Now nothing happens when I switch it to firstPlayer_Object.rect.move(-8,0) and firstPlayer_Object.rect.move(8,0) and hit the arrow keys. It doesn't move.

Link to comment
https://linustechtips.com/topic/114813-pygame-moving-an-object/#findComment-1536033
Share on other sites

Link to post
Share on other sites

Okay sorry, I read more into move_ip and you were right in using it (move_ip modifies the internal variables of rect)

 

So my quick guess then (again I haven't worked with this).  Given your explanation of what happened before I am guessing pygame doesn't clear the screen of previous graphics (so it is painting the rectangle over the previous rectangle) which is why you get a line.  To do this you can add the following line

screen.fill((0, 0, 0))          Sorry my enter button just broke so this final bit might be ugly *edit fixed my enter

Put the screen fill right before you call sprite.draw and use your original code not the one I posted earlier because you were right in using move_in

0b10111010 10101101 11110000 00001101

Link to comment
https://linustechtips.com/topic/114813-pygame-moving-an-object/#findComment-1540039
Share on other sites

Link to post
Share on other sites

Okay sorry, I read more into move_ip and you were right in using it (move_ip modifies the internal variables of rect)

 

So my quick guess then (again I haven't worked with this).  Given your explanation of what happened before I am guessing pygame doesn't clear the screen of previous graphics (so it is painting the rectangle over the previous rectangle) which is why you get a line.  To do this you can add the following line

screen.fill((0, 0, 0))          Sorry my enter button just broke so this final bit might be ugly *edit fixed my enter

Put the screen fill right before you call sprite.draw and use your original code not the one I posted earlier because you were right in using move_in

Hey, thanks! It did the trick. What I ended up doing was did what you said but instead of screen.fill((0,0,0)), I used screen.fill((255,255,255)) to keep the white background instead of a black one and just got rid of the backgroundClass. So now it moves to left or right across the screen when I hit the arrow keys instead of having a big long line. I kinda took a break from this project but will start it back up now that I'm over this wall. I will probably end up hitting another wall but that is learning for you. Thanks again for taking the time to help me!

Link to comment
https://linustechtips.com/topic/114813-pygame-moving-an-object/#findComment-1543835
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×