Skip to content

Instantly share code, notes, and snippets.

@HarryZ10
Created August 11, 2022 17:30
Show Gist options
  • Save HarryZ10/826674ff69f295bfc4137e86f29c56de to your computer and use it in GitHub Desktop.
Save HarryZ10/826674ff69f295bfc4137e86f29c56de to your computer and use it in GitHub Desktop.
Flappy Corgi (Flappy Bird Parody)
// EA Spawnpoint 2019
//"Corgi" demo
// Mechanic:
// Click anywhere on the screen for the dog to move to find its way into the doghouse
//dog
var speedX = 0;
var speedY = 0;
var positionX;
var positionY;
var gravityVector;
//doghouse
var posX, posY;
var sizeX, sizeY;
//canvas size
var canvasH;
var canvasW;
//game manager
var cpsCounter = new ClicksPerSecCounter();
var highScore = 0;
var score = 0;
var cooldown = 10;
var gameOver = false;
//pointer
var numClicks;
var secondsSpent;
var cps;
//ClicksPerSecCounter constructor
function ClicksPerSecCounter () {
//instance variables
numClicks = 0;
secondsSpent = 0;
//instance functions
//onClick event on the page provided
document.onclick = function () {
numClicks++;
};
//for every ms, add one to secondsSpent
this.secondsIntervalID = setInterval(function () {
secondsSpent++;
}, 1000);
//divide num of clicks by the secondsSpent
this.clicksPerSecond = function () {
if(numClicks != 0){
return numClicks / secondsSpent;
}
return 0;
}
}
function preload() {
soundFormats('mp3');
dog_bark = loadSound('dogbark.mp3');
}
function setup() {
canvasH = 500;
canvasW = 500;
gravityVector = createVector(0, 0);
rectMode(CENTER);
imageMode(CENTER);
createCanvas(canvasW, canvasH);
background(120, 190, 180);
//images of objects
walking_corgi = loadImage("corgi-walk_pintado.gif");
dog_house = loadImage("doghouse.png");
//dog's position
positionX = 0;
positionY = canvasH / 2;
//doghouse's size
sizeX = 228;
sizeY = 166;
//doghouse's position and volume
posX = sizeX / 2;
posY = 150;
dog_bark.setVolume(0.5);
}
function draw() {
//checks if game is not over
if (!gameOver) {
//UI/UX
background(120, 190, 180);
cps = parseInt(cpsCounter.clicksPerSecond());
drawUI();
//doghouse
dog_house.resize(sizeX, sizeY);
image(dog_house, 480, posY);
//corgi itself
drawAnimal();
//movement of player
positionX += speedX;
positionY += speedY;
//player shakes up and down
positionY += random(-1.5, 1.5);
//checks if player is at the top of the screen
//if so, stops the player by removing speed and setting its position
//back to default
if (positionY <= 100) {
positionY = 100;
speedY = 0;
}
//gravity force due to acceleration
speedY += gravityVector.y;
//Plays barking sounds at random # of frames
if (cooldown <= 0) {
dog_bark.play();
cooldown = random(100, 200);
}
cooldown--;
//checks if player is colliding with doghouse
if (positionX > canvasW - 100) {
if ((positionY - posY < 30) && (positionY - posY > -100)) {
posY = random(110, 400);
score++;
speedX += 0.2;
positionX = 0;
positionY = canvasH/2;
}
}
//checks if player is out of bounds bottom of the screen or right of the screen,
//if so, player loses
if (positionY >= 500 || positionX >= 500) {
gameOver = true;
dog_bark.stop();
background(120, 190, 180);
fill(220);
strokeWeight(10);
stroke(200, 100, 100);
rect(250, 250, 300, 200, 15);
drawUI();
fill(200, 100, 100);
strokeWeight(1);
textSize(30);
text("Try Again!", 130, 320);
text("Press R to Restart", 130, 200);
}
//checks if player's score is 10, if so, player wins
if (score === 10) {
gameOver = true;
dog_bark.stop();
background(120, 190, 180);
fill(220);
strokeWeight(10);
stroke(200, 100, 100);
rect(250, 250, 300, 200, 15);
drawUI();
fill(200, 100, 100);
strokeWeight(1)
textSize(30);
text("You won!", 130, 320);
text("Press R to Restart", 130, 200);
}
//clicking indicator
if(mouseIsPressed && !gameOver){
fill(220);
strokeWeight(2);
circle(mouseX, mouseY, 60);
}
}
isGameOver();
}
//returns high score and checks if current score is higher than the current high score
//if so, set high score to score
function isScore(){
if(score > highScore){
highScore = score;
return highScore;
}
return highScore;
}
function isGameOver() {
//key needs to be pressed AND gameOver has to be true
if (keyIsPressed && gameOver) {
//if player hits the R key on the keyboard...
if (key == "r") {
//resets the dog house's position
positionY = canvasH/2;
positionX = 50;
//resets the gravity vector so player has a chance to rest before playing again
gravityVector.set(0, 0.0);
//resets the player's position, speed, score, number of clicks, secondsSpent
posY = random(110, 400);
posX = sizeX / 2;
speedX = 0;
speedY = 0;
score = 0;
cps = 0;
numClicks = 0
secondsSpent = 0;
//returns to gameplay
gameOver = false;
}
}
}
function mouseReleased() {
//gravity is turned on
gravityVector.set(0, 0.2);
//speed is turned on
speedX = 3;
speedY -= 20 * gravityVector.y;
speedY = constrain(speedY, -500, 500);
//actual movement
positionY -= speedY;
}
function drawUI(){
//gray body, 5px red border
fill(220);
stroke(200, 100, 100);
strokeWeight(5);
//boxes for UI text
rect(250, 27, 500, 50);
rect(250, 475, 500, 45);
//red text, 1px red border, 36 font size
fill(200, 100, 100);
strokeWeight(1);
textSize(36);
//UI text
text("Score: " + score, 10, 40);
text("CPS: " + cps, 350, 40);
text("High Score: " + isScore(), 10, 485);
}
function drawAnimal() {
let topWall = 100;
let bottomWall = canvasH;
let xxm = positionX;
let yym = positionY;
let xxc = constrain(positionX, topWall, bottomWall);
let yyc = constrain(positionY, topWall, bottomWall);
walking_corgi.resize(180, 117);
image(walking_corgi, xxc, yyc);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment