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 18:38:09 2020 +0000
Revision:
14:bd8c59a4b641
Parent:
13:600a34a047ca
Child:
15:68114eae4053
compiles

Who changed what in which revision?

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