在pygame中使用线程

时间:2013-12-11 12:41:26

标签: python multithreading pygame raspberry-pi

我有一个覆盆子pi和一个gpio引脚我发送脉冲,因此我有python代码来检测该引脚上的中断,它们每秒最多2个中断。现在我想将这个值(总中断数)传递给pygame应用程序。

目前用于检测中断的python代码写入总数为。检测到中断时检测到中断,然后pygame应用程序从文件中读取该数字。因此我的问题是如何使用线程在pygame中集成中断检测代码,因为我希望pygame应用程序和中断检测代码并行运行。我在某处读到pygame不是线程安全的。

我的中断检测代码:

GPIO.setmode(GPIO.BCM)
count = 0
GPIO.setup(2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def my_callback(channel):
    file = open('hello.txt','w')
    global count
    count += 1
    file.write(str(count))
GPIO.add_event_detect(2,GPIO.BOTH, callback=my_callback)

while True:
   print "Waiting for input."
   sleep(60)
GPIO.cleanup()

pygame应用程序代码:

pygame.init()
size=[640,640]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Test")
done=False
clock=pygame.time.Clock()
font = pygame.font.SysFont("consolas", 25, True)
frame_rate = 20
frame_count = 0
count = 0
while done == False:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done=True # Flag that we are done so we exit this loop
            pygame.quit()
            sys.exit()

    f = open("hello.txt", "r")
    count = int(f.read())
    output_string = "ACTUAL          %s" %count
    text = font.render(output_string,True,red)
    screen.blit(text, [250,420])
    frame_count += 1
    clock.tick(frame_rate)
    pygame.display.flip()

pygame.quit ()

1 个答案:

答案 0 :(得分:6)

您可以使用例如线程安全Queue类让你的线程相互通信。

quick'n'dirty示例:

import pygame
from pygame.color import Color
from Queue import Queue
from threading import Thread

q = Queue()

def worker():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    def my_callback(channel):
        q.put(True)
    GPIO.add_event_detect(2,GPIO.BOTH, callback=my_callback)

    while True:
        print "Waiting for input."
        sleep(60)
    GPIO.cleanup()


t = Thread(target=worker)
t.daemon = True
t.start()

pygame.init()

screen = pygame.display.set_mode([640,640])
clock  = pygame.time.Clock()
font   = pygame.font.SysFont("consolas", 25, True)
count  = 0
pygame.display.set_caption("Test")

done = False
while not done:
    screen.fill(Color('black'))
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done = True
    try: 
        q.get()
        count += 1
    except: pass
    output_string = "ACTUAL          %s"  % count
    text = font.render(output_string, True, Color('red'))
    screen.blit(text, [250,420])
    clock.tick(20)
    pygame.display.flip()