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