Brick Breaker
Dependencies: 4DGL-uLCD-SE mbed-rtos mbed
Game.h@0:099e4258aba4, 2014-10-21 (annotated)
- Committer:
- hotwheelharry
- Date:
- Tue Oct 21 16:14:11 2014 +0000
- Revision:
- 0:099e4258aba4
Brick Breaker! It's okay
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hotwheelharry | 0:099e4258aba4 | 1 | #include <vector> |
hotwheelharry | 0:099e4258aba4 | 2 | #include "Graphics.h" |
hotwheelharry | 0:099e4258aba4 | 3 | #include <cmath> |
hotwheelharry | 0:099e4258aba4 | 4 | #include <string> |
hotwheelharry | 0:099e4258aba4 | 5 | #include <sstream> |
hotwheelharry | 0:099e4258aba4 | 6 | |
hotwheelharry | 0:099e4258aba4 | 7 | |
hotwheelharry | 0:099e4258aba4 | 8 | float frand(){ |
hotwheelharry | 0:099e4258aba4 | 9 | return (float)rand()/(float)RAND_MAX; |
hotwheelharry | 0:099e4258aba4 | 10 | } |
hotwheelharry | 0:099e4258aba4 | 11 | double round(double d) |
hotwheelharry | 0:099e4258aba4 | 12 | { |
hotwheelharry | 0:099e4258aba4 | 13 | return floor(d + 0.5); |
hotwheelharry | 0:099e4258aba4 | 14 | } |
hotwheelharry | 0:099e4258aba4 | 15 | |
hotwheelharry | 0:099e4258aba4 | 16 | class Melody{ |
hotwheelharry | 0:099e4258aba4 | 17 | public: |
hotwheelharry | 0:099e4258aba4 | 18 | std::vector<float> notes; |
hotwheelharry | 0:099e4258aba4 | 19 | }; |
hotwheelharry | 0:099e4258aba4 | 20 | |
hotwheelharry | 0:099e4258aba4 | 21 | class Audio{ |
hotwheelharry | 0:099e4258aba4 | 22 | private: |
hotwheelharry | 0:099e4258aba4 | 23 | Mutex mutex; |
hotwheelharry | 0:099e4258aba4 | 24 | AnalogOut& audio; |
hotwheelharry | 0:099e4258aba4 | 25 | public: |
hotwheelharry | 0:099e4258aba4 | 26 | Audio(AnalogOut& ao) : audio(ao) |
hotwheelharry | 0:099e4258aba4 | 27 | { |
hotwheelharry | 0:099e4258aba4 | 28 | } |
hotwheelharry | 0:099e4258aba4 | 29 | |
hotwheelharry | 0:099e4258aba4 | 30 | void playSong(Melody& s){ |
hotwheelharry | 0:099e4258aba4 | 31 | mutex.lock(); |
hotwheelharry | 0:099e4258aba4 | 32 | |
hotwheelharry | 0:099e4258aba4 | 33 | mutex.unlock(); |
hotwheelharry | 0:099e4258aba4 | 34 | } |
hotwheelharry | 0:099e4258aba4 | 35 | void triggerShot(){ |
hotwheelharry | 0:099e4258aba4 | 36 | mutex.lock(); |
hotwheelharry | 0:099e4258aba4 | 37 | |
hotwheelharry | 0:099e4258aba4 | 38 | mutex.unlock(); |
hotwheelharry | 0:099e4258aba4 | 39 | } |
hotwheelharry | 0:099e4258aba4 | 40 | void run(){ |
hotwheelharry | 0:099e4258aba4 | 41 | while(true){ |
hotwheelharry | 0:099e4258aba4 | 42 | |
hotwheelharry | 0:099e4258aba4 | 43 | Thread::wait(100); //ms |
hotwheelharry | 0:099e4258aba4 | 44 | } |
hotwheelharry | 0:099e4258aba4 | 45 | } |
hotwheelharry | 0:099e4258aba4 | 46 | }; |
hotwheelharry | 0:099e4258aba4 | 47 | |
hotwheelharry | 0:099e4258aba4 | 48 | class Controller{ |
hotwheelharry | 0:099e4258aba4 | 49 | public: |
hotwheelharry | 0:099e4258aba4 | 50 | |
hotwheelharry | 0:099e4258aba4 | 51 | AnalogIn& sliderh; |
hotwheelharry | 0:099e4258aba4 | 52 | AnalogIn& sliderv; |
hotwheelharry | 0:099e4258aba4 | 53 | DigitalIn& button; |
hotwheelharry | 0:099e4258aba4 | 54 | public: |
hotwheelharry | 0:099e4258aba4 | 55 | Controller(AnalogIn& sliderh, AnalogIn& sliderv, DigitalIn& btn) : sliderh(sliderh), sliderv(sliderv), button(btn) |
hotwheelharry | 0:099e4258aba4 | 56 | { |
hotwheelharry | 0:099e4258aba4 | 57 | |
hotwheelharry | 0:099e4258aba4 | 58 | } |
hotwheelharry | 0:099e4258aba4 | 59 | }; |
hotwheelharry | 0:099e4258aba4 | 60 | |
hotwheelharry | 0:099e4258aba4 | 61 | |
hotwheelharry | 0:099e4258aba4 | 62 | |
hotwheelharry | 0:099e4258aba4 | 63 | class Physics{ |
hotwheelharry | 0:099e4258aba4 | 64 | private: |
hotwheelharry | 0:099e4258aba4 | 65 | |
hotwheelharry | 0:099e4258aba4 | 66 | public: |
hotwheelharry | 0:099e4258aba4 | 67 | float dt; |
hotwheelharry | 0:099e4258aba4 | 68 | |
hotwheelharry | 0:099e4258aba4 | 69 | void step(Scene& scene){ |
hotwheelharry | 0:099e4258aba4 | 70 | Block& b = scene.ball; |
hotwheelharry | 0:099e4258aba4 | 71 | |
hotwheelharry | 0:099e4258aba4 | 72 | if(b.locked) |
hotwheelharry | 0:099e4258aba4 | 73 | { |
hotwheelharry | 0:099e4258aba4 | 74 | b.store_prev(); |
hotwheelharry | 0:099e4258aba4 | 75 | b.updated = true; |
hotwheelharry | 0:099e4258aba4 | 76 | b.next_coord.x = scene.paddle.coord.x + scene.paddle.size.x/2.0 - b.size.x/2.0; |
hotwheelharry | 0:099e4258aba4 | 77 | b.next_coord.y = scene.paddle.coord.y - b.size.y - 1; |
hotwheelharry | 0:099e4258aba4 | 78 | b.next_velocity.x = 0; |
hotwheelharry | 0:099e4258aba4 | 79 | b.next_velocity.y = 0; |
hotwheelharry | 0:099e4258aba4 | 80 | |
hotwheelharry | 0:099e4258aba4 | 81 | b.next_swap(); |
hotwheelharry | 0:099e4258aba4 | 82 | return; |
hotwheelharry | 0:099e4258aba4 | 83 | } |
hotwheelharry | 0:099e4258aba4 | 84 | |
hotwheelharry | 0:099e4258aba4 | 85 | |
hotwheelharry | 0:099e4258aba4 | 86 | //move things |
hotwheelharry | 0:099e4258aba4 | 87 | if(b.velocity.x != 0 || b.velocity.y != 0) |
hotwheelharry | 0:099e4258aba4 | 88 | { |
hotwheelharry | 0:099e4258aba4 | 89 | b.store_prev(); |
hotwheelharry | 0:099e4258aba4 | 90 | b.updated = true; |
hotwheelharry | 0:099e4258aba4 | 91 | b.next_coord.x = b.coord.x + dt * b.velocity.x; |
hotwheelharry | 0:099e4258aba4 | 92 | b.next_coord.y = b.coord.y + dt * b.velocity.y; |
hotwheelharry | 0:099e4258aba4 | 93 | |
hotwheelharry | 0:099e4258aba4 | 94 | b.next_velocity.x = b.velocity.x; |
hotwheelharry | 0:099e4258aba4 | 95 | b.next_velocity.y = b.velocity.y; |
hotwheelharry | 0:099e4258aba4 | 96 | } |
hotwheelharry | 0:099e4258aba4 | 97 | |
hotwheelharry | 0:099e4258aba4 | 98 | if( b.coord.x < 0) |
hotwheelharry | 0:099e4258aba4 | 99 | { |
hotwheelharry | 0:099e4258aba4 | 100 | b.next_coord.x = 0; |
hotwheelharry | 0:099e4258aba4 | 101 | b.next_velocity.x = b.velocity.x * -1.0; |
hotwheelharry | 0:099e4258aba4 | 102 | } |
hotwheelharry | 0:099e4258aba4 | 103 | if( b.coord.x + b.size.x > 128){ |
hotwheelharry | 0:099e4258aba4 | 104 | b.next_coord.x = 128-b.size.x; |
hotwheelharry | 0:099e4258aba4 | 105 | b.next_velocity.x = b.velocity.x * -1.0; |
hotwheelharry | 0:099e4258aba4 | 106 | } |
hotwheelharry | 0:099e4258aba4 | 107 | |
hotwheelharry | 0:099e4258aba4 | 108 | if(b.coord.y < 0) |
hotwheelharry | 0:099e4258aba4 | 109 | { |
hotwheelharry | 0:099e4258aba4 | 110 | b.next_coord.y = 0; |
hotwheelharry | 0:099e4258aba4 | 111 | b.next_velocity.y = b.velocity.y * -1.0; |
hotwheelharry | 0:099e4258aba4 | 112 | } |
hotwheelharry | 0:099e4258aba4 | 113 | |
hotwheelharry | 0:099e4258aba4 | 114 | //collision with blocks |
hotwheelharry | 0:099e4258aba4 | 115 | int hitcount = 0; |
hotwheelharry | 0:099e4258aba4 | 116 | for(std::list<Block>::iterator j = scene.blocks.begin(); j != scene.blocks.end(); j++){ |
hotwheelharry | 0:099e4258aba4 | 117 | if(j->color == 0x444444){ |
hotwheelharry | 0:099e4258aba4 | 118 | hitcount++; |
hotwheelharry | 0:099e4258aba4 | 119 | } |
hotwheelharry | 0:099e4258aba4 | 120 | if( j->interactive && b.touching( *j ) ){ |
hotwheelharry | 0:099e4258aba4 | 121 | bool h = false; |
hotwheelharry | 0:099e4258aba4 | 122 | bool v = false; |
hotwheelharry | 0:099e4258aba4 | 123 | |
hotwheelharry | 0:099e4258aba4 | 124 | //from right |
hotwheelharry | 0:099e4258aba4 | 125 | if( b.coord.x > j->coord.x && b.coord.x < (j->coord.x + j->size.x) ){ |
hotwheelharry | 0:099e4258aba4 | 126 | b.next_coord.x = j->coord.x + j->size.x; |
hotwheelharry | 0:099e4258aba4 | 127 | b.next_velocity.x = b.next_velocity.x * -1.0; |
hotwheelharry | 0:099e4258aba4 | 128 | h = true; |
hotwheelharry | 0:099e4258aba4 | 129 | } |
hotwheelharry | 0:099e4258aba4 | 130 | //from left |
hotwheelharry | 0:099e4258aba4 | 131 | if(b.coord.x + b.size.x > j->coord.x && b.coord.x < (j->coord.x) ){ |
hotwheelharry | 0:099e4258aba4 | 132 | b.next_coord.x = j->coord.x - b.size.x; |
hotwheelharry | 0:099e4258aba4 | 133 | b.next_velocity.x = b.next_velocity.x * -1.0; |
hotwheelharry | 0:099e4258aba4 | 134 | h = true; |
hotwheelharry | 0:099e4258aba4 | 135 | } |
hotwheelharry | 0:099e4258aba4 | 136 | |
hotwheelharry | 0:099e4258aba4 | 137 | // from top |
hotwheelharry | 0:099e4258aba4 | 138 | if( b.coord.y + b.size.y > j->coord.y && b.coord.y < (j->coord.y) ){ |
hotwheelharry | 0:099e4258aba4 | 139 | b.next_coord.y = (j->coord.y - b.size.y); |
hotwheelharry | 0:099e4258aba4 | 140 | b.next_velocity.y = b.next_velocity.y * -1.0; |
hotwheelharry | 0:099e4258aba4 | 141 | b.next_coord.x = b.coord.x; |
hotwheelharry | 0:099e4258aba4 | 142 | v = true; |
hotwheelharry | 0:099e4258aba4 | 143 | } |
hotwheelharry | 0:099e4258aba4 | 144 | //from bottom |
hotwheelharry | 0:099e4258aba4 | 145 | if( b.coord.y < j->coord.y + j->size.y && b.coord.y + b.size.y > (j->coord.y) ){ |
hotwheelharry | 0:099e4258aba4 | 146 | b.next_coord.y = (j->coord.y + j->size.y); |
hotwheelharry | 0:099e4258aba4 | 147 | b.next_velocity.y = b.next_velocity.y * -1.0; |
hotwheelharry | 0:099e4258aba4 | 148 | b.next_coord.x = b.coord.x; |
hotwheelharry | 0:099e4258aba4 | 149 | v = true; |
hotwheelharry | 0:099e4258aba4 | 150 | } |
hotwheelharry | 0:099e4258aba4 | 151 | |
hotwheelharry | 0:099e4258aba4 | 152 | if(h || v){ |
hotwheelharry | 0:099e4258aba4 | 153 | |
hotwheelharry | 0:099e4258aba4 | 154 | j->color = 0x444444; |
hotwheelharry | 0:099e4258aba4 | 155 | j->updated = true; |
hotwheelharry | 0:099e4258aba4 | 156 | j->interactive = false; |
hotwheelharry | 0:099e4258aba4 | 157 | } |
hotwheelharry | 0:099e4258aba4 | 158 | |
hotwheelharry | 0:099e4258aba4 | 159 | |
hotwheelharry | 0:099e4258aba4 | 160 | |
hotwheelharry | 0:099e4258aba4 | 161 | |
hotwheelharry | 0:099e4258aba4 | 162 | |
hotwheelharry | 0:099e4258aba4 | 163 | |
hotwheelharry | 0:099e4258aba4 | 164 | scene.score++; |
hotwheelharry | 0:099e4258aba4 | 165 | std::stringstream ss; |
hotwheelharry | 0:099e4258aba4 | 166 | ss << "Score:" << scene.score; |
hotwheelharry | 0:099e4258aba4 | 167 | scene.text[0].message = ss.str(); |
hotwheelharry | 0:099e4258aba4 | 168 | scene.text[0].updated = true; |
hotwheelharry | 0:099e4258aba4 | 169 | |
hotwheelharry | 0:099e4258aba4 | 170 | } |
hotwheelharry | 0:099e4258aba4 | 171 | else{ |
hotwheelharry | 0:099e4258aba4 | 172 | b.color = WHITE; |
hotwheelharry | 0:099e4258aba4 | 173 | } |
hotwheelharry | 0:099e4258aba4 | 174 | } |
hotwheelharry | 0:099e4258aba4 | 175 | |
hotwheelharry | 0:099e4258aba4 | 176 | if(hitcount == scene.blocks.size()){ |
hotwheelharry | 0:099e4258aba4 | 177 | scene.beat = true; |
hotwheelharry | 0:099e4258aba4 | 178 | } |
hotwheelharry | 0:099e4258aba4 | 179 | |
hotwheelharry | 0:099e4258aba4 | 180 | |
hotwheelharry | 0:099e4258aba4 | 181 | |
hotwheelharry | 0:099e4258aba4 | 182 | //collision with paddle |
hotwheelharry | 0:099e4258aba4 | 183 | if(b.next_coord.y + b.size.y > scene.paddle.coord.y && b.next_coord.y - b.velocity.y < scene.paddle.coord.y |
hotwheelharry | 0:099e4258aba4 | 184 | && (b.next_coord.x + b.size.x > scene.paddle.coord.x && b.next_coord.x < scene.paddle.coord.x + scene.paddle.size.x)){ |
hotwheelharry | 0:099e4258aba4 | 185 | if(!b.locked){ |
hotwheelharry | 0:099e4258aba4 | 186 | float pmid = scene.paddle.coord.x + scene.paddle.size.x/2.0; |
hotwheelharry | 0:099e4258aba4 | 187 | float bmid = b.next_coord.x + b.size.x/2.0; |
hotwheelharry | 0:099e4258aba4 | 188 | float d = bmid - pmid; |
hotwheelharry | 0:099e4258aba4 | 189 | b.next_velocity.x = b.velocity.x + d/(scene.paddle.size.x/2.0) * 30.0; |
hotwheelharry | 0:099e4258aba4 | 190 | if(b.next_velocity.x > 50) |
hotwheelharry | 0:099e4258aba4 | 191 | { b.next_velocity.x = 50; } |
hotwheelharry | 0:099e4258aba4 | 192 | if(b.next_velocity.x < -50) |
hotwheelharry | 0:099e4258aba4 | 193 | { b.next_velocity.x = -50;} |
hotwheelharry | 0:099e4258aba4 | 194 | |
hotwheelharry | 0:099e4258aba4 | 195 | b.next_velocity.y = (b.velocity.y * -1.0) * 1.01; |
hotwheelharry | 0:099e4258aba4 | 196 | b.next_coord.x = b.coord.x; |
hotwheelharry | 0:099e4258aba4 | 197 | b.next_coord.y = scene.paddle.next_coord.y - b.size.y - 1; |
hotwheelharry | 0:099e4258aba4 | 198 | } |
hotwheelharry | 0:099e4258aba4 | 199 | } |
hotwheelharry | 0:099e4258aba4 | 200 | |
hotwheelharry | 0:099e4258aba4 | 201 | |
hotwheelharry | 0:099e4258aba4 | 202 | //update all the new positions |
hotwheelharry | 0:099e4258aba4 | 203 | b.next_swap(); |
hotwheelharry | 0:099e4258aba4 | 204 | |
hotwheelharry | 0:099e4258aba4 | 205 | }; |
hotwheelharry | 0:099e4258aba4 | 206 | |
hotwheelharry | 0:099e4258aba4 | 207 | }; |
hotwheelharry | 0:099e4258aba4 | 208 | |
hotwheelharry | 0:099e4258aba4 | 209 | |
hotwheelharry | 0:099e4258aba4 | 210 | class Game{ |
hotwheelharry | 0:099e4258aba4 | 211 | private: |
hotwheelharry | 0:099e4258aba4 | 212 | Renderer& renderer; |
hotwheelharry | 0:099e4258aba4 | 213 | Audio& audio; |
hotwheelharry | 0:099e4258aba4 | 214 | Controller& controller; |
hotwheelharry | 0:099e4258aba4 | 215 | |
hotwheelharry | 0:099e4258aba4 | 216 | Physics physics; |
hotwheelharry | 0:099e4258aba4 | 217 | Scene scene; |
hotwheelharry | 0:099e4258aba4 | 218 | |
hotwheelharry | 0:099e4258aba4 | 219 | int level; |
hotwheelharry | 0:099e4258aba4 | 220 | |
hotwheelharry | 0:099e4258aba4 | 221 | Scene menu; |
hotwheelharry | 0:099e4258aba4 | 222 | |
hotwheelharry | 0:099e4258aba4 | 223 | public: |
hotwheelharry | 0:099e4258aba4 | 224 | Game(Renderer& renderer, Audio& aud, Controller& ctrl) |
hotwheelharry | 0:099e4258aba4 | 225 | : renderer(renderer), audio(aud), controller(ctrl) |
hotwheelharry | 0:099e4258aba4 | 226 | { |
hotwheelharry | 0:099e4258aba4 | 227 | level = 0; |
hotwheelharry | 0:099e4258aba4 | 228 | scene.bg = Color((int)renderer.background); |
hotwheelharry | 0:099e4258aba4 | 229 | |
hotwheelharry | 0:099e4258aba4 | 230 | buildScene(); |
hotwheelharry | 0:099e4258aba4 | 231 | physics.dt = 0.1; |
hotwheelharry | 0:099e4258aba4 | 232 | } |
hotwheelharry | 0:099e4258aba4 | 233 | |
hotwheelharry | 0:099e4258aba4 | 234 | private: |
hotwheelharry | 0:099e4258aba4 | 235 | void buildMenu(){ |
hotwheelharry | 0:099e4258aba4 | 236 | |
hotwheelharry | 0:099e4258aba4 | 237 | } |
hotwheelharry | 0:099e4258aba4 | 238 | void buildScene(){ |
hotwheelharry | 0:099e4258aba4 | 239 | |
hotwheelharry | 0:099e4258aba4 | 240 | scene.lives = 3; |
hotwheelharry | 0:099e4258aba4 | 241 | scene.score = 0; |
hotwheelharry | 0:099e4258aba4 | 242 | scene.beat = false; |
hotwheelharry | 0:099e4258aba4 | 243 | |
hotwheelharry | 0:099e4258aba4 | 244 | // build bricks |
hotwheelharry | 0:099e4258aba4 | 245 | float vertical = 10; |
hotwheelharry | 0:099e4258aba4 | 246 | float spacing = 2; |
hotwheelharry | 0:099e4258aba4 | 247 | vec2 bgrid = {5,3}; |
hotwheelharry | 0:099e4258aba4 | 248 | vec2 bsize; |
hotwheelharry | 0:099e4258aba4 | 249 | bsize.x = (renderer.screen.x-spacing) / (bgrid.x) - 2; |
hotwheelharry | 0:099e4258aba4 | 250 | bsize.y = 4; |
hotwheelharry | 0:099e4258aba4 | 251 | |
hotwheelharry | 0:099e4258aba4 | 252 | int colorscheme = level; |
hotwheelharry | 0:099e4258aba4 | 253 | |
hotwheelharry | 0:099e4258aba4 | 254 | for(int i = 0; i < bgrid.x; i++){ |
hotwheelharry | 0:099e4258aba4 | 255 | for(int j = 0; j < bgrid.y; j++){ |
hotwheelharry | 0:099e4258aba4 | 256 | Block b; |
hotwheelharry | 0:099e4258aba4 | 257 | b.velocity.x = 0; |
hotwheelharry | 0:099e4258aba4 | 258 | b.velocity.y = 0; |
hotwheelharry | 0:099e4258aba4 | 259 | b.size = bsize; |
hotwheelharry | 0:099e4258aba4 | 260 | b.coord.x = spacing + (bsize.x + spacing)*i; |
hotwheelharry | 0:099e4258aba4 | 261 | b.coord.y = spacing + (bsize.y + spacing)*j + vertical; |
hotwheelharry | 0:099e4258aba4 | 262 | b.copy(); |
hotwheelharry | 0:099e4258aba4 | 263 | b.color = RED; |
hotwheelharry | 0:099e4258aba4 | 264 | switch(colorscheme){ |
hotwheelharry | 0:099e4258aba4 | 265 | case 0: b.color = Color(0x66+round(0x99*((float)(bgrid.y-j)/(float)bgrid.y)),0,0); |
hotwheelharry | 0:099e4258aba4 | 266 | break; |
hotwheelharry | 0:099e4258aba4 | 267 | case 1: b.color = Color(0,0x66+round(0x99*((float)(bgrid.y-j)/(float)bgrid.y)), 0); |
hotwheelharry | 0:099e4258aba4 | 268 | break; |
hotwheelharry | 0:099e4258aba4 | 269 | case 2: b.color = Color(0,0,0x66+round(0x99*((float)(bgrid.y-j)/(float)bgrid.y))); |
hotwheelharry | 0:099e4258aba4 | 270 | break; |
hotwheelharry | 0:099e4258aba4 | 271 | case 3: b.color = Color(round(0xff*(frand())), |
hotwheelharry | 0:099e4258aba4 | 272 | round(0xff*(frand())), |
hotwheelharry | 0:099e4258aba4 | 273 | round(0xff*(frand())) |
hotwheelharry | 0:099e4258aba4 | 274 | ); |
hotwheelharry | 0:099e4258aba4 | 275 | break; |
hotwheelharry | 0:099e4258aba4 | 276 | |
hotwheelharry | 0:099e4258aba4 | 277 | default: b.color = RED; |
hotwheelharry | 0:099e4258aba4 | 278 | } |
hotwheelharry | 0:099e4258aba4 | 279 | scene.blocks.push_back(b); |
hotwheelharry | 0:099e4258aba4 | 280 | } |
hotwheelharry | 0:099e4258aba4 | 281 | } |
hotwheelharry | 0:099e4258aba4 | 282 | |
hotwheelharry | 0:099e4258aba4 | 283 | Block pad; |
hotwheelharry | 0:099e4258aba4 | 284 | pad.locked = true; |
hotwheelharry | 0:099e4258aba4 | 285 | pad.size.x = renderer.screen.x / 4.0; |
hotwheelharry | 0:099e4258aba4 | 286 | pad.size.y = 4; |
hotwheelharry | 0:099e4258aba4 | 287 | pad.coord.x = renderer.screen.x / 4.0 + pad.size.x / 2.0; |
hotwheelharry | 0:099e4258aba4 | 288 | pad.coord.y = renderer.screen.y - spacing - pad.size.y; |
hotwheelharry | 0:099e4258aba4 | 289 | pad.copy(); |
hotwheelharry | 0:099e4258aba4 | 290 | pad.color = WHITE; |
hotwheelharry | 0:099e4258aba4 | 291 | scene.paddle = pad; |
hotwheelharry | 0:099e4258aba4 | 292 | |
hotwheelharry | 0:099e4258aba4 | 293 | |
hotwheelharry | 0:099e4258aba4 | 294 | Block b; |
hotwheelharry | 0:099e4258aba4 | 295 | b.size.x = 4; |
hotwheelharry | 0:099e4258aba4 | 296 | b.size.y = 4; |
hotwheelharry | 0:099e4258aba4 | 297 | b.coord.x = pad.coord.x + pad.size.x/2.0 - b.size.x/2.0; |
hotwheelharry | 0:099e4258aba4 | 298 | b.coord.y = pad.coord.y - b.size.y - 1; |
hotwheelharry | 0:099e4258aba4 | 299 | b.color = WHITE; |
hotwheelharry | 0:099e4258aba4 | 300 | b.locked = true; |
hotwheelharry | 0:099e4258aba4 | 301 | b.copy(); |
hotwheelharry | 0:099e4258aba4 | 302 | scene.ball = b; |
hotwheelharry | 0:099e4258aba4 | 303 | |
hotwheelharry | 0:099e4258aba4 | 304 | Text tscore; |
hotwheelharry | 0:099e4258aba4 | 305 | tscore.coord.x = renderer.screen.x / 6 - 12; |
hotwheelharry | 0:099e4258aba4 | 306 | tscore.coord.y = 0; |
hotwheelharry | 0:099e4258aba4 | 307 | tscore.font = FONT_7X8; |
hotwheelharry | 0:099e4258aba4 | 308 | tscore.color = WHITE; |
hotwheelharry | 0:099e4258aba4 | 309 | tscore.message = "Score:0"; |
hotwheelharry | 0:099e4258aba4 | 310 | scene.text.push_back(tscore); |
hotwheelharry | 0:099e4258aba4 | 311 | |
hotwheelharry | 0:099e4258aba4 | 312 | Text tlives; |
hotwheelharry | 0:099e4258aba4 | 313 | tlives.coord.x = 0; |
hotwheelharry | 0:099e4258aba4 | 314 | tlives.coord.y = 0; |
hotwheelharry | 0:099e4258aba4 | 315 | tlives.font = FONT_7X8; |
hotwheelharry | 0:099e4258aba4 | 316 | tlives.color = WHITE; |
hotwheelharry | 0:099e4258aba4 | 317 | tlives.message = "Lives:3"; |
hotwheelharry | 0:099e4258aba4 | 318 | scene.text.push_back(tlives); |
hotwheelharry | 0:099e4258aba4 | 319 | } |
hotwheelharry | 0:099e4258aba4 | 320 | void getInput(){ |
hotwheelharry | 0:099e4258aba4 | 321 | float pos = controller.sliderh; |
hotwheelharry | 0:099e4258aba4 | 322 | scene.paddle.store_prev(); |
hotwheelharry | 0:099e4258aba4 | 323 | scene.paddle.coord.x = (renderer.screen.x - scene.paddle.size.x - 4) * pos + 2; |
hotwheelharry | 0:099e4258aba4 | 324 | scene.paddle.updated = true; |
hotwheelharry | 0:099e4258aba4 | 325 | |
hotwheelharry | 0:099e4258aba4 | 326 | Block& b = scene.ball; |
hotwheelharry | 0:099e4258aba4 | 327 | if(b.locked) |
hotwheelharry | 0:099e4258aba4 | 328 | { |
hotwheelharry | 0:099e4258aba4 | 329 | if(controller.button ){ |
hotwheelharry | 0:099e4258aba4 | 330 | b.locked = false; |
hotwheelharry | 0:099e4258aba4 | 331 | b.velocity.y = -80.0; |
hotwheelharry | 0:099e4258aba4 | 332 | } |
hotwheelharry | 0:099e4258aba4 | 333 | } |
hotwheelharry | 0:099e4258aba4 | 334 | } |
hotwheelharry | 0:099e4258aba4 | 335 | void gameLogic(){ |
hotwheelharry | 0:099e4258aba4 | 336 | if(scene.ball.coord.y > renderer.screen.y){ |
hotwheelharry | 0:099e4258aba4 | 337 | scene.lives--; |
hotwheelharry | 0:099e4258aba4 | 338 | std::stringstream ss; |
hotwheelharry | 0:099e4258aba4 | 339 | ss << "Lives:" << scene.lives; |
hotwheelharry | 0:099e4258aba4 | 340 | scene.text[1].message = ss.str(); |
hotwheelharry | 0:099e4258aba4 | 341 | scene.text[1].updated = true; |
hotwheelharry | 0:099e4258aba4 | 342 | |
hotwheelharry | 0:099e4258aba4 | 343 | scene.ball.locked = true; |
hotwheelharry | 0:099e4258aba4 | 344 | } |
hotwheelharry | 0:099e4258aba4 | 345 | |
hotwheelharry | 0:099e4258aba4 | 346 | if(scene.lives == 0){ |
hotwheelharry | 0:099e4258aba4 | 347 | scene = Scene(); |
hotwheelharry | 0:099e4258aba4 | 348 | level = 0; |
hotwheelharry | 0:099e4258aba4 | 349 | buildScene(); |
hotwheelharry | 0:099e4258aba4 | 350 | |
hotwheelharry | 0:099e4258aba4 | 351 | renderer.g.filled_rectangle(0, 0, 128, 10, renderer.background); |
hotwheelharry | 0:099e4258aba4 | 352 | } |
hotwheelharry | 0:099e4258aba4 | 353 | |
hotwheelharry | 0:099e4258aba4 | 354 | if(scene.beat){ |
hotwheelharry | 0:099e4258aba4 | 355 | level++; |
hotwheelharry | 0:099e4258aba4 | 356 | scene.ball.store_prev(); |
hotwheelharry | 0:099e4258aba4 | 357 | Block cb = scene.ball; |
hotwheelharry | 0:099e4258aba4 | 358 | |
hotwheelharry | 0:099e4258aba4 | 359 | int score = scene.score; |
hotwheelharry | 0:099e4258aba4 | 360 | scene = Scene(); |
hotwheelharry | 0:099e4258aba4 | 361 | buildScene(); |
hotwheelharry | 0:099e4258aba4 | 362 | scene.score = score; |
hotwheelharry | 0:099e4258aba4 | 363 | std::stringstream ss; |
hotwheelharry | 0:099e4258aba4 | 364 | ss << "Score:" << scene.score; |
hotwheelharry | 0:099e4258aba4 | 365 | scene.text[0].message = ss.str(); |
hotwheelharry | 0:099e4258aba4 | 366 | scene.text[0].updated = true; |
hotwheelharry | 0:099e4258aba4 | 367 | |
hotwheelharry | 0:099e4258aba4 | 368 | renderer.g.filled_rectangle(cb.coord.x, cb.coord.y, cb.coord.x+cb.size.x, cb.coord.y+cb.size.y, renderer.background); |
hotwheelharry | 0:099e4258aba4 | 369 | renderer.g.filled_rectangle(0, 0, 128, 10, renderer.background); |
hotwheelharry | 0:099e4258aba4 | 370 | |
hotwheelharry | 0:099e4258aba4 | 371 | } |
hotwheelharry | 0:099e4258aba4 | 372 | } |
hotwheelharry | 0:099e4258aba4 | 373 | public: |
hotwheelharry | 0:099e4258aba4 | 374 | void loop(){ |
hotwheelharry | 0:099e4258aba4 | 375 | getInput(); |
hotwheelharry | 0:099e4258aba4 | 376 | gameLogic(); |
hotwheelharry | 0:099e4258aba4 | 377 | physics.step(scene); |
hotwheelharry | 0:099e4258aba4 | 378 | renderer.render(scene); |
hotwheelharry | 0:099e4258aba4 | 379 | } |
hotwheelharry | 0:099e4258aba4 | 380 | |
hotwheelharry | 0:099e4258aba4 | 381 | }; |
hotwheelharry | 0:099e4258aba4 | 382 |