xxxxxxxxxx
//License: Creative Commons Attribution-Share Alike 3.0 and GNU GPL license
//From KTByte.com http://tinyurl.com/reddit-crappybird
/*For the high score, it is fairly similar to score. Only difference, do not reset the
high score when the user is redirected to the start screen. Check out the walls class to
see how to change the high score.
*/
character bird;
background back;
walls wall0;
walls wall1;
PImage start_screen;
char game_state = 's'; //game state --> s is for start screen, p is for play, etc.
int score = 0;
int highscore = 0;
void setup() {
size(600, 800);
frameRate(30);
fill(0);
textSize(40);
textAlign(CENTER);
//initialize sprites
bird = new character("http://i.imgur.com/mw0ai3K.png", height/2);
back = new background("http://i.imgur.com/cXaR0vS.png", 0);
wall0 = new walls("http://i.imgur.com/4SUsUuc.png", 600, height/2);
wall1 = new walls("http://i.imgur.com/4SUsUuc.png", 900, 600);
//initalize start screen
start_screen = loadImage("start.png");
}
void draw(){
switch(game_state){
case 's':
imageMode(CORNER);
image(start_screen, 0, 0);
textSize(20);
text("Highscore: " + highscore, 70, 20); //display high score in top right corner
break;
case 'p':
back.update();
wall0.update();
wall1.update();
bird.update();
textSize(40);
text(score, width/2, 700);
textSize(20);
text("Highscore: " + highscore, 70, 20); //display high score in top right corner
break;
}
}
void mouseClicked(){
if(game_state == 's'){
//reset all sprites
bird.y = height/2;
bird.vy = 1;
back.x = 0;
wall0.x = 600;
wall0.y = height/2;
wall1.x = 900;
wall1.y = 600;
//reset score
score = 0;
//change game state back to play mode
game_state = 'p';
}
else if(game_stes = 'p'){
//if mouse is clicked, move bird up
bird.vy -= 10;
}
}
class background{
PImage backImg;
float x;
float y;
background(String img, float xpos){
backImg = loadImage(img);
x = xpos;
y= 0;
}
void update(){
//change x position
x-= 6;
//reset background when it reaches the edge
if(x <= -1800){
x = 0;
}
//display background
imageMode(CORNER);
image(backImg, x, y);
image(backImg, x+backImg.width, y);
}
}
class character{
PImage user;
float x; //x position is always middle of screen
float y; //y position
float vy; //velocity
character(String img, float ypos){
user = loadImage(img);
y = ypos;
x = width/2;
vy = 1;
}
void update(){
//increase velocity to rapidly make bird fall
vy += 1;
y += vy;
//display bird
image(user, x, y);
}
}
class walls{
PImage wallImg;
float x;
float y;
walls(String img, float xpos, float ypos){
wallImg = loadImage(img);
x = xpos;
y = ypos;
}
boolean hitBird(){
if(abs(width/2-x) < 25 && abs(bird.y-y) > 100){
return true;
}
return false;
}
void update(){
//check if the bird hit
if(hitBird() == true){
game_state = 's';
return;
}
//check if score needs to increase
if(x == width/2){
score++;
//check if score is greater than highscore. if so, set high score to score
if(score > highscore){
highscore = score;
}
}
//if walls reach the end of the screen, reset
if(x < 0){
y = (int)random(200, height -200);
x = width;
}
//move walls to the left
x -= 6;
//display walls
imageMode(CENTER);
image(wallImg, x, y - (wallImg.height/2 + 100));
image(wallImg, x, y + (wallImg.height/2 +100));
}
}