xxxxxxxxxx
322
var startgame;
let mybird;
let mypipes = [];
let j = 0;
let bgimage;
var x1;
var x2;
let scrollspeed = 2;
let myfont;
var themesong;
var slider;
let collide;
var reset = 0;
var button;
var score = 0;
let spritesheet;
let sprites = [];
//var gif_loadImg, gif_createImg;
function preload() {
spritesheet = loadImage("pidgeot3.png");
//gif_loadImg = loadImage("pidgeot.gif");
gif_createImg = createImg("pidgeot.gif");
bgimage = loadImage("pokemonbgimage-min.png");
myfont = loadFont("PocketMonk-15ze.ttf");
themesong = loadSound("pokemonbattle.mp3");
}
function setup() {
textFont(myfont);
createCanvas(480, 600);
resetSketch();
var button = createButton("reset");
button.mousePressed(resetSketch);
themesong.setVolume(0.2);
themesong.play();
}
function resetSketch() {
clear();
imageMode(CORNER);
background(173, 216, 230);
image(bgimage, 0, 0, width, height);
mypipes.splice(0, mypipes.length);
startgame = 0;
score = 0;
mybird = new bird();
mypipes.push(new pipes());
x2 = width;
x1 = 0;
loop();
}
function draw() {
if (!themesong.isPlaying()) {
themesong.play();
}
if (startgame == 0) {
noStroke();
push();
fill(230);
rect(width / 4 - 15, 225, 300, 100, 20);
fill(255);
rect(width / 4 - 25, 235, 300, 100, 20);
fill(199, 160, 80);
textSize(60);
text("FLAPPY BIRD", width / 4 - 2, height / 2 + 2);
//fill(52, 102, 175);
stroke(29, 44, 94);
strokeWeight(5);
fill(255, 203, 5);
text("FLAPPY BIRD", width / 4 - 12, height / 2 + 8);
//strokeWeight(2);
//rect(width/2-75, 435, 150, 50, 20);
fill(29, 44, 94);
noStroke();
textSize(20);
textAlign(CENTER);
text("Press Enter to Play", width / 2, 472);
pop();
}
if (startgame == 1) {
clear();
print(mypipes.length);
background(173, 216, 230);
push();
imageMode(CORNER);
image(bgimage, x1, 0, width, height);
image(bgimage, x2, 0, width, height);
x1 -= scrollspeed;
x2 -= scrollspeed;
if (x1 < -width) {
x1 = width;
}
if (x2 < -width) {
x2 = width;
}
pop();
mybird.draw();
mybird.downwardmotion();
mybird.collisiondetector();
for (i = mypipes.length - 1; i >= 0; i--) {
mypipes[i].drawpipe();
mypipes[i].hits();
mypipes[i].movepipesleft();
mypipes[i].offscreen();
if (mypipes[i].offscreen()) {
mypipes.splice(i, 1);
}
}
if (frameCount % 50 == 0) {
// if (mypipes[].xPos - mypipes[].xPos >= 50){
mypipes.push(new pipes());
//mypipes = new pipes();
print(mypipes.length);
// }
}
if (frameCount % 50 == 0 && startgame == 1) {
score++;
}
}
}
class pipes {
constructor() {
this.gap = 165;
this.top = random(height / 6, (4 / 5) * height);
this.bottom = height - (this.top + this.gap);
this.xPos = width;
this.w = 80;
this.speed = 6;
this.collide = 0;
}
hits() {
if (mybird.y < this.top || mybird.y > height - this.bottom) {
if (mybird.x > this.xPos && mybird.x < this.xPos + this.w) {
this.collide = 1;
}
} else this.collide = 0;
}
drawpipe() {
fill(255);
if (this.collide) {
imageMode(CORNER);
image(bgimage, 0, 0, width, height);
noStroke();
push();
fill(230);
rect(width / 4 - 15, 225, 300, 100, 20);
fill(255);
rect(width / 4 - 25, 235, 300, 100, 20);
fill(199, 160, 80);
textSize(60);
text("GAME OVER", width / 4 - 2, height / 2 + 2);
//fill(52, 102, 175);
stroke(29, 44, 94);
strokeWeight(5);
fill(255, 203, 5);
text("GAME OVER", width / 4 - 12, height / 2 + 8);
textAlign(CORNER);
text("score:" + score, width / 2, height / 4);
//strokeWeight(2);
//rect(width/2-75, 435, 150, 50, 20);
fill(29, 44, 94);
noStroke();
textSize(20);
textAlign(CENTER);
text("Use reset Button to Play Again", width / 2, 472);
pop();
fill(255, 0, 0);
noLoop();
}
rect(this.xPos, 0, this.w, this.top);
rect(this.xPos, height - this.bottom, this.w, this.bottom);
//put more stuff here to make the GAME OVER!!!!
}
movepipesleft() {
this.xPos -= this.speed;
}
offscreen() {
if (this.xPos < -this.w) {
return true;
} else {
return false;
}
}
}
class bird {
constructor() {
this.x = 50;
this.y = height / 2;
this.speed = 0;
this.gravity = 0.5;
this.upvalue = -12;
let w = int(spritesheet.width / 6); //0.5 for making the //image appear in full
let h = int(spritesheet.height / 1);
for (let y = 0; y < 1; y++) {
sprites[y] = [];
for (let x = 0; x < 6; x++) {
sprites[y][x] = spritesheet.get(x * w, y * h, w, h);
} // iterate over rows
} // iterate over columns
imageMode(CENTER);
}
downwardmotion() {
//image(gif_loadImg, this.x,this.y);
//gif_loadImg.resize(10, 112);
gif_createImg.position(this.x - 40, this.y - 70);
//image(sprites[0][3], this.x, this.y);
this.speed += this.gravity;
this.y += this.speed;
if (this.speed > 10) {
this.speed = 10;
}
//print(this.speed);
}
up() {
this.speed += this.upvalue;
if (this.speed < -10) {
this.speed = -10;
}
}
collisiondetector() {
if (this.y > height - 9) {
this.y = height - 10;
this.speed = 0;
}
if (this.y < 0) {
this.y = 0;
this.speed = 0;
}
}
draw() {
//circle(this.x, this.y, 20);
}
}
function keyPressed() {
if (keyCode == ENTER) {
startgame = 1;
}
// if (keyCode == ESCAPE){
// reset = 1;
if (key == " " && startgame == 1) {
mybird.up();
}
}
function Wave(waveheight, colors, variance) {
this.yPos = 0.001;
this.display = function () {
fill(colors, 170, 206);
// Drawing a polygon out of the wave points
beginShape();
noStroke();
//stroke(255);
let xPos = 0;
// Iterate over horizontal pixels
for (let x = 0; x <= width; x += 20) {
// Calculate a y value according to noise and map it
let y = map(noise(xPos, this.yPos), 0, 0.8, 200, 380);
// Set the vertex
vertex(x, y + waveheight);
// Increment x dimension for noise
xPos += 0.04;
}
// Increment y dimension for noise
this.yPos += 0.005;
vertex(width, height);
vertex(0, height);
endShape(CLOSE);
};
}