Python Forum
[PyGame] "if keys[pygame.K_y]" being troublesome
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] "if keys[pygame.K_y]" being troublesome
#1
This is code for selecting a star from a map and asking if you want to travel there. Move a selector sprite over the star sprite, hit enter, and it asked the right question with the "destination" name correct. It works up to the bottom While loop. It loops but doesn't accept the key input.

 def get_keys(self):
        self.vx, self.vy = 0, 0
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] or keys[pygame.K_a]:
            self.vx = -selector_speed
        if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
            self.vx = selector_speed
        if keys[pygame.K_UP] or keys[pygame.K_w]:
            self.vy = -selector_speed
        if keys[pygame.K_DOWN] or keys[pygame.K_s]:
            self.vy = selector_speed
        if self.vx != 0 and self.vy != 0:
            self.vx *= 0.7071
            self.vy *= 0.7071
        if keys[pygame.K_RETURN]:
            self.destination()
        
    def destination(self):
        hits = pygame.sprite.groupcollide(stars, selectors, False, False)
        for hit in hits:
            destination = hit.name
            print("Travel to " + destination + "?")

            asking = True
            while asking:
                keys = pygame.key.get_pressed()
                if keys[pygame.K_y]:
                    print("yes")
                    #going = Go_destination(destination)    
                if keys[pygame.K_n]:
                    print("no")
I've gone cross-eyes staring at it.
Reply
#2
Learn to use one game loop. This will save a lot of headaches.

It because pygame.key.get_pressed() need to be refresh.
Either by pygame.event.get(), pygame.event.poll(), or pygame.event.pump().
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Ahh.. Thank you very much.

As far as game loops go, every tutorial I can find is just a single screen arcade game or text adventure.

I've been struggling with how to have different "modes" or screens in one program: map screen, flying screen, in game menus etc. But it has been hard going. I can make a menu or a flying game or an interactive map, but every attempt to put them together gets messy fast. If you could point me to some diagrams or terms to search I would be a great help.
Reply
#4
Here a functional style.
When you ready for object orient programming.
Then you use something like a state machine / scene manager.
99 percent of computer problems exists between chair and keyboard.
Reply
#5
I'm in your debt!

"state" and "scene" are exactly the information I was looking for. I use classes to limited success (the example above are class methods), but I know I don't put operations where I should and forever run into problems with not having the things I need to "see" each other. I rely on sprite groups too much rather than being able to refer to the instances directly.

Onward and upward. Thanks to you.
Reply


Forum Jump:

User Panel Messages

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