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:
Sun May 31 20:41:41 2020 +0000
Revision:
6:39cbec524483
Parent:
5:53e021832adc
Child:
8:cebb2aca8e19
Compiles but needs fixing

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