Skip to content

Instantly share code, notes, and snippets.

@bmoren
Last active August 29, 2015 14:10
Show Gist options
  • Save bmoren/6ee47ebf855e5903132f to your computer and use it in GitHub Desktop.
Save bmoren/6ee47ebf855e5903132f to your computer and use it in GitHub Desktop.
a simple 2 player lightbike game in processing using WASD and ARROW Keys
///a simple 2 player lightbike game in processing using WASD and Arrow Keys.
//game params
int speed = 4;
int playerSize = speed + 1 ;
color p1color = color(255, 255, 0); //must have green for detection
color p2color = color(0, 255, 255); //must have green for detection
//player alive?
int player1status = 1;
int player2status = 1;
float p1detection;
float p2detection;
//player positions
int p1x;
int p1y;
int p2x;
int p2y;
String p1loc = "right";
String p2loc = "left";
void setup(){
size(1000,1000);
background(0);
//starting positions
p1x = playerSize *2;
p1y = playerSize *2;
p2x = width - playerSize *2;
p2y = height - playerSize *2;
rectMode(CORNER);
strokeWeight(10);
stroke(0,255,0);
fill(0,0,0);
rect(0,0,width,height);
} //close setup
void draw(){
//background(0);
rectMode(CENTER);
noStroke();
if( player1status == 0){ //player 1 lost
rectMode(CORNER);
strokeWeight(10);
stroke(p2color);
fill(0,0,0);
rect(0,0,width,height);
p1loc = "stopped";
p2loc = "stopped";
}
//p1
if (p1loc == "right"){
p1x = p1x + speed;
}else if (p1loc == "left"){
p1x = p1x - speed;
}else if (p1loc == "up"){
p1y = p1y - speed;
} else if (p1loc == "down"){
p1y = p1y + speed;
}
loadPixels();
p1detection = green(pixels[p1x + p1y * width]);
println(p1detection);
if(p1detection == 0){
println("allgood");
} else{
println("1 hit something");
player1status = 0;
}
fill(p1color); //player 1 color
rect(p1x,p1y, playerSize, playerSize);
//rect(width/2,height/2, 100,100); //testing
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
if(player2status == 0){ //player 2 lost
rectMode(CORNER);
strokeWeight(10);
stroke(p1color);
fill(0,0,0);
rect(0,0,width,height);
p1loc = "stopped";
p2loc = "stopped";
}
//p2
if (p2loc == "right"){
p2x = p2x + speed;
}else if (p2loc == "left"){
p2x = p2x - speed;
}else if (p2loc == "up"){
p2y = p2y - speed;
} else if (p2loc == "down"){
p2y = p2y + speed;
}
loadPixels();
p2detection = green(pixels[p2x + p2y * width]);
println(p2detection);
if(p2detection == 0){
println("allgood");
} else{
println("2 hit something");
player2status = 0;
}
fill(p2color); //player 2 color
rect(p2x,p2y,playerSize,playerSize);
//rect(width/2,height/2, 100,100); //testing
} //close draw
void keyPressed(){
if (key == CODED) {
if (keyCode == RIGHT) {
p2loc = "right";
} else if (keyCode == LEFT) {
p2loc = "left";
} else if (keyCode == DOWN) {
p2loc = "down";
} else if (keyCode == UP) {
p2loc = "up";
}
} //close coded
if(key == 'd'){ //right
p1loc = "right";
} else if(key == 'a'){ //left
p1loc = "left";
} else if (key == 's'){ //down
p1loc = "down";
} else if( key == 'w'){ //up
p1loc = "up";
}
} // close keypressed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment