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:
Mon Jun 01 21:02:52 2020 +0000
Revision:
10:7310c9440547
Parent:
9:58103274b2ab
Child:
11:0183a52c1077
print statements added to help fix errors

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