Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Ball/Ball.cpp@18:a260ce8db9e7, 2019-04-27 (annotated)
- Committer:
- shahidsajid
- Date:
- Sat Apr 27 15:42:36 2019 +0000
- Revision:
- 18:a260ce8db9e7
- Parent:
- 15:81a3aaf52647
- Child:
- 20:9d21599fe350
Created no. of balls completed; created a target; printed the target and no. of balls onto the screen; Created displays for when game is won and game is lost
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shahidsajid | 12:954da4f4e565 | 1 | |
shahidsajid | 3:bd3465a70a5a | 2 | #include "Ball.h" |
shahidsajid | 3:bd3465a70a5a | 3 | Ball::Ball() |
shahidsajid | 3:bd3465a70a5a | 4 | { |
shahidsajid | 3:bd3465a70a5a | 5 | |
shahidsajid | 3:bd3465a70a5a | 6 | } |
shahidsajid | 3:bd3465a70a5a | 7 | |
shahidsajid | 3:bd3465a70a5a | 8 | Ball::~Ball() |
shahidsajid | 3:bd3465a70a5a | 9 | { |
shahidsajid | 3:bd3465a70a5a | 10 | |
shahidsajid | 3:bd3465a70a5a | 11 | } |
shahidsajid | 3:bd3465a70a5a | 12 | |
shahidsajid | 3:bd3465a70a5a | 13 | void Ball::init(int size,int speed) |
shahidsajid | 3:bd3465a70a5a | 14 | { |
shahidsajid | 3:bd3465a70a5a | 15 | _size = size; |
shahidsajid | 7:a1a6bff238c1 | 16 | _x1=42; |
shahidsajid | 7:a1a6bff238c1 | 17 | _y1=3; |
shahidsajid | 3:bd3465a70a5a | 18 | _x = 42; |
shahidsajid | 3:bd3465a70a5a | 19 | _y = 15; |
shahidsajid | 18:a260ce8db9e7 | 20 | _ball_count=0; |
shahidsajid | 15:81a3aaf52647 | 21 | set_tone=0; |
shahidsajid | 6:3e50f2cf4366 | 22 | bat.init(3,4); |
shahidsajid | 9:a81db6a703b7 | 23 | bowled=0; |
shahidsajid | 18:a260ce8db9e7 | 24 | _ball_count=0; |
shahidsajid | 3:bd3465a70a5a | 25 | |
shahidsajid | 3:bd3465a70a5a | 26 | } |
shahidsajid | 18:a260ce8db9e7 | 27 | void Ball::increment_ball_count(){ |
shahidsajid | 18:a260ce8db9e7 | 28 | _ball_count++; |
shahidsajid | 18:a260ce8db9e7 | 29 | } |
shahidsajid | 18:a260ce8db9e7 | 30 | int Ball::get_ball_count(){ |
shahidsajid | 18:a260ce8db9e7 | 31 | return _ball_count; |
shahidsajid | 18:a260ce8db9e7 | 32 | } |
shahidsajid | 18:a260ce8db9e7 | 33 | |
shahidsajid | 12:954da4f4e565 | 34 | Vector2D Ball::get_pos() |
shahidsajid | 12:954da4f4e565 | 35 | { |
shahidsajid | 12:954da4f4e565 | 36 | Vector2D p = {_x,_y}; |
shahidsajid | 12:954da4f4e565 | 37 | return p; |
shahidsajid | 12:954da4f4e565 | 38 | } |
shahidsajid | 11:f481ec642cc5 | 39 | |
shahidsajid | 12:954da4f4e565 | 40 | void Ball::set_pos(Vector2D p) |
shahidsajid | 12:954da4f4e565 | 41 | { |
shahidsajid | 12:954da4f4e565 | 42 | _x = p.x; |
shahidsajid | 12:954da4f4e565 | 43 | _y = p.y; |
shahidsajid | 12:954da4f4e565 | 44 | } |
shahidsajid | 15:81a3aaf52647 | 45 | int Ball::bowler_start(Gamepad &pad){ |
shahidsajid | 7:a1a6bff238c1 | 46 | if(_y1!=15){ |
shahidsajid | 12:954da4f4e565 | 47 | _y1+=1; |
shahidsajid | 7:a1a6bff238c1 | 48 | return 0; |
shahidsajid | 7:a1a6bff238c1 | 49 | } |
shahidsajid | 7:a1a6bff238c1 | 50 | else { |
shahidsajid | 15:81a3aaf52647 | 51 | if (set_tone==0){ |
shahidsajid | 15:81a3aaf52647 | 52 | //pad.tone(750.0,0.3); |
shahidsajid | 15:81a3aaf52647 | 53 | set_tone=1; |
shahidsajid | 15:81a3aaf52647 | 54 | } |
shahidsajid | 7:a1a6bff238c1 | 55 | return 1; |
shahidsajid | 7:a1a6bff238c1 | 56 | } |
shahidsajid | 7:a1a6bff238c1 | 57 | } |
shahidsajid | 15:81a3aaf52647 | 58 | int Ball::ball_start(Gamepad &pad){ |
shahidsajid | 18:a260ce8db9e7 | 59 | |
shahidsajid | 15:81a3aaf52647 | 60 | int bowler_start_check=bowler_start(pad); |
shahidsajid | 10:6c6e09023942 | 61 | if (bowler_start_check==1){ |
shahidsajid | 9:a81db6a703b7 | 62 | if (_y!=36){ |
shahidsajid | 10:6c6e09023942 | 63 | _y+=1; |
shahidsajid | 10:6c6e09023942 | 64 | return 0; |
shahidsajid | 9:a81db6a703b7 | 65 | } |
shahidsajid | 10:6c6e09023942 | 66 | else{ |
shahidsajid | 7:a1a6bff238c1 | 67 | bowled=1; |
shahidsajid | 10:6c6e09023942 | 68 | return 1; |
shahidsajid | 7:a1a6bff238c1 | 69 | } |
shahidsajid | 12:954da4f4e565 | 70 | } |
shahidsajid | 7:a1a6bff238c1 | 71 | } |
shahidsajid | 11:f481ec642cc5 | 72 | |
shahidsajid | 7:a1a6bff238c1 | 73 | void Ball::draw(N5110 &lcd) |
shahidsajid | 7:a1a6bff238c1 | 74 | { |
shahidsajid | 18:a260ce8db9e7 | 75 | char buffer[5]; |
shahidsajid | 18:a260ce8db9e7 | 76 | int c=get_ball_count(); |
shahidsajid | 18:a260ce8db9e7 | 77 | int length=sprintf(buffer,"B%i",c); |
shahidsajid | 18:a260ce8db9e7 | 78 | lcd.printString(buffer,0,0); |
shahidsajid | 13:924891519a95 | 79 | lcd.drawCircle(_x1,_y1,2,FILL_TRANSPARENT); |
shahidsajid | 13:924891519a95 | 80 | lcd.drawCircle(_x,_y,2,FILL_BLACK); |
shahidsajid | 7:a1a6bff238c1 | 81 | //lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); |
shahidsajid | 7:a1a6bff238c1 | 82 | } |
shahidsajid | 12:954da4f4e565 | 83 | |
shahidsajid | 14:122eaa3b7a50 | 84 | int Ball::update_ball(int expected_x,int expected_y){ |
shahidsajid | 12:954da4f4e565 | 85 | if (_y!=expected_y){ |
shahidsajid | 12:954da4f4e565 | 86 | if (_y>expected_y){ |
shahidsajid | 12:954da4f4e565 | 87 | _y--; |
shahidsajid | 9:a81db6a703b7 | 88 | } |
shahidsajid | 12:954da4f4e565 | 89 | else{ |
shahidsajid | 12:954da4f4e565 | 90 | _y++; |
shahidsajid | 14:122eaa3b7a50 | 91 | } |
shahidsajid | 6:3e50f2cf4366 | 92 | } |
shahidsajid | 14:122eaa3b7a50 | 93 | if (_x!=expected_x){ |
shahidsajid | 12:954da4f4e565 | 94 | if (_x>expected_x){ |
shahidsajid | 12:954da4f4e565 | 95 | _x--; |
shahidsajid | 12:954da4f4e565 | 96 | } |
shahidsajid | 12:954da4f4e565 | 97 | else{ |
shahidsajid | 12:954da4f4e565 | 98 | _x++; |
shahidsajid | 12:954da4f4e565 | 99 | } |
shahidsajid | 15:81a3aaf52647 | 100 | } |
shahidsajid | 15:81a3aaf52647 | 101 | printf(" %i %i %i %i \n",_x,expected_x,_y,expected_y); |
shahidsajid | 14:122eaa3b7a50 | 102 | if (_x==expected_x && _y==expected_y){ |
shahidsajid | 14:122eaa3b7a50 | 103 | return 1; |
shahidsajid | 12:954da4f4e565 | 104 | } |
shahidsajid | 14:122eaa3b7a50 | 105 | else{ |
shahidsajid | 14:122eaa3b7a50 | 106 | return 0; |
shahidsajid | 14:122eaa3b7a50 | 107 | } |
shahidsajid | 14:122eaa3b7a50 | 108 | } |
shahidsajid | 14:122eaa3b7a50 | 109 | |
shahidsajid | 18:a260ce8db9e7 | 110 | void Ball::reset_ball_count(){ |
shahidsajid | 18:a260ce8db9e7 | 111 | _ball_count=0; |
shahidsajid | 18:a260ce8db9e7 | 112 | } |
shahidsajid | 12:954da4f4e565 | 113 | |
shahidsajid | 12:954da4f4e565 | 114 | |
shahidsajid | 9:a81db6a703b7 | 115 | void Ball::reset(){ |
shahidsajid | 9:a81db6a703b7 | 116 | bowled=0; |
shahidsajid | 9:a81db6a703b7 | 117 | ballHit=0; |
shahidsajid | 14:122eaa3b7a50 | 118 | _x1=42; |
shahidsajid | 14:122eaa3b7a50 | 119 | _y1=3; |
shahidsajid | 14:122eaa3b7a50 | 120 | _x = 42; |
shahidsajid | 14:122eaa3b7a50 | 121 | _y = 15; |
shahidsajid | 15:81a3aaf52647 | 122 | set_tone=0; |
shahidsajid | 18:a260ce8db9e7 | 123 | //_ball_count=0; |
shahidsajid | 9:a81db6a703b7 | 124 | } |
shahidsajid | 9:a81db6a703b7 | 125 | |
shahidsajid | 12:954da4f4e565 | 126 |