0

While I realize that there are quite a few posts about this on the StackOverflow forums, however none that I was able to find have a problem quite like mine. So basically, as you can probably tell from the title, I am having difficults moving an image in pygame. There are no errors, or anything of that nature, it's just that the image isn't moving. I am currently coding this in OOP style, so I will paste what is relavent.

class DisplayImage:
    def __init__(self, image, pos) -> None:
        self.x = pos[0]
        self.y = pos[1]
        self.image = pygame.image.load(image).convert()
        self.rect = self.image.get_rect(center=(self.x, self.y))
        


    def update(self, screen):
        self.rect.x = self.x
        self.rect.y = self.y
        screen.blit(self.image, self.rect)

    def checkForInput(self, event):
        if(self.rect.collidepoint(event.pos)):
            return True
        return False
    def move(self, direction):
        print("moving!")
        self.x += direction[0]
        self.y += direction[1]

Then there is my setup:

import pygame

pygame.init()

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

clock = pygame.time.Clock()

running = True
moving = False
while running:
    screen.fill("black")
    ran_candy = Candy('image.png', (100, 100))
    mouse_pos = pygame.mouse.get_pos()
  
    screen.blit(ran_candy.image, (ran_candy.x, ran_candy.y))
  
    for event in pygame.event.get():
        if (event.type == pygame.QUIT):
            running = False
        if (event.type == pygame.MOUSEBUTTONDOWN):
            if (ran_candy.checkForInput(event)):
                moving = True
        elif (event.type == pygame.MOUSEBUTTONUP):
            moving = False
        elif (event.type == pygame.MOUSEMOTION and moving):
            ran_candy.move(event.rel)
       

    

    pygame.display.flip()
    pygame.display.update()

This should be enough for reproducability. Right now my main guess would be that it's not properly updating the class self.x and self.y coordinates when I call the function, but I'm not sure. Feel free to help.

Edit: Another "problem" that I am having with this currently is that it is not really registering that I am trying to move it. I added some print statements to print out if I am moving it (while it does not update the image) it takes a while before it prints that I am moving it. What I mean is, the pygame.MOUSEMOTION seems quite sensitive and difficult to activate, any alleviations for that aswell?

Edit: Solution: Basically all I had to do was move it out of the loop and everything worked. So I had to create the class outside the loop and it seemed to work

  • create a variable x and y. These will mark the location of your image. The events should update the value of x and y. – Conic Feb 28 '23 at 21:59
  • I'm not sure what you mean -> there is already variables keeping track of the x and y in the DisplayImage class. Do you mean to put the variables x and y in the local scope? – Frank van Paassen Feb 28 '23 at 22:47
  • I also editted again with a new "problem" that I have been having – Frank van Paassen Feb 28 '23 at 22:52
  • Yet another update. I've tried changing the code to add a swap function. This way, I can just click 2 pictures and that they would swap x and y coordinates (which is basically the end functionality anyways). However, this does not really work. It registers that I clicked 2 different "images" but it never actually swaps them. This is making me think that it is more so the class structure of this all then it is the code itself??? if that helps – Frank van Paassen Feb 28 '23 at 23:11

1 Answers1

0

Since you are drawing the image at self.rect, you will need to update the rect position to match the x and y values.

def update(self, screen):
    self.rect.x = self.x
    self.rect.y = self.y
    screen.blit(self.image, self.rect)
DaNubCoding
  • 320
  • 2
  • 11
  • I have tested this on my system and it does not work. I understand the idea that you are going for, however it, like I said, did not work. Might you be able to suggest/supply me with other fixes? – Frank van Paassen Mar 01 '23 at 20:00
  • I didn't notice that you weren't using the update function to actually move the image. From what I can tell your code should work, there may be other parts of your code that is making things break – DaNubCoding Mar 02 '23 at 01:15
  • Might you know where this might be? Because there really isn't any other code in here? I can also provide an updated code block so it more closely matches my current iteration? – Frank van Paassen Mar 02 '23 at 10:28
  • I can only guess the move method is not being triggered. Is the print statement in there actually printing when you want it to? – DaNubCoding Mar 02 '23 at 13:45
  • For the most part yes. It does indeed on occasion print that it is moving, although it is quite "difficult" to get this to work aswell – Frank van Paassen Mar 02 '23 at 17:06