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 2:006a2ddfabb6 1 #include "Ball.h"
kocemax 2:006a2ddfabb6 2 #include "PlayerControl.h"
kocemax 2:006a2ddfabb6 3
kocemax 2:006a2ddfabb6 4 // Constructor
kocemax 2:006a2ddfabb6 5 Ball::Ball()
kocemax 2:006a2ddfabb6 6 {
kocemax 7:cd3cafda3dd4 7 reset();
kocemax 2:006a2ddfabb6 8 }
kocemax 2:006a2ddfabb6 9
kocemax 2:006a2ddfabb6 10 // Destructor
kocemax 2:006a2ddfabb6 11 Ball::~Ball()
kocemax 2:006a2ddfabb6 12 {
kocemax 2:006a2ddfabb6 13
kocemax 2:006a2ddfabb6 14 }
kocemax 2:006a2ddfabb6 15
kocemax 6:39bda45efeed 16 //Ball screen edge detection
kocemax 7:cd3cafda3dd4 17 void Ball::move()
kocemax 7:cd3cafda3dd4 18 {
kocemax 6:39bda45efeed 19 GameObject::move();
kocemax 7:cd3cafda3dd4 20
kocemax 7:cd3cafda3dd4 21 if (pos.x > WIDTH-1) {
kocemax 6:39bda45efeed 22 velocity.x = -1;
kocemax 7:cd3cafda3dd4 23 } else if(pos.x < 1) {
kocemax 6:39bda45efeed 24 velocity.x = 1;
kocemax 2:006a2ddfabb6 25 }
kocemax 7:cd3cafda3dd4 26 if (pos.y < 1) {
kocemax 6:39bda45efeed 27 velocity.y = 1;
kocemax 7:cd3cafda3dd4 28 } else if (pos.y > HEIGHT-1) {
kocemax 6:39bda45efeed 29 velocity.y = -1;
kocemax 2:006a2ddfabb6 30 }
kocemax 3:fe856d0890ee 31 }
kocemax 3:fe856d0890ee 32
kocemax 6:39bda45efeed 33 //Pad and Ball collision detection
kocemax 7:cd3cafda3dd4 34 void Ball::hitPad(PlayerControl &pl)
kocemax 3:fe856d0890ee 35 {
kocemax 7:cd3cafda3dd4 36 Vector2D& posPad = pl.get_padPos();
kocemax 7:cd3cafda3dd4 37 if (pos.y == posPad.y - 1 && (pos.x >= posPad.x && pos.x <= posPad.x + 12)) {
kocemax 6:39bda45efeed 38 velocity.y = -1;
kocemax 2:006a2ddfabb6 39 }
kocemax 2:006a2ddfabb6 40
kocemax 4:0e01cbb95434 41
kocemax 6:39bda45efeed 42 }
kocemax 6:39bda45efeed 43
kocemax 6:39bda45efeed 44 int Ball::randomize()
kocemax 4:0e01cbb95434 45 {
kocemax 6:39bda45efeed 46 AnalogIn noisy(PTB0);
kocemax 7:cd3cafda3dd4 47 srand(1000000*noisy.read());
kocemax 7:cd3cafda3dd4 48 int direction = rand() % 2; // randomise initial direction.
kocemax 6:39bda45efeed 49 int movement;
kocemax 7:cd3cafda3dd4 50 if (direction == 0) {
kocemax 6:39bda45efeed 51 movement = -1;
kocemax 7:cd3cafda3dd4 52 } else if (direction == 1) {
kocemax 6:39bda45efeed 53 movement = 1;
kocemax 6:39bda45efeed 54 }
kocemax 7:cd3cafda3dd4 55 return movement;
kocemax 6:39bda45efeed 56 }
kocemax 6:39bda45efeed 57
kocemax 7:cd3cafda3dd4 58 void Ball::reset()
kocemax 6:39bda45efeed 59 {
kocemax 7:cd3cafda3dd4 60 pos.x = WIDTH/2;
kocemax 7:cd3cafda3dd4 61 pos.y = HEIGHT - GAP - 2;
kocemax 7:cd3cafda3dd4 62 velocity.x = randomize();
kocemax 7:cd3cafda3dd4 63 velocity.y = -1;
kocemax 7:cd3cafda3dd4 64 w = 1;
kocemax 7:cd3cafda3dd4 65 h = 1;
kocemax 2:006a2ddfabb6 66 }