Processing
import processing.serial.*;Serial myPort; // Create object from Serial class
int val;
PImage []player=new PImage[10];
PImage []gamer=new PImage[10];
PImage bg;
int playerNow=0,gamerNow=0;
int life1=100,life2=100;
void setup(){
size(700,393);
bg=loadImage("321.png");
for(int i=0;i<=2;i++)
player[i]=loadImage("player"+i+".png");
for(int j=0;j<=2;j++)
gamer[j]=loadImage("gamer"+j+".png");
imageMode(CENTER);
myPort = new Serial(this, "COM4", 9600);
}
float playerX=150,playerY=300, gamerX=550,gamerY=300;
int state=0;
void draw(){
while (myPort.available ()>0) {
val=myPort.read();
if (val==48) {
playerX-=1;
}
else if (val==49) {
playerX+=1;
}
else if (val==50) {
playerNow=1;
if(dist(playerX,playerY,gamerX,gamerY)<100){
life2-=10;
}
}
else if (val==51){
playerNow=0;
}
else if (val==52) {
playerNow=2;
if(dist(playerX,playerY,gamerX,gamerY)<100){
life2-=10;
}
}
else if (val==53){
playerNow=0;
}
}
println(val);
if(state==0){
fill(255,0,0);
background(bg);
image(player[playerNow],playerX,playerY,200,200);
image(gamer[gamerNow],gamerX,gamerY,200,200);
textSize(30);
text("Player1 : "+life1+"%",20,50);
text("Player2 : "+life2+"%",470,50);
}
if(life1<=0) state=1; if(life2<=0) state=2;
if(state==1){
background(0);
fill(255); textSize(50); text("Player2 Win!!!",175,175);
textSize(30); text("Click Mouse to Restart.",175,225);
}
if(state==2){
background(0);
fill(255); textSize(50); text("Player1 Win!!!",175,175);
textSize(30); text("Click Mouse to Restart.",175,225);
}
}
void keyPressed(){
if(keyPressed && keyCode=='A'){
playerX-=20;
}
if(keyPressed && keyCode=='D'){
playerX+=20;
}
if(keyPressed && keyCode=='F'){
playerNow=1;
if(dist(playerX,playerY,gamerX,gamerY)<100){
life2-=10;
}
}
if(keyPressed && keyCode=='G'){
playerNow=2;
if(dist(playerX,playerY,gamerX,gamerY)<100){
life2-=10;
}
}
if(keyPressed && keyCode==LEFT){
gamerX-=20;
}
if(keyPressed && keyCode==RIGHT){
gamerX+=20;
}
if(keyPressed && keyCode=='.'){
gamerNow=1;
if(dist(playerX,playerY,gamerX,gamerY)<100){
life1-=10;
}
}
if(keyPressed && keyCode=='/'){
gamerNow=2;if(dist(playerX,playerY,gamerX,gamerY)<100){
life1-=10;
}
}
}
void keyReleased(){
playerNow=0;
gamerNow=0;
}
void mouseClicked(){
if(state==1 || state==2){
state=0;
life1=100;
life2=100;
playerX=150; playerY=300; gamerX=550; gamerY=300;
}
}
DigitalInputPullup
void setup(){//start serial connection
Serial.begin(9600);
//configure pin2 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(13, OUTPUT);
}
int attack[2]={0,0};
void loop(){
//read the pushbutton value into a variable
int sensorVal = digitalRead(2);
int sensorVal3 = digitalRead(3);
int sensorVal4 = digitalRead(4);
int sensorVal5 = digitalRead(5);
if (sensorVal == LOW)Serial.print('0');
if (sensorVal3 == LOW)Serial.print('1');
if (attack[0]==0 && sensorVal4 == LOW){Serial.print('2'); attack[0]=1;}
else if (attack[0]==1 && sensorVal4 == HIGH){Serial.print('3');attack[0]=0;}
if (attack[1]==0 && sensorVal5 == LOW){Serial.print('4');attack[1]=1;}
else if (attack[1]==1 && sensorVal5 == HIGH){Serial.print('5');attack[1]=0;}
}
沒有留言:
張貼留言