![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
All there but errors need fixing
main.cpp@3:522c6f850e91, 2020-05-24 (annotated)
- Committer:
- el18rs
- Date:
- Sun May 24 11:18:33 2020 +0000
- Revision:
- 3:522c6f850e91
- Parent:
- 2:55092965eadd
- Child:
- 4:7ddd287a5d28
Game engine added
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 | 3:522c6f850e91 | 19 | #define TETROMINO_WIDTH 4 |
el18rs | 3:522c6f850e91 | 20 | #define TETROMINO_HEIGHT 4 |
el18rs | 3:522c6f850e91 | 21 | |
el18rs | 3:522c6f850e91 | 22 | struct UserInput { |
el18rs | 3:522c6f850e91 | 23 | Direction d; |
el18rs | 3:522c6f850e91 | 24 | float mag; |
el18rs | 3:522c6f850e91 | 25 | }; |
el18rs | 3:522c6f850e91 | 26 | |
el18rs | 3:522c6f850e91 | 27 | N5110 lcd; |
el18rs | 3:522c6f850e91 | 28 | Gamepad pad; |
el18rs | 3:522c6f850e91 | 29 | TetrisGame tetris; |
el18rs | 3:522c6f850e91 | 30 | |
el18rs | 3:522c6f850e91 | 31 | void init(); |
el18rs | 3:522c6f850e91 | 32 | void update_game(UserInput input); |
el18rs | 3:522c6f850e91 | 33 | void render(); |
el18rs | 3:522c6f850e91 | 34 | void welcome(); |
eencae | 0:b7f1f47bb26a | 35 | |
eencae | 0:b7f1f47bb26a | 36 | |
el18rs | 3:522c6f850e91 | 37 | // int ScreenHeight = 84; |
el18rs | 3:522c6f850e91 | 38 | // int ScreenWidth = 48; |
el18rs | 3:522c6f850e91 | 39 | // string block[7] // store tetromino blocks as a w-string |
el18rs | 2:55092965eadd | 40 | |
el18rs | 2:55092965eadd | 41 | |
el18rs | 2:55092965eadd | 42 | |
el18rs | 2:55092965eadd | 43 | |
el18rs | 2:55092965eadd | 44 | /* int Rotate(int px, int py, int r) |
el18rs | 2:55092965eadd | 45 | { |
el18rs | 2:55092965eadd | 46 | int pi = 0; |
el18rs | 2:55092965eadd | 47 | switch (r % 4); |
el18rs | 2:55092965eadd | 48 | { |
el18rs | 2:55092965eadd | 49 | case 0: pi = py * 4 + px; // shape width = 4 |
el18rs | 2:55092965eadd | 50 | break; |
el18rs | 2:55092965eadd | 51 | |
el18rs | 2:55092965eadd | 52 | case 1: pi = 12 + py - (px * 4); // rotates the shape by 90 degrees |
el18rs | 2:55092965eadd | 53 | break; |
el18rs | 2:55092965eadd | 54 | |
el18rs | 2:55092965eadd | 55 | case 2: pi = 15 - (py * 4) - px; // rotates the shape by 180 degrees |
el18rs | 2:55092965eadd | 56 | break; |
el18rs | 2:55092965eadd | 57 | |
el18rs | 2:55092965eadd | 58 | case 3: pi = 3 - py + (px * 4); // rotates the shape by 270 degrees |
el18rs | 2:55092965eadd | 59 | break; |
el18rs | 2:55092965eadd | 60 | |
el18rs | 2:55092965eadd | 61 | } |
el18rs | 2:55092965eadd | 62 | return pi; |
el18rs | 2:55092965eadd | 63 | } |
el18rs | 2:55092965eadd | 64 | */ |
el18rs | 2:55092965eadd | 65 | |
el18rs | 3:522c6f850e91 | 66 | int main() |
el18rs | 2:55092965eadd | 67 | { |
el18rs | 3:522c6f850e91 | 68 | int fps = 6; |
el18rs | 2:55092965eadd | 69 | |
el18rs | 3:522c6f850e91 | 70 | init(); |
el18rs | 3:522c6f850e91 | 71 | welcome(); |
el18rs | 2:55092965eadd | 72 | |
el18rs | 3:522c6f850e91 | 73 | render(); |
el18rs | 3:522c6f850e91 | 74 | wait(1.0f/fps); |
el18rs | 2:55092965eadd | 75 | |
el18rs | 3:522c6f850e91 | 76 | while(1) { |
el18rs | 3:522c6f850e91 | 77 | tetris.read_input(pad); |
el18rs | 3:522c6f850e91 | 78 | tetris.update(pad); |
el18rs | 3:522c6f850e91 | 79 | render(); |
el18rs | 3:522c6f850e91 | 80 | wait(1.0f/fps); |
el18rs | 3:522c6f850e91 | 81 | } |
el18rs | 3:522c6f850e91 | 82 | } |
el18rs | 3:522c6f850e91 | 83 | |
el18rs | 3:522c6f850e91 | 84 | void init() |
el18rs | 3:522c6f850e91 | 85 | { |
el18rs | 3:522c6f850e91 | 86 | lcd.init(); |
el18rs | 3:522c6f850e91 | 87 | padinit(); |
el18rs | 2:55092965eadd | 88 | |
el18rs | 3:522c6f850e91 | 89 | tetris.init(TETROMINO_WIDTH, TETROMINO_HEIGHT); |
el18rs | 2:55092965eadd | 90 | |
el18rs | 2:55092965eadd | 91 | } |
el18rs | 2:55092965eadd | 92 | |
el18rs | 3:522c6f850e91 | 93 | void render() |
el18rs | 2:55092965eadd | 94 | { |
el18rs | 3:522c6f850e91 | 95 | lcd.clear(); |
el18rs | 3:522c6f850e91 | 96 | tetris.draw(lcd); |
el18rs | 3:522c6f850e91 | 97 | lcd.refresh(); |
eencae | 0:b7f1f47bb26a | 98 | } |
eencae | 0:b7f1f47bb26a | 99 | |
el18rs | 3:522c6f850e91 | 100 | void welcome() { |
el18rs | 3:522c6f850e91 | 101 | |
el18rs | 3:522c6f850e91 | 102 | lcd.printString(" TETRIS ",0,1); |
el18rs | 3:522c6f850e91 | 103 | lcd.printString(" Press Start ",0,4); |
el18rs | 3:522c6f850e91 | 104 | lcd.refresh(); |
el18rs | 3:522c6f850e91 | 105 | |
el18rs | 3:522c6f850e91 | 106 | while (pad.start_pressed() == false) { |
el18rs | 3:522c6f850e91 | 107 | lcd.setContrast(pad.read_pot1()); |
el18rs | 3:522c6f850e91 | 108 | pad.leds_on(); |
el18rs | 3:522c6f850e91 | 109 | wait(0.1); |
el18rs | 3:522c6f850e91 | 110 | pad.leds_off(); |
el18rs | 3:522c6f850e91 | 111 | wait(0.1); |
el18rs | 3:522c6f850e91 | 112 | } |
el18rs | 3:522c6f850e91 | 113 | } |