在 pygame 中使用线程 [英] using threading in pygame

查看:86
本文介绍了在 pygame 中使用线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 raspberry pi 并且在其中一个 gpio 引脚上我正在发送脉冲,因此我有 python 代码来检测该引脚上的中断,它们每秒钟最多有 2 个中断.现在我想将此值(中断总数)传递给 pygame 应用程序.

I have a raspberry pi and on one of the gpio pins I am sending pulse, thus I have python code to detect interrupt on that pin and their are at most 2 interrupts every seconds. Now I want to pass this value(total no. of interrupts) to pygame application.

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

Currently python code for detecting interrupts writes total no. interrupts to file as interrupt is detected and then pygame application reads that number from the file. Thus my question how can I integrate interrupt detecting code in pygame using threads since I want both pygame application and interrupt detecting code to run in parallel. I read somewhere that pygame is not thread safe.

我的中断检测代码:

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 application code:

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 ()

推荐答案

您可以使用例如线程安全的 Queue 类让你的线程与之通信彼此.

You can use e.g. the threadsafe Queue class to let your threads communicate with each other.

快速不脏的例子:

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()

这篇关于在 pygame 中使用线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆