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()