Zhang Xin yu
/
ZhangXinyu201090208
zhangxinyu01text
cxkEngine/cxkEngine.cpp@20:5efed4030c7b, 2019-05-06 (annotated)
- Committer:
- Jenny121
- Date:
- Mon May 06 07:32:01 2019 +0000
- Revision:
- 20:5efed4030c7b
- Parent:
- 12:3952ba0683c7
- Child:
- 21:a5292aacb38f
10
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jenny121 | 12:3952ba0683c7 | 1 | #include "cxkEngine.h" |
Jenny121 | 12:3952ba0683c7 | 2 | |
Jenny121 | 12:3952ba0683c7 | 3 | cxkEngine::cxkEngine() |
Jenny121 | 12:3952ba0683c7 | 4 | { |
Jenny121 | 12:3952ba0683c7 | 5 | |
Jenny121 | 12:3952ba0683c7 | 6 | } |
Jenny121 | 12:3952ba0683c7 | 7 | |
Jenny121 | 12:3952ba0683c7 | 8 | cxkEngine::~cxkEngine() |
Jenny121 | 12:3952ba0683c7 | 9 | { |
Jenny121 | 12:3952ba0683c7 | 10 | |
Jenny121 | 12:3952ba0683c7 | 11 | } |
Jenny121 | 12:3952ba0683c7 | 12 | |
Jenny121 | 12:3952ba0683c7 | 13 | void cxkEngine::init(int CXK_width,int CXK_height,int ball_size,int speed) |
Jenny121 | 12:3952ba0683c7 | 14 | { |
Jenny121 | 12:3952ba0683c7 | 15 | // initialise the game parameters |
Jenny121 | 12:3952ba0683c7 | 16 | _CXK_width = CXK_width; |
Jenny121 | 12:3952ba0683c7 | 17 | _CXK_height = CXK_height; |
Jenny121 | 12:3952ba0683c7 | 18 | _ball_size = ball_size; |
Jenny121 | 12:3952ba0683c7 | 19 | _speed = speed; |
Jenny121 | 12:3952ba0683c7 | 20 | |
Jenny121 | 12:3952ba0683c7 | 21 | // x position on screen - WIDTH is defined in N5110.h |
Jenny121 | 12:3952ba0683c7 | 22 | |
Jenny121 | 12:3952ba0683c7 | 23 | _cxkpx = 80 - _CXK_width; |
Jenny121 | 12:3952ba0683c7 | 24 | _cxkpy = 40 - _CXK_height;// 80 ;40 are the edge of the wall |
Jenny121 | 12:3952ba0683c7 | 25 | |
Jenny121 | 12:3952ba0683c7 | 26 | // puts CXKs and ball in middle |
Jenny121 | 12:3952ba0683c7 | 27 | |
Jenny121 | 12:3952ba0683c7 | 28 | _cxkp.init(_cxkpx,_cxkpy,_CXK_height,_CXK_width); |
Jenny121 | 12:3952ba0683c7 | 29 | _ball.init(_ball_size,_speed,_direction); |
Jenny121 | 12:3952ba0683c7 | 30 | } |
Jenny121 | 12:3952ba0683c7 | 31 | |
Jenny121 | 12:3952ba0683c7 | 32 | void cxkEngine::read_input(Gamepad &pad) |
Jenny121 | 12:3952ba0683c7 | 33 | { |
Jenny121 | 12:3952ba0683c7 | 34 | _d = pad.get_direction(); |
Jenny121 | 12:3952ba0683c7 | 35 | _vara = pad.get_vara(); |
Jenny121 | 12:3952ba0683c7 | 36 | } |
Jenny121 | 12:3952ba0683c7 | 37 | |
Jenny121 | 12:3952ba0683c7 | 38 | void cxkEngine::draw(N5110 &lcd) |
Jenny121 | 12:3952ba0683c7 | 39 | { |
Jenny121 | 12:3952ba0683c7 | 40 | // draw the wall basket in the LCD buffer |
Jenny121 | 12:3952ba0683c7 | 41 | |
Jenny121 | 12:3952ba0683c7 | 42 | lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);//wal |
Jenny121 | 12:3952ba0683c7 | 43 | lcd.drawCircle(6,20,5,FILL_TRANSPARENT);// kuang |
Jenny121 | 12:3952ba0683c7 | 44 | //score |
Jenny121 | 12:3952ba0683c7 | 45 | print_scores(lcd); |
Jenny121 | 12:3952ba0683c7 | 46 | // CXKs |
Jenny121 | 12:3952ba0683c7 | 47 | |
Jenny121 | 12:3952ba0683c7 | 48 | _cxkp.draw(lcd); |
Jenny121 | 12:3952ba0683c7 | 49 | int cxkp_score = _cxkp.get_score(); |
Jenny121 | 12:3952ba0683c7 | 50 | // ball |
Jenny121 | 12:3952ba0683c7 | 51 | _ball.draw(lcd); |
Jenny121 | 20:5efed4030c7b | 52 | |
Jenny121 | 12:3952ba0683c7 | 53 | } |
Jenny121 | 12:3952ba0683c7 | 54 | } |
Jenny121 | 12:3952ba0683c7 | 55 | |
Jenny121 | 12:3952ba0683c7 | 56 | void cxkEngine::update(Gamepad &pad) |
Jenny121 | 12:3952ba0683c7 | 57 | { |
Jenny121 | 12:3952ba0683c7 | 58 | check_goal(pad); |
Jenny121 | 12:3952ba0683c7 | 59 | // important to update CXKs and ball before checking collisions so can |
Jenny121 | 12:3952ba0683c7 | 60 | // correct for it before updating the display |
Jenny121 | 12:3952ba0683c7 | 61 | |
Jenny121 | 12:3952ba0683c7 | 62 | _cxkp.update(_d,_vara); |
Jenny121 | 12:3952ba0683c7 | 63 | _ball.update(); |
Jenny121 | 12:3952ba0683c7 | 64 | |
Jenny121 | 12:3952ba0683c7 | 65 | check_wall_collision(pad); |
Jenny121 | 12:3952ba0683c7 | 66 | check_CXK_collisions(pad); |
Jenny121 | 12:3952ba0683c7 | 67 | } |
Jenny121 | 12:3952ba0683c7 | 68 | |
Jenny121 | 12:3952ba0683c7 | 69 | void cxkEngine::check_wall_collision(Gamepad &pad) |
Jenny121 | 12:3952ba0683c7 | 70 | { |
Jenny121 | 12:3952ba0683c7 | 71 | // read current ball attributes |
Jenny121 | 12:3952ba0683c7 | 72 | Vector2D ball_pos = _ball.get_pos(); |
Jenny121 | 12:3952ba0683c7 | 73 | Vector2D ball_velocity = _ball.get_velocity(); |
Jenny121 | 12:3952ba0683c7 | 74 | |
Jenny121 | 12:3952ba0683c7 | 75 | // check if hit top wall |
Jenny121 | 12:3952ba0683c7 | 76 | if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary |
Jenny121 | 12:3952ba0683c7 | 77 | ball_pos.y = 1; // bounce off ceiling without going off screen |
Jenny121 | 12:3952ba0683c7 | 78 | ball_velocity.y = -ball_velocity.y; |
Jenny121 | 12:3952ba0683c7 | 79 | // audio feedback |
Jenny121 | 12:3952ba0683c7 | 80 | pad.tone(650.0,0.1); |
Jenny121 | 12:3952ba0683c7 | 81 | } |
Jenny121 | 12:3952ba0683c7 | 82 | else if (ball_pos.x <= 1) { // 1 due to 1 pixel boundary |
Jenny121 | 12:3952ba0683c7 | 83 | ball_pos.x = 1; // bounce off ceiling without going off screen |
Jenny121 | 12:3952ba0683c7 | 84 | ball_velocity.x = -ball_velocity.x; |
Jenny121 | 12:3952ba0683c7 | 85 | // audio feedback |
Jenny121 | 12:3952ba0683c7 | 86 | pad.tone(750.0,0.1); |
Jenny121 | 12:3952ba0683c7 | 87 | } |
Jenny121 | 12:3952ba0683c7 | 88 | // check if hit right wall |
Jenny121 | 12:3952ba0683c7 | 89 | else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47 |
Jenny121 | 12:3952ba0683c7 | 90 | // hit bottom |
Jenny121 | 12:3952ba0683c7 | 91 | ball_pos.x = (WIDTH-1) - _ball_size; // stops ball going off screen |
Jenny121 | 12:3952ba0683c7 | 92 | ball_velocity.x = -ball_velocity.x; |
Jenny121 | 12:3952ba0683c7 | 93 | // audio feedback |
Jenny121 | 12:3952ba0683c7 | 94 | pad.tone(750.0,0.1); |
Jenny121 | 12:3952ba0683c7 | 95 | } |
Jenny121 | 12:3952ba0683c7 | 96 | |
Jenny121 | 12:3952ba0683c7 | 97 | // check if hit bottom wall |
Jenny121 | 12:3952ba0683c7 | 98 | else if (ball_pos.y + _ball_size >= (HEIGHT-1) ) { // bottom pixel is 47 |
Jenny121 | 12:3952ba0683c7 | 99 | // hit bottom |
Jenny121 | 12:3952ba0683c7 | 100 | ball_pos.y = (HEIGHT-1) - _ball_size; // stops ball going off screen |
Jenny121 | 12:3952ba0683c7 | 101 | ball_velocity.y = -ball_velocity.y; |
Jenny121 | 12:3952ba0683c7 | 102 | // audio feedback |
Jenny121 | 12:3952ba0683c7 | 103 | pad.tone(650.0,0.1); |
Jenny121 | 12:3952ba0683c7 | 104 | } |
Jenny121 | 12:3952ba0683c7 | 105 | |
Jenny121 | 12:3952ba0683c7 | 106 | // update ball parameters |
Jenny121 | 12:3952ba0683c7 | 107 | _ball.set_velocity(ball_velocity); |
Jenny121 | 12:3952ba0683c7 | 108 | _ball.set_pos(ball_pos); |
Jenny121 | 12:3952ba0683c7 | 109 | } |
Jenny121 | 12:3952ba0683c7 | 110 | |
Jenny121 | 12:3952ba0683c7 | 111 | void cxkEngine::check_CXK_collisions(Gamepad &pad) |
Jenny121 | 12:3952ba0683c7 | 112 | { |
Jenny121 | 12:3952ba0683c7 | 113 | // read current ball attributes |
Jenny121 | 12:3952ba0683c7 | 114 | Vector2D ball_pos = _ball.get_pos(); |
Jenny121 | 12:3952ba0683c7 | 115 | Vector2D ball_velocity = _ball.get_velocity(); |
Jenny121 | 12:3952ba0683c7 | 116 | |
Jenny121 | 12:3952ba0683c7 | 117 | |
Jenny121 | 12:3952ba0683c7 | 118 | // see if ball has hit the |
Jenny121 | 12:3952ba0683c7 | 119 | |
Jenny121 | 12:3952ba0683c7 | 120 | // check cxkp next |
Jenny121 | 12:3952ba0683c7 | 121 | Vector2D cxkp_pos = _cxkp.get_pos(); |
Jenny121 | 12:3952ba0683c7 | 122 | |
Jenny121 | 12:3952ba0683c7 | 123 | // see if ball has hit the CXK |
Jenny121 | 12:3952ba0683c7 | 124 | if ((ball_pos.y - cxkp_pos.y >= -5) && |
Jenny121 | 12:3952ba0683c7 | 125 | (ball_pos.y - cxkp_pos.y <= 5) && |
Jenny121 | 12:3952ba0683c7 | 126 | (ball_pos.x - cxkp_pos.x >= -5) && |
Jenny121 | 12:3952ba0683c7 | 127 | (ball_pos.x - cxkp_pos.x <= 5) |
Jenny121 | 12:3952ba0683c7 | 128 | ) //right |
Jenny121 | 12:3952ba0683c7 | 129 | { |
Jenny121 | 12:3952ba0683c7 | 130 | // if it has, change the position and reflect x,y velocity |
Jenny121 | 12:3952ba0683c7 | 131 | ball_pos.x= ball_pos.x + 3; |
Jenny121 | 12:3952ba0683c7 | 132 | ball_pos.y = ball_pos.y + 1; |
Jenny121 | 12:3952ba0683c7 | 133 | ball_velocity.x = -ball_velocity.x ; |
Jenny121 | 12:3952ba0683c7 | 134 | ball_velocity.y = -ball_velocity.y; |
Jenny121 | 12:3952ba0683c7 | 135 | // audio feedback |
Jenny121 | 12:3952ba0683c7 | 136 | pad.tone(1000.0,0.1); |
Jenny121 | 12:3952ba0683c7 | 137 | pad.tone(900.0,0.1); |
Jenny121 | 12:3952ba0683c7 | 138 | pad.tone(800.0,0.1); |
Jenny121 | 12:3952ba0683c7 | 139 | } |
Jenny121 | 12:3952ba0683c7 | 140 | |
Jenny121 | 12:3952ba0683c7 | 141 | // write new attributes |
Jenny121 | 12:3952ba0683c7 | 142 | _ball.set_velocity(ball_velocity); |
Jenny121 | 12:3952ba0683c7 | 143 | _ball.set_pos(ball_pos); |
Jenny121 | 12:3952ba0683c7 | 144 | } |
Jenny121 | 12:3952ba0683c7 | 145 | |
Jenny121 | 12:3952ba0683c7 | 146 | void cxkEngine::check_goal(Gamepad &pad) |
Jenny121 | 12:3952ba0683c7 | 147 | { |
Jenny121 | 12:3952ba0683c7 | 148 | Vector2D ball_pos = _ball.get_pos(); |
Jenny121 | 12:3952ba0683c7 | 149 | Vector2D cxkp_pos = _cxkp.get_pos(); |
Jenny121 | 12:3952ba0683c7 | 150 | // cxkp has scored if the ball in the basket |
Jenny121 | 12:3952ba0683c7 | 151 | if ( |
Jenny121 | 12:3952ba0683c7 | 152 | (ball_pos.y <= 25) && |
Jenny121 | 12:3952ba0683c7 | 153 | (ball_pos.y >= 15) && |
Jenny121 | 12:3952ba0683c7 | 154 | (ball_pos.x <= 6) && |
Jenny121 | 12:3952ba0683c7 | 155 | (ball_pos.x >= 1) |
Jenny121 | 12:3952ba0683c7 | 156 | |
Jenny121 | 12:3952ba0683c7 | 157 | ) { |
Jenny121 | 12:3952ba0683c7 | 158 | _cxkp.add_score(); |
Jenny121 | 12:3952ba0683c7 | 159 | _ball.init(_ball_size,_speed,_direction); |
Jenny121 | 12:3952ba0683c7 | 160 | pad.tone(1500.0,0.5); |
Jenny121 | 12:3952ba0683c7 | 161 | pad.leds_on(); |
Jenny121 | 12:3952ba0683c7 | 162 | wait(0.5); // wiat show the goal |
Jenny121 | 12:3952ba0683c7 | 163 | pad.leds_off(); |
Jenny121 | 12:3952ba0683c7 | 164 | } |
Jenny121 | 12:3952ba0683c7 | 165 | |
Jenny121 | 12:3952ba0683c7 | 166 | } |
Jenny121 | 12:3952ba0683c7 | 167 | |
Jenny121 | 12:3952ba0683c7 | 168 | void cxkEngine::print_scores(N5110 &lcd) |
Jenny121 | 12:3952ba0683c7 | 169 | { |
Jenny121 | 12:3952ba0683c7 | 170 | // get scores from CXKs |
Jenny121 | 12:3952ba0683c7 | 171 | |
Jenny121 | 12:3952ba0683c7 | 172 | int cxkp_score = _cxkp.get_score(); |
Jenny121 | 12:3952ba0683c7 | 173 | |
Jenny121 | 12:3952ba0683c7 | 174 | // print to LCD i |
Jenny121 | 12:3952ba0683c7 | 175 | // font is 8 wide, so leave 4 pixel 2e from middle assuming two digits |
Jenny121 | 12:3952ba0683c7 | 176 | char buffer2[14]; |
Jenny121 | 12:3952ba0683c7 | 177 | sprintf(buffer2,"%2d",cxkp_score); |
Jenny121 | 12:3952ba0683c7 | 178 | lcd.printString(buffer2,40 ,1); |
Jenny121 | 12:3952ba0683c7 | 179 | |
Jenny121 | 12:3952ba0683c7 | 180 | } |
Jenny121 | 12:3952ba0683c7 | 181 |