Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Melhorias funcionais e visuais #50

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 33 additions & 33 deletions bird.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,40 @@
/* exported Bird */

class Bird {
constructor() {
this.y = height / 2;
this.x = 64;

this.gravity = 0.6;
this.lift = -10;
this.velocity = 0;

this.icon = birdSprite;
this.width = 64;
this.height = 64;
}

show() {
// draw the icon CENTERED around the X and Y coords of the bird object
image(this.icon, this.x - this.width / 2, this.y - this.height / 2, this.width, this.height);
}

up() {
this.velocity = this.lift;
}

update() {
this.velocity += this.gravity;
this.y += this.velocity;

if (this.y >= height - this.height / 2) {
this.y = height - this.height / 2;
this.velocity = 0;
constructor() {
this.y = height / 2;
this.x = 64;

this.gravity = 0.6;
this.lift = -10;
this.velocity = 0;

this.icon = birdSprite;
this.width = 64;
this.height = 64;
}

show() {
// draw the icon CENTERED around the X and Y coords of the bird object
image(this.icon, this.x - this.width / 2, this.y - this.height / 2, this.width, this.height);
}

if (this.y <= this.height / 2) {
this.y = this.height / 2;
this.velocity = 0;
up() {
this.velocity = this.lift;
}

update() {
this.velocity += this.gravity;
this.y += this.velocity;

if (this.y >= height - this.height / 2) {
this.y = height - this.height / 2;
this.velocity = 0;
}

if (this.y <= this.height / 2) {
this.y = this.height / 2;
this.velocity = 0;
}
}
}
}
20 changes: 10 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<html>
<head>
<meta charset="UTF-8">
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.0/p5.min.js"></script>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.0/addons/p5.dom.min.js"></script>
<script language="javascript" type="text/javascript" src="sketch.js"></script>
<script language="javascript" type="text/javascript" src="bird.js"></script>
<script language="javascript" type="text/javascript" src="pipe.js"></script>
</head>
<body>
</body>
<head>
<meta charset="UTF-8">
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.0/p5.min.js"></script>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.6.0/addons/p5.dom.min.js"></script>
<script language="javascript" type="text/javascript" src="sketch.js"></script>
<script language="javascript" type="text/javascript" src="bird.js"></script>
<script language="javascript" type="text/javascript" src="pipe.js"></script>
</head>
<body>
</body>
</html>
114 changes: 57 additions & 57 deletions pipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,72 +7,72 @@
/* exported Pipe */

class Pipe {
constructor() {
this.spacing = 125;
this.top = random(height / 6, 3 / 4 * height);
this.bottom = this.top + this.spacing;
constructor() {
this.spacing = 125;
this.top = random(height / 6, 3 / 4 * height);
this.bottom = this.top + this.spacing;

this.x = width;
this.w = 80;
this.speed = 3;
this.x = width;
this.w = 80;
this.speed = 3;

this.passed = false;
this.highlight = false;
}
this.passed = false;
this.highlight = false;
}

hits(bird) {
let halfBirdHeight = bird.height / 2;
let halfBirdwidth = bird.width / 2;
if (bird.y - halfBirdHeight < this.top || bird.y + halfBirdHeight > this.bottom) {
//if this.w is huge, then we need different collision model
if (bird.x + halfBirdwidth > this.x && bird.x - halfBirdwidth < this.x + this.w) {
this.highlight = true;
this.passed = true;
return true;
}
hits(bird) {
let halfBirdHeight = bird.height / 2;
let halfBirdwidth = bird.width / 2;
if (bird.y - halfBirdHeight < this.top || bird.y + halfBirdHeight > this.bottom) {
//if this.w is huge, then we need different collision model
if (bird.x + halfBirdwidth > this.x && bird.x - halfBirdwidth < this.x + this.w) {
this.highlight = true;
this.passed = true;
return true;
}
}
this.highlight = false;
return false;
}
this.highlight = false;
return false;
}

//this function is used to calculate scores and checks if we've went through the pipes
pass(bird) {
if (bird.x > this.x && !this.passed) {
this.passed = true;
return true;
//this function is used to calculate scores and checks if we've went through the pipes
pass(bird) {
if (bird.x > this.x && !this.passed) {
this.passed = true;
return true;
}
return false;
}
return false;
}

drawHalf() {
let howManyNedeed = 0;
let peakRatio = pipePeakSprite.height / pipePeakSprite.width;
let bodyRatio = pipeBodySprite.height / pipeBodySprite.width;
//this way we calculate, how many tubes we can fit without stretching
howManyNedeed = Math.round(height / (this.w * bodyRatio));
//this <= and start from 1 is just my HACK xD But it's working
for (let i = 0; i < howManyNedeed; ++i) {
let offset = this.w * (i * bodyRatio + peakRatio);
image(pipeBodySprite, -this.w / 2, offset, this.w, this.w * bodyRatio);
drawHalf() {
let howManyNedeed = 0;
let peakRatio = pipePeakSprite.height / pipePeakSprite.width;
let bodyRatio = pipeBodySprite.height / pipeBodySprite.width;
//this way we calculate, how many tubes we can fit without stretching
howManyNedeed = Math.round(height / (this.w * bodyRatio));
//this <= and start from 1 is just my HACK xD But it's working
for (let i = 0; i < howManyNedeed; ++i) {
let offset = this.w * (i * bodyRatio + peakRatio);
image(pipeBodySprite, -this.w / 2, offset, this.w, this.w * bodyRatio);
}
image(pipePeakSprite, -this.w / 2, 0, this.w, this.w * peakRatio);
}
image(pipePeakSprite, -this.w / 2, 0, this.w, this.w * peakRatio);
}

show() {
push();
translate(this.x + this.w / 2, this.bottom);
this.drawHalf();
translate(0, -this.spacing);
rotate(PI);
this.drawHalf();
pop();
}
show() {
push();
translate(this.x + this.w / 2, this.bottom);
this.drawHalf();
translate(0, -this.spacing);
rotate(PI);
this.drawHalf();
pop();
}

update() {
this.x -= this.speed;
}
update() {
this.x -= this.speed;
}

offscreen() {
return (this.x < -this.w);
}
offscreen() {
return (this.x < -this.w);
}
}
Loading