写程序呢最重要的技能之一就是面向浏览器编程(不是jupyter滑稽)
1 #服务端 2 import socket 3 def setup(): 4 global s,colorIndex,COLOR 5 size(300,300) 6 rectMode(CENTER) 7 colorIndex = 0 8 COLOR = [color(255,0,0),color(0,255,0),color(0,0,255)] 9 s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) 10 s.bind((‘127.0.0.1‘,9999)) 11 def draw(): 12 global s,colorIndex,COLOR 13 background(204) 14 data,addr=s.recvfrom(100) 15 data = eval(data) 16 fill(COLOR[colorIndex]) 17 if data[‘mouseEvent‘]: 18 colorIndex += 1 19 if colorIndex==len(COLOR): 20 colorIndex = 0 21 if data[‘mousePressed‘]: 22 ellipse(data[‘mouseX‘],data[‘mouseY‘],50,50) 23 else: 24 rect(data[‘mouseX‘],data[‘mouseY‘],50,50) 25
1 #客户端 2 import socket 3 def setup(): 4 global s,mouseEvent 5 size(300,300) 6 mouseEvent = False 7 s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) 8 def draw(): 9 global s,mouseEvent 10 data = { 11 ‘key‘:key,‘mousePressed‘:mousePressed, 12 ‘mouseX‘:mouseX,‘mouseY‘:mouseY, 13 ‘mouseEvent‘:mouseEvent 14 } 15 s.sendto(str(data), (‘127.0.0.1‘, 9999)) 16 mouseEvent = False 17 def mousePressed(): 18 global mouseEvent 19 mouseEvent = True
原文:https://www.cnblogs.com/szh1213/p/12720636.html