ELEC2645 (2018/19) / Mbed 2 deprecated el17szs

Dependencies:   mbed

Committer:
shahidsajid
Date:
Thu Apr 18 13:04:37 2019 +0000
Revision:
7:a1a6bff238c1
Parent:
6:3e50f2cf4366
Child:
8:7b7e1a5b8200
Child:
9:a81db6a703b7
Bowler created

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shahidsajid 3:bd3465a70a5a 1 #include "Ball.h"
shahidsajid 3:bd3465a70a5a 2
shahidsajid 4:55a0509c4874 3
shahidsajid 3:bd3465a70a5a 4 Ball::Ball()
shahidsajid 3:bd3465a70a5a 5 {
shahidsajid 3:bd3465a70a5a 6
shahidsajid 3:bd3465a70a5a 7 }
shahidsajid 3:bd3465a70a5a 8
shahidsajid 3:bd3465a70a5a 9 Ball::~Ball()
shahidsajid 3:bd3465a70a5a 10 {
shahidsajid 3:bd3465a70a5a 11
shahidsajid 3:bd3465a70a5a 12 }
shahidsajid 3:bd3465a70a5a 13
shahidsajid 3:bd3465a70a5a 14 void Ball::init(int size,int speed)
shahidsajid 3:bd3465a70a5a 15 {
shahidsajid 3:bd3465a70a5a 16 _size = size;
shahidsajid 7:a1a6bff238c1 17 _x1=42;
shahidsajid 7:a1a6bff238c1 18 _y1=3;
shahidsajid 3:bd3465a70a5a 19 _x = 42;
shahidsajid 3:bd3465a70a5a 20 _y = 15;
shahidsajid 6:3e50f2cf4366 21 bat.init(3,4);
shahidsajid 3:bd3465a70a5a 22
shahidsajid 3:bd3465a70a5a 23 srand(time(NULL));
shahidsajid 3:bd3465a70a5a 24 int direction = rand() % 4; // randomise initial direction.
shahidsajid 3:bd3465a70a5a 25
shahidsajid 3:bd3465a70a5a 26 // 4 possibilities. Get random modulo and set velocities accordingly
shahidsajid 3:bd3465a70a5a 27 if (direction == 0) {
shahidsajid 3:bd3465a70a5a 28 _velocity.x = speed;
shahidsajid 3:bd3465a70a5a 29 _velocity.y = speed;
shahidsajid 3:bd3465a70a5a 30 } else if (direction == 1) {
shahidsajid 3:bd3465a70a5a 31 _velocity.x = speed;
shahidsajid 3:bd3465a70a5a 32 _velocity.y = -speed;
shahidsajid 3:bd3465a70a5a 33 } else if (direction == 2) {
shahidsajid 3:bd3465a70a5a 34 _velocity.x = speed;
shahidsajid 3:bd3465a70a5a 35 _velocity.y = speed;
shahidsajid 3:bd3465a70a5a 36 } else {
shahidsajid 3:bd3465a70a5a 37 _velocity.x = -speed;
shahidsajid 3:bd3465a70a5a 38 _velocity.y = -speed;
shahidsajid 3:bd3465a70a5a 39 }
shahidsajid 3:bd3465a70a5a 40 }
shahidsajid 7:a1a6bff238c1 41 int Ball::bowler_start(){
shahidsajid 7:a1a6bff238c1 42 if(_y1!=15){
shahidsajid 7:a1a6bff238c1 43 _y1++;
shahidsajid 7:a1a6bff238c1 44 return 0;
shahidsajid 7:a1a6bff238c1 45 }
shahidsajid 7:a1a6bff238c1 46 else {
shahidsajid 7:a1a6bff238c1 47 return 1;
shahidsajid 7:a1a6bff238c1 48 }
shahidsajid 7:a1a6bff238c1 49 }
shahidsajid 7:a1a6bff238c1 50 void Ball::start(N5110 &lcd,int checkHit, Direction dir){
shahidsajid 7:a1a6bff238c1 51 int ball_start_check=bowler_start();
shahidsajid 7:a1a6bff238c1 52 if (ball_start_check==1){
shahidsajid 7:a1a6bff238c1 53 printf("HELLO\n");
shahidsajid 7:a1a6bff238c1 54 if (bowled==1){
shahidsajid 7:a1a6bff238c1 55 update_ball(checkHit, dir);
shahidsajid 7:a1a6bff238c1 56 }
shahidsajid 7:a1a6bff238c1 57 else if (_y!=36){
shahidsajid 7:a1a6bff238c1 58 _y+=1;
shahidsajid 7:a1a6bff238c1 59 }
shahidsajid 7:a1a6bff238c1 60 else {
shahidsajid 7:a1a6bff238c1 61 bowled=1;
shahidsajid 7:a1a6bff238c1 62 }
shahidsajid 7:a1a6bff238c1 63 }
shahidsajid 7:a1a6bff238c1 64 }
shahidsajid 7:a1a6bff238c1 65 void Ball::draw(N5110 &lcd)
shahidsajid 7:a1a6bff238c1 66 {
shahidsajid 7:a1a6bff238c1 67 lcd.drawCircle(_x1,_y1,2,FILL_BLACK);
shahidsajid 7:a1a6bff238c1 68 lcd.drawCircle(_x,_y,1,FILL_BLACK);
shahidsajid 7:a1a6bff238c1 69 //lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
shahidsajid 7:a1a6bff238c1 70 }
shahidsajid 7:a1a6bff238c1 71 void Ball::update_ball(int checkHit, Direction dir){
shahidsajid 7:a1a6bff238c1 72 if (ballHit!=1 && checkHit==1){
shahidsajid 7:a1a6bff238c1 73 ballHit=1;
shahidsajid 7:a1a6bff238c1 74 }
shahidsajid 7:a1a6bff238c1 75 if(bowled==1 && ballHit==1){
shahidsajid 6:3e50f2cf4366 76 printf("TEST\n");
shahidsajid 6:3e50f2cf4366 77 if (dir==NE){
shahidsajid 6:3e50f2cf4366 78 printf("x=%i y=%i \n",_x,_y);
shahidsajid 6:3e50f2cf4366 79 if (!(_y == 2 )){
shahidsajid 6:3e50f2cf4366 80 _y--;
shahidsajid 6:3e50f2cf4366 81 }
shahidsajid 6:3e50f2cf4366 82 if (_x!=65){
shahidsajid 6:3e50f2cf4366 83 _x++;
shahidsajid 6:3e50f2cf4366 84 }
shahidsajid 6:3e50f2cf4366 85 printf("HERE");
shahidsajid 6:3e50f2cf4366 86 }
shahidsajid 6:3e50f2cf4366 87
shahidsajid 6:3e50f2cf4366 88 }
shahidsajid 7:a1a6bff238c1 89
shahidsajid 3:bd3465a70a5a 90 }
shahidsajid 3:bd3465a70a5a 91
shahidsajid 3:bd3465a70a5a 92 void Ball::update()
shahidsajid 3:bd3465a70a5a 93 {
shahidsajid 3:bd3465a70a5a 94 _x += _velocity.x;
shahidsajid 3:bd3465a70a5a 95 _y += _velocity.y;
shahidsajid 3:bd3465a70a5a 96 }
shahidsajid 3:bd3465a70a5a 97
shahidsajid 3:bd3465a70a5a 98 void Ball::set_velocity(Vector2D v)
shahidsajid 3:bd3465a70a5a 99 {
shahidsajid 3:bd3465a70a5a 100 _velocity.x = v.x;
shahidsajid 3:bd3465a70a5a 101 _velocity.y = v.y;
shahidsajid 3:bd3465a70a5a 102 }
shahidsajid 3:bd3465a70a5a 103
shahidsajid 3:bd3465a70a5a 104 Vector2D Ball::get_velocity()
shahidsajid 3:bd3465a70a5a 105 {
shahidsajid 3:bd3465a70a5a 106 Vector2D v = {_velocity.x,_velocity.y};
shahidsajid 3:bd3465a70a5a 107 return v;
shahidsajid 3:bd3465a70a5a 108 }
shahidsajid 3:bd3465a70a5a 109
shahidsajid 3:bd3465a70a5a 110 Vector2D Ball::get_pos()
shahidsajid 3:bd3465a70a5a 111 {
shahidsajid 3:bd3465a70a5a 112 Vector2D p = {_x,_y};
shahidsajid 3:bd3465a70a5a 113 return p;
shahidsajid 3:bd3465a70a5a 114 }
shahidsajid 3:bd3465a70a5a 115
shahidsajid 3:bd3465a70a5a 116 void Ball::set_pos(Vector2D p)
shahidsajid 3:bd3465a70a5a 117 {
shahidsajid 3:bd3465a70a5a 118 _x = p.x;
shahidsajid 3:bd3465a70a5a 119 _y = p.y;
shahidsajid 3:bd3465a70a5a 120 }
shahidsajid 3:bd3465a70a5a 121 void Ball::get_direction(Gamepad &pad){
shahidsajid 3:bd3465a70a5a 122 _d = pad.get_direction();
shahidsajid 3:bd3465a70a5a 123 }
shahidsajid 3:bd3465a70a5a 124 void Ball::set_field(N5110 &lcd){
shahidsajid 4:55a0509c4874 125 lcd.drawLine(40,11,40,14,1);
shahidsajid 4:55a0509c4874 126 lcd.drawLine(42,11,42,14,1);
shahidsajid 4:55a0509c4874 127 lcd.drawLine(44,11,44,14,1);
shahidsajid 4:55a0509c4874 128 lcd.drawLine(40,14,44,14,2);
shahidsajid 4:55a0509c4874 129 /*
shahidsajid 3:bd3465a70a5a 130 lcd.drawCircle(25,2,2,FILL_BLACK); //NE
shahidsajid 3:bd3465a70a5a 131 lcd.drawCircle(65,2,2,FILL_BLACK); //NW
shahidsajid 3:bd3465a70a5a 132 lcd.drawCircle(65,40,2,FILL_BLACK); //SE
shahidsajid 3:bd3465a70a5a 133 lcd.drawCircle(25,40,2,FILL_BLACK); //SW
shahidsajid 3:bd3465a70a5a 134 lcd.drawCircle(3,20,2,FILL_BLACK); //E
shahidsajid 4:55a0509c4874 135 lcd.drawCircle(80,20,2,FILL_BLACK);
shahidsajid 4:55a0509c4874 136 */
shahidsajid 4:55a0509c4874 137 int d=0;
shahidsajid 5:915bcac4e9b9 138 srand(time(NULL));
shahidsajid 4:55a0509c4874 139 for(int i=0;i<3;i++){
shahidsajid 5:915bcac4e9b9 140 d = 1+ rand() % 6;
shahidsajid 5:915bcac4e9b9 141 //printf("Direction %i \n",d);
shahidsajid 4:55a0509c4874 142 //while (d==N||d==S){
shahidsajid 4:55a0509c4874 143 //get_direction(pad);
shahidsajid 4:55a0509c4874 144 //}
shahidsajid 4:55a0509c4874 145 if (d==1){ //NE
shahidsajid 4:55a0509c4874 146 field[i].x=25;
shahidsajid 4:55a0509c4874 147 field[i].y=2;
shahidsajid 4:55a0509c4874 148 field[i].position=1;
shahidsajid 3:bd3465a70a5a 149 }
shahidsajid 4:55a0509c4874 150 if (d==2){ //NW
shahidsajid 4:55a0509c4874 151 field[i].x=65;
shahidsajid 4:55a0509c4874 152 field[i].y=2;
shahidsajid 4:55a0509c4874 153 field[i].position=1;
shahidsajid 4:55a0509c4874 154 }
shahidsajid 4:55a0509c4874 155 if (d==3){ //SW
shahidsajid 4:55a0509c4874 156 field[i].x=25;
shahidsajid 4:55a0509c4874 157 field[i].y=40;
shahidsajid 4:55a0509c4874 158 field[i].position=1;
shahidsajid 4:55a0509c4874 159 }
shahidsajid 5:915bcac4e9b9 160 if (d==4){ //SE
shahidsajid 5:915bcac4e9b9 161 field[i].x=65;
shahidsajid 5:915bcac4e9b9 162 field[i].y=40;
shahidsajid 5:915bcac4e9b9 163 field[i].position=1;
shahidsajid 5:915bcac4e9b9 164 }
shahidsajid 5:915bcac4e9b9 165 if (d==5){ //E
shahidsajid 5:915bcac4e9b9 166 field[i].x=3;
shahidsajid 5:915bcac4e9b9 167 field[i].y=20;
shahidsajid 5:915bcac4e9b9 168 field[i].position=1;
shahidsajid 5:915bcac4e9b9 169 }
shahidsajid 5:915bcac4e9b9 170 if (d==3){ //W
shahidsajid 5:915bcac4e9b9 171 field[i].x=80;
shahidsajid 5:915bcac4e9b9 172 field[i].y=20;
shahidsajid 5:915bcac4e9b9 173 field[i].position=1;
shahidsajid 5:915bcac4e9b9 174 }
shahidsajid 4:55a0509c4874 175 }
shahidsajid 4:55a0509c4874 176 lcd.drawCircle(field[0].x,field[0].y,2,FILL_BLACK);
shahidsajid 4:55a0509c4874 177 lcd.drawCircle(field[1].x,field[1].y,2,FILL_BLACK);
shahidsajid 4:55a0509c4874 178 lcd.drawCircle(field[2].x,field[2].y,2,FILL_BLACK);
shahidsajid 4:55a0509c4874 179 lcd.drawCircle(field[3].x,field[3].y,2,FILL_BLACK);
shahidsajid 4:55a0509c4874 180
shahidsajid 4:55a0509c4874 181 }