Skip to content

Instantly share code, notes, and snippets.

@c010
Created April 3, 2016 02:40
Show Gist options
  • Save c010/bb2d90ca91e40e52ee3073aca23a493b to your computer and use it in GitHub Desktop.
Save c010/bb2d90ca91e40e52ee3073aca23a493b to your computer and use it in GitHub Desktop.
// Libraries
import processing.serial.*;
import cc.arduino.*;
//Calling Arduino Ports
//Numbers represent pins
Arduino arduino;
int RightPin1 = 2;
int DownPin1 = 3;
int LeftPin1 = 4;
int UpPin1 = 5;
int RightPin2 = 6;
int DownPin2 = 7;
int LeftPin2 = 8;
int UpPin2 = 9;
// Proceesing ints and floats
PImage img;
String time = "010";
int t;
int interval = 20;
String GameOver = "GAME OVER";
int directionX1 = 2, directionY1 = 0, directionX2 = -2, directionY2 = 0;
float x1=0, y1=500, x2=700, y2=500, speed=3.5;
void setup()
{
size(1920, 1080);
background(0);
smooth();
noStroke();
// Calling on arduino and listenting for the pins
arduino = new Arduino(this, "COM6", 9600); // Will have to change for MAC users
arduino.pinMode(RightPin1, Arduino.INPUT);
arduino.pinMode(DownPin1, Arduino.INPUT);
arduino.pinMode(LeftPin1, Arduino.INPUT);
arduino.pinMode(UpPin1, Arduino.INPUT);
arduino.pinMode(RightPin2, Arduino.INPUT);
arduino.pinMode(DownPin2, Arduino.INPUT);
arduino.pinMode(LeftPin2, Arduino.INPUT);
arduino.pinMode(UpPin2, Arduino.INPUT);
}
void draw() {
img = loadImage("GameOver.png");
boolean GamePlay =true;
t = interval-int(millis()/1000);
time = nf(t, 3);
if (t <= 0) {
background(img);
GamePlay=false;
//interval=0;
}
//giving the elipeses momentum
if (GamePlay == true) {
x1 = x1+speed*directionX1;
y1 = y1+speed*directionY1;
x2 = x2+speed*directionX2;
y2 = y2+speed*directionY2;
//Checking boundries
if ((x1>width) || (x1<0))
{
directionX1=-directionX1;
}
if ((y1>height) || (y1<0))
{
directionY1=-directionY1;
}
if ((x2>width) || (x2<0))
{
directionX2=-directionX2;
}
if ((y2>height) || (y2<0))
{
directionY2=-directionY2;
}
// Our player 1 Player 2 ellipses
fill(0, 255, 0);
ellipse(x1, y1, 100, 75);
fill(255, 0, 0);
ellipse(x2, y2, 100, 75);
}
//Controls for player 1 and player 2
if (arduino.digitalRead(4) == Arduino.HIGH)
{
directionX1=-1;
directionY1=0;
} else if (arduino.digitalRead(2) == Arduino.HIGH) {
directionX1=1;
directionY1=0;
} else if (arduino.digitalRead(5) == Arduino.HIGH) {
directionY1=-1;
directionX1=0;
} else if (arduino.digitalRead(3) == Arduino.HIGH) {
directionY1=1;
directionX1=0;
} else if (arduino.digitalRead(8) == Arduino.HIGH) {
directionX1=-1;
directionY1=0;
} else if (arduino.digitalRead(6) == Arduino.HIGH) {
directionX1=1;
directionY1=0;
} else if (arduino.digitalRead(9) == Arduino.HIGH) {
directionY1=-1;
directionX1=0;
} else if (arduino.digitalRead(3) == Arduino.HIGH) {
directionY1=1;
directionX1=0;
}
}
// Libraries
//import processing.serial.*;
//import cc.arduino.*;
//Calling Arduino Ports
//Arduino arduino;
//int RightPin1 = 2;
//int DownPin1 = 3;
//int LeftPin1 = 4;
//int UpPin1 = 5;
//int RightPin2 = 6;
//int DownPin2 = 7;
//int LeftPin2 = 8;
//int UpPin2 = 9;
// Proceesing ints and floats
PImage img;
String time = "010";
int t;
int interval = 20;
String GameOver = "GAME OVER";
int directionX1 = 2, directionY1 = 0, directionX2 = -2, directionY2 = 0;
float x1=0, y1=500, x2=700, y2=500, speed=3.5;
void setup()
{
size(700, 700);
background(0);
smooth();
noStroke();
//arduino = new Arduino(this, "COM3", 9600); // Will have to change for MAC users
//arduino.pinMode(RightPin1, Arduino.INPUT);
//arduino.pinMode(DownPin1, Arduino.INPUT);
//arduino.pinMode(LeftPin1, Arduino.INPUT);
//arduino.pinMode(UpPin1, Arduino.INPUT);
//arduino.pinMode(RightPin2, Arduino.INPUT);
//arduino.pinMode(DownPin2, Arduino.INPUT);
//arduino.pinMode(LeftPin2, Arduino.INPUT);
//arduino.pinMode(UpPin2, Arduino.INPUT);
}
//
void draw() {
img = loadImage("GameOver.png");
boolean GamePlay =true;
t = interval-int(millis()/1000);
time = nf(t, 3);
if (t <= 0) {
background(img);
GamePlay=false;
//interval=0;
}
//text(time, 20, 20);
// fill(255);
//giving the elipeses momentum
if (GamePlay == true) {
x1 = x1+speed*directionX1;
y1 = y1+speed*directionY1;
x2 = x2+speed*directionX2;
y2 = y2+speed*directionY2;
//Checking boundries
if ((x1>width) || (x1<0))
{
directionX1=-directionX1;
}
if ((y1>height) || (y1<0))
{
directionY1=-directionY1;
}
if ((x2>width) || (x2<0))
{
directionX2=-directionX2;
}
if ((y2>height) || (y2<0))
{
directionY2=-directionY2;
}
fill(0, 255, 0);
ellipse(x1, y1, 100, 75);
fill(255, 0, 0);
ellipse(x2, y2, 100, 75);
}
/* if ( GamePlay ==false)
{
background(0);
fill(255);
ellipse(width/2, height/2, 300, 300);
}*/
}
void keyPressed()
{
if (key == CODED)
{
if (keyCode == LEFT)
{
//if (directionX>0) {
directionX1=-1;
directionY1=0;
//}
} else if (keyCode == RIGHT)
{
//if (directionX<0) {
directionX1=1;
directionY1=0;
//}
} else if (keyCode == UP)
{
//if (directionY<0) {
directionY1=-1;
directionX1=0;
//}
} else if (keyCode == DOWN)
{
//if (directionY<0) {
directionY1=1;
directionX1=0;
//}
}
}
if (key == 'a')
{
//if (directionX>0) {
directionX2=-1;
directionY2=0;
//}
} else if (key == 'd')
{
//if (directionX<0) {
directionX2=1;
directionY2=0;
//}
} else if (key == 'w')
{
//if (directionY<0) {
directionY2=-1;
directionX2=0;
//}
} else if (key == 's')
{
//if (directionY<0) {
directionY2=1;
directionX2=0;
//}
}
}
// =========================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment