Having trouble moving objects with pygame

Hi everyone, so im working with a simple pygame project, im just trying to get the rectangle that ive drawn to automatically move to the right, however it always remains stationary, please could someone tell me what im doing wrong, any help would be appreciated, thank you :).

heres the class that creates the rectangle:

import pygame
from pygame.sprite import Sprite

class Rectangle(Sprite):
    """A class to Create our rectangle"""

    def __init__(self):
        super().__init__()
        self.image = pygame.Surface([400, 50])
        self.rect = self.image.get_rect()
        self.color = (0, 40, 255)
        self.rect.x = 100
        self.rect.y = 0
        self.x = float(self.rect.x)
        self.y = float(self.rect.y)
        self.movement_speed = 1.1
        self.rec_direction = 1

    def update(self):
        """Move rectangle to the right"""
        self.x += (self.movement_speed *
                        self.rec_direction)
        self.rect.x = self.x

heres the class with the code for the main loop:

import sys
import time
from rectangle import Rectangle
import pygame

class Mainrec:
    """A class to manage the main target practice game"""
    def __init__(self):
        pygame.init()

        self.bg_color = (110, 5, 15)
        self.rectangle = Rectangle()

        self.screen = pygame.display.set_mode(
        (1800, 800))
        pygame.display.set_caption("Target practice")

        self.clock = pygame.time.Clock()
        self.rectangle_group = pygame.sprite.Group()
        self.rectangle_group.add(Rectangle())

    def check_events(self):
        """Respond to keypresses and mouse events."""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

    def update_screen(self):
        """Update images on the screen and flip to the new screen."""
        #Redraw the screen for each pass through the loop.
        self.screen.fill(self.bg_color)
        self.rectangle_group.draw(self.screen)
        pygame.display.flip()
        self.clock.tick(40)

    def _change_rec_direction(self):
        self.rectangle.rec_direction *= -1

    def _check_rec_edges(self):
        """Respond apporpriately if any queens have touched the edge."""
        for rectangle in self.rectangle_group.sprites():
            self._change_rec_direction()
            break


    def update_rec(self):
        self.rectangle.update()

    def run_game(self):
        """Our main loop for the game"""
        while True:
            self.check_events()
            self.rectangle.update()
            self.update_screen()


if __name__ == '__main__':
    ai = Mainrec()
    ai.run_game()

Could you point to place where the updated rectangle is supposed to be re-drawn?

the updated rectangle is simply supposed to move to the right until it disappears off the screen…

What I meant - where in code that happens?

ahh right… well in the section of the run_game loop, I called self.rectangle.update(), which calls this function:

def update(self):
        """Move rectangle to the right"""
        self.x += (self.movement_speed *
                        self.rec_direction)
        self.rect.x = self.x


I assumed because I had initialised the x position in the rectangles init method that I could just add an integer onto x to update it… obviously not the case though…

I think you can do that, this part though is for updating of coordinates. What about drawing it again?

i got it… so i was calling self.rectangle.update()… it should have been self.rectangle_group.update(), as my rectangle is actually in a group of sprites… thank you anyway

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.