Processing-Python模式下的UDP传输

写程序呢最重要的技能之一就是面向浏览器编程(不是jupyter滑稽)
Processing-Python模式下的UDP传输_第1张图片

#服务端
import socket
def setup():
    global s,colorIndex,COLOR
    size(300,300)
    rectMode(CENTER)
    colorIndex = 0
    COLOR = [color(255,0,0),color(0,255,0),color(0,0,255)]
    s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)  
    s.bind(('127.0.0.1',9999))  
def draw():
    global s,colorIndex,COLOR
    background(204)
    data,addr=s.recvfrom(100)
    data = eval(data)
    fill(COLOR[colorIndex])
    if data['mouseEvent']:
        colorIndex += 1
        if colorIndex==len(COLOR):
            colorIndex = 0
    if data['mousePressed']:
        ellipse(data['mouseX'],data['mouseY'],50,50)
    else:
        rect(data['mouseX'],data['mouseY'],50,50)
    

#客户端
import socket
def setup():
    global s,mouseEvent
    size(300,300)
    mouseEvent = False
    s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)  
def draw():
    global s,mouseEvent
    data = {
            'key':key,'mousePressed':mousePressed,
            'mouseX':mouseX,'mouseY':mouseY,
            'mouseEvent':mouseEvent
            }
    s.sendto(str(data), ('127.0.0.1', 9999))
    mouseEvent = False
def mousePressed():
    global mouseEvent
    mouseEvent = True

你可能感兴趣的:(Processing-Python模式下的UDP传输)