2020年12月7日 星期一

喵~搖桿~多人遊戲~

 maker uno+搖桿

  • 組裝:
    • SW  -  pin2
    • GND  -  GND
    • +5V  -  5V
Arduino v.s Processing
  • Arduino
    • void setup() {
    •   // put your setup code here, to run once:
    •   Serial.begin(9600);
    •   pinMode(2,INPUT_PULLUP);
    •   pinMode(8,OUTPUT);
    • }
    • int state=0;
    • void loop() {
    •   // put your main code here, to run repeatedly:
    •   if(digitalRead(2)==LOW && state==0){
    •     state=1;
    •     Serial.write("1");
    •   }
    •   if(digitalRead(2)==HIGH && state==1){
    •     state=0;
    •     Serial.write("0");
    •   }
    •   delay(100);
    • }
  • Processing
    • import processing.serial.*;
    • Serial myPort;
    • void setup() 
    • {
    •   size(200, 200);
    •   myPort = new Serial(this,"COM4",9600);
    • }
    • int btn=0;
    • void draw() {
    •   if(myPort.available()>0){
    •     int now=myPort.read();
    •     if(now=='1') btn=1;
    •     else btn=0;
    •   }
    •   if(btn==1) background(200,100,255);
    •   else background(255);
    • }
連線到老師的電腦~(多人連線)
  • 載入外掛
  • Processing(客戶端)
    • import hypermedia.net.*;//外掛
    • UDP udp;
    • void setup(){
    •   size(500,500);
    •   udp = new UDP(this,6000);
    •   udp.send("hello world","120.125.70.53",6100);
    • }
    • void draw(){}
    • void mousePressed(){
    •   udp.send("mousePressed","120.125.70.53",6100);
    • }

沒有留言:

張貼留言