Skip to content
This repository has been archived by the owner on Sep 17, 2020. It is now read-only.

Commit

Permalink
Introduce wall kick, refactor
Browse files Browse the repository at this point in the history
Refactor controls a little: move key repeat clocks reset to the keys
handler.
Refactor field: rename rotation function (add "clockwise" postfix).
  • Loading branch information
Oxore committed Jul 9, 2018
1 parent a673d86 commit a3eaa5d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ make all
```
File named "tetris" is the final binary. Run it. Hit *S* to start game.

- `s` - start a game.
- `Left arrow` - move shape left.
- `Right arrow` - move shape right.
- `Up arrow` - rotate shape clockwise.
- `Space` - hard drop.

Cleaning:
```
make clean
Expand All @@ -36,7 +42,7 @@ Look at the [repo releases](https://github.com/Oxore/tetris-csfml/releases)! Now
- Add screenshots and gameplay gif
- [x] Levels speed and scores calibration
- [x] Scoring system as in Tetris for NES which is described on [this page](https://tetris.wiki/Scoring)
- [Wall kick](https://tetris.wiki/Wall_kick)
- [x] [Wall kick](https://tetris.wiki/Wall_kick). Implemented order: left wall, right wall, floor
- [x] Hard drop
- [x] Phantom shape
- Table of records
Expand Down
2 changes: 1 addition & 1 deletion include/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void field_init(struct field *fld);
void field_deinit(struct field *fld);
void field_fill_random(struct field *fld);
void field_clear(struct field *fld);
void field_rotate_shape(struct field *fld, unsigned int index);
void field_rotate_shape_clockwise(struct field *fld, unsigned int index);
int field_move_shape_down(struct field *fld, unsigned int index);
int field_move_shape_left(struct field *fld, unsigned int index);
int field_move_shape_right(struct field *fld, unsigned int index);
Expand Down
9 changes: 5 additions & 4 deletions src/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,9 @@ static void game_tick()

static void signal_up()
{
field_rotate_shape(&fld, 1);
field_rotate_shape_clockwise(&fld, 1);
project_ghost_shape(&fld, 1, 0);
sfClock_restart(game.putTick);
}

static void signal_harddrop()
Expand All @@ -239,7 +240,6 @@ static void signal_down()
sfClock_restart(game.gameTick);
game.scoreCurrent++;
}
sfClock_restart(game.repPushDown);
}

static void signal_left()
Expand All @@ -248,7 +248,6 @@ static void signal_left()
project_ghost_shape(&fld, 1, 0);
sfClock_restart(game.putTick);
}
sfClock_restart(game.repKeyLeft);
}

static void signal_right()
Expand All @@ -257,7 +256,6 @@ static void signal_right()
project_ghost_shape(&fld, 1, 0);
sfClock_restart(game.putTick);
}
sfClock_restart(game.repKeyRight);
}

static void game_keys()
Expand Down Expand Up @@ -287,6 +285,7 @@ static void game_keys()
if (!(arrKeys & DOWN)) {
arrKeys = arrKeys | DOWN;
signal_down();
sfClock_restart(game.repPushDown);
} else {
if (sfClock_getElapsedTime(game.repPushDown).microseconds >= moveRepeatLatency2)
arrKeys = arrKeys & ~DOWN;
Expand All @@ -300,6 +299,7 @@ static void game_keys()
if (!(arrKeys & LEFT)) {
arrKeys = arrKeys | LEFT;
signal_left();
sfClock_restart(game.repKeyLeft);
} else if (!(arrKeys & LEFTHOLD)) {
if (sfClock_getElapsedTime(game.repKeyLeft).microseconds >= moveRepeatLatency1) {
arrKeys = arrKeys | LEFTHOLD;
Expand All @@ -319,6 +319,7 @@ static void game_keys()
if (!(arrKeys & RIGHT)) {
arrKeys = arrKeys | RIGHT;
signal_right();
sfClock_restart(game.repKeyRight);
} else if (!(arrKeys & RIGHTHOLD)) {
if (sfClock_getElapsedTime(game.repKeyRight).microseconds >= moveRepeatLatency1) {
arrKeys = arrKeys | RIGHTHOLD;
Expand Down
47 changes: 45 additions & 2 deletions src/field.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,55 @@ static void rotate_shape_right(struct shape *shape)
shape->c[j][i] = arr[i+1][3-j];
}

void field_rotate_shape(struct field *fld, unsigned int index)
static int wall_kick(struct field *fld, struct shape *shape)
{
// try kick the left wall
shape->x++;
if (field_shape_collision(fld, shape))
shape->x--;
else
return 1;

// try kick the right wall
shape->x--;
if (field_shape_collision(fld, shape)) {
if (shape->t == 6) {
shape->x--;
if (field_shape_collision(fld, shape))
shape->x++;
else
return 1;
}
shape->x++;
} else {
return 1;
}

// try kick the floor
shape->y++;
if (field_shape_collision(fld, shape)) {
if (shape->t == 6) {
shape->y++;
if (field_shape_collision(fld, shape))
shape->y--;
else
return 1;
}
shape->y--;
} else {
return 1;
}

return 0;
}

void field_rotate_shape_clockwise(struct field *fld, unsigned int index)
{
struct shape *shape = &fld->shape[index];
rotate_shape_right(shape);
if (field_shape_collision(fld, shape))
rotate_shape_left(shape);
if (!wall_kick(fld, shape))
rotate_shape_left(shape);
}

int field_move_shape_down(struct field *fld, unsigned int index)
Expand Down

0 comments on commit a3eaa5d

Please sign in to comment.