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

Ajustes e correção de bugs #1

Open
wants to merge 2 commits into
base: f7fde5c74e
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions breakout/breakout.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ <h1>CanvasBreak</h1>
<canvas id="canvas"></canvas>
</div>


<script src="breakout.js"></script>
</body>
</html>
56 changes: 34 additions & 22 deletions breakout/breakout.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
var brick_height = 20;
var brick_lines = 5;
var brick_columns = screen_width / brick_width;
var bricks = brick_lines * Math.floor(brick_columns);

//keys
var KEY_LEFT = 37;
Expand All @@ -41,9 +42,9 @@
var bricks_board = new Array();


for(var i=0; i<brick_lines; i++){
for(var i=0; i<brick_columns; i++){
bricks_board[i] = new Array();
for(var j=0; j<brick_columns; j++){
for(var j=0; j<brick_lines; j++){
bricks_board[i][j] = 1;
}
}
Expand All @@ -56,12 +57,22 @@
}

var bricks_collided = function(ball_x, ball_y){
var xspace = ball_x/brick_width;
var yspace = ball_y/brick_height;
var xspace = Math.floor((ball_x + ball_radius)/brick_width);
var yspace = Math.floor((ball_y - ball_radius - ball_speed)/brick_height);

return [xspace,yspace];
return [xspace, yspace, (xspace>=0 && xspace<=9 && yspace>=0 && yspace<=4)];
}

var bricks_draw = function(column, line){
ctx.fillStyle = "#5FF";
ctx.strokeStyle = "black";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.rect(column*brick_width, line*brick_height, brick_width, brick_height);
ctx.fill();
ctx.stroke();

}

var update = function(){
//clear the screen
Expand All @@ -80,19 +91,23 @@
ctx.fill();

if(ball_moving){
//alert(ball_y);
bricks_point = bricks_collided(ball_x, ball_y);
if (bricks_point[0]>0 && bricks_point[0]<=10 && bricks_point[1]>0 && bricks_point[1]<=5 && (bricks_board[parseInt(bricks_point[1],10)-1][parseInt(bricks_point[0],10)]==1)){
bricks_board[parseInt(bricks_point[1],10)-1][parseInt(bricks_point[0],10)] = 0;

ball_ydirection *= -1;
}
else {
if(ball_x - ball_speed <= 0 || ball_x >= screen_width - ball_radius)
ball_xdirection *= -1;
if(ball_y - ball_speed <= ball_radius / 2 || bar_collided())
if (bricks_point[2]==true) {
if (bricks_board[bricks_point[0]][bricks_point[1]]==1) {
ball_ydirection *= -1;
}
bricks_board[bricks_point[0]][bricks_point[1]]=0;
bricks--;
if (bricks==0){
alert('Parabés, voce ganhou!');
ball_moving = false;
}
}
}
if(ball_x - ball_speed <= 0 || ball_x >= screen_width - ball_radius)
ball_xdirection *= -1;
if(ball_y - ball_speed <= ball_radius / 2 || bar_collided())
ball_ydirection *= -1;

ball_x += ball_speed * ball_xdirection;
ball_y += ball_speed * ball_ydirection;

Expand All @@ -107,13 +122,10 @@
ctx.fillStyle = "#5FF";
ctx.strokeStyle = "black";
ctx.lineWidth = 2;
for(var i=0; i<brick_lines; i++){
for(var j=0; j<brick_columns; j++){
for(var i=0; i<brick_columns; i++){
for(var j=0; j<brick_lines; j++){
if (bricks_board[i][j]==1) {
ctx.beginPath();
ctx.rect(j*brick_width, i*brick_height, brick_width, brick_height);
ctx.fill();
ctx.stroke();
bricks_draw(i,j);
}
}
}
Expand Down