Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@6:39bda45efeed, 2019-04-08 (annotated)
- Committer:
- kocemax
- Date:
- Mon Apr 08 09:14:33 2019 +0000
- Revision:
- 6:39bda45efeed
- Parent:
- 5:12c179da4788
- Child:
- 7:cd3cafda3dd4
Finally had more time during the holiday, reedited lots of the code to make it simpler, more readable, more robust, and better structured.Also added collision detection with the bricks, but no deflection yet.(Took the algorithm from an online forum).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kocemax | 0:2f0362fef062 | 1 | /* |
kocemax | 0:2f0362fef062 | 2 | ELEC2645 Embedded Systems Project |
kocemax | 0:2f0362fef062 | 3 | School of Electronic & Electrical Engineering University of Leeds |
kocemax | 0:2f0362fef062 | 4 | Name: Kostadin Chakarov |
kocemax | 0:2f0362fef062 | 5 | Username: el17kec |
kocemax | 0:2f0362fef062 | 6 | Student ID Number:201171978 Date:11/03/2019 |
kocemax | 0:2f0362fef062 | 7 | */ |
kocemax | 2:006a2ddfabb6 | 8 | // Breakout++ logo sprite |
kocemax | 1:b82c4c2de9a8 | 9 | const int breakout[10][75] = { |
kocemax | 1:b82c4c2de9a8 | 10 | { 1,0,0,1,1,0,0,0,0,0,1,0,0,1,1,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0 }, |
kocemax | 1:b82c4c2de9a8 | 11 | { 1,0,1,0,0,1,0,0,1,0,1,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,1,1,1,0,0,1,0,1,1,1,1,1,1,1,1,1 }, |
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1,1,1,1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0 }, |
kocemax | 1:b82c4c2de9a8 | 18 | { 1,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0 }, |
kocemax | 1:b82c4c2de9a8 | 19 | { 1,0,0,1,1,1,0,0,0,0,1,0,0,0,0,0,1,0,1,1,1,1,1,1,1,1,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0 }, |
kocemax | 1:b82c4c2de9a8 | 20 | }; |
kocemax | 0:2f0362fef062 | 21 | |
kocemax | 1:b82c4c2de9a8 | 22 | ///////// pre-processor directives //////// |
kocemax | 0:2f0362fef062 | 23 | #include "mbed.h" |
kocemax | 1:b82c4c2de9a8 | 24 | #include "N5110.h" |
kocemax | 1:b82c4c2de9a8 | 25 | #include "Gamepad.h" |
kocemax | 6:39bda45efeed | 26 | #include "PlayerControl.h" |
kocemax | 6:39bda45efeed | 27 | #include "Ball.h" |
kocemax | 6:39bda45efeed | 28 | #include "Map.h" |
kocemax | 1:b82c4c2de9a8 | 29 | |
kocemax | 4:0e01cbb95434 | 30 | const int fps = 10; //sets the fps of the game doesn't change |
kocemax | 4:0e01cbb95434 | 31 | |
kocemax | 1:b82c4c2de9a8 | 32 | /////////////// objects /////////////// |
kocemax | 1:b82c4c2de9a8 | 33 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // K64F - pwr from 3V3 |
kocemax | 1:b82c4c2de9a8 | 34 | Gamepad pad; // Gamepad buttons |
kocemax | 1:b82c4c2de9a8 | 35 | PlayerControl cont; // Object for player |
kocemax | 2:006a2ddfabb6 | 36 | Ball ball; |
kocemax | 5:12c179da4788 | 37 | Map map; |
kocemax | 1:b82c4c2de9a8 | 38 | |
kocemax | 1:b82c4c2de9a8 | 39 | ///////////// function prototypes /////////////// |
kocemax | 1:b82c4c2de9a8 | 40 | void init(); |
kocemax | 1:b82c4c2de9a8 | 41 | void welcome(); |
kocemax | 1:b82c4c2de9a8 | 42 | void render(); |
kocemax | 0:2f0362fef062 | 43 | |
kocemax | 1:b82c4c2de9a8 | 44 | ///////////// functions //////////////// |
kocemax | 1:b82c4c2de9a8 | 45 | int main() |
kocemax | 1:b82c4c2de9a8 | 46 | { |
kocemax | 1:b82c4c2de9a8 | 47 | init(); |
kocemax | 1:b82c4c2de9a8 | 48 | welcome(); // waiting for the user to start |
kocemax | 5:12c179da4788 | 49 | |
kocemax | 4:0e01cbb95434 | 50 | render(); |
kocemax | 1:b82c4c2de9a8 | 51 | } |
kocemax | 1:b82c4c2de9a8 | 52 | |
kocemax | 1:b82c4c2de9a8 | 53 | void init() |
kocemax | 1:b82c4c2de9a8 | 54 | { |
kocemax | 1:b82c4c2de9a8 | 55 | // need to initialise LCD and Gamepad |
kocemax | 1:b82c4c2de9a8 | 56 | lcd.init(); |
kocemax | 1:b82c4c2de9a8 | 57 | pad.init(); |
kocemax | 1:b82c4c2de9a8 | 58 | lcd.setContrast(0.55); // setting contrast to 55% seems good |
kocemax | 1:b82c4c2de9a8 | 59 | lcd.normalMode(); // normal colour mode |
kocemax | 1:b82c4c2de9a8 | 60 | lcd.setBrightness(0.5); // put LED backlight on 50% |
kocemax | 1:b82c4c2de9a8 | 61 | } |
kocemax | 1:b82c4c2de9a8 | 62 | |
kocemax | 1:b82c4c2de9a8 | 63 | void welcome() |
kocemax | 0:2f0362fef062 | 64 | { |
kocemax | 1:b82c4c2de9a8 | 65 | lcd.printString("Welcome to",12,0); |
kocemax | 1:b82c4c2de9a8 | 66 | // x origin, y origin, rows, cols, sprite |
kocemax | 1:b82c4c2de9a8 | 67 | lcd.drawSprite(5,15,10,75,(int *)breakout); |
kocemax | 1:b82c4c2de9a8 | 68 | lcd.printString("Press Start",9,4); |
kocemax | 1:b82c4c2de9a8 | 69 | lcd.refresh(); |
kocemax | 1:b82c4c2de9a8 | 70 | |
kocemax | 1:b82c4c2de9a8 | 71 | // flash the LEDs until start button is pressed |
kocemax | 1:b82c4c2de9a8 | 72 | while (pad.check_event(Gamepad::START_PRESSED) == false) |
kocemax | 1:b82c4c2de9a8 | 73 | { |
kocemax | 1:b82c4c2de9a8 | 74 | for (int i = 1; i < 7; i++) |
kocemax | 1:b82c4c2de9a8 | 75 | { |
kocemax | 5:12c179da4788 | 76 | pad.led(i,1); |
kocemax | 5:12c179da4788 | 77 | wait(0.1); |
kocemax | 5:12c179da4788 | 78 | pad.led(i,0); |
kocemax | 1:b82c4c2de9a8 | 79 | } |
kocemax | 1:b82c4c2de9a8 | 80 | } |
kocemax | 1:b82c4c2de9a8 | 81 | } |
kocemax | 0:2f0362fef062 | 82 | |
kocemax | 6:39bda45efeed | 83 | void render() |
kocemax | 6:39bda45efeed | 84 | { |
kocemax | 6:39bda45efeed | 85 | map.initBricks(); |
kocemax | 6:39bda45efeed | 86 | |
kocemax | 4:0e01cbb95434 | 87 | while(1) |
kocemax | 6:39bda45efeed | 88 | { |
kocemax | 6:39bda45efeed | 89 | // physics, input + game logic |
kocemax | 6:39bda45efeed | 90 | cont.controlPlayer(pad); |
kocemax | 6:39bda45efeed | 91 | cont.get_padPos(pad); |
kocemax | 6:39bda45efeed | 92 | ball.getballPos(); |
kocemax | 6:39bda45efeed | 93 | ball.move(); |
kocemax | 6:39bda45efeed | 94 | ball.hitPad(pad, cont); |
kocemax | 6:39bda45efeed | 95 | map.checkCollision(ball); |
kocemax | 6:39bda45efeed | 96 | ball.endCondition(pad, lcd, cont); |
kocemax | 6:39bda45efeed | 97 | |
kocemax | 6:39bda45efeed | 98 | // rendering |
kocemax | 4:0e01cbb95434 | 99 | lcd.clear(); // initialise the LCD |
kocemax | 5:12c179da4788 | 100 | map.drawMap(lcd); |
kocemax | 4:0e01cbb95434 | 101 | cont.drawPlayer(lcd); |
kocemax | 6:39bda45efeed | 102 | ball.draw(lcd); |
kocemax | 4:0e01cbb95434 | 103 | lcd.refresh(); |
kocemax | 6:39bda45efeed | 104 | |
kocemax | 4:0e01cbb95434 | 105 | wait(1.0f/fps); |
kocemax | 4:0e01cbb95434 | 106 | } |
kocemax | 0:2f0362fef062 | 107 | } |