如果只有来自另一个Python脚本的新传入数据时,如何更新Pygame的显示?

时间:2016-06-15 18:03:28

标签: python pygame

我有一个Python脚本,通过蓝牙定期接收数据。具体来说,它通过蓝牙接收图像的坐标。使用这些坐标我想在单独的Pygame脚本中更新图像的显示。我不希望Pygame显示屏关闭再打开。 Pygame脚本应该连续运行,只有在收到新数据时才会更新图像的显示。

这是我的蓝牙脚本:

import bluetooth
server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
port = 1
server_sock.bind(("", port))
server_sock.listen(1)

client_sock, address = server_sock.accept()
print "Accepted connection from ", address

while True:

    data = client_sock.recv(1024)
    print "received [%s]" % data

任何有关如何实现此行为的指南都将不胜感激!

谢谢

1 个答案:

答案 0 :(得分:0)

import pygame
from pygame.locals import *

def main():
    # Initialise screen
    pygame.init()
    screen = pygame.display.set_mode((150, 50))
    pygame.display.set_caption('Basic Pygame program')

    # Fill background
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((250, 250, 250))

    # Display some text
    font = pygame.font.Font(None, 36)
    text = font.render("Hello There", 1, (10, 10, 10))
    textpos = text.get_rect()
    textpos.centerx = background.get_rect().centerx
    background.blit(text, textpos)

    # Blit everything to the screen
    screen.blit(background, (0, 0))
    pygame.display.flip()

    # Event loop
    while 1:
        for event in pygame.event.get():
            if event.type == QUIT:
                return

        screen.blit(background, (0, 0))
        data = client_sock.recv(1024)
        #do something with the data
        pygame.display.flip()


if __name__ == '__main__': main()

我刚从示例中得到了这个。你应该能够在游戏循环中做任何你想做的事。