Python Forum
[PyGame] Variables shared between game states, how to?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Variables shared between game states, how to?
#4
You have a player class already (with position, rotation, hit boxes, etc), add the info there. I'm not sure the LanderState (or any state) should be creating the player, though.

So instead of...
class Landerstate(States):
    
    def __init__(self, screenrect, player.location, player.money):    ##  <--------- and add info here?
        States.__init__(self)
        self.next = 'starmap'
        self.players = pygame.sprite.Group()
        self.all_sprites = pygame.sprite.Group()
        self.planet_surface = Planet_surface()
        self.all_sprites.add(self.planet_surface)
        self.minerals = pygame.sprite.Group()
        self.player = Player(300, 400)
        self.all_sprites.add(self.player)
        self.players.add(self.player)
        self.place_minerals()
...try this...
class Landerstate(States):
    
    def __init__(self, screenrect, player):    ##  <--------- player already exists
        States.__init__(self)
        self.next = 'starmap'
        self.players = pygame.sprite.Group()
        self.all_sprites = pygame.sprite.Group()
        self.planet_surface = Planet_surface()
        self.all_sprites.add(self.planet_surface)
        self.minerals = pygame.sprite.Group()
        self.player = player
### self.player = Player(300, 400)  # no longer needed, player already exists
        self.all_sprites.add(self.player)
        self.players.add(self.player)
        self.place_minerals()
Reply


Messages In This Thread
RE: Variables shared between game states, how to? - by nilamo - Dec-09-2019, 08:18 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Best way to pass data to the few game states with the same superclass? Milosz 5 3,147 Oct-20-2021, 01:47 PM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020