1

So I have revamped some code by using 'pygame.sprite' and making a function to handle all drawing. If you see the code below, I am trying to make a target shooter game, player fires bullets towards cursor and tries to hit targets. The 'movement()' function is meant to do what it says on the tin, make the targets move each time the screen refreshes by increasing the 'self.rect.x' value by 0.5. I have called the function inside of another function (refresh_window()'. The 'refresh_window()' function just handles all drawing. When I run the game however, the targets do not move. I get no errors or anything, I guessed it is because self.rect.x in 'movement()' is not global but when i tried to make it global, i got an error saying:

File "main.py", line 60
    global item.rect.x
               ^
SyntaxError: invalid syntax

Anyway, I am struggling to see the issue with the code so if you can see the issue, please do point it out, it will be much appreciated. Cheers.

class Target(pygame.sprite.Sprite):
    def __init__(self, width, height, offset, threshold):
        pygame.sprite.Sprite.__init__(self, target_sprites)
        self.image = pygame.Surface([width, height])
        self.image = target_img
        self.rect = self.image.get_rect()
        self.rect.center = (self.rect.x + 50, self.rect.y + offset)


target_sprites = pygame.sprite.Group()

target_1 = Target(100, 100, 100, 0)
target_2 = Target(100, 100, 300, 1000)
target_3 = Target(100, 100, 200, 2000)

#Function to make targets move each time screen refreshes.
def movement():
  global item.rect.x
  for item in target_sprites:
    item.rect.x += 0.5
    return


#Creating a function which will deal with redrawing all sprites and updating the screen.
def refresh_window():
  window.blit(bgr, (0,0))

  player_sprites.draw(window)
  target_sprites.draw(window)
  movement()
  pygame.display.update()
j_yerbe
  • 111
  • 1
  • 9
  • What do you expect by `global item.rect.x`!? Delete this line of code and read about [`global` statement](https://docs.python.org/3/reference/simple_stmts.html?highlight=global#grammar-token-global-stmt) – Rabbid76 Mar 26 '20 at 15:07
  • @Rabbid76 After reading that I assume it is nothing to do with it not being globalised? – j_yerbe Mar 26 '20 at 15:17

1 Answers1

1

global item.rect.x does not make any sense at all. Remove this line and read about the global statement.


The Target objects do not move, because you didn't add them to target_sprites. You missed add the pygame.sprite.Sprite objects to the pygame.sprite.Group():

target_sprites = pygame.sprite.Group()

target_1 = Target(100, 100, 100, 0)
target_2 = Target(100, 100, 300, 1000)
target_3 = Target(100, 100, 200, 2000)

target_sprites.add([target_1 , target_2, target_3])

Or pass the sprites to the constructor of the grpup

target_1 = Target(100, 100, 100, 0)
target_2 = Target(100, 100, 300, 1000)
target_3 = Target(100, 100, 200, 2000)
target_sprites = pygame.sprite.Group([target_1 , target_2, target_3])
Rabbid76
  • 202,892
  • 27
  • 131
  • 174