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:
Tue Jun 02 21:07:17 2020 +0000
Revision:
15:68114eae4053
Parent:
14:bd8c59a4b641
Child:
16:fca1bbb06c44
works/compiles

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