![](/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.
TetrisGame/TetrisGame.cpp@19:2ad31dbd5d11, 2020-06-03 (annotated)
- Committer:
- el18rs
- Date:
- Wed Jun 03 12:14:47 2020 +0000
- Revision:
- 19:2ad31dbd5d11
- Parent:
- 18:a089d9d184b1
- Child:
- 20:ad685b35a1a4
x boundaries added and board drawn
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el18rs | 3:522c6f850e91 | 1 | #include "TetrisGame.h" |
el18rs | 3:522c6f850e91 | 2 | |
el18rs | 15:68114eae4053 | 3 | volatile int iH; |
el18rs | 15:68114eae4053 | 4 | |
el18rs | 3:522c6f850e91 | 5 | TetrisGame::TetrisGame() |
el18rs | 3:522c6f850e91 | 6 | { |
el18rs | 15:68114eae4053 | 7 | |
el18rs | 3:522c6f850e91 | 8 | } |
el18rs | 3:522c6f850e91 | 9 | |
el18rs | 3:522c6f850e91 | 10 | TetrisGame::~TetrisGame() |
el18rs | 3:522c6f850e91 | 11 | { |
el18rs | 15:68114eae4053 | 12 | |
el18rs | 3:522c6f850e91 | 13 | } |
el18rs | 3:522c6f850e91 | 14 | |
el18rs | 11:0183a52c1077 | 15 | void TetrisGame::init(int speed) |
el18rs | 3:522c6f850e91 | 16 | { |
el18rs | 4:7ddd287a5d28 | 17 | // initialise game parameters |
el18rs | 11:0183a52c1077 | 18 | _px = 40; |
el18rs | 11:0183a52c1077 | 19 | _py = 4; |
el18rs | 9:58103274b2ab | 20 | _speed = speed; |
el18rs | 15:68114eae4053 | 21 | |
el18rs | 19:2ad31dbd5d11 | 22 | _tetromino.init(_px, _py); // puts tetromino in middle |
el18rs | 15:68114eae4053 | 23 | |
el18rs | 11:0183a52c1077 | 24 | printf("initiate(%i)\n",_speed); |
el18rs | 15:68114eae4053 | 25 | |
el18rs | 3:522c6f850e91 | 26 | } |
el18rs | 3:522c6f850e91 | 27 | |
el18rs | 4:7ddd287a5d28 | 28 | |
el18rs | 3:522c6f850e91 | 29 | void TetrisGame::read_input(Gamepad &pad) |
el18rs | 3:522c6f850e91 | 30 | { |
el18rs | 3:522c6f850e91 | 31 | _d = pad.get_direction(); |
el18rs | 4:7ddd287a5d28 | 32 | _mag = pad.get_mag(); |
el18rs | 3:522c6f850e91 | 33 | } |
el18rs | 3:522c6f850e91 | 34 | |
el18rs | 3:522c6f850e91 | 35 | void TetrisGame::draw(N5110 &lcd) |
el18rs | 3:522c6f850e91 | 36 | { |
el18rs | 15:68114eae4053 | 37 | // draw elements in lcd buffer |
el18rs | 15:68114eae4053 | 38 | |
el18rs | 19:2ad31dbd5d11 | 39 | // lcd.drawRect(0,0,WIDTH - 2,HEIGHT,FILL_TRANSPARENT); |
el18rs | 19:2ad31dbd5d11 | 40 | |
el18rs | 19:2ad31dbd5d11 | 41 | lcd.drawRect(19, 4, 42, HEIGHT - 4, FILL_TRANSPARENT); |
el18rs | 19:2ad31dbd5d11 | 42 | |
el18rs | 19:2ad31dbd5d11 | 43 | // lcd.drawLine(19, HEIGHT, 61, HEIGHT, 0); |
el18rs | 19:2ad31dbd5d11 | 44 | // lcd.drawLine(4, 19, HEIGHT, 19, 0); |
el18rs | 19:2ad31dbd5d11 | 45 | // lcd.drawLine(4, 61, HEIGHT, 61, 0); |
el18rs | 15:68114eae4053 | 46 | |
el18rs | 6:39cbec524483 | 47 | _tetromino.draw(lcd); |
el18rs | 15:68114eae4053 | 48 | |
el18rs | 19:2ad31dbd5d11 | 49 | // printf("draw (%i,%i)\n",_px,_py); |
el18rs | 15:68114eae4053 | 50 | |
el18rs | 3:522c6f850e91 | 51 | } |
el18rs | 3:522c6f850e91 | 52 | |
el18rs | 6:39cbec524483 | 53 | void TetrisGame::update(Gamepad &pad, N5110 &lcd) |
el18rs | 3:522c6f850e91 | 54 | { |
el18rs | 4:7ddd287a5d28 | 55 | // check_score(pad); |
el18rs | 15:68114eae4053 | 56 | |
el18rs | 4:7ddd287a5d28 | 57 | _tetromino.update(_d,_mag); |
el18rs | 15:68114eae4053 | 58 | |
el18rs | 6:39cbec524483 | 59 | check_wall_collision(pad, lcd); |
el18rs | 6:39cbec524483 | 60 | check_tetromino_collisions(pad, lcd); |
el18rs | 15:68114eae4053 | 61 | |
el18rs | 19:2ad31dbd5d11 | 62 | // printf("update (%i,%i,%i)\n",_px,_py,_d); |
el18rs | 3:522c6f850e91 | 63 | } |
el18rs | 4:7ddd287a5d28 | 64 | |
el18rs | 4:7ddd287a5d28 | 65 | |
el18rs | 6:39cbec524483 | 66 | void TetrisGame::check_wall_collision(Gamepad &pad, N5110 &lcd) |
el18rs | 3:522c6f850e91 | 67 | { |
el18rs | 3:522c6f850e91 | 68 | Vector2D tetromino_pos = _tetromino.get_pos(); |
el18rs | 3:522c6f850e91 | 69 | Vector2D tetromino_velocity = _tetromino.get_velocity(); |
el18rs | 17:70d45e9e44a8 | 70 | // floor_hit_flag = 0; |
el18rs | 15:68114eae4053 | 71 | |
el18rs | 4:7ddd287a5d28 | 72 | // check if hit floor |
el18rs | 11:0183a52c1077 | 73 | if (tetromino_pos.y + 4 >= (HEIGHT-1) ) { |
el18rs | 15:68114eae4053 | 74 | |
el18rs | 11:0183a52c1077 | 75 | tetromino_pos.y = (HEIGHT-1) - 4; |
el18rs | 15:68114eae4053 | 76 | // tetromino_velocity.y = 0; |
el18rs | 15:68114eae4053 | 77 | |
el18rs | 4:7ddd287a5d28 | 78 | pad.tone(750.0,0.1); |
el18rs | 15:68114eae4053 | 79 | |
el18rs | 18:a089d9d184b1 | 80 | cancel_line(lcd); // check if line full and cancel + drop down if it is |
el18rs | 15:68114eae4053 | 81 | |
el18rs | 6:39cbec524483 | 82 | lcd.drawRect(tetromino_pos.x, tetromino_pos.y, 4, 4, FILL_BLACK); |
el18rs | 15:68114eae4053 | 83 | |
el18rs | 15:68114eae4053 | 84 | _tetromino.init(_px, _py); // drop new shame |
el18rs | 19:2ad31dbd5d11 | 85 | |
el18rs | 13:600a34a047ca | 86 | a = tetromino_pos.x; |
el18rs | 13:600a34a047ca | 87 | b = tetromino_pos.y; |
el18rs | 15:68114eae4053 | 88 | |
el18rs | 13:600a34a047ca | 89 | floor_hit_flag = 1; |
el18rs | 15:68114eae4053 | 90 | |
el18rs | 11:0183a52c1077 | 91 | printf("floor hit!\n"); |
el18rs | 15:68114eae4053 | 92 | |
el18rs | 4:7ddd287a5d28 | 93 | } |
el18rs | 4:7ddd287a5d28 | 94 | // check if hit roof |
el18rs | 4:7ddd287a5d28 | 95 | else if (tetromino_pos.y <= 1) { |
el18rs | 19:2ad31dbd5d11 | 96 | |
el18rs | 18:a089d9d184b1 | 97 | floor_hit_flag = 0; |
el18rs | 15:68114eae4053 | 98 | |
el18rs | 11:0183a52c1077 | 99 | printf("roof hit!\n"); |
el18rs | 15:68114eae4053 | 100 | |
el18rs | 6:39cbec524483 | 101 | exit_game(lcd); |
el18rs | 15:68114eae4053 | 102 | |
el18rs | 3:522c6f850e91 | 103 | } |
el18rs | 15:68114eae4053 | 104 | |
el18rs | 19:2ad31dbd5d11 | 105 | // printf("wall collision\n"); |
el18rs | 15:68114eae4053 | 106 | |
el18rs | 4:7ddd287a5d28 | 107 | // SAVE OLD SCREEN AND DROP NEW TETROMINO???? // |
el18rs | 15:68114eae4053 | 108 | // update tetromino parameters?? |
el18rs | 4:7ddd287a5d28 | 109 | } |
el18rs | 4:7ddd287a5d28 | 110 | |
el18rs | 6:39cbec524483 | 111 | void TetrisGame::check_tetromino_collisions(Gamepad &pad, N5110 &lcd) |
el18rs | 5:53e021832adc | 112 | { |
el18rs | 5:53e021832adc | 113 | Vector2D tetromino_pos = _tetromino.get_pos(); |
el18rs | 5:53e021832adc | 114 | Vector2D tetromino_velocity = _tetromino.get_velocity(); |
el18rs | 5:53e021832adc | 115 | |
el18rs | 5:53e021832adc | 116 | if ( |
el18rs | 17:70d45e9e44a8 | 117 | (lcd.getPixel(tetromino_pos.x, tetromino_pos.y + 4) == 1) || // not sure if it should be 4 or 5 ??? |
el18rs | 17:70d45e9e44a8 | 118 | (lcd.getPixel(tetromino_pos.x + 1, tetromino_pos.y + 4) == 1) || |
el18rs | 17:70d45e9e44a8 | 119 | (lcd.getPixel(tetromino_pos.x + 2, tetromino_pos.y + 4) == 1) || |
el18rs | 17:70d45e9e44a8 | 120 | (lcd.getPixel(tetromino_pos.x + 3, tetromino_pos.y + 4) == 1) |
el18rs | 5:53e021832adc | 121 | ) { |
el18rs | 5:53e021832adc | 122 | tetromino_velocity.x = 0; |
el18rs | 15:68114eae4053 | 123 | |
el18rs | 5:53e021832adc | 124 | pad.tone(1000.0,0.1); |
el18rs | 6:39cbec524483 | 125 | cancel_line(lcd); |
el18rs | 15:68114eae4053 | 126 | |
el18rs | 15:68114eae4053 | 127 | // lcd.drawRect(tetromino_pos.x, tetromino_pos.y, 4, 4, FILL_BLACK); |
el18rs | 15:68114eae4053 | 128 | |
el18rs | 11:0183a52c1077 | 129 | _tetromino.init(_px, _py); // break to loop back or keep this ?? |
el18rs | 19:2ad31dbd5d11 | 130 | |
el18rs | 16:fca1bbb06c44 | 131 | a = tetromino_pos.x; |
el18rs | 16:fca1bbb06c44 | 132 | b = tetromino_pos.y; |
el18rs | 16:fca1bbb06c44 | 133 | |
el18rs | 16:fca1bbb06c44 | 134 | floor_hit_flag = 1; |
el18rs | 15:68114eae4053 | 135 | |
el18rs | 11:0183a52c1077 | 136 | printf("blocks stacked!\n"); |
el18rs | 19:2ad31dbd5d11 | 137 | } |
el18rs | 19:2ad31dbd5d11 | 138 | // printf("tetromino collision\n"); |
el18rs | 5:53e021832adc | 139 | } |
el18rs | 6:39cbec524483 | 140 | |
el18rs | 15:68114eae4053 | 141 | void TetrisGame::cancel_line(N5110 &lcd) |
el18rs | 6:39cbec524483 | 142 | { |
el18rs | 15:68114eae4053 | 143 | int count; |
el18rs | 19:2ad31dbd5d11 | 144 | for(int j=4; j<=42; j+=1) { |
el18rs | 19:2ad31dbd5d11 | 145 | for(int i=19; i<=40; i++) { |
el18rs | 15:68114eae4053 | 146 | if (lcd.getPixel(i,j)==0) { // if the pixel is empty the counting stops and exits loop |
el18rs | 15:68114eae4053 | 147 | count=0; |
el18rs | 15:68114eae4053 | 148 | break; |
el18rs | 15:68114eae4053 | 149 | } else if (lcd.getPixel(i,j)==1) { // if the pixel is full carries on counting |
el18rs | 15:68114eae4053 | 150 | count ++; |
el18rs | 6:39cbec524483 | 151 | } |
el18rs | 15:68114eae4053 | 152 | } |
el18rs | 19:2ad31dbd5d11 | 153 | if(count==41) { // if gets to end of line |
el18rs | 6:39cbec524483 | 154 | count = 0; |
el18rs | 19:2ad31dbd5d11 | 155 | lcd.drawLine(19,j,40,j,0); // clear line |
el18rs | 12:965b39a2e049 | 156 | printf("line canceled!\n"); |
el18rs | 15:68114eae4053 | 157 | // score++; |
el18rs | 19:2ad31dbd5d11 | 158 | for(int x=19; x<=40; x++) { |
el18rs | 6:39cbec524483 | 159 | for(int y=j; y>=0; y--) { |
el18rs | 19:2ad31dbd5d11 | 160 | lcd.setPixel(x,y,false); |
el18rs | 19:2ad31dbd5d11 | 161 | lcd.setPixel(x,y+1); // sets pixel to the one above |
el18rs | 12:965b39a2e049 | 162 | printf("line dropped!\n"); |
el18rs | 6:39cbec524483 | 163 | } |
el18rs | 6:39cbec524483 | 164 | } |
el18rs | 6:39cbec524483 | 165 | } |
el18rs | 15:68114eae4053 | 166 | } |
el18rs | 19:2ad31dbd5d11 | 167 | // printf("cancel line\n"); |
el18rs | 6:39cbec524483 | 168 | |
el18rs | 15:68114eae4053 | 169 | } |
el18rs | 6:39cbec524483 | 170 | |
el18rs | 15:68114eae4053 | 171 | void TetrisGame::exit_game(N5110 &lcd) |
el18rs | 15:68114eae4053 | 172 | { |
el18rs | 15:68114eae4053 | 173 | lcd.clear(); |
el18rs | 15:68114eae4053 | 174 | lcd.printString(" GAME OVER ",0, 3); |
el18rs | 15:68114eae4053 | 175 | lcd.refresh(); |
el18rs | 15:68114eae4053 | 176 | wait(10); |
el18rs | 15:68114eae4053 | 177 | printf("game over!\n"); |
el18rs | 15:68114eae4053 | 178 | lcd.clear(); |
el18rs | 15:68114eae4053 | 179 | lcd.printString(" press RESET ",0,6); |
el18rs | 15:68114eae4053 | 180 | lcd.refresh(); |
el18rs | 15:68114eae4053 | 181 | printf("press reset!\n"); |
el18rs | 15:68114eae4053 | 182 | exit(1.0); |
el18rs | 15:68114eae4053 | 183 | } |
el18rs | 15:68114eae4053 | 184 | |
el18rs | 15:68114eae4053 | 185 | void TetrisGame::fallenblocks(N5110 &lcd, int iH) |
el18rs | 15:68114eae4053 | 186 | { |
el18rs | 15:68114eae4053 | 187 | for (int i=0; i<=iH; i++) { |
el18rs | 15:68114eae4053 | 188 | x0 = X[i]; |
el18rs | 15:68114eae4053 | 189 | y0 = Y[i]; |
el18rs | 16:fca1bbb06c44 | 190 | lcd.drawRect(x0, y0, 4, 4, FILL_BLACK); |
el18rs | 19:2ad31dbd5d11 | 191 | // printf("block drawn\n"); |
el18rs | 15:68114eae4053 | 192 | } |
el18rs | 15:68114eae4053 | 193 | } |