0
I was trying to create a "shell" to be able to communicate with a python program during its execution, so far I have the following (this code is just an example):
import sys, pygame
from threading import Thread
import math
pygame.init()
# INIT SCREEN
size = width, height = 800, 600
screen = pygame.display.set_mode(size)
pygame.display.set_caption("teste")
black = 0,0,0
white = 255,255,255
rgb = 255,50,120
running = True
clock = pygame.time.Clock()
timer = 0
angulo = 0
x = 0
global x_var
x_var = 2
def process_input():
while True:
c = input("Python Shell > ")
exec(c)
t = Thread(target=process_input)
t.daemon = True
t.start()
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT: running = False
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: running = False
angulo = ( angulo % 360 ) + 0.1
sin = math.sin(angulo) * 50
y = int( sin + height / 2)
x+= x_var
pos = x,y
rgb = ( (rgb[0] % 255 ) +1 ) , ( (rgb[1] % 255 ) +1 ), ( (rgb[2] % 255 ) +1 )
screen.set_at(pos, rgb)
pygame.display.flip()
My goal would be to change a variable at runtime, for example change x_var
of 2
for -2
but I’m not succeeding. I’m a little young with python, but I’ve seen something similar in other situations.
I put the variable x_var
as Global
until I don’t know the range of the command exec
. I think the fact that you’re in charge process_input
another thread may be affecting access, but I always need to have a loop running.