-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from hitesh1199/patch-7
Create flappy_bird.pde
- Loading branch information
Showing
11 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
PImage mountain; | ||
|
||
Bird b; | ||
|
||
|
||
int score = 0; | ||
PVector gravity = new PVector(0,1); | ||
ArrayList<Pipe> pipes = new ArrayList<Pipe>(); | ||
|
||
void setup(){ | ||
|
||
size(881,629); | ||
b = new Bird(); | ||
pipes.add(new Pipe()); | ||
} | ||
|
||
void draw(){ | ||
mountain = loadImage("mount.jpg"); | ||
background(mountain); | ||
|
||
if(frameCount % 100 == 0){ | ||
pipes.add(new Pipe()); | ||
} | ||
|
||
if(keyPressed){ | ||
|
||
PVector up = new PVector(0 , -5); | ||
b.applyForce(up); | ||
} | ||
|
||
b.update(); | ||
b.show(); | ||
|
||
boolean safe = true; | ||
for(int i = pipes.size()-1 ; i>= 0 ; i--){ | ||
Pipe p = pipes.get(i); | ||
p.update(); | ||
|
||
if(p.hits(b)){ | ||
p.show(true); | ||
safe = false; | ||
} | ||
else { | ||
p.show(false); | ||
} | ||
|
||
if(p.x < -p.w){ | ||
pipes.remove(i); | ||
} | ||
} | ||
|
||
if(safe) | ||
{ | ||
score++; | ||
} | ||
else{ | ||
score -= 50; | ||
} | ||
|
||
fill(255,255,255); | ||
textSize(30); | ||
textAlign(CENTER); | ||
text(score , width-80,50); | ||
score = constrain(score , 0 , score); | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
int wid = 400; | ||
|
||
class Pipe { | ||
float x; | ||
float bottom; | ||
float top; | ||
float w = 30; | ||
|
||
Pipe(){ | ||
x = wid + w; | ||
top = random(100,height/2); | ||
bottom = random(100,height/2); | ||
} | ||
|
||
boolean hits(Bird b){ | ||
if((b.pos.x > x ) && (b.pos.x<(x+w))) | ||
{ | ||
if((b.pos.y < (top+b.r)) || (b.pos.y > (height-bottom-b.r))) | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
|
||
} | ||
|
||
void update(){ | ||
x -= 3; | ||
} | ||
|
||
void show(boolean hit){ | ||
stroke(0); | ||
strokeWeight(2); | ||
if(hit){ | ||
fill(0,0,0); | ||
}else{ | ||
fill(255); | ||
} | ||
rect(x,0,w,top); | ||
rect(x,height-bottom , w , bottom); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
let bg; //background | ||
let s; //snake | ||
let scl = 25; //scale of the objects - snake and food | ||
let food; //food for the snake | ||
let score = 0; | ||
let fd; | ||
function setup() { | ||
bg = loadImage('img.jpg'); //load the background, always remember to add it into the sketch first | ||
fd = loadImage("FOOD.png"); | ||
createCanvas(1200 , 600); //create canvas according to the browser window size | ||
s = new Snake(); //initialize the snake object | ||
frameRate(7); //choose a frame rate for the snake animation | ||
pickLocation(); //pick the location of the food in each frame | ||
|
||
} | ||
|
||
function pickLocation() { | ||
let cols = floor(width / scl); //calculates the floor of width/scale - floor calculates the closest int less than or equal to the passed param | ||
let rows = floor(height / scl); | ||
food = createVector(floor(random(cols)), floor(random(rows))); //create the location for the food | ||
food.mult(scl); //multiplying each element of the vector by the scale. | ||
} | ||
|
||
|
||
function draw() { | ||
background(bg); //add the background | ||
if (s.eat(food)) { //if snake eats the food, food needs to relocate | ||
score = score + 10; | ||
pickLocation(); //call the pickLocation function to relocate the food | ||
} | ||
|
||
s.death(); | ||
s.update(); //update the length of the snake if food is eaten | ||
s.show(); //show the snake | ||
stroke(0,0,0); //add border | ||
strokeWeight(2); //weight of border | ||
fill(255, 0, 100); //red color for food | ||
ellipseMode(CORNER); | ||
image(fd,food.x, food.y); //shape of food | ||
fill(255,255,255); | ||
textSize(40); | ||
textAlign(CENTER); | ||
text(score , width-80,50); | ||
score = constrain(score , 0 , score); | ||
|
||
fill(255,255,255); | ||
textSize(40); | ||
textAlign(CENTER); | ||
text("Score" , width - 160 ,50); | ||
|
||
|
||
fill(255,255,255); | ||
textSize(20); | ||
textAlign(CENTER); | ||
text("Made by Hitesh Ahuja" , 400 ,50); | ||
} | ||
//Movement of the snake according to the key pressed | ||
function keyPressed() { | ||
if (keyCode === UP_ARROW) { | ||
s.dir(0, -1); | ||
} else if (keyCode === DOWN_ARROW) { | ||
s.dir(0, 1); | ||
} else if (keyCode === RIGHT_ARROW) { | ||
s.dir(1, 0); | ||
} else if (keyCode === LEFT_ARROW) { | ||
s.dir(-1, 0); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
|
||
<!-- PLEASE NO CHANGES BELOW THIS LINE (UNTIL I SAY SO) --> | ||
<script language="javascript" type="text/javascript" src="libraries/p5.min.js"></script> | ||
<script language="javascript" type="text/javascript" src="snake.js"></script> | ||
<script language="javascript" type="text/javascript" src="p5js-temp-Snake_Game7597164085984371015.js"></script> | ||
<!-- OK, YOU CAN MAKE CHANGES BELOW THIS LINE AGAIN --> | ||
|
||
<!-- This line removes any default padding and style. | ||
You might only need one of these values set. --> | ||
<style> body { padding: 0; margin: 0; } </style> | ||
</head> | ||
|
||
<body> | ||
</body> | ||
</html> |
68 changes: 68 additions & 0 deletions
68
ANISHA'S TEAM/Snake game/Hitesh Ahuja/p5js-temp-Snake_Game7597164085984371015.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
let bg; //background | ||
let s; //snake | ||
let scl = 25; //scale of the objects - snake and food | ||
let food; //food for the snake | ||
let score = 0; | ||
let fd; | ||
function setup() { | ||
bg = loadImage('img.jpg'); //load the background, always remember to add it into the sketch first | ||
fd = loadImage("FOOD.png"); | ||
createCanvas(1200 , 600); //create canvas according to the browser window size | ||
s = new Snake(); //initialize the snake object | ||
frameRate(7); //choose a frame rate for the snake animation | ||
pickLocation(); //pick the location of the food in each frame | ||
|
||
} | ||
|
||
function pickLocation() { | ||
let cols = floor(width / scl); //calculates the floor of width/scale - floor calculates the closest int less than or equal to the passed param | ||
let rows = floor(height / scl); | ||
food = createVector(floor(random(cols)), floor(random(rows))); //create the location for the food | ||
food.mult(scl); //multiplying each element of the vector by the scale. | ||
} | ||
|
||
|
||
function draw() { | ||
background(bg); //add the background | ||
if (s.eat(food)) { //if snake eats the food, food needs to relocate | ||
score = score + 10; | ||
pickLocation(); //call the pickLocation function to relocate the food | ||
} | ||
|
||
s.death(); | ||
s.update(); //update the length of the snake if food is eaten | ||
s.show(); //show the snake | ||
stroke(0,0,0); //add border | ||
strokeWeight(2); //weight of border | ||
fill(255, 0, 100); //red color for food | ||
ellipseMode(CORNER); | ||
image(fd,food.x, food.y); //shape of food | ||
fill(255,255,255); | ||
textSize(40); | ||
textAlign(CENTER); | ||
text(score , width-80,50); | ||
score = constrain(score , 0 , score); | ||
|
||
fill(255,255,255); | ||
textSize(40); | ||
textAlign(CENTER); | ||
text("Score" , width - 160 ,50); | ||
|
||
|
||
fill(255,255,255); | ||
textSize(20); | ||
textAlign(CENTER); | ||
text("Made by Hitesh Ahuja" , 600 ,500); | ||
} | ||
//Movement of the snake according to the key pressed | ||
function keyPressed() { | ||
if (keyCode === UP_ARROW) { | ||
s.dir(0, -1); | ||
} else if (keyCode === DOWN_ARROW) { | ||
s.dir(0, 1); | ||
} else if (keyCode === RIGHT_ARROW) { | ||
s.dir(1, 0); | ||
} else if (keyCode === LEFT_ARROW) { | ||
s.dir(-1, 0); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mode=p5.js | ||
mode.id=processing.mode.p5js.p5jsMode |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
function Snake() { | ||
this.x = 0; //position along x | ||
this.y = 0; //positon along y | ||
this.xspeed = 1; //speed | ||
this.yspeed = 0; | ||
this.total = 0; //total length initally | ||
this.tail = []; //array of the tail | ||
let skin = loadImage("skin.jpg"); | ||
let fc = loadImage("head.png"); | ||
//function to check if snake ate the food | ||
this.eat = function(pos) { //if eaten at that position | ||
let d = dist(this.x, this.y, pos.x, pos.y); //then find the distance between them | ||
if (d < 0.5) { //if the distance is less than 1 | ||
this.total++; //increase the size of the snake by 1 | ||
return true; //and return true that it has eaten the food | ||
} else { | ||
return false; //else return false | ||
} | ||
}; | ||
|
||
//direction of the snake | ||
this.dir = function(x, y) { | ||
this.xspeed = x; | ||
this.yspeed = y; | ||
}; | ||
|
||
this.death = function() { | ||
for (let i = 0; i < this.tail.length; i++) { | ||
let pos = this.tail[i]; | ||
let d = dist(this.x, this.y, pos.x, pos.y); | ||
if (d < 1) { | ||
console.log('starting over'); | ||
score = 0; | ||
this.total = 0; | ||
this.tail = []; | ||
} | ||
} | ||
}; | ||
|
||
//updating the length and speed of the snake across the axes | ||
this.update = function() { | ||
for (let i = 0; i < this.tail.length - 1; i++) { | ||
this.tail[i] = this.tail[i + 1]; | ||
} | ||
if (this.total >= 1) { | ||
this.tail[this.total - 1] = createVector(this.x, this.y); | ||
} | ||
|
||
this.x = this.x + this.xspeed * scl; | ||
this.y = this.y + this.yspeed * scl; | ||
|
||
this.x = constrain(this.x, 0, width - scl); | ||
this.y = constrain(this.y, 0, height - scl); | ||
}; | ||
|
||
//showing the snake on the screen + filling with color etc | ||
this.show = function() { | ||
stroke(0,0,0); | ||
strokeWeight(2); | ||
fill(255); | ||
for (let i = 0; i < this.tail.length; i++) { | ||
image(skin,this.tail[i].x,this.tail[i].y,); | ||
//rect(, this.tail[i].y, scl, scl); | ||
} | ||
image(fc,this.x, this.y); | ||
//rect(this.x, this.y, scl, scl); | ||
}; | ||
} |