Is there a way that while running pygame, I can also run the console too?

前端 未结 2 833
伪装坚强ぢ
伪装坚强ぢ 2021-01-19 17:02

I was wondering if there is a way, in python, that while my graphical piece inside my games.screen.mainloop() is running, if I can do something like get user input through r

2条回答
  •  天涯浪人
    2021-01-19 17:25

    Yes, have a look at the following example:

    import pygame
    import threading
    import queue
    
    pygame.init()
    screen = pygame.display.set_mode((300, 300))
    quit_game = False
    
    commands = queue.Queue()
    
    pos = pygame.Vector2(10, 10)
    
    m = {'w': (0, -10),
         'a': (-10, 0),
         's': (0, 10),
         'd': (10, 0)}
    
    class Input(threading.Thread):
      def run(self):
        while not quit_game:
          command = input()
          commands.put(command)
    
    i = Input()
    i.start()
    
    old_pos = []
    
    while not quit_game:
      try:
        command = commands.get(False)
      except queue.Empty:
        command = None
    
      if command in m:
        old_pos.append((int(pos.x), int(pos.y)))
        pos += m[command]
    
      for e in pygame.event.get():
        if e.type == pygame.QUIT:
          print("press enter to exit")
          quit_game = True
    
      screen.fill((0, 0, 0))
      for p in old_pos:
          pygame.draw.circle(screen, (75, 0, 0), p, 10, 2)
      pygame.draw.circle(screen, (200, 0, 0), (int(pos.x), int(pos.y)), 10, 2)
      pygame.display.flip()
    
    i.join()
    

    It creates a little red circle. You can move it around with entering w, a, s or d into the console.

    enter image description here

提交回复
热议问题