1

I'm making a text-based game and I'd like the player to be able to move around the map, so I am using pygame to let the player use arrow keys to move but the arrow keys don't work.

def dungeon_001():
    os.system("cls")
    print()
    print("|---0----     ------|")
    print("| 웃                |")
    print("|                   |")
    print("|               S   |")
    print("|                   |")
    print("|-------------------|")
    keys = pygame.key.get_pressed()
    while True:
     if keys[pygame.K_RIGHT]:
        dungeon_005()
     if keys[pygame.K_DOWN]:
        dungeon_002()

dungeon_002() and dungeon_005() have already been defined.

Any help would be greatly appreciated!

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
kible
  • 31
  • 5
  • 1
    You need to update `keys` within the loop. – Barmar Feb 03 '21 at 21:41
  • 1
    I don't understand your design. If you call a function to go to the next room, you'll return to the old room when the function returns. That doesn't seem right. – Barmar Feb 03 '21 at 21:44
  • 1
    Are you sure you need the loop? Once you move to another room, you should be following the paths from there, not continue the loop in the original room. – Barmar Feb 03 '21 at 21:45
  • 1
    Maybe you just want a loop that waits until the user presses a key. The loop shouldn't be around the actions. – Barmar Feb 03 '21 at 21:46
  • 1
    This is not how you use pygame. Pygame requires a graphical window and event handling. You can't mix it with a traditional console text game. – Ted Klein Bergman Feb 03 '21 at 21:47

2 Answers2

0

PyGame does not automatically update keys. You must call the update function pygame.key.get_pressed() manually, which writes the current keyboard state to keys. You also have to update the keyboard state in memory by calling pygame.event.pump(). This is what your loop should look like:

while True:
  pygame.event.pump() # update the keyboard state in memory
  keys = pygame.key.get_pressed()   
  if keys[pygame.K_RIGHT]:
    dungeon_005()
  if keys[pygame.K_DOWN]:
    dungeon_002()
  • This is not fully correct. You must call one of the functions in the event module for pygame to internally interact with the os and get all key events. This must happen every few seconds, or else the [os will think the app has crashed](https://stackoverflow.com/a/42719689/6486738). – Ted Klein Bergman Feb 03 '21 at 21:58
  • Ah, yes. Sorry, I'm a bit rusty with PyGame. I'll fix it right away. – Awesomepotato29 Feb 03 '21 at 22:01
  • Also, you need a [graphical window that's in focus to receive the events](https://stackoverflow.com/a/34360142/6486738), which is why using pygame isn't the correct approach for this. – Ted Klein Bergman Feb 03 '21 at 22:03
0

This is not how you use pygame and will not work (unless you do some really hacky stuff). Pygame requires a graphical window that's in focus to receive events and regular event handling every few seconds or else the os will think the app has crashed. You can't mix it with a traditional console text game.

A text-based console game uses print to "draw" things and input to prompt the user for input. You won't be able to query for left and right arrow keys (there might be a module which can, like the keyboard module). Instead, ask the user to input a string, like 'left' or maybe 'a'.

Here's an example of what you can do:

def dungeon_001():
    os.system("cls")
    print()
    print("|---0----     ------|")
    print("| 웃                |")
    print("|                   |")
    print("|               S   |")
    print("|                   |")
    print("|-------------------|")
    
    direction = input('Input direction (left or down): ')
    while direction not in ('left', 'down')
        direction = input('Input direction (left or down): ')

    if direction == 'left':
        dungeon_005()
    elif direction == 'down':
        dungeon_002()
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50