![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
All there but errors need fixing
main.cpp@4:7ddd287a5d28, 2020-05-31 (annotated)
- Committer:
- el18rs
- Date:
- Sun May 31 17:01:53 2020 +0000
- Revision:
- 4:7ddd287a5d28
- Parent:
- 3:522c6f850e91
- Child:
- 5:53e021832adc
Includes most parts but still has errors that need fixing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el18rs | 2:55092965eadd | 1 | /* |
eencae | 0:b7f1f47bb26a | 2 | ELEC2645 Embedded Systems Project |
eencae | 0:b7f1f47bb26a | 3 | School of Electronic & Electrical Engineering |
eencae | 0:b7f1f47bb26a | 4 | University of Leeds |
eencae | 0:b7f1f47bb26a | 5 | 2019/20 |
eencae | 0:b7f1f47bb26a | 6 | |
el18rs | 1:35dba0833c7d | 7 | Name: Ruby Smith |
el18rs | 1:35dba0833c7d | 8 | Username: el18rs |
el18rs | 1:35dba0833c7d | 9 | Student ID Number: 201259470 |
el18rs | 1:35dba0833c7d | 10 | Date: 22/05/20 |
eencae | 0:b7f1f47bb26a | 11 | */ |
eencae | 0:b7f1f47bb26a | 12 | |
eencae | 0:b7f1f47bb26a | 13 | // includes |
eencae | 0:b7f1f47bb26a | 14 | #include "mbed.h" |
eencae | 0:b7f1f47bb26a | 15 | #include "Gamepad.h" |
eencae | 0:b7f1f47bb26a | 16 | #include "N5110.h" |
el18rs | 3:522c6f850e91 | 17 | #include "TetrisGame.h" |
el18rs | 3:522c6f850e91 | 18 | |
el18rs | 4:7ddd287a5d28 | 19 | // set defaults |
el18rs | 3:522c6f850e91 | 20 | #define TETROMINO_WIDTH 4 |
el18rs | 3:522c6f850e91 | 21 | #define TETROMINO_HEIGHT 4 |
el18rs | 4:7ddd287a5d28 | 22 | // #define TETROMINO_SIZE 4 |
el18rs | 4:7ddd287a5d28 | 23 | #define TETROMINO_SPEED 1 |
el18rs | 4:7ddd287a5d28 | 24 | #define HEIGHT 48 |
el18rs | 4:7ddd287a5d28 | 25 | #define WIDTH 84 |
el18rs | 4:7ddd287a5d28 | 26 | // STRUCTS // |
el18rs | 3:522c6f850e91 | 27 | |
el18rs | 3:522c6f850e91 | 28 | struct UserInput { |
el18rs | 3:522c6f850e91 | 29 | Direction d; |
el18rs | 3:522c6f850e91 | 30 | float mag; |
el18rs | 3:522c6f850e91 | 31 | }; |
el18rs | 3:522c6f850e91 | 32 | |
el18rs | 4:7ddd287a5d28 | 33 | // OBJECTS // |
el18rs | 4:7ddd287a5d28 | 34 | |
el18rs | 3:522c6f850e91 | 35 | N5110 lcd; |
el18rs | 3:522c6f850e91 | 36 | Gamepad pad; |
el18rs | 3:522c6f850e91 | 37 | TetrisGame tetris; |
el18rs | 3:522c6f850e91 | 38 | |
el18rs | 4:7ddd287a5d28 | 39 | // PROTOTYPES // |
el18rs | 4:7ddd287a5d28 | 40 | |
el18rs | 3:522c6f850e91 | 41 | void init(); |
el18rs | 3:522c6f850e91 | 42 | void update_game(UserInput input); |
el18rs | 3:522c6f850e91 | 43 | void render(); |
el18rs | 3:522c6f850e91 | 44 | void welcome(); |
el18rs | 4:7ddd287a5d28 | 45 | void exit_game(); |
el18rs | 4:7ddd287a5d28 | 46 | void cancel_line(); |
el18rs | 4:7ddd287a5d28 | 47 | int score = 0; |
eencae | 0:b7f1f47bb26a | 48 | |
el18rs | 3:522c6f850e91 | 49 | // int ScreenHeight = 84; |
el18rs | 3:522c6f850e91 | 50 | // int ScreenWidth = 48; |
el18rs | 2:55092965eadd | 51 | |
el18rs | 4:7ddd287a5d28 | 52 | // FUNCTIONS // |
el18rs | 2:55092965eadd | 53 | |
el18rs | 3:522c6f850e91 | 54 | int main() |
el18rs | 2:55092965eadd | 55 | { |
el18rs | 4:7ddd287a5d28 | 56 | int fps = 6; // 6 frames per second |
el18rs | 2:55092965eadd | 57 | |
el18rs | 4:7ddd287a5d28 | 58 | init(); // initialise and display welcome screen |
el18rs | 4:7ddd287a5d28 | 59 | welcome(); // wait for user to press start |
el18rs | 4:7ddd287a5d28 | 60 | |
el18rs | 4:7ddd287a5d28 | 61 | render(); // draw initial frame |
el18rs | 4:7ddd287a5d28 | 62 | wait(1.0f/fps); // wait one frame period |
el18rs | 2:55092965eadd | 63 | |
el18rs | 4:7ddd287a5d28 | 64 | // game loop |
el18rs | 3:522c6f850e91 | 65 | while(1) { |
el18rs | 4:7ddd287a5d28 | 66 | tetris.read_input(pad); // read input |
el18rs | 3:522c6f850e91 | 67 | tetris.update(pad); |
el18rs | 4:7ddd287a5d28 | 68 | //play_game(); // update game state |
el18rs | 4:7ddd287a5d28 | 69 | render(); // render the display |
el18rs | 4:7ddd287a5d28 | 70 | wait(1.0f/fps); // wait one frame period |
el18rs | 4:7ddd287a5d28 | 71 | break; |
el18rs | 4:7ddd287a5d28 | 72 | |
el18rs | 4:7ddd287a5d28 | 73 | } |
el18rs | 4:7ddd287a5d28 | 74 | } |
el18rs | 4:7ddd287a5d28 | 75 | |
el18rs | 4:7ddd287a5d28 | 76 | void exit_game() { |
el18rs | 4:7ddd287a5d28 | 77 | lcd.clear(); |
el18rs | 4:7ddd287a5d28 | 78 | lcd.printString(" GAME OVER ",0, 3); |
el18rs | 4:7ddd287a5d28 | 79 | lcd.refresh(); |
el18rs | 4:7ddd287a5d28 | 80 | wait(10); |
el18rs | 4:7ddd287a5d28 | 81 | lcd.clear(); |
el18rs | 4:7ddd287a5d28 | 82 | lcd.printString(" press RESET ",0,6); |
el18rs | 4:7ddd287a5d28 | 83 | lcd.refresh(); |
el18rs | 4:7ddd287a5d28 | 84 | exit(1.0); |
el18rs | 4:7ddd287a5d28 | 85 | } |
el18rs | 4:7ddd287a5d28 | 86 | |
el18rs | 4:7ddd287a5d28 | 87 | |
el18rs | 4:7ddd287a5d28 | 88 | void cancel_line() { |
el18rs | 4:7ddd287a5d28 | 89 | int count; |
el18rs | 4:7ddd287a5d28 | 90 | for(int j=0; j<=46; j+=1) { |
el18rs | 4:7ddd287a5d28 | 91 | for(int i=0; i<=82; i++) { |
el18rs | 4:7ddd287a5d28 | 92 | if (lcd.getPixel(i,j)==0) { |
el18rs | 4:7ddd287a5d28 | 93 | count=0; |
el18rs | 4:7ddd287a5d28 | 94 | break; |
el18rs | 4:7ddd287a5d28 | 95 | } else if (lcd.getPixel(i,j)==1){ |
el18rs | 4:7ddd287a5d28 | 96 | count ++; |
el18rs | 4:7ddd287a5d28 | 97 | } |
el18rs | 4:7ddd287a5d28 | 98 | } |
el18rs | 4:7ddd287a5d28 | 99 | if(count==83) { |
el18rs | 4:7ddd287a5d28 | 100 | count = 0; |
el18rs | 4:7ddd287a5d28 | 101 | lcd.drawLine(0,j,82,j,0); // clear line |
el18rs | 4:7ddd287a5d28 | 102 | score++; |
el18rs | 4:7ddd287a5d28 | 103 | for(int x=0; x<=82; x++) { |
el18rs | 4:7ddd287a5d28 | 104 | for(int y=j; y>=0; y--) { |
el18rs | 4:7ddd287a5d28 | 105 | lcd.setPixel(x,y,false); |
el18rs | 4:7ddd287a5d28 | 106 | lcd.setPixel(x,y+1); |
el18rs | 4:7ddd287a5d28 | 107 | } |
el18rs | 4:7ddd287a5d28 | 108 | } |
el18rs | 4:7ddd287a5d28 | 109 | } |
el18rs | 4:7ddd287a5d28 | 110 | } |
el18rs | 4:7ddd287a5d28 | 111 | |
el18rs | 4:7ddd287a5d28 | 112 | |
el18rs | 3:522c6f850e91 | 113 | } |
el18rs | 3:522c6f850e91 | 114 | |
el18rs | 4:7ddd287a5d28 | 115 | |
el18rs | 4:7ddd287a5d28 | 116 | // initialise classes and libraries |
el18rs | 4:7ddd287a5d28 | 117 | void init() { |
el18rs | 4:7ddd287a5d28 | 118 | lcd.init(); // initialise lcd |
el18rs | 4:7ddd287a5d28 | 119 | pad.init(); // initialise Gamepad |
el18rs | 2:55092965eadd | 120 | |
el18rs | 4:7ddd287a5d28 | 121 | // initialise the game with correct tetromino sizes etc |
el18rs | 4:7ddd287a5d28 | 122 | tetris.init(0, WIDTH/2 - 2, HEIGHT, 1); |
el18rs | 2:55092965eadd | 123 | |
el18rs | 2:55092965eadd | 124 | } |
el18rs | 2:55092965eadd | 125 | |
el18rs | 4:7ddd287a5d28 | 126 | void render() { // draws each frame on the lcd |
el18rs | 4:7ddd287a5d28 | 127 | |
el18rs | 4:7ddd287a5d28 | 128 | lcd.clear(); // clear screen |
el18rs | 4:7ddd287a5d28 | 129 | tetris.draw(lcd); // re-draw |
el18rs | 4:7ddd287a5d28 | 130 | lcd.refresh(); // refresh screen |
eencae | 0:b7f1f47bb26a | 131 | } |
eencae | 0:b7f1f47bb26a | 132 | |
el18rs | 4:7ddd287a5d28 | 133 | // welcome screen display |
el18rs | 3:522c6f850e91 | 134 | void welcome() { |
el18rs | 3:522c6f850e91 | 135 | |
el18rs | 3:522c6f850e91 | 136 | lcd.printString(" TETRIS ",0,1); |
el18rs | 3:522c6f850e91 | 137 | lcd.printString(" Press Start ",0,4); |
el18rs | 3:522c6f850e91 | 138 | lcd.refresh(); |
el18rs | 3:522c6f850e91 | 139 | |
el18rs | 4:7ddd287a5d28 | 140 | // flash LEDs untile start button pressed |
el18rs | 3:522c6f850e91 | 141 | while (pad.start_pressed() == false) { |
el18rs | 3:522c6f850e91 | 142 | lcd.setContrast(pad.read_pot1()); |
el18rs | 3:522c6f850e91 | 143 | pad.leds_on(); |
el18rs | 3:522c6f850e91 | 144 | wait(0.1); |
el18rs | 3:522c6f850e91 | 145 | pad.leds_off(); |
el18rs | 3:522c6f850e91 | 146 | wait(0.1); |
el18rs | 3:522c6f850e91 | 147 | } |
el18rs | 4:7ddd287a5d28 | 148 | } |
el18rs | 4:7ddd287a5d28 | 149 | |
el18rs | 4:7ddd287a5d28 | 150 | void check_tetromino_collisions() |
el18rs | 4:7ddd287a5d28 | 151 | { |
el18rs | 4:7ddd287a5d28 | 152 | Vector2D tetromino_pos = _tetromino.get_pos(); |
el18rs | 4:7ddd287a5d28 | 153 | Vector2D tetromino_velocity = _tetromino.get_velocity(); |
el18rs | 4:7ddd287a5d28 | 154 | |
el18rs | 4:7ddd287a5d28 | 155 | if ( |
el18rs | 4:7ddd287a5d28 | 156 | (lcd.get_pixel(tetromino_pos.y + 4, tetromino_pos.x) = 1) || // not sure if it should be 4 or 5 ??? |
el18rs | 4:7ddd287a5d28 | 157 | (lcd.get_pixel(tetromino_pos.y + 4, tetromino_pos.x + 1) = 1) || |
el18rs | 4:7ddd287a5d28 | 158 | (lcd.get_pixel(tetromino_pos.y + 4, tetromino_pos.x + 2) = 1) || |
el18rs | 4:7ddd287a5d28 | 159 | (lcd.get_pixel(tetromino_pos.y + 4, tetromino_pos.x + 3) = 1) |
el18rs | 4:7ddd287a5d28 | 160 | ) { |
el18rs | 4:7ddd287a5d28 | 161 | tetromino_velocity.x = 0; |
el18rs | 4:7ddd287a5d28 | 162 | |
el18rs | 4:7ddd287a5d28 | 163 | pad.tone(1000.0,0.1); |
el18rs | 4:7ddd287a5d28 | 164 | cancel_line(); |
el18rs | 4:7ddd287a5d28 | 165 | |
el18rs | 4:7ddd287a5d28 | 166 | blocknumber ++ |
el18rs | 4:7ddd287a5d28 | 167 | if (blocknumber > 4) { blocknumber = 0; } |
el18rs | 4:7ddd287a5d28 | 168 | |
el18rs | 4:7ddd287a5d28 | 169 | _tetromino.init(_number, WIDTH/2 - 2, HEIGHT, _speed); // break to loop back or keep this ?? |
el18rs | 4:7ddd287a5d28 | 170 | |
el18rs | 4:7ddd287a5d28 | 171 | } |