-
Notifications
You must be signed in to change notification settings - Fork 233
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
Change Models, new Game Button and Score #4
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,20 +4,34 @@ var lose; | |
var interval; | ||
var current; // current moving shape | ||
var currentX, currentY; // position of current shape | ||
|
||
var shapes = [ | ||
[ 1, 1, 1, 1 ], | ||
[ 1, 1, 1, 0, | ||
1 ], | ||
[ 1, 1, 1, 0, | ||
0, 0, 1 ], | ||
[ 1, 1, 0, 0, | ||
1, 1 ], | ||
[ 1, 1, 0, 0, | ||
0, 1, 1 ], | ||
[ 0, 1, 1, 0, | ||
1, 1 ], | ||
[ 0, 1, 0, 0, | ||
1, 1, 1 ] | ||
|
||
[0,0,0,0, | ||
0,0,0,0, | ||
1,1,1,1], | ||
|
||
[0,0,0,0, | ||
1,1,1,0, | ||
1,0,0,0], | ||
|
||
[0,0,0,0, | ||
1,1,1,0, | ||
0,0,1,0], | ||
|
||
[0,0,0,0, | ||
0,1,1,0, | ||
0,1,1,0], | ||
|
||
[0,0,0,0, | ||
0,0,1,1, | ||
0,1,1,0], | ||
|
||
[0,0,0,0, | ||
0,1,0,0, | ||
1,1,1,0] | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For centering the objects and understanding |
||
]; | ||
var colors = [ | ||
'cyan', 'orange', 'blue', 'yellow', 'red', 'green', 'purple' | ||
|
@@ -59,19 +73,25 @@ function init() { | |
|
||
// keep the element moving down, creating new shapes and clearing lines | ||
function tick() { | ||
if ( valid( 0, 1 ) ) { | ||
++currentY; | ||
|
||
if(pause) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a space after |
||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please be consistent in your indentation method |
||
// if the element settled | ||
else { | ||
freeze(); | ||
clearLines(); | ||
if (lose) { | ||
newGame(); | ||
return false; | ||
|
||
if (valid(0, 1)) { | ||
++currentY; | ||
} | ||
newShape(); | ||
} | ||
// if the element settled | ||
else { | ||
freeze(); | ||
clearLines(); | ||
if (lose) { | ||
newGame(); | ||
return false; | ||
} | ||
newShape(); | ||
} | ||
|
||
} | ||
|
||
// stop shape at its position and fix it to board | ||
|
@@ -87,19 +107,19 @@ function freeze() { | |
|
||
// returns rotates the rotated shape 'current' perpendicularly anticlockwise | ||
function rotate( current ) { | ||
var newCurrent = []; | ||
for ( var y = 0; y < 4; ++y ) { | ||
newCurrent[ y ] = []; | ||
for ( var x = 0; x < 4; ++x ) { | ||
newCurrent[ y ][ x ] = current[ 3 - x ][ y ]; | ||
var newCurrent = []; | ||
for (var y = 0; y < 4; ++y) { | ||
newCurrent[y] = []; | ||
for (var x = 0; x < 4; ++x) { | ||
newCurrent[y][x] = current[3 - x][y]; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why you are making these changes? If you want to change the existing coding style into something else, please do so in a separate PR and do it across the whole project, not only incidentally. |
||
} | ||
|
||
return newCurrent; | ||
} | ||
|
||
// check if any lines are filled and clear them | ||
function clearLines() { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not begin a function with an empty line |
||
for ( var y = ROWS - 1; y >= 0; --y ) { | ||
var rowFilled = true; | ||
for ( var x = 0; x < COLS; ++x ) { | ||
|
@@ -109,6 +129,9 @@ function clearLines() { | |
} | ||
} | ||
if ( rowFilled ) { | ||
|
||
score = score + 1; | ||
document.getElementById("score").innerHTML = "Score: " + score.toString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use single quotes |
||
document.getElementById( 'clearsound' ).play(); | ||
for ( var yy = y; yy > 0; --yy ) { | ||
for ( var x = 0; x < COLS; ++x ) { | ||
|
@@ -123,26 +146,29 @@ function clearLines() { | |
function keyPress( key ) { | ||
switch ( key ) { | ||
case 'left': | ||
if ( valid( -1 ) ) { | ||
if ( valid( -1 ) && !pause ) { | ||
--currentX; | ||
} | ||
break; | ||
case 'right': | ||
if ( valid( 1 ) ) { | ||
if ( valid( 1 ) && !pause ) { | ||
++currentX; | ||
} | ||
break; | ||
case 'down': | ||
if ( valid( 0, 1 ) ) { | ||
if ( valid( 0, 1 ) && !pause ) { | ||
++currentY; | ||
} | ||
break; | ||
case 'rotate': | ||
var rotated = rotate( current ); | ||
if ( valid( 0, 0, rotated ) ) { | ||
if ( valid( 0, 0, rotated ) && !pause ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code can be made simpler by moving the check for a pause key to the beginning of the function before the |
||
current = rotated; | ||
} | ||
break; | ||
case 'pause': | ||
pause = !pause; | ||
break; | ||
} | ||
} | ||
|
||
|
@@ -175,6 +201,8 @@ function valid( offsetX, offsetY, newCurrent ) { | |
} | ||
|
||
function newGame() { | ||
score = 0; | ||
document.getElementById("score").innerHTML = "Score: " + score.toString(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move to renderer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how to move this to renderer without creating a reference anyway, since renderer doesn´t get information about a new Game or ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you need to create a reference. The |
||
clearInterval(interval); | ||
init(); | ||
newShape(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,19 @@ canvas { | |
margin: auto; | ||
border: 1px solid black; | ||
} | ||
|
||
div{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. space after |
||
font-family: sans-serif; | ||
font-size: 14px; | ||
font-weight: bold; | ||
text-align: center; | ||
} | ||
|
||
Button { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lower-case |
||
display: block; | ||
margin: auto; | ||
} | ||
|
||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use single quotes to be consistent with the rest of the coding style.