Created
August 26, 2022 05:46
-
-
Save Nekodigi/4c565d500bc7ffe5e961f3459094faed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float obstacleD = 250; | |
float obstacleW = 50; | |
float obstacleSpeed = 2; | |
float framePpipe = 200; | |
float forbiddenArea = 100; | |
float pulseVelocity = -10; | |
float g = 0.5; | |
float birdSize = 50; | |
float birdX; | |
Bird bird; | |
ArrayList<Obstacle> obstacles = new ArrayList<Obstacle>(); | |
int score; | |
boolean gameEnd = false; | |
void setup(){ | |
size(500, 500); | |
birdX = width/3; | |
bird = new Bird(); | |
obstacles.add(new Obstacle(random(forbiddenArea, height-forbiddenArea))); | |
} | |
void draw(){ | |
if(frameCount % 200 == 0){ | |
obstacles.add(new Obstacle(random(forbiddenArea, height-forbiddenArea))); | |
} | |
score = ceil((frameCount-(width-birdX)/obstacleSpeed)/framePpipe); | |
//println(score); | |
background(#71C4CF); | |
stroke(#5e3f36); | |
strokeWeight(4); | |
bird.update(); | |
bird.show(); | |
for(Obstacle obstacle : obstacles){ | |
obstacle.update(); | |
obstacle.show(); | |
} | |
for(int i=obstacles.size()-1; i>=0; i--){ | |
Obstacle obstacle = obstacles.get(i); | |
if(obstacle.x < -obstacleW)obstacles.remove(i); | |
} | |
textAlign(LEFT, TOP); | |
textSize(40); | |
fill(255); | |
text("Score:"+score, 0, 0); | |
if(gameEnd)stop(); | |
} | |
void keyPressed(){ | |
bird.vy = pulseVelocity; | |
} | |
void mousePressed(){ | |
bird.vy = pulseVelocity; | |
} | |
void gameOver(){ | |
textAlign(CENTER, CENTER); | |
textSize(80); | |
fill(255); | |
text("Your Score:"+score, width/2, height/2); | |
gameEnd = true; | |
} | |
class Bird{//Collision is box | |
float y; | |
float x; | |
float vy = pulseVelocity; | |
Bird(){ | |
y = height/2; | |
x = birdX; | |
} | |
void update(){ | |
vy+=g; | |
y+= vy; | |
if(y > height)gameOver(); | |
for(Obstacle obstacle : obstacles){ | |
float bxl = x-birdSize/2;//bird left | |
float bxm = x+birdSize/2;//bird right | |
float byl = y-birdSize/2;//bird top | |
float bym = y+birdSize/2;//bird bottom | |
float o1xl = obstacle.x; | |
float o1xm = obstacle.x+obstacleW; | |
float o1yl = obstacle.y + obstacle.d/2; | |
float o1ym = obstacle.y + obstacle.d/2+height; | |
boolean collide = bxl<o1xm && o1xl<bxm && byl < o1ym && o1yl < bym; | |
float o2xl = obstacle.x; | |
float o2xm = obstacle.x+obstacleW; | |
float o2yl = obstacle.y - obstacle.d/2-height; | |
float o2ym = obstacle.y - obstacle.d/2; | |
collide = collide || (bxl<o2xm && o2xl<bxm && byl < o2ym && o2yl < bym); | |
if(collide)gameOver(); | |
} | |
} | |
void show(){ | |
fill(#EEF12B); | |
ellipse(x, y, birdSize, birdSize); | |
} | |
} | |
class Obstacle{ | |
float d = obstacleD;//space between up and bottom pipe | |
float y; | |
float x; | |
Obstacle(float y){ | |
this.y = y; | |
x = width;//fix to width | |
} | |
void update(){ | |
x-=obstacleSpeed; | |
} | |
void show(){ | |
fill(#73CB3F); | |
rect(x, y+d/2, obstacleW, height); | |
rect(x, y-d/2, obstacleW, -height); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment