ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18rs

Dependencies:   mbed

Committer:
el18rs
Date:
Mon Jun 01 22:52:07 2020 +0000
Revision:
12:965b39a2e049
Parent:
11:0183a52c1077
Child:
13:600a34a047ca
works but picture drags on screen

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 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 3:522c6f850e91 21
el18rs 11:0183a52c1077 22 _tetromino.init(_px, _py); // puts tetromino in middle SHOULD THIS INCLUDE ARRAY????
el18rs 10:7310c9440547 23
el18rs 11:0183a52c1077 24 printf("initiate(%i)\n",_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 11:0183a52c1077 44 printf("draw (%i,%i)\n",_px,_py);
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 _tetromino.update(_d,_mag);
el18rs 3:522c6f850e91 53
el18rs 6:39cbec524483 54 check_wall_collision(pad, lcd);
el18rs 6:39cbec524483 55 check_tetromino_collisions(pad, lcd);
el18rs 10:7310c9440547 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 3:522c6f850e91 65
el18rs 4:7ddd287a5d28 66 // check if hit floor
el18rs 11:0183a52c1077 67 if (tetromino_pos.y + 4 >= (HEIGHT-1) ) {
el18rs 4:7ddd287a5d28 68
el18rs 11:0183a52c1077 69 tetromino_pos.y = (HEIGHT-1) - 4;
el18rs 4:7ddd287a5d28 70 tetromino_velocity.y = 0;
el18rs 4:7ddd287a5d28 71
el18rs 4:7ddd287a5d28 72 pad.tone(750.0,0.1);
el18rs 3:522c6f850e91 73
el18rs 6:39cbec524483 74 cancel_line(lcd); // check if line full and cancel + drop down if it is
el18rs 4:7ddd287a5d28 75
el18rs 6:39cbec524483 76 lcd.drawRect(tetromino_pos.x, tetromino_pos.y, 4, 4, FILL_BLACK);
el18rs 6:39cbec524483 77
el18rs 11:0183a52c1077 78 _tetromino.init(_px, _py); // drop new shame
el18rs 11:0183a52c1077 79
el18rs 11:0183a52c1077 80 printf("floor hit!\n");
el18rs 6:39cbec524483 81
el18rs 4:7ddd287a5d28 82 }
el18rs 4:7ddd287a5d28 83 // check if hit roof
el18rs 4:7ddd287a5d28 84 else if (tetromino_pos.y <= 1) {
el18rs 4:7ddd287a5d28 85
el18rs 11:0183a52c1077 86 printf("roof hit!\n");
el18rs 11:0183a52c1077 87
el18rs 6:39cbec524483 88 exit_game(lcd);
el18rs 4:7ddd287a5d28 89
el18rs 3:522c6f850e91 90 }
el18rs 3:522c6f850e91 91
el18rs 10:7310c9440547 92 printf("wall collision\n");
el18rs 10:7310c9440547 93
el18rs 4:7ddd287a5d28 94 // SAVE OLD SCREEN AND DROP NEW TETROMINO???? //
el18rs 4:7ddd287a5d28 95 // update tetromino parameters??
el18rs 4:7ddd287a5d28 96 }
el18rs 4:7ddd287a5d28 97
el18rs 6:39cbec524483 98 void TetrisGame::check_tetromino_collisions(Gamepad &pad, N5110 &lcd)
el18rs 5:53e021832adc 99 {
el18rs 5:53e021832adc 100 Vector2D tetromino_pos = _tetromino.get_pos();
el18rs 5:53e021832adc 101 Vector2D tetromino_velocity = _tetromino.get_velocity();
el18rs 5:53e021832adc 102
el18rs 5:53e021832adc 103 if (
el18rs 12:965b39a2e049 104 (lcd.getPixel(tetromino_pos.x, tetromino_pos.y + 4) == 1) || // not sure if it should be 4 or 5 ???
el18rs 12:965b39a2e049 105 (lcd.getPixel(tetromino_pos.x + 1, tetromino_pos.y + 4) == 1) ||
el18rs 12:965b39a2e049 106 (lcd.getPixel(tetromino_pos.x + 2, tetromino_pos.y + 4) == 1) ||
el18rs 12:965b39a2e049 107 (lcd.getPixel(tetromino_pos.x + 3, tetromino_pos.y + 4) == 1)
el18rs 5:53e021832adc 108 ) {
el18rs 5:53e021832adc 109 tetromino_velocity.x = 0;
el18rs 5:53e021832adc 110
el18rs 5:53e021832adc 111 pad.tone(1000.0,0.1);
el18rs 6:39cbec524483 112 cancel_line(lcd);
el18rs 5:53e021832adc 113
el18rs 6:39cbec524483 114 lcd.drawRect(tetromino_pos.x, tetromino_pos.y, 4, 4, FILL_BLACK);
el18rs 5:53e021832adc 115
el18rs 11:0183a52c1077 116 _tetromino.init(_px, _py); // break to loop back or keep this ??
el18rs 11:0183a52c1077 117
el18rs 11:0183a52c1077 118 printf("blocks stacked!\n");
el18rs 6:39cbec524483 119 }
el18rs 10:7310c9440547 120 printf("tetromino collision\n");
el18rs 5:53e021832adc 121 }
el18rs 6:39cbec524483 122
el18rs 6:39cbec524483 123 void TetrisGame::cancel_line(N5110 &lcd)
el18rs 6:39cbec524483 124 {
el18rs 6:39cbec524483 125 int count;
el18rs 6:39cbec524483 126 for(int j=0; j<=46; j+=1) {
el18rs 6:39cbec524483 127 for(int i=0; i<=82; i++) {
el18rs 6:39cbec524483 128 if (lcd.getPixel(i,j)==0) {
el18rs 6:39cbec524483 129 count=0;
el18rs 6:39cbec524483 130 break;
el18rs 6:39cbec524483 131 } else if (lcd.getPixel(i,j)==1){
el18rs 6:39cbec524483 132 count ++;
el18rs 6:39cbec524483 133 }
el18rs 6:39cbec524483 134 }
el18rs 6:39cbec524483 135 if(count==83) {
el18rs 6:39cbec524483 136 count = 0;
el18rs 6:39cbec524483 137 lcd.drawLine(0,j,82,j,0); // clear line
el18rs 12:965b39a2e049 138 printf("line canceled!\n");
el18rs 6:39cbec524483 139 // score++;
el18rs 6:39cbec524483 140 for(int x=0; x<=82; x++) {
el18rs 6:39cbec524483 141 for(int y=j; y>=0; y--) {
el18rs 6:39cbec524483 142 lcd.setPixel(x,y,false);
el18rs 6:39cbec524483 143 lcd.setPixel(x,y+1);
el18rs 12:965b39a2e049 144 printf("line dropped!\n");
el18rs 6:39cbec524483 145 }
el18rs 6:39cbec524483 146 }
el18rs 6:39cbec524483 147 }
el18rs 6:39cbec524483 148 }
el18rs 10:7310c9440547 149 printf("cancel line\n");
el18rs 6:39cbec524483 150
el18rs 6:39cbec524483 151 }
el18rs 6:39cbec524483 152
el18rs 6:39cbec524483 153 void TetrisGame::exit_game(N5110 &lcd) {
el18rs 6:39cbec524483 154 lcd.clear();
el18rs 6:39cbec524483 155 lcd.printString(" GAME OVER ",0, 3);
el18rs 6:39cbec524483 156 lcd.refresh();
el18rs 6:39cbec524483 157 wait(10);
el18rs 12:965b39a2e049 158 printf("game over!\n");
el18rs 6:39cbec524483 159 lcd.clear();
el18rs 6:39cbec524483 160 lcd.printString(" press RESET ",0,6);
el18rs 6:39cbec524483 161 lcd.refresh();
el18rs 12:965b39a2e049 162 printf("press reset!\n");
el18rs 6:39cbec524483 163 exit(1.0);
el18rs 6:39cbec524483 164 }