Processing Game
Processing Game
//defining variables
boolean left, right, up, down;
boolean emote;
Player p; //References player class
float rectX, rectY,rectW, rectH;
int score; //keeps track of game score
//images
PImage playerImages []; //defines player animation frame images
int playerFrames; //defines player animation frames
String s;
int backgroundColor;//background color variable
int textColor;//text color variable
void setup(){
size(700,700);
background(random(250),random(250),random(250));
playerFrames = 21; //number of images for all animations
playerImages = new PImage[playerFrames];
for(int i = 0; i<playerFrames; i++){ //setting image specifications
playerImages[i] = loadImage("pixil-frame-"+nf(i,1)+".png"); //"i" is the end digit on each image
rectX = random(0,600); //paper starts at a random x coord within 700,700
rectY = random(0,600); //paper starts at random y coord within 700,700
score = 0; //score starts at 0
}
backgroundColor = color(random(0,250),random(0,250),random(0,250));
void draw(){
background(backgroundColor);
p.update();//update the player sprite
p.display();//display the player sprite
//paper Sprite
//white
fill(#FFFFFF);
noStroke();
rect(rectX+30/2,rectY+80/2,90/2,140/2);
rect(rectX+170/2,rectY+10,80/2,25);
rect(rectX+190/2,rectY+70/2,20,5);
rect(rectX+180/2,rectY+80/2,10,5);
//exclamation point
fill(#FF0000);
noStroke();
rect(rectX+200/2,rectY+15,10,10);
rect(rectX+200/2,rectY+30,10,5);
//shadow
fill(#808080);
noStroke();
rect(rectX+120/2,rectY+110/2,10,130/2);
rect(rectX+25,rectY+220/2,70/2,10);
//outline
fill(0);
noStroke();
rect(rectX + 180/2, rectY + 10/2, 60/2, 10/2);
rect(rectX + 170/2, rectY + 20/2, 10/2, 10/2);
rect(rectX + 240/2, rectY + 20/2, 10/2,10/2);
rect(rectX+160/2, rectY+30/2, 10/2, 30/2);
rect(rectX+170/2, rectY+80/2,5,5);
rect(rectX+240/2, rectY+60/2,5,5);
rect(rectX+40/2,rectY+70/2,20,5);
rect(rectX+80/2, rectY+80/2,20,5);
rect(rectX+170/2, rectY+60/2,5,5);
rect(rectX+230/2, rectY+70/2, 5,5);
rect(rectX+30/2, rectY+40,5,30/2);
rect(rectX+120/2, rectY+90/2,5,25);
rect(rectX+130/2, rectY+100/2,10,5);
rect(rectX+140/2, rectY+50,5,70/2);
rect(rectX+180/2, rectY+70/2, 5,5);
rect(rectX+160/2, rectY+90/2, 20, 5);
rect(rectX+20/2,rectY+110/2,5,50);
rect(rectX+110/2, rectY+140/2,5,90/2);
rect(rectX+70/2, rectY+220/2,25,5);
rect(rectX+15,rectY+210/2,20,5);
rect(rectX+20,rectY+220/2,5,10);
rect(rectX+25,rectY+230/2,5,5);
rect(rectX+30,rectY+240/2,40,5);
rect(rectX+130/2,rectY+170/2,5,40);
rect(rectX+25,rectY+50,10,5);
rect(rectX+40,rectY+110/2,5,5);
rect(rectX+50,rectY+110/2,5,5);
rect(rectX+20,rectY+130/2,15,5);
rect(rectX+70/2,rectY+150/2,15,5);
rect(rectX+70/2,rectY+180/2,10,5);
rect(rectX+250/2,rectY+30/2,5,15);
rect(rectX+200/2,rectY+80/2,30/2,5);
rect(rectX+230/2,rectY+70/2,5,5);
rect(rectX+40/2,rectY+170/2,15,5);
if(p.checkCollision(rectX, rectY)){
}
//score text
fill(textColor);
textSize(20);
text("Papers collected: " + score, 10, 40);
//Control text
fill(textColor);
textSize(20);
text("WASD to move", 10, 60);
fill(textColor);
textSize(20);
text("SPACEBAR to change colors", 10, 80);
}
}
}
//insert on new class
class Player {
//properties
float x, y, w, h;
float speedX, speedY, maxSpeed;
int currentFrame, loopFrames, offset, delay;
//constructor
Player(){
x = width/2;
y = height/2;
w = 120;
h = 150;
maxSpeed = 5;
speedX = 0;
speedY = 0;
currentFrame = 0;
loopFrames = 5;
offset = 5; //the frame the animation starts at when going in this direction (starts facing
forward).
delay = 0; //Teh specified stationary frame for when the sprite is not moving.
}
//methods
void update(){
//horizontal movement
if (left){
speedY = 0;
speedX = -maxSpeed;
offset = 10;
}
if (right){
speedY = 0;
speedX = maxSpeed;
offset = 15;
}
if ((!left && !right) || (left && right)){
speedX = 0;
}
//vertical movement
if (up){
speedX = 0;
speedY = -maxSpeed;
offset = 5;
}
if (down){
speedX = 0;
speedY = maxSpeed;
offset = 0;
}
if ((!up && !down) || (up && down)){
speedY = 0;
}
if(!up&&!down&&!left&&!right){
currentFrame = 2;
// }
//if(emote){
// speedX = 0;
// speedY = 0;
// currentFrame = 20;
} else {
if (delay == 0){
currentFrame = (currentFrame+1)%loopFrames;
}
delay = (delay+1)%10;
}//end no keys
checkBounds();
//update position
x += speedX;
y += speedY;
}//end update
//sprite drawing
void display(){
noFill();
noStroke();
rect(x, y, 120, 150); //rectangle for collision specifications
image(playerImages[currentFrame+offset],x,y);
}
//makes player stay on screen
void checkBounds(){
if(x > width){x = -w;}
if(x < -w){x = width;}
if(y > height){y = -h;}
if(y < -h){y = height;}
}
boolean checkCollision(float rectX, float rectY){
if(x < rectX + 50 && x +50 > rectX && y < rectY + 50 && y +50 > rectY &&
y + h > rectY){
return true; //collision detected
} else{
return false; //no collision detected
}
}
}