All there but errors need fixing

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TetrisGame.cpp Source File

TetrisGame.cpp

00001 #include "TetrisGame.h"
00002 
00003 TetrisGame::TetrisGame()
00004 {
00005     
00006 }
00007 
00008 TetrisGame::~TetrisGame()
00009 {
00010     
00011 }
00012 
00013 void TetrisGame::init(int number, int x, int y, int speed)
00014 {
00015     // initialise game parameters
00016     _number = blockArray[blocknumber];
00017     _x = x;
00018     _y = y;
00019     _speed = speed;
00020     
00021     _tetromino.init(_number, WIDTH/2 - 2, HEIGHT, _speed); // puts tetromino in middle SHOULD THIS INCLUDE ARRAY????
00022     
00023 }
00024 
00025 
00026 void TetrisGame::read_input(Gamepad &pad)
00027 {
00028     _d = pad.get_direction();
00029     _mag = pad.get_mag();
00030 }
00031 
00032 void TetrisGame::draw(N5110 &lcd)
00033 {
00034     // draw elements in lcd buffer 
00035     
00036     // board
00037     lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
00038     
00039     _tetromino.draw(lcd, _number);
00040     
00041     
00042 }
00043 
00044 void TetrisGame::update(Gamepad &pad)
00045 {
00046     // check_score(pad);
00047     
00048     // _p1.update(_d,_mag);
00049     _tetromino.update(_d,_mag);
00050     
00051     check_wall_collision(pad);
00052     check_tetromino_collisions();
00053 }
00054 
00055 
00056 void TetrisGame::check_wall_collision(Gamepad &pad)
00057 {
00058     Vector2D tetromino_pos = _tetromino.get_pos();
00059     Vector2D tetromino_velocity = _tetromino.get_velocity();
00060     
00061     // check if hit floor
00062     if (tetromino_pos.y + 4 >= (HEIGHT-1) ) {
00063         
00064         tetromino_pos.y = (HEIGHT-1) - 4;
00065         tetromino_velocity.y = 0;
00066         
00067         pad.tone(750.0,0.1);
00068         
00069         cancel_line(); // check if line full and cancel + drop down if it is 
00070                 
00071         _tetromino.init(_number, WIDTH/2 - 2, HEIGHT, _speed); // drop new shame
00072         
00073         blocknumber ++;
00074         if (blocknumber > 4) {
00075             blocknumber = 0;
00076             }
00077     }
00078     // check if hit roof
00079     else if (tetromino_pos.y <= 1) {
00080         
00081         exit_game();
00082                 
00083     }
00084     
00085     // SAVE OLD SCREEN AND DROP NEW TETROMINO???? //
00086  // update tetromino parameters??   
00087 }
00088 
00089 void check_tetromino_collisions(Tetromino tetromino)
00090 {
00091     Vector2D tetromino_pos = _tetromino.get_pos();
00092     Vector2D tetromino_velocity = _tetromino.get_velocity();
00093 
00094     if (
00095     (lcd.get_pixel(tetromino_pos.y + 4, tetromino_pos.x) = 1) || // not sure if it should be 4 or 5 ??? 
00096     (lcd.get_pixel(tetromino_pos.y + 4, tetromino_pos.x + 1) = 1) || 
00097     (lcd.get_pixel(tetromino_pos.y + 4, tetromino_pos.x + 2) = 1) ||
00098     (lcd.get_pixel(tetromino_pos.y + 4, tetromino_pos.x + 3) = 1)
00099     ) {
00100         tetromino_velocity.x = 0;
00101         
00102         pad.tone(1000.0,0.1);
00103         cancel_line();
00104         
00105         blocknumber ++ 
00106         if (blocknumber > 4) { blocknumber = 0; }
00107             
00108         _tetromino.init(_number, WIDTH/2 - 2, HEIGHT, _speed); // break to loop back or keep this ??
00109         
00110 }