Zhang Xin yu
/
ZhangXinyu201090208
zhangxinyu01text
cxkEngine/cxkEngine.cpp@24:8b1494a6bdbe, 2019-05-06 (annotated)
- Committer:
- Jenny121
- Date:
- Mon May 06 09:33:50 2019 +0000
- Revision:
- 24:8b1494a6bdbe
- Parent:
- 23:b9b98e400e8b
the final WORK
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 | 21:a5292aacb38f | 52 | if (cxkp_score >= 30) { |
Jenny121 | 21:a5292aacb38f | 53 | lcd.printString("Congratulations",0,1); |
Jenny121 | 21:a5292aacb38f | 54 | lcd.printString(" u win CXK",0,2); |
Jenny121 | 21:a5292aacb38f | 55 | lcd.printString("GAME OVER",0,4); |
Jenny121 | 21:a5292aacb38f | 56 | lcd.refresh(); |
Jenny121 | 12:3952ba0683c7 | 57 | } |
Jenny121 | 12:3952ba0683c7 | 58 | } |
Jenny121 | 12:3952ba0683c7 | 59 | |
Jenny121 | 12:3952ba0683c7 | 60 | void cxkEngine::update(Gamepad &pad) |
Jenny121 | 12:3952ba0683c7 | 61 | { |
Jenny121 | 12:3952ba0683c7 | 62 | check_goal(pad); |
Jenny121 | 12:3952ba0683c7 | 63 | // important to update CXKs and ball before checking collisions so can |
Jenny121 | 12:3952ba0683c7 | 64 | // correct for it before updating the display |
Jenny121 | 12:3952ba0683c7 | 65 | |
Jenny121 | 12:3952ba0683c7 | 66 | _cxkp.update(_d,_vara); |
Jenny121 | 12:3952ba0683c7 | 67 | _ball.update(); |
Jenny121 | 12:3952ba0683c7 | 68 | |
Jenny121 | 12:3952ba0683c7 | 69 | check_wall_collision(pad); |
Jenny121 | 12:3952ba0683c7 | 70 | check_CXK_collisions(pad); |
Jenny121 | 12:3952ba0683c7 | 71 | } |
Jenny121 | 12:3952ba0683c7 | 72 | |
Jenny121 | 12:3952ba0683c7 | 73 | void cxkEngine::check_wall_collision(Gamepad &pad) |
Jenny121 | 12:3952ba0683c7 | 74 | { |
Jenny121 | 12:3952ba0683c7 | 75 | // read current ball attributes |
Jenny121 | 12:3952ba0683c7 | 76 | Vector2D ball_pos = _ball.get_pos(); |
Jenny121 | 12:3952ba0683c7 | 77 | Vector2D ball_velocity = _ball.get_velocity(); |
Jenny121 | 12:3952ba0683c7 | 78 | |
Jenny121 | 12:3952ba0683c7 | 79 | // check if hit top wall |
Jenny121 | 12:3952ba0683c7 | 80 | if (ball_pos.y <= 1) { // 1 due to 1 pixel boundary |
Jenny121 | 12:3952ba0683c7 | 81 | ball_pos.y = 1; // bounce off ceiling without going off screen |
Jenny121 | 12:3952ba0683c7 | 82 | ball_velocity.y = -ball_velocity.y; |
Jenny121 | 12:3952ba0683c7 | 83 | // audio feedback |
Jenny121 | 12:3952ba0683c7 | 84 | pad.tone(650.0,0.1); |
Jenny121 | 12:3952ba0683c7 | 85 | } |
Jenny121 | 12:3952ba0683c7 | 86 | else if (ball_pos.x <= 1) { // 1 due to 1 pixel boundary |
Jenny121 | 12:3952ba0683c7 | 87 | ball_pos.x = 1; // bounce off ceiling without going off screen |
Jenny121 | 12:3952ba0683c7 | 88 | ball_velocity.x = -ball_velocity.x; |
Jenny121 | 12:3952ba0683c7 | 89 | // audio feedback |
Jenny121 | 12:3952ba0683c7 | 90 | pad.tone(750.0,0.1); |
Jenny121 | 12:3952ba0683c7 | 91 | } |
Jenny121 | 12:3952ba0683c7 | 92 | // check if hit right wall |
Jenny121 | 12:3952ba0683c7 | 93 | else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47 |
Jenny121 | 12:3952ba0683c7 | 94 | // hit bottom |
Jenny121 | 12:3952ba0683c7 | 95 | ball_pos.x = (WIDTH-1) - _ball_size; // stops ball going off screen |
Jenny121 | 12:3952ba0683c7 | 96 | ball_velocity.x = -ball_velocity.x; |
Jenny121 | 12:3952ba0683c7 | 97 | // audio feedback |
Jenny121 | 12:3952ba0683c7 | 98 | pad.tone(750.0,0.1); |
Jenny121 | 12:3952ba0683c7 | 99 | } |
Jenny121 | 12:3952ba0683c7 | 100 | |
Jenny121 | 12:3952ba0683c7 | 101 | // check if hit bottom wall |
Jenny121 | 12:3952ba0683c7 | 102 | else if (ball_pos.y + _ball_size >= (HEIGHT-1) ) { // bottom pixel is 47 |
Jenny121 | 12:3952ba0683c7 | 103 | // hit bottom |
Jenny121 | 12:3952ba0683c7 | 104 | ball_pos.y = (HEIGHT-1) - _ball_size; // stops ball going off screen |
Jenny121 | 12:3952ba0683c7 | 105 | ball_velocity.y = -ball_velocity.y; |
Jenny121 | 12:3952ba0683c7 | 106 | // audio feedback |
Jenny121 | 12:3952ba0683c7 | 107 | pad.tone(650.0,0.1); |
Jenny121 | 12:3952ba0683c7 | 108 | } |
Jenny121 | 12:3952ba0683c7 | 109 | |
Jenny121 | 12:3952ba0683c7 | 110 | // update ball parameters |
Jenny121 | 12:3952ba0683c7 | 111 | _ball.set_velocity(ball_velocity); |
Jenny121 | 12:3952ba0683c7 | 112 | _ball.set_pos(ball_pos); |
Jenny121 | 12:3952ba0683c7 | 113 | } |
Jenny121 | 12:3952ba0683c7 | 114 | |
Jenny121 | 12:3952ba0683c7 | 115 | void cxkEngine::check_CXK_collisions(Gamepad &pad) |
Jenny121 | 12:3952ba0683c7 | 116 | { |
Jenny121 | 12:3952ba0683c7 | 117 | // read current ball attributes |
Jenny121 | 12:3952ba0683c7 | 118 | Vector2D ball_pos = _ball.get_pos(); |
Jenny121 | 12:3952ba0683c7 | 119 | Vector2D ball_velocity = _ball.get_velocity(); |
Jenny121 | 12:3952ba0683c7 | 120 | |
Jenny121 | 12:3952ba0683c7 | 121 | |
Jenny121 | 12:3952ba0683c7 | 122 | // see if ball has hit the |
Jenny121 | 12:3952ba0683c7 | 123 | |
Jenny121 | 12:3952ba0683c7 | 124 | // check cxkp next |
Jenny121 | 12:3952ba0683c7 | 125 | Vector2D cxkp_pos = _cxkp.get_pos(); |
Jenny121 | 12:3952ba0683c7 | 126 | |
Jenny121 | 12:3952ba0683c7 | 127 | // see if ball has hit the CXK |
Jenny121 | 24:8b1494a6bdbe | 128 | if ((ball_pos.y - cxkp_pos.y >= -5) && |
Jenny121 | 24:8b1494a6bdbe | 129 | (ball_pos.y - cxkp_pos.y <= 5) && |
Jenny121 | 24:8b1494a6bdbe | 130 | (ball_pos.x - cxkp_pos.x >= -5) && |
Jenny121 | 24:8b1494a6bdbe | 131 | (ball_pos.x - cxkp_pos.x <= 5) |
Jenny121 | 24:8b1494a6bdbe | 132 | ) //right |
Jenny121 | 24:8b1494a6bdbe | 133 | { |
Jenny121 | 12:3952ba0683c7 | 134 | // if it has, change the position and reflect x,y velocity |
Jenny121 | 12:3952ba0683c7 | 135 | ball_pos.x= ball_pos.x + 3; |
Jenny121 | 12:3952ba0683c7 | 136 | ball_pos.y = ball_pos.y + 1; |
Jenny121 | 12:3952ba0683c7 | 137 | ball_velocity.x = -ball_velocity.x ; |
Jenny121 | 12:3952ba0683c7 | 138 | ball_velocity.y = -ball_velocity.y; |
Jenny121 | 12:3952ba0683c7 | 139 | // audio feedback |
Jenny121 | 12:3952ba0683c7 | 140 | pad.tone(1000.0,0.1); |
Jenny121 | 12:3952ba0683c7 | 141 | pad.tone(900.0,0.1); |
Jenny121 | 12:3952ba0683c7 | 142 | pad.tone(800.0,0.1); |
Jenny121 | 12:3952ba0683c7 | 143 | } |
Jenny121 | 12:3952ba0683c7 | 144 | |
Jenny121 | 12:3952ba0683c7 | 145 | // write new attributes |
Jenny121 | 12:3952ba0683c7 | 146 | _ball.set_velocity(ball_velocity); |
Jenny121 | 12:3952ba0683c7 | 147 | _ball.set_pos(ball_pos); |
Jenny121 | 12:3952ba0683c7 | 148 | } |
Jenny121 | 12:3952ba0683c7 | 149 | |
Jenny121 | 12:3952ba0683c7 | 150 | void cxkEngine::check_goal(Gamepad &pad) |
Jenny121 | 12:3952ba0683c7 | 151 | { |
Jenny121 | 12:3952ba0683c7 | 152 | Vector2D ball_pos = _ball.get_pos(); |
Jenny121 | 12:3952ba0683c7 | 153 | Vector2D cxkp_pos = _cxkp.get_pos(); |
Jenny121 | 12:3952ba0683c7 | 154 | // cxkp has scored if the ball in the basket |
Jenny121 | 12:3952ba0683c7 | 155 | if ( |
Jenny121 | 12:3952ba0683c7 | 156 | (ball_pos.y <= 25) && |
Jenny121 | 12:3952ba0683c7 | 157 | (ball_pos.y >= 15) && |
Jenny121 | 12:3952ba0683c7 | 158 | (ball_pos.x <= 6) && |
Jenny121 | 12:3952ba0683c7 | 159 | (ball_pos.x >= 1) |
Jenny121 | 12:3952ba0683c7 | 160 | |
Jenny121 | 12:3952ba0683c7 | 161 | ) { |
Jenny121 | 12:3952ba0683c7 | 162 | _cxkp.add_score(); |
Jenny121 | 12:3952ba0683c7 | 163 | _ball.init(_ball_size,_speed,_direction); |
Jenny121 | 12:3952ba0683c7 | 164 | pad.tone(1500.0,0.5); |
Jenny121 | 12:3952ba0683c7 | 165 | pad.leds_on(); |
Jenny121 | 12:3952ba0683c7 | 166 | wait(0.5); // wiat show the goal |
Jenny121 | 12:3952ba0683c7 | 167 | pad.leds_off(); |
Jenny121 | 12:3952ba0683c7 | 168 | } |
Jenny121 | 12:3952ba0683c7 | 169 | |
Jenny121 | 12:3952ba0683c7 | 170 | } |
Jenny121 | 12:3952ba0683c7 | 171 | |
Jenny121 | 12:3952ba0683c7 | 172 | void cxkEngine::print_scores(N5110 &lcd) |
Jenny121 | 12:3952ba0683c7 | 173 | { |
Jenny121 | 12:3952ba0683c7 | 174 | // get scores from CXKs |
Jenny121 | 12:3952ba0683c7 | 175 | |
Jenny121 | 12:3952ba0683c7 | 176 | int cxkp_score = _cxkp.get_score(); |
Jenny121 | 12:3952ba0683c7 | 177 | |
Jenny121 | 12:3952ba0683c7 | 178 | // print to LCD i |
Jenny121 | 12:3952ba0683c7 | 179 | // font is 8 wide, so leave 4 pixel 2e from middle assuming two digits |
Jenny121 | 12:3952ba0683c7 | 180 | char buffer2[14]; |
Jenny121 | 12:3952ba0683c7 | 181 | sprintf(buffer2,"%2d",cxkp_score); |
Jenny121 | 12:3952ba0683c7 | 182 | lcd.printString(buffer2,40 ,1); |
Jenny121 | 12:3952ba0683c7 | 183 | |
Jenny121 | 12:3952ba0683c7 | 184 | } |
Jenny121 | 12:3952ba0683c7 | 185 |