Kostadin Chakarov / Mbed 2 deprecated el17kec

Dependencies:   mbed

Committer:
kocemax
Date:
Wed May 08 13:49:01 2019 +0000
Revision:
8:9b77eea95088
Parent:
7:cd3cafda3dd4
Child:
9:f720f5d87420
Finally, implemented power ups and fixed the angles after pad collision. Also started commenting, not much time left, so will finish the commenting and if I have more time will add a couple more 'negative' power ups.

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 },
k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,0,0,0,1,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 8:9b77eea95088 74 /** Flash the LEDs until start button is pressed */
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 1:b82c4c2de9a8 87 while (pad.check_event(Gamepad::START_PRESSED) == false)
kocemax 1:b82c4c2de9a8 88 {
kocemax 1:b82c4c2de9a8 89 for (int i = 1; i < 7; i++)
kocemax 1:b82c4c2de9a8 90 {
kocemax 5:12c179da4788 91 pad.led(i,1);
kocemax 5:12c179da4788 92 wait(0.1);
kocemax 5:12c179da4788 93 pad.led(i,0);
kocemax 8:9b77eea95088 94 }
kocemax 1:b82c4c2de9a8 95 }
kocemax 1:b82c4c2de9a8 96 }
kocemax 0:2f0362fef062 97
kocemax 8:9b77eea95088 98 /** Resets everything if game is won / lost */
kocemax 7:cd3cafda3dd4 99 void resetGame()
kocemax 7:cd3cafda3dd4 100 {
kocemax 8:9b77eea95088 101 paddle.reset(); /** Reset paddle */
kocemax 8:9b77eea95088 102 ball.reset(); /** Reset ball */
kocemax 8:9b77eea95088 103 map.reset(); /** Reset map */
kocemax 7:cd3cafda3dd4 104 }
kocemax 7:cd3cafda3dd4 105
kocemax 8:9b77eea95088 106 /** Checks if game is lost */
kocemax 7:cd3cafda3dd4 107 void checkLost()
kocemax 7:cd3cafda3dd4 108 {
kocemax 8:9b77eea95088 109 const Vector2D& posPad = paddle.getPos();
kocemax 8:9b77eea95088 110 /** Checks if ball is below paddle, i.e. lost */
kocemax 7:cd3cafda3dd4 111 if (ball.getPos().y > posPad.y)
kocemax 7:cd3cafda3dd4 112 {
kocemax 7:cd3cafda3dd4 113 while (1)
kocemax 7:cd3cafda3dd4 114 {
kocemax 8:9b77eea95088 115 /** Prints a simple 'lost' message and a restart prompt */
kocemax 7:cd3cafda3dd4 116 lcd.clear();
kocemax 7:cd3cafda3dd4 117 lcd.printString("You Lost",17,2);
kocemax 7:cd3cafda3dd4 118 lcd.printString("Press A",20,3);
kocemax 7:cd3cafda3dd4 119 lcd.printString("to restart",12,4);
kocemax 7:cd3cafda3dd4 120 wait(0.1);
kocemax 7:cd3cafda3dd4 121 lcd.refresh();
kocemax 8:9b77eea95088 122 /** Checks if reset button is pressed, if it is reset */
kocemax 7:cd3cafda3dd4 123 if (pad.check_event(Gamepad::A_PRESSED) == true)
kocemax 7:cd3cafda3dd4 124 {
kocemax 7:cd3cafda3dd4 125 resetGame();
kocemax 7:cd3cafda3dd4 126 break;
kocemax 7:cd3cafda3dd4 127 }
kocemax 7:cd3cafda3dd4 128 }
kocemax 7:cd3cafda3dd4 129 }
kocemax 7:cd3cafda3dd4 130 }
kocemax 7:cd3cafda3dd4 131
kocemax 8:9b77eea95088 132 /** Checks if level / game is won */
kocemax 8:9b77eea95088 133 void checkWon()
kocemax 8:9b77eea95088 134 {
kocemax 7:cd3cafda3dd4 135 if (map.checkLevel()) {
kocemax 8:9b77eea95088 136 /** We have cleared a level */
kocemax 8:9b77eea95088 137 ball.reset(); /** Reset ball */
kocemax 8:9b77eea95088 138 paddle.reset(); /** Reset paddle */
kocemax 7:cd3cafda3dd4 139
kocemax 7:cd3cafda3dd4 140 if (map.hasWon()) {
kocemax 8:9b77eea95088 141 /** We have cleared all levels */
kocemax 7:cd3cafda3dd4 142 while (1)
kocemax 8:9b77eea95088 143 {
kocemax 8:9b77eea95088 144 /** Prints a win message and asks if you want to restart */
kocemax 7:cd3cafda3dd4 145 lcd.clear();
kocemax 7:cd3cafda3dd4 146 lcd.printString("You WON!",17,2);
kocemax 7:cd3cafda3dd4 147 lcd.printString("Press A",20,3);
kocemax 7:cd3cafda3dd4 148 lcd.printString("to restart",12,4);
kocemax 7:cd3cafda3dd4 149 wait(0.1);
kocemax 7:cd3cafda3dd4 150 lcd.refresh();
kocemax 8:9b77eea95088 151 /** Checks if reset button is pressed, if it is reset */
kocemax 7:cd3cafda3dd4 152 if (pad.check_event(Gamepad::A_PRESSED) == true)
kocemax 7:cd3cafda3dd4 153 {
kocemax 7:cd3cafda3dd4 154 resetGame();
kocemax 7:cd3cafda3dd4 155 break;
kocemax 7:cd3cafda3dd4 156 }
kocemax 7:cd3cafda3dd4 157 }
kocemax 7:cd3cafda3dd4 158 }
kocemax 7:cd3cafda3dd4 159 }
kocemax 7:cd3cafda3dd4 160 }
kocemax 7:cd3cafda3dd4 161
kocemax 8:9b77eea95088 162 /** Game loop */
kocemax 6:39bda45efeed 163 void render()
kocemax 6:39bda45efeed 164 {
kocemax 8:9b77eea95088 165
kocemax 8:9b77eea95088 166
kocemax 8:9b77eea95088 167 map.initBricks(); /** Initialize the map */
kocemax 6:39bda45efeed 168
kocemax 4:0e01cbb95434 169 while(1)
kocemax 8:9b77eea95088 170 {
kocemax 8:9b77eea95088 171
kocemax 8:9b77eea95088 172 /** Physics, movement, control input + game logic */
kocemax 8:9b77eea95088 173 /** Control Input */
kocemax 8:9b77eea95088 174 paddle.controlPaddle(pad);
kocemax 8:9b77eea95088 175
kocemax 8:9b77eea95088 176 /** Movement and physics */
kocemax 6:39bda45efeed 177 ball.move();
kocemax 8:9b77eea95088 178 paddle.move();
kocemax 8:9b77eea95088 179
kocemax 8:9b77eea95088 180 map.checkCollision(ball, paddle);
kocemax 8:9b77eea95088 181 map.update();
kocemax 8:9b77eea95088 182 ball.hitPad(paddle);
kocemax 8:9b77eea95088 183
kocemax 8:9b77eea95088 184 /** Rendering */
kocemax 8:9b77eea95088 185 lcd.clear(); /** Initialize render */
kocemax 8:9b77eea95088 186 contrast(); /** Sets contrast */
kocemax 8:9b77eea95088 187 map.drawMap(lcd); /** Draw map objects */
kocemax 8:9b77eea95088 188 paddle.draw(lcd); /** Draws paddle */
kocemax 8:9b77eea95088 189 ball.draw(lcd); /** Draws ball */
kocemax 8:9b77eea95088 190
kocemax 8:9b77eea95088 191 lcd.refresh(); /** Finalize render */
kocemax 8:9b77eea95088 192
kocemax 8:9b77eea95088 193 wait(1.0f/fps); /** Frames per second */
kocemax 8:9b77eea95088 194
kocemax 8:9b77eea95088 195 /** Check Lost/Won */
kocemax 7:cd3cafda3dd4 196 checkLost();
kocemax 7:cd3cafda3dd4 197 checkWon();
kocemax 4:0e01cbb95434 198 }
kocemax 8:9b77eea95088 199 }
kocemax 8:9b77eea95088 200
kocemax 8:9b77eea95088 201 /** Allows to set contrast, if not suitable */
kocemax 8:9b77eea95088 202 void contrast()
kocemax 8:9b77eea95088 203 {
kocemax 8:9b77eea95088 204 float contrast = pot.read(); /** Read value from potentiometer(0-1) */
kocemax 8:9b77eea95088 205 lcd.setContrast(contrast); /** Set that value for the contrast */
kocemax 8:9b77eea95088 206 }
kocemax 8:9b77eea95088 207