Is there a way that during pygame launch, I can also start the console?

I was wondering if there is a way in python when my graphic part inside my game .screen.mainloop () is running, if I can do something like getting user input via raw_input () from the console.

+4
source share
2 answers

Yes, 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 = 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 = raw_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(pos) pos = map(sum, zip(pos, m[command])) if pygame.event.get(pygame.QUIT): print "press enter to exit" quit_game = True pygame.event.poll() screen.fill((0, 0, 0)) for p in old_pos: pygame.draw.circle(screen, (50, 0, 0), p, 10, 2) pygame.draw.circle(screen, (200, 0, 0), pos, 10, 2) pygame.display.flip() i.join() 

Creates a small red circle. You can move it using the input w , a , s or d in the console.

enter image description here

+4
source

The thing is, if you do something like raw_input , it will stop the program until that input is entered, so that the program will stop the program for each cycle to enter the input, but you can do things like print but they will print each cycle

if you want to use the input InputBox Module , this will lead to a small input window on the screen, which is in a loop

if you want to do this from the console, you can try Threading, which I am not familiar with, but you can check it out Multithreaded tutorial

here are questions that may help you

Pygame is written to the terminal

Good luck! :)

0
source

Source: https://habr.com/ru/post/1490935/


All Articles