Kostadin Chakarov / Mbed 2 deprecated el17kec

Dependencies:   mbed

Committer:
kocemax
Date:
Wed Apr 10 09:18:25 2019 +0000
Revision:
7:cd3cafda3dd4
Parent:
6:39bda45efeed
Child:
8:9b77eea95088
Made a working collision and added 2 levels, can add more later. Also started doing power-ups but haven't finished yet. Need to think of some of them and also thinking of adding some randomisation to the ball<->pad collision.

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 7:cd3cafda3dd4 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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,0,0,0,0,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 6:39bda45efeed 27 #include "PlayerControl.h"
kocemax 6:39bda45efeed 28 #include "Ball.h"
kocemax 6:39bda45efeed 29 #include "Map.h"
kocemax 1:b82c4c2de9a8 30
kocemax 4:0e01cbb95434 31 const int fps = 10; //sets the fps of the game doesn't change
kocemax 4:0e01cbb95434 32
kocemax 1:b82c4c2de9a8 33 /////////////// objects ///////////////
kocemax 1:b82c4c2de9a8 34 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // K64F - pwr from 3V3
kocemax 1:b82c4c2de9a8 35 Gamepad pad; // Gamepad buttons
kocemax 1:b82c4c2de9a8 36 PlayerControl cont; // Object for player
kocemax 2:006a2ddfabb6 37 Ball ball;
kocemax 5:12c179da4788 38 Map map;
kocemax 1:b82c4c2de9a8 39
kocemax 1:b82c4c2de9a8 40 ///////////// function prototypes ///////////////
kocemax 1:b82c4c2de9a8 41 void init();
kocemax 1:b82c4c2de9a8 42 void welcome();
kocemax 1:b82c4c2de9a8 43 void render();
kocemax 0:2f0362fef062 44
kocemax 1:b82c4c2de9a8 45 ///////////// functions ////////////////
kocemax 1:b82c4c2de9a8 46 int main()
kocemax 1:b82c4c2de9a8 47 {
kocemax 7:cd3cafda3dd4 48 init(); //initialise LCD and Gamepad
kocemax 1:b82c4c2de9a8 49 welcome(); // waiting for the user to start
kocemax 7:cd3cafda3dd4 50 render(); //game loop
kocemax 1:b82c4c2de9a8 51 }
kocemax 1:b82c4c2de9a8 52
kocemax 1:b82c4c2de9a8 53 void init()
kocemax 1:b82c4c2de9a8 54 {
kocemax 1:b82c4c2de9a8 55 lcd.init();
kocemax 1:b82c4c2de9a8 56 pad.init();
kocemax 1:b82c4c2de9a8 57 lcd.setContrast(0.55); // setting contrast to 55% seems good
kocemax 1:b82c4c2de9a8 58 lcd.normalMode(); // normal colour mode
kocemax 1:b82c4c2de9a8 59 lcd.setBrightness(0.5); // put LED backlight on 50%
kocemax 1:b82c4c2de9a8 60 }
kocemax 1:b82c4c2de9a8 61
kocemax 1:b82c4c2de9a8 62 void welcome()
kocemax 0:2f0362fef062 63 {
kocemax 1:b82c4c2de9a8 64 lcd.printString("Welcome to",12,0);
kocemax 1:b82c4c2de9a8 65 // x origin, y origin, rows, cols, sprite
kocemax 1:b82c4c2de9a8 66 lcd.drawSprite(5,15,10,75,(int *)breakout);
kocemax 1:b82c4c2de9a8 67 lcd.printString("Press Start",9,4);
kocemax 1:b82c4c2de9a8 68 lcd.refresh();
kocemax 1:b82c4c2de9a8 69
kocemax 1:b82c4c2de9a8 70 // flash the LEDs until start button is pressed
kocemax 1:b82c4c2de9a8 71 while (pad.check_event(Gamepad::START_PRESSED) == false)
kocemax 1:b82c4c2de9a8 72 {
kocemax 1:b82c4c2de9a8 73 for (int i = 1; i < 7; i++)
kocemax 1:b82c4c2de9a8 74 {
kocemax 5:12c179da4788 75 pad.led(i,1);
kocemax 5:12c179da4788 76 wait(0.1);
kocemax 5:12c179da4788 77 pad.led(i,0);
kocemax 1:b82c4c2de9a8 78 }
kocemax 1:b82c4c2de9a8 79 }
kocemax 1:b82c4c2de9a8 80 }
kocemax 0:2f0362fef062 81
kocemax 7:cd3cafda3dd4 82
kocemax 7:cd3cafda3dd4 83 void resetGame()
kocemax 7:cd3cafda3dd4 84 {
kocemax 7:cd3cafda3dd4 85 ball.reset();
kocemax 7:cd3cafda3dd4 86 cont.reset();
kocemax 7:cd3cafda3dd4 87 map.reset();
kocemax 7:cd3cafda3dd4 88 }
kocemax 7:cd3cafda3dd4 89
kocemax 7:cd3cafda3dd4 90 void checkLost()
kocemax 7:cd3cafda3dd4 91 {
kocemax 7:cd3cafda3dd4 92 Vector2D posPad = cont.get_padPos();
kocemax 7:cd3cafda3dd4 93 if (ball.getPos().y > posPad.y)
kocemax 7:cd3cafda3dd4 94 {
kocemax 7:cd3cafda3dd4 95 while (1)
kocemax 7:cd3cafda3dd4 96 {
kocemax 7:cd3cafda3dd4 97 lcd.clear();
kocemax 7:cd3cafda3dd4 98 lcd.printString("You Lost",17,2);
kocemax 7:cd3cafda3dd4 99 lcd.printString("Press A",20,3);
kocemax 7:cd3cafda3dd4 100 lcd.printString("to restart",12,4);
kocemax 7:cd3cafda3dd4 101 wait(0.1);
kocemax 7:cd3cafda3dd4 102 lcd.refresh();
kocemax 7:cd3cafda3dd4 103 if (pad.check_event(Gamepad::A_PRESSED) == true)
kocemax 7:cd3cafda3dd4 104 {
kocemax 7:cd3cafda3dd4 105 resetGame();
kocemax 7:cd3cafda3dd4 106 break;
kocemax 7:cd3cafda3dd4 107 }
kocemax 7:cd3cafda3dd4 108 }
kocemax 7:cd3cafda3dd4 109 }
kocemax 7:cd3cafda3dd4 110 }
kocemax 7:cd3cafda3dd4 111
kocemax 7:cd3cafda3dd4 112 void checkWon() {
kocemax 7:cd3cafda3dd4 113 if (map.checkLevel()) {
kocemax 7:cd3cafda3dd4 114 // we have cleared a level
kocemax 7:cd3cafda3dd4 115 ball.reset();
kocemax 7:cd3cafda3dd4 116 cont.reset();
kocemax 7:cd3cafda3dd4 117
kocemax 7:cd3cafda3dd4 118 if (map.hasWon()) {
kocemax 7:cd3cafda3dd4 119 while (1)
kocemax 7:cd3cafda3dd4 120 {
kocemax 7:cd3cafda3dd4 121 lcd.clear();
kocemax 7:cd3cafda3dd4 122 lcd.printString("You WON!",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 7:cd3cafda3dd4 127 if (pad.check_event(Gamepad::A_PRESSED) == true)
kocemax 7:cd3cafda3dd4 128 {
kocemax 7:cd3cafda3dd4 129 resetGame();
kocemax 7:cd3cafda3dd4 130 break;
kocemax 7:cd3cafda3dd4 131 }
kocemax 7:cd3cafda3dd4 132 }
kocemax 7:cd3cafda3dd4 133 }
kocemax 7:cd3cafda3dd4 134 }
kocemax 7:cd3cafda3dd4 135 }
kocemax 7:cd3cafda3dd4 136
kocemax 6:39bda45efeed 137 void render()
kocemax 6:39bda45efeed 138 {
kocemax 6:39bda45efeed 139 map.initBricks();
kocemax 6:39bda45efeed 140
kocemax 4:0e01cbb95434 141 while(1)
kocemax 6:39bda45efeed 142 {
kocemax 6:39bda45efeed 143 // physics, input + game logic
kocemax 6:39bda45efeed 144 cont.controlPlayer(pad);
kocemax 7:cd3cafda3dd4 145 cont.get_padPos();
kocemax 7:cd3cafda3dd4 146 ball.getPos();
kocemax 6:39bda45efeed 147 ball.move();
kocemax 7:cd3cafda3dd4 148 ball.hitPad(cont);
kocemax 6:39bda45efeed 149 map.checkCollision(ball);
kocemax 7:cd3cafda3dd4 150 checkLost();
kocemax 7:cd3cafda3dd4 151 checkWon();
kocemax 6:39bda45efeed 152
kocemax 6:39bda45efeed 153 // rendering
kocemax 4:0e01cbb95434 154 lcd.clear(); // initialise the LCD
kocemax 5:12c179da4788 155 map.drawMap(lcd);
kocemax 4:0e01cbb95434 156 cont.drawPlayer(lcd);
kocemax 6:39bda45efeed 157 ball.draw(lcd);
kocemax 4:0e01cbb95434 158 lcd.refresh();
kocemax 6:39bda45efeed 159
kocemax 4:0e01cbb95434 160 wait(1.0f/fps);
kocemax 4:0e01cbb95434 161 }
kocemax 0:2f0362fef062 162 }