![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
All there but errors need fixing
Dependencies: mbed
Overview:
Rookie Tetris is a jigsaw style game based on the classic Tetris.
A block will appear at the top of the screen, you must move it (your options for movement are left, right and down - you cannot move up the board). The block will stop when it if placed either on the floor of the board or on-top of another block.
Your goal is to fill a complete row of the board with the blocks; when you do so the row will delete and the pattern above it will drop down. The game is over when your pattern is tall enough to reach to the top of the board!
Controls:
Use the joystick to move your block! Your block cannot move out of the parameters of the board.
Pot 2 controls the contrast of the screen.
Diff: TetrisGame/TetrisGame.cpp
- Revision:
- 21:6d4dd906edde
- Parent:
- 20:ad685b35a1a4
- Child:
- 22:403759d2f093
--- a/TetrisGame/TetrisGame.cpp Wed Jun 03 12:47:33 2020 +0000 +++ b/TetrisGame/TetrisGame.cpp Wed Jun 03 13:34:52 2020 +0000 @@ -21,114 +21,94 @@ _tetromino.init(_px, _py); // puts tetromino in middle - printf("initiate(%i)\n",_speed); +// printf("initiate(%i)\n",_speed); } -void TetrisGame::read_input(Gamepad &pad) +void TetrisGame::read_input(Gamepad &pad) // read inputs { _d = pad.get_direction(); _mag = pad.get_mag(); } -void TetrisGame::draw(N5110 &lcd) +void TetrisGame::draw(N5110 &lcd) // draw elements in lcd buffer { - // draw elements in lcd buffer -// lcd.drawRect(0,0,WIDTH - 2,HEIGHT,FILL_TRANSPARENT); lcd.drawRect(19, 4, 42, HEIGHT - 4, FILL_TRANSPARENT); -// lcd.drawLine(19, HEIGHT, 61, HEIGHT, 0); -// lcd.drawLine(4, 19, HEIGHT, 19, 0); -// lcd.drawLine(4, 61, HEIGHT, 61, 0); - _tetromino.draw(lcd); // printf("draw (%i,%i)\n",_px,_py); } -void TetrisGame::update(Gamepad &pad, N5110 &lcd) +void TetrisGame::update(Gamepad &pad, N5110 &lcd) // update values { // check_score(pad); _tetromino.update(_d,_mag); - check_wall_collision(pad, lcd); - check_tetromino_collisions(pad, lcd); + check_wall_collision(pad, lcd); // check if block has collided with the floor + check_tetromino_collisions(pad, lcd); // check if block has collided with an old block // printf("update (%i,%i,%i)\n",_px,_py,_d); } -void TetrisGame::check_wall_collision(Gamepad &pad, N5110 &lcd) +void TetrisGame::check_wall_collision(Gamepad &pad, N5110 &lcd) // check if block has hit floor { - Vector2D tetromino_pos = _tetromino.get_pos(); - Vector2D tetromino_velocity = _tetromino.get_velocity(); -// floor_hit_flag = 0; + Vector2D tetromino_pos = _tetromino.get_pos(); // get current positioning of block - // check if hit floor - if (tetromino_pos.y + 4 >= (HEIGHT-1) ) { + if (tetromino_pos.y + 4 >= (HEIGHT-1) ) { // if bottom of block is on bottom boundary - tetromino_pos.y = (HEIGHT-1) - 4; -// tetromino_velocity.y = 0; + tetromino_pos.y = (HEIGHT-1) - 4; // stop block - pad.tone(750.0,0.1); + pad.tone(750.0,0.1); // play tone - cancel_line(lcd); // check if line full and cancel + drop down if it is - - lcd.drawRect(tetromino_pos.x, tetromino_pos.y, 4, 4, FILL_BLACK); + cancel_line(lcd); // check if row is full _tetromino.init(_px, _py); // drop new shame - a = tetromino_pos.x; + a = tetromino_pos.x; // save positions to be drawn on next screen b = tetromino_pos.y; - floor_hit_flag = 1; + floor_hit_flag = 1; // set flag so program knows to draw block on next screen - if (b <= 4) { - exit_game(lcd); + if (b <= 4) { // if pattern reaches top of screen + exit_game(lcd); // game over } printf("floor hit!\n"); } - // printf("wall collision\n"); - - // SAVE OLD SCREEN AND DROP NEW TETROMINO???? // -// update tetromino parameters?? } -void TetrisGame::check_tetromino_collisions(Gamepad &pad, N5110 &lcd) +void TetrisGame::check_tetromino_collisions(Gamepad &pad, N5110 &lcd) // check if block has old block beneath it { - Vector2D tetromino_pos = _tetromino.get_pos(); - Vector2D tetromino_velocity = _tetromino.get_velocity(); + Vector2D tetromino_pos = _tetromino.get_pos(); // get current position of block if ( - (lcd.getPixel(tetromino_pos.x, tetromino_pos.y + 4) == 1) || // not sure if it should be 4 or 5 ??? + (lcd.getPixel(tetromino_pos.x, tetromino_pos.y + 4) == 1) || (lcd.getPixel(tetromino_pos.x + 1, tetromino_pos.y + 4) == 1) || (lcd.getPixel(tetromino_pos.x + 2, tetromino_pos.y + 4) == 1) || - (lcd.getPixel(tetromino_pos.x + 3, tetromino_pos.y + 4) == 1) + (lcd.getPixel(tetromino_pos.x + 3, tetromino_pos.y + 4) == 1) // if any of the 4 pixels directly below the block are black ) { - tetromino_velocity.x = 0; - pad.tone(1000.0,0.1); - cancel_line(lcd); + pad.tone(1000.0,0.1); // play tone + cancel_line(lcd); // check if row is full -// lcd.drawRect(tetromino_pos.x, tetromino_pos.y, 4, 4, FILL_BLACK); + _tetromino.init(_px, _py); // drop new block - _tetromino.init(_px, _py); // break to loop back or keep this ?? - - a = tetromino_pos.x; + a = tetromino_pos.x; // save positioning of old block b = tetromino_pos.y; - floor_hit_flag = 1; + floor_hit_flag = 1; // set flag so program knows to draw block on next screen - if (b <= 4) { - exit_game(lcd); + if (b <= 4) { // if pattern reaches top of screen + exit_game(lcd); // game over } printf("blocks stacked!\n"); @@ -136,15 +116,15 @@ // printf("tetromino collision\n"); } -void TetrisGame::cancel_line(N5110 &lcd) +void TetrisGame::cancel_line(N5110 &lcd) // check if row is full, if it is then delete line and drop pattern down { int count; - for(int j=4; j<=46; j+=1) { + for(int j=4; j<=46; j++) { for(int i=20; i<=60; i++) { - if (lcd.getPixel(i,j)==0) { // if the pixel is empty the counting stops and exits loop + if (lcd.getPixel(i,j)==0) { // if the pixel is empty the counting stops and exits loop (row is not full) count=0; break; - } else if (lcd.getPixel(i,j)==1) { // if the pixel is full carries on counting + } else if (lcd.getPixel(i,j)==1) { // if the pixel is full carries on counting count ++; } }