xxxxxxxxxx
173
let mybird;
let mypipes = [];
let j = 0;
let spritesheet;
let sprites = [];
function preload() {
spritesheet = loadImage("pacman_sprites.png");
}
function setup() {
createCanvas(480, 640);
background(200);
mybird = new bird();
mypipes.push(new pipes());
//mypipes= new pipes();
}
function draw() {
print(mypipes.length);
background(200);
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) {
mypipes.push(new pipes());
//mypipes = new pipes();
print(mypipes.length);
}
}
class pipes {
constructor(){
this.gap = 140;
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) {
fill(255, 0, 0);
}
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 = 40;
this.y = height / 2;
this.speed = 0;
this.gravity = 0.5;
this.upvalue = -12;
let w = int(spritesheet.width / 18.5); //0.5 for making the //image appear in full
let h = int(spritesheet.height / 18.5);
for (let y = 0; y < 19; y++) {
sprites[y] = [];
for (let x = 0; x < 19; x++) {
sprites[y][x] = spritesheet.get(x * w, y * h, w, h);
} // iterate over rows
} // iterate over columns
imageMode(CENTER);
}
downwardmotion() {
image(sprites[1][16], 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 (key == " ") {
mybird.up();
}
}