Kostadin Chakarov / Mbed 2 deprecated el17kec

Dependencies:   mbed

Committer:
kocemax
Date:
Wed May 08 23:09:43 2019 +0000
Revision:
9:f720f5d87420
Parent:
8:9b77eea95088
Child:
10:da5743dfb137
I've finished the commenting and also added 1 new level, also added a score system. This is pretty much ready for submission now. Tomorrow might have a look again just to ensure everything is correct.

Who changed what in which revision?

UserRevisionLine numberNew 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 7:cd3cafda3dd4 8
kocemax 8:9b77eea95088 9 /** Breakout logo sprite */
kocemax 1:b82c4c2de9a8 10 const int breakout[10][75] = {
kocemax 1:b82c4c2de9a8 11 { 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 12 { 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 },
kocemax 1:b82c4c2de9a8 13 { 1,1,0,0,0,0,1,0,0,1,1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1,0,0,1,1,1,0,0,1,0,0,0,0,0,1,0,0,0,0 },
kocemax 1:b82c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0,0,0,0 },
kocemax 1:b82c4c2de9a8 20 { 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 21 };
kocemax 0:2f0362fef062 22
kocemax 1:b82c4c2de9a8 23 ///////// pre-processor directives ////////
kocemax 0:2f0362fef062 24 #include "mbed.h"
kocemax 1:b82c4c2de9a8 25 #include "N5110.h"
kocemax 1:b82c4c2de9a8 26 #include "Gamepad.h"
kocemax 8:9b77eea95088 27 #include "Paddle.h"
kocemax 6:39bda45efeed 28 #include "Ball.h"
kocemax 6:39bda45efeed 29 #include "Map.h"
kocemax 1:b82c4c2de9a8 30
kocemax 8:9b77eea95088 31 const int fps = 20; /** Sets the fps of the game, doesn't change */
kocemax 4:0e01cbb95434 32
kocemax 1:b82c4c2de9a8 33 /////////////// objects ///////////////
kocemax 8:9b77eea95088 34 AnalogIn pot(PTB2); /** Potentiometer to set contrast of screen */
kocemax 8:9b77eea95088 35 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); /** K64F - pwr from 3V3 */
kocemax 8:9b77eea95088 36 Gamepad pad; /** Gamepad buttons */
kocemax 8:9b77eea95088 37 Paddle paddle; /** Paddle object */
kocemax 8:9b77eea95088 38 Ball ball; /** Ball object */
kocemax 8:9b77eea95088 39 Map map; /** Map object */
kocemax 1:b82c4c2de9a8 40
kocemax 1:b82c4c2de9a8 41 ///////////// function prototypes ///////////////
kocemax 8:9b77eea95088 42 void init(); /** Initialize LCD and Gamepad */
kocemax 8:9b77eea95088 43 void welcome(); /** Simple welcome screen displayed on start-up */
kocemax 8:9b77eea95088 44 void render(); /** Used as the game loop */
kocemax 8:9b77eea95088 45 void contrast(); /** Allows to set contrast, if not suitable */
kocemax 0:2f0362fef062 46
kocemax 1:b82c4c2de9a8 47 ///////////// functions ////////////////
kocemax 1:b82c4c2de9a8 48 int main()
kocemax 1:b82c4c2de9a8 49 {
kocemax 8:9b77eea95088 50 init(); /** Initialize LCD and Gamepad */
kocemax 8:9b77eea95088 51 welcome(); /** Waiting for the user to start */
kocemax 8:9b77eea95088 52 render(); /** Game Loop */
kocemax 1:b82c4c2de9a8 53 }
kocemax 1:b82c4c2de9a8 54
kocemax 8:9b77eea95088 55
kocemax 1:b82c4c2de9a8 56 void init()
kocemax 1:b82c4c2de9a8 57 {
kocemax 8:9b77eea95088 58 lcd.init(); /** Initialize the LCD */
kocemax 8:9b77eea95088 59 pad.init(); /** Initialize the Gamepad */
kocemax 8:9b77eea95088 60 lcd.setContrast(0.55); /** Initially set contrast to 55% */
kocemax 8:9b77eea95088 61 lcd.normalMode(); /** Normal color mode */
kocemax 8:9b77eea95088 62 lcd.setBrightness(0.5); /** Puts LED backlight on 50% */
kocemax 1:b82c4c2de9a8 63 }
kocemax 1:b82c4c2de9a8 64
kocemax 8:9b77eea95088 65 /** Simple welcome screen displayed on start-up */
kocemax 1:b82c4c2de9a8 66 void welcome()
kocemax 0:2f0362fef062 67 {
kocemax 1:b82c4c2de9a8 68 lcd.printString("Welcome to",12,0);
kocemax 8:9b77eea95088 69 lcd.drawSprite(5,15,10,75,(int *)breakout); // x origin, y origin, rows, cols, sprite
kocemax 8:9b77eea95088 70 lcd.printString("Press A ",2,4);
kocemax 8:9b77eea95088 71 lcd.printString("for Controls ",11,5);
kocemax 1:b82c4c2de9a8 72 lcd.refresh();
kocemax 1:b82c4c2de9a8 73
kocemax 9:f720f5d87420 74 /** Wait to move to next menu */
kocemax 8:9b77eea95088 75 while (pad.check_event(Gamepad::A_PRESSED) == false)
kocemax 8:9b77eea95088 76 {
kocemax 8:9b77eea95088 77
kocemax 8:9b77eea95088 78 }
kocemax 8:9b77eea95088 79 lcd.clear();
kocemax 8:9b77eea95088 80 lcd.printString("Use pot to ->",4,0);
kocemax 8:9b77eea95088 81 lcd.printString("set contrast",4,1);
kocemax 8:9b77eea95088 82 lcd.printString("Use Joystick ",6,2);
kocemax 8:9b77eea95088 83 lcd.printString("to move paddle ",0,3);
kocemax 8:9b77eea95088 84 lcd.printString("Press Start ",9,4);
kocemax 8:9b77eea95088 85 lcd.printString("to begin ",16,5);
kocemax 8:9b77eea95088 86 lcd.refresh();
kocemax 9:f720f5d87420 87 /** Flash LEDs until start button is pressed */
kocemax 1:b82c4c2de9a8 88 while (pad.check_event(Gamepad::START_PRESSED) == false)
kocemax 1:b82c4c2de9a8 89 {
kocemax 1:b82c4c2de9a8 90 for (int i = 1; i < 7; i++)
kocemax 1:b82c4c2de9a8 91 {
kocemax 5:12c179da4788 92 pad.led(i,1);
kocemax 5:12c179da4788 93 wait(0.1);
kocemax 5:12c179da4788 94 pad.led(i,0);
kocemax 8:9b77eea95088 95 }
kocemax 1:b82c4c2de9a8 96 }
kocemax 1:b82c4c2de9a8 97 }
kocemax 0:2f0362fef062 98
kocemax 8:9b77eea95088 99 /** Resets everything if game is won / lost */
kocemax 7:cd3cafda3dd4 100 void resetGame()
kocemax 7:cd3cafda3dd4 101 {
kocemax 8:9b77eea95088 102 paddle.reset(); /** Reset paddle */
kocemax 8:9b77eea95088 103 ball.reset(); /** Reset ball */
kocemax 8:9b77eea95088 104 map.reset(); /** Reset map */
kocemax 9:f720f5d87420 105 map.score = 0; /** Reset score */
kocemax 7:cd3cafda3dd4 106 }
kocemax 7:cd3cafda3dd4 107
kocemax 8:9b77eea95088 108 /** Checks if game is lost */
kocemax 7:cd3cafda3dd4 109 void checkLost()
kocemax 7:cd3cafda3dd4 110 {
kocemax 8:9b77eea95088 111 const Vector2D& posPad = paddle.getPos();
kocemax 8:9b77eea95088 112 /** Checks if ball is below paddle, i.e. lost */
kocemax 7:cd3cafda3dd4 113 if (ball.getPos().y > posPad.y)
kocemax 7:cd3cafda3dd4 114 {
kocemax 7:cd3cafda3dd4 115 while (1)
kocemax 7:cd3cafda3dd4 116 {
kocemax 9:f720f5d87420 117 /** Prints a simple 'lost' message and score and asks if you want to restart */
kocemax 7:cd3cafda3dd4 118 lcd.clear();
kocemax 9:f720f5d87420 119 char finalscore[14];
kocemax 9:f720f5d87420 120 sprintf(finalscore,"Score = %d",map.score);
kocemax 9:f720f5d87420 121 lcd.printString(finalscore,0,0);
kocemax 7:cd3cafda3dd4 122 lcd.printString("You Lost",17,2);
kocemax 7:cd3cafda3dd4 123 lcd.printString("Press A",20,3);
kocemax 7:cd3cafda3dd4 124 lcd.printString("to restart",12,4);
kocemax 7:cd3cafda3dd4 125 wait(0.1);
kocemax 7:cd3cafda3dd4 126 lcd.refresh();
kocemax 8:9b77eea95088 127 /** Checks if reset button is pressed, if it is reset */
kocemax 7:cd3cafda3dd4 128 if (pad.check_event(Gamepad::A_PRESSED) == true)
kocemax 7:cd3cafda3dd4 129 {
kocemax 7:cd3cafda3dd4 130 resetGame();
kocemax 7:cd3cafda3dd4 131 break;
kocemax 7:cd3cafda3dd4 132 }
kocemax 7:cd3cafda3dd4 133 }
kocemax 7:cd3cafda3dd4 134 }
kocemax 7:cd3cafda3dd4 135 }
kocemax 7:cd3cafda3dd4 136
kocemax 8:9b77eea95088 137 /** Checks if level / game is won */
kocemax 8:9b77eea95088 138 void checkWon()
kocemax 8:9b77eea95088 139 {
kocemax 7:cd3cafda3dd4 140 if (map.checkLevel()) {
kocemax 8:9b77eea95088 141 /** We have cleared a level */
kocemax 8:9b77eea95088 142 ball.reset(); /** Reset ball */
kocemax 8:9b77eea95088 143 paddle.reset(); /** Reset paddle */
kocemax 7:cd3cafda3dd4 144
kocemax 7:cd3cafda3dd4 145 if (map.hasWon()) {
kocemax 8:9b77eea95088 146 /** We have cleared all levels */
kocemax 7:cd3cafda3dd4 147 while (1)
kocemax 8:9b77eea95088 148 {
kocemax 9:f720f5d87420 149 /** Prints win message and score and asks if you want to restart */
kocemax 7:cd3cafda3dd4 150 lcd.clear();
kocemax 9:f720f5d87420 151 char finalscore[14];
kocemax 9:f720f5d87420 152 sprintf(finalscore,"Score = %d",map.score);
kocemax 9:f720f5d87420 153 lcd.printString(finalscore,0,0);
kocemax 7:cd3cafda3dd4 154 lcd.printString("You WON!",17,2);
kocemax 7:cd3cafda3dd4 155 lcd.printString("Press A",20,3);
kocemax 7:cd3cafda3dd4 156 lcd.printString("to restart",12,4);
kocemax 7:cd3cafda3dd4 157 wait(0.1);
kocemax 7:cd3cafda3dd4 158 lcd.refresh();
kocemax 8:9b77eea95088 159 /** Checks if reset button is pressed, if it is reset */
kocemax 7:cd3cafda3dd4 160 if (pad.check_event(Gamepad::A_PRESSED) == true)
kocemax 7:cd3cafda3dd4 161 {
kocemax 7:cd3cafda3dd4 162 resetGame();
kocemax 7:cd3cafda3dd4 163 break;
kocemax 7:cd3cafda3dd4 164 }
kocemax 7:cd3cafda3dd4 165 }
kocemax 7:cd3cafda3dd4 166 }
kocemax 7:cd3cafda3dd4 167 }
kocemax 7:cd3cafda3dd4 168 }
kocemax 7:cd3cafda3dd4 169
kocemax 8:9b77eea95088 170 /** Game loop */
kocemax 6:39bda45efeed 171 void render()
kocemax 6:39bda45efeed 172 {
kocemax 8:9b77eea95088 173
kocemax 8:9b77eea95088 174
kocemax 8:9b77eea95088 175 map.initBricks(); /** Initialize the map */
kocemax 6:39bda45efeed 176
kocemax 4:0e01cbb95434 177 while(1)
kocemax 8:9b77eea95088 178 {
kocemax 8:9b77eea95088 179
kocemax 8:9b77eea95088 180 /** Physics, movement, control input + game logic */
kocemax 8:9b77eea95088 181 /** Control Input */
kocemax 8:9b77eea95088 182 paddle.controlPaddle(pad);
kocemax 8:9b77eea95088 183
kocemax 8:9b77eea95088 184 /** Movement and physics */
kocemax 6:39bda45efeed 185 ball.move();
kocemax 8:9b77eea95088 186 paddle.move();
kocemax 8:9b77eea95088 187
kocemax 8:9b77eea95088 188 map.checkCollision(ball, paddle);
kocemax 8:9b77eea95088 189 map.update();
kocemax 8:9b77eea95088 190 ball.hitPad(paddle);
kocemax 8:9b77eea95088 191
kocemax 8:9b77eea95088 192 /** Rendering */
kocemax 8:9b77eea95088 193 lcd.clear(); /** Initialize render */
kocemax 8:9b77eea95088 194 contrast(); /** Sets contrast */
kocemax 8:9b77eea95088 195 map.drawMap(lcd); /** Draw map objects */
kocemax 8:9b77eea95088 196 paddle.draw(lcd); /** Draws paddle */
kocemax 8:9b77eea95088 197 ball.draw(lcd); /** Draws ball */
kocemax 8:9b77eea95088 198
kocemax 8:9b77eea95088 199 lcd.refresh(); /** Finalize render */
kocemax 8:9b77eea95088 200
kocemax 8:9b77eea95088 201 wait(1.0f/fps); /** Frames per second */
kocemax 8:9b77eea95088 202
kocemax 8:9b77eea95088 203 /** Check Lost/Won */
kocemax 7:cd3cafda3dd4 204 checkLost();
kocemax 7:cd3cafda3dd4 205 checkWon();
kocemax 4:0e01cbb95434 206 }
kocemax 8:9b77eea95088 207 }
kocemax 8:9b77eea95088 208
kocemax 8:9b77eea95088 209 /** Allows to set contrast, if not suitable */
kocemax 8:9b77eea95088 210 void contrast()
kocemax 8:9b77eea95088 211 {
kocemax 8:9b77eea95088 212 float contrast = pot.read(); /** Read value from potentiometer(0-1) */
kocemax 8:9b77eea95088 213 lcd.setContrast(contrast); /** Set that value for the contrast */
kocemax 8:9b77eea95088 214 }
kocemax 8:9b77eea95088 215