Skip to content
Snippets Groups Projects
Commit c6e912c9 authored by ja3-saxby's avatar ja3-saxby
Browse files

Fixed lag and false game overs!

The game logic loop/collision-detection was at fault
parent bdd9dfe9
No related branches found
No related tags found
No related merge requests found
......@@ -276,18 +276,19 @@ bool can_shape_move(
if (shape_image.getPixelValue(x, y) != 0) {
// calculate the translated position, including origin
Point destination = origin + Point(x, y);
// if Y is negative, skip this pixel as a special case
if (destination.y < 0) {
continue; // we allow Blocks to be out of the top frame
}
// if out-of-bounds, of course it can't move
if (
destination.x < 0 or
destination.x > 4 or
destination.y > 4
// NOTE: there is no check for negative Y since we allow it
) {
return false;
}
// if Y is negative, we don't need to check this row
if (destination.y < 0) {
break; // no point checking this row, out of screen
}
// check if the translated position of this pixel is not free
if (stacked.getPixelValue(destination.x, destination.y) != 0) {
return false;
......@@ -388,8 +389,13 @@ void start_new_game() {
Point origin(1, -3);
// this is the down unit-vector
Vector down(0, 1);
// draw the scene
UBIT.display.print(stacked_shapes);
// draw Block with transparency enabled on clear image pixels
UBIT.display.print(block.image(), origin.x, origin.y, 1);
// while Block can go down
while (can_shape_move(block, origin, stacked_shapes, down)) {
if (can_shape_move(block, origin, stacked_shapes, down)) {
while (true) {
// allow player to shift the Block left or right
/*
* read accelerometer manually for more controlled movement
......@@ -416,8 +422,12 @@ void start_new_game() {
// check if enough time has elapsed to move the block
unsigned long stopwatch_now = UBIT.systemTime();
if ((stopwatch_now - stopwatch) >= move_speed) {
// shift the shape down
// shift the shape down if it can
if (can_shape_move(block, origin, stacked_shapes, down)) {
origin = origin + down;
} else {
break;
}
// update the "stopwatch"
stopwatch = stopwatch_now;
}
......@@ -429,6 +439,7 @@ void start_new_game() {
// delay
UBIT.sleep(10); // 10ms sleep = 100ticks/second
}
}
// if Block Y origin is less than 0 upon not being able to go down
if (origin.y < 0) {
// it's Game Over, because the Block could never fully enter screen
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment