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.

Committer:
el18rs
Date:
Wed Jun 03 16:44:09 2020 +0000
Revision:
22:403759d2f093
Parent:
21:6d4dd906edde
Child:
23:cbfa9d1ee3db
line clears when full

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el18rs 3:522c6f850e91 1 #include "TetrisGame.h"
el18rs 3:522c6f850e91 2
el18rs 15:68114eae4053 3 volatile int iH;
el18rs 22:403759d2f093 4 volatile int iB;
el18rs 15:68114eae4053 5
el18rs 3:522c6f850e91 6 TetrisGame::TetrisGame()
el18rs 3:522c6f850e91 7 {
el18rs 15:68114eae4053 8
el18rs 3:522c6f850e91 9 }
el18rs 3:522c6f850e91 10
el18rs 3:522c6f850e91 11 TetrisGame::~TetrisGame()
el18rs 3:522c6f850e91 12 {
el18rs 15:68114eae4053 13
el18rs 3:522c6f850e91 14 }
el18rs 3:522c6f850e91 15
el18rs 11:0183a52c1077 16 void TetrisGame::init(int speed)
el18rs 3:522c6f850e91 17 {
el18rs 4:7ddd287a5d28 18 // initialise game parameters
el18rs 11:0183a52c1077 19 _px = 40;
el18rs 11:0183a52c1077 20 _py = 4;
el18rs 9:58103274b2ab 21 _speed = speed;
el18rs 15:68114eae4053 22
el18rs 19:2ad31dbd5d11 23 _tetromino.init(_px, _py); // puts tetromino in middle
el18rs 15:68114eae4053 24
el18rs 21:6d4dd906edde 25 // printf("initiate(%i)\n",_speed);
el18rs 15:68114eae4053 26
el18rs 3:522c6f850e91 27 }
el18rs 3:522c6f850e91 28
el18rs 4:7ddd287a5d28 29
el18rs 22:403759d2f093 30 void TetrisGame::read_input(Gamepad &pad) // read inputs
el18rs 3:522c6f850e91 31 {
el18rs 3:522c6f850e91 32 _d = pad.get_direction();
el18rs 4:7ddd287a5d28 33 _mag = pad.get_mag();
el18rs 3:522c6f850e91 34 }
el18rs 3:522c6f850e91 35
el18rs 21:6d4dd906edde 36 void TetrisGame::draw(N5110 &lcd) // draw elements in lcd buffer
el18rs 3:522c6f850e91 37 {
el18rs 15:68114eae4053 38
el18rs 19:2ad31dbd5d11 39
el18rs 19:2ad31dbd5d11 40 lcd.drawRect(19, 4, 42, HEIGHT - 4, FILL_TRANSPARENT);
el18rs 19:2ad31dbd5d11 41
el18rs 6:39cbec524483 42 _tetromino.draw(lcd);
el18rs 15:68114eae4053 43
el18rs 19:2ad31dbd5d11 44 // printf("draw (%i,%i)\n",_px,_py);
el18rs 15:68114eae4053 45
el18rs 3:522c6f850e91 46 }
el18rs 3:522c6f850e91 47
el18rs 22:403759d2f093 48 void TetrisGame::update(Gamepad &pad, N5110 &lcd) // update values
el18rs 3:522c6f850e91 49 {
el18rs 4:7ddd287a5d28 50 // check_score(pad);
el18rs 15:68114eae4053 51
el18rs 4:7ddd287a5d28 52 _tetromino.update(_d,_mag);
el18rs 15:68114eae4053 53
el18rs 21:6d4dd906edde 54 check_wall_collision(pad, lcd); // check if block has collided with the floor
el18rs 21:6d4dd906edde 55 check_tetromino_collisions(pad, lcd); // check if block has collided with an old block
el18rs 15:68114eae4053 56
el18rs 19:2ad31dbd5d11 57 // printf("update (%i,%i,%i)\n",_px,_py,_d);
el18rs 3:522c6f850e91 58 }
el18rs 4:7ddd287a5d28 59
el18rs 4:7ddd287a5d28 60
el18rs 21:6d4dd906edde 61 void TetrisGame::check_wall_collision(Gamepad &pad, N5110 &lcd) // check if block has hit floor
el18rs 3:522c6f850e91 62 {
el18rs 21:6d4dd906edde 63 Vector2D tetromino_pos = _tetromino.get_pos(); // get current positioning of block
el18rs 15:68114eae4053 64
el18rs 22:403759d2f093 65 if (tetromino_pos.y + 4 >= (HEIGHT-1) ) { // if bottom of block is on bottom boundary
el18rs 15:68114eae4053 66
el18rs 21:6d4dd906edde 67 tetromino_pos.y = (HEIGHT-1) - 4; // stop block
el18rs 15:68114eae4053 68
el18rs 22:403759d2f093 69 pad.tone(750.0,0.1); // play tone
el18rs 15:68114eae4053 70
el18rs 22:403759d2f093 71 cancel_line(lcd); // check if row is full
el18rs 15:68114eae4053 72
el18rs 15:68114eae4053 73 _tetromino.init(_px, _py); // drop new shame
el18rs 19:2ad31dbd5d11 74
el18rs 21:6d4dd906edde 75 a = tetromino_pos.x; // save positions to be drawn on next screen
el18rs 13:600a34a047ca 76 b = tetromino_pos.y;
el18rs 15:68114eae4053 77
el18rs 21:6d4dd906edde 78 floor_hit_flag = 1; // set flag so program knows to draw block on next screen
el18rs 22:403759d2f093 79
el18rs 22:403759d2f093 80 if (b <= 4) { // if pattern reaches top of screen
el18rs 22:403759d2f093 81 exit_game(lcd); // game over
el18rs 20:ad685b35a1a4 82 }
el18rs 15:68114eae4053 83
el18rs 11:0183a52c1077 84 printf("floor hit!\n");
el18rs 15:68114eae4053 85
el18rs 4:7ddd287a5d28 86 }
el18rs 19:2ad31dbd5d11 87 // printf("wall collision\n");
el18rs 4:7ddd287a5d28 88 }
el18rs 4:7ddd287a5d28 89
el18rs 22:403759d2f093 90 void TetrisGame::check_tetromino_collisions(Gamepad &pad, N5110 &lcd) // check if block has old block beneath it
el18rs 5:53e021832adc 91 {
el18rs 21:6d4dd906edde 92 Vector2D tetromino_pos = _tetromino.get_pos(); // get current position of block
el18rs 5:53e021832adc 93
el18rs 5:53e021832adc 94 if (
el18rs 22:403759d2f093 95 (lcd.getPixel(tetromino_pos.x, tetromino_pos.y + 4) == 1) ||
el18rs 17:70d45e9e44a8 96 (lcd.getPixel(tetromino_pos.x + 1, tetromino_pos.y + 4) == 1) ||
el18rs 17:70d45e9e44a8 97 (lcd.getPixel(tetromino_pos.x + 2, tetromino_pos.y + 4) == 1) ||
el18rs 22:403759d2f093 98 (lcd.getPixel(tetromino_pos.x + 3, tetromino_pos.y + 4) == 1) // if any of the 4 pixels directly below the block are black
el18rs 5:53e021832adc 99 ) {
el18rs 15:68114eae4053 100
el18rs 22:403759d2f093 101 pad.tone(1000.0,0.1); // play tone
el18rs 21:6d4dd906edde 102 cancel_line(lcd); // check if row is full
el18rs 15:68114eae4053 103
el18rs 21:6d4dd906edde 104 _tetromino.init(_px, _py); // drop new block
el18rs 15:68114eae4053 105
el18rs 21:6d4dd906edde 106 a = tetromino_pos.x; // save positioning of old block
el18rs 16:fca1bbb06c44 107 b = tetromino_pos.y;
el18rs 16:fca1bbb06c44 108
el18rs 21:6d4dd906edde 109 floor_hit_flag = 1; // set flag so program knows to draw block on next screen
el18rs 22:403759d2f093 110
el18rs 22:403759d2f093 111 if (b <= 4) { // if pattern reaches top of screen
el18rs 22:403759d2f093 112 exit_game(lcd); // game over
el18rs 20:ad685b35a1a4 113 }
el18rs 15:68114eae4053 114
el18rs 11:0183a52c1077 115 printf("blocks stacked!\n");
el18rs 19:2ad31dbd5d11 116 }
el18rs 19:2ad31dbd5d11 117 // printf("tetromino collision\n");
el18rs 5:53e021832adc 118 }
el18rs 6:39cbec524483 119
el18rs 22:403759d2f093 120 void TetrisGame::cancel_line(N5110 &lcd) // check if row is full, if it is then delete line and drop pattern down
el18rs 6:39cbec524483 121 {
el18rs 22:403759d2f093 122 for(int j=4; j<=46; j++) {
el18rs 20:ad685b35a1a4 123 for(int i=20; i<=60; i++) {
el18rs 21:6d4dd906edde 124 if (lcd.getPixel(i,j)==0) { // if the pixel is empty the counting stops and exits loop (row is not full)
el18rs 15:68114eae4053 125 count=0;
el18rs 22:403759d2f093 126 printf("line not full\n");
el18rs 15:68114eae4053 127 break;
el18rs 22:403759d2f093 128 } else if (lcd.getPixel(i,j)==1) { // if the pixel is full carries on counting
el18rs 15:68114eae4053 129 count ++;
el18rs 22:403759d2f093 130 printf("counting... count=%i\n",count);
el18rs 6:39cbec524483 131 }
el18rs 22:403759d2f093 132 if (count==48) { // if gets to end of line
el18rs 22:403759d2f093 133 count = 0;
el18rs 22:403759d2f093 134 printf("line full\n");
el18rs 22:403759d2f093 135 // lcd.drawRect(20,j,40,4,FILL_WHITE); // clear line
el18rs 22:403759d2f093 136 c = j;
el18rs 22:403759d2f093 137 clear_line_flag = 1;
el18rs 22:403759d2f093 138 floor_hit_flag = 0;
el18rs 22:403759d2f093 139 printf("line canceled!\n");
el18rs 22:403759d2f093 140 }
el18rs 15:68114eae4053 141 // score++;
el18rs 22:403759d2f093 142 // for(int x=20; x<=60; x++) {
el18rs 22:403759d2f093 143 // for(int y=j; y>=4; y--) {
el18rs 22:403759d2f093 144 // lcd.setPixel(x,y,false);
el18rs 22:403759d2f093 145 // lcd.setPixel(x,y+1); // sets pixel to the one above
el18rs 22:403759d2f093 146 // printf("line dropped!\n");
el18rs 22:403759d2f093 147 // }
el18rs 22:403759d2f093 148 // }
el18rs 6:39cbec524483 149 }
el18rs 15:68114eae4053 150 }
el18rs 22:403759d2f093 151 printf("cancel line\n");
el18rs 15:68114eae4053 152 }
el18rs 6:39cbec524483 153
el18rs 15:68114eae4053 154 void TetrisGame::exit_game(N5110 &lcd)
el18rs 15:68114eae4053 155 {
el18rs 15:68114eae4053 156 lcd.clear();
el18rs 15:68114eae4053 157 lcd.printString(" GAME OVER ",0, 3);
el18rs 15:68114eae4053 158 lcd.refresh();
el18rs 15:68114eae4053 159 wait(10);
el18rs 15:68114eae4053 160 printf("game over!\n");
el18rs 15:68114eae4053 161 lcd.clear();
el18rs 15:68114eae4053 162 lcd.printString(" press RESET ",0,6);
el18rs 15:68114eae4053 163 lcd.refresh();
el18rs 15:68114eae4053 164 printf("press reset!\n");
el18rs 15:68114eae4053 165 exit(1.0);
el18rs 15:68114eae4053 166 }
el18rs 15:68114eae4053 167
el18rs 15:68114eae4053 168 void TetrisGame::fallenblocks(N5110 &lcd, int iH)
el18rs 15:68114eae4053 169 {
el18rs 15:68114eae4053 170 for (int i=0; i<=iH; i++) {
el18rs 15:68114eae4053 171 x0 = X[i];
el18rs 15:68114eae4053 172 y0 = Y[i];
el18rs 16:fca1bbb06c44 173 lcd.drawRect(x0, y0, 4, 4, FILL_BLACK);
el18rs 19:2ad31dbd5d11 174 // printf("block drawn\n");
el18rs 15:68114eae4053 175 }
el18rs 22:403759d2f093 176 }
el18rs 22:403759d2f093 177
el18rs 22:403759d2f093 178 void TetrisGame::clearline(N5110 &lcd, int iP)
el18rs 22:403759d2f093 179 {
el18rs 22:403759d2f093 180 for (int i=0; i<=iB; i++) {
el18rs 22:403759d2f093 181 z0 = Z[i];
el18rs 22:403759d2f093 182 lcd.drawRect(20, z0, 40, 4, FILL_WHITE);
el18rs 22:403759d2f093 183 }
el18rs 15:68114eae4053 184 }