xxxxxxxxxx
/* @pjs preload="bg_hd2.png,flappy-down.png,flappy-middle.png,flappy-up.png,ground.png,sprite-sheet.png,tube-bottom.png,tube-top.png,logo.png"; */
PImage bg;
double scroll;
double oldScroll;
int mousePresses;
int score;
boolean dead;
boolean died;
boolean started;
ArrayList tubes;
Flappy player;
Ground gnd;
// Settings
boolean soundEnabled;
boolean logoMode = 0;
// Debug Mode
boolean debugEnabled;
void setup() {
if (logoMode == false) {
size(1024, 512);
bg = loadImage("bg_hd2.png");
soundEnabled = true;
textSize(20);
started = false;
// Game setup
setupGame();
} else {
size(500, 500);
bg = loadImage("logo.png");
}
}
// Main game loop
void draw() {
background(bg);
if (logoMode == false) {
//if (dead != true) {
// scroll += 1.9;
//}
if (started == false){
scroll = 0;
player.yPos = 256;
player.yVel = 0;
}
else if (dead != true) {
scroll += 1.9;
}
// Tubes
//PREF: Determines Tube Frequency
if (scroll - oldScroll >= 280) {
tubes.add(new Tube(200));
oldScroll = scroll;
}
for (int i = tubes.size() - 1; i >= 0; i--) {
Tube test = (Tube) tubes.get(i);
test.update(scroll);
if (test.isColliding()) {
dead = true;
if (died == false) {
died = true;
playSound("hit.ogg");
}
}
if (test.finished(scroll)) {
tubes.remove(i);
}
}
gnd.update();
// Player
player.update();
resetMatrix();
text(score, 130, 30);
}
}
void mousePressed() {
mousePresses += 1;
}
void keyPressed() {
if (key == 'D' || key == 'D') {
if (debugEnabled == false) {
debugEnabled = true;
} else {
debugEnabled = false;
}
} else if (key == ' ' || key == ' ') {
started = true;
player.flap();
} else if (key == 's' || key == 'S') {
if (soundEnabled == false) {
soundEnabled = true;
} else {
soundEnabled = false;
}
} else if (key == 'r' || key == 'R') {
setupGame();
}
}
// Plays sound from file
void playSound(String file) {
if (soundEnabled == true) {
var audio = new Audio(file);
audio.play();
}
}
void setupGame() {
player = new Flappy();
gnd = new Ground();
scroll = 0;
oldScroll = scroll;
tubes = new ArrayList();
tubes.add(new Tube(0));
tubes.add(new Tube(280));
//tubes.add(new Tube(460));
dead = false;
died = false;
started = false;
score = 0;
}
class Flappy {
PImage skinUp;
PImage skinMiddle;
PImage skinDown;
double _width;
double _height;
double xPos;
double yPos;
double yVel;
double yAcc;
double turn;
int oldMousePresses;
Flappy() {
skinUp = loadImage("flappy-up.png");
skinMiddle = loadImage("flappy-middle.png");
skinDown = loadImage("flappy-down.png");
_width = skinUp.width;
_height = skinUp.height;
xPos = 100;
yPos = 256;
yVel = -9.1;
yAcc = 0.6;
turn = 0;
oldMousePresses = mousePresses;
}
void flap() {
if (dead == false) {
oldMousePresses = mousePresses;
yVel = -9.1;
playSound("wing.ogg");
}
}
void update() {
resetMatrix();
if (yPos < 257) {
yVel += yAcc;
yPos += yVel;
}
//TODO: Adjust here for height lock
if (yPos >= 257){
yVel += yAcc;
yPos = 256;
}
if (yPos >= 400) {
if (died == false) {
playSound("hit.ogg");
died = true;
}
dead = true;
yPos = 400;
}
if (oldMousePresses != mousePresses) {
flap();
}
translate(xPos - (_width / 2), yPos - (_height / 2));
// Debug flappy's bounds
if (debugEnabled == true) {
rect(0 - (_width / 2), 0 - (_height / 2), _width, _height);
}
// Rotation
/*
turn = (yVel - 2) * .15;
if (turn > PI / 2) {
turn = PI / 2;
} else if (turn < -0.2) {
turn = -0.2;
}
rotate(turn);
*/
// Draw correct skin
double skinChooser;
skinChooser = frameCount % 20;
if (skinChooser <= 5) {
image(skinUp, (_width / 2) * -1, (_height / 3) * -1);
} else if (skinChooser <= 10) {
image (skinMiddle, (_width / 2) * -1, (_height / 3) * -1);
} else if (skinChooser <= 15) {
image (skinDown, (_width / 2) * -1, (_height / 3) * -1);
} else {
image (skinMiddle, (_width / 2) * -1, (_height / 3) * -1);
}
}
double getTop() {
return yPos - _height;
}
double getBottom() {
return yPos;
}
double getLeft() {
return xPos - _width;
}
double getRight() {
return xPos;
}
}
class Tube {
PImage top;
PImage bottom;
double x;
double y;
boolean clear;
Tube(int offset) {
top = loadImage("tube-top.png");
bottom = loadImage("tube-bottom.png");
// Need offset here
x = scroll + offset;
//PREF: value to determine tube spacing
y = random(-190, -200);
clear = false;
}
void update() {
resetMatrix();
int xpos = floor(scroll - x);
xpos = 400 - xpos;
int ypos = (int)y;
image(top, xpos, ypos);
// Spacing between top and bottom
image(bottom, xpos, ypos + top.height + 150);
if (clear == false && xpos <= 50) {
clear = true;
score += 1;
playSound("point.ogg");
}
}
boolean isColliding() {
int xPos = floor(scroll - x);
xPos = 400 - xPos;
int yPos = (int)y;
// Needs: to add to the top number
if (player.getTop() < yPos + 250 || player.getBottom() > 400 + yPos) {
if (player.getRight() > xPos && player.getLeft() < xPos + 52) {
dead = true;
if (died == false) {
playSound("hit.ogg");
died = true;
if (player.yVel < 2) {
player.yVel = 2;
} else {
player.yVel = 2;
}
}
return true;
}
}
return false;
}
boolean finished() {
int xpos = floor(scroll - x);
xpos = 400 - xpos;
if (xpos <= -100) {
return true;
} else {
return false;
}
}
}
class Ground {
PImage skin;
double xPos;
double yPos;
int offset;
Ground() {
skin = loadImage("ground.png");
yPos = height - skin.height;
}
void update() {
xPos = floor(scroll);
offset = xPos%32;
resetMatrix();
translate(0, yPos); // Shift down
pushMatrix(); // Save matrix
translate(offset * -1, 0);
image(skin, 1, 1); // Draw left skin
popMatrix(); // Load matrix
translate(skin.width - offset, 0);
image(skin, 0, 0); // Draw right skin
}
}