2222222

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cxkEngine.cpp Source File

cxkEngine.cpp

00001 #include "cxkEngine.h"
00002 
00003 cxkEngine::cxkEngine()
00004 {
00005 
00006 }
00007 
00008 cxkEngine::~cxkEngine()
00009 {
00010 
00011 }
00012 
00013 void cxkEngine::init(int CXK_width,int CXK_height,int ball_size,int speed)
00014 {
00015     // initialise the game parameters
00016     _CXK_width = CXK_width;
00017     _CXK_height = CXK_height;
00018     _ball_size = ball_size;
00019     _speed = speed;
00020 
00021     // x position on screen - WIDTH is defined in N5110.h
00022    
00023     _cxkpx = 80 - _CXK_width;
00024      _cxkpy = 40 - _CXK_height;// 80 ;40 are the edge of the wall 
00025      
00026     // puts CXKs and ball in middle
00027   
00028     _cxkp.init(_cxkpx,_cxkpy,_CXK_height,_CXK_width);
00029     _ball.init(_ball_size,_speed,_direction);
00030 }
00031 
00032 void cxkEngine::read_input(Gamepad &pad)
00033 {
00034     _d = pad.get_direction();
00035     _vara = pad.get_vara();
00036 }
00037 
00038 void cxkEngine::draw(N5110 &lcd)
00039 {
00040     // draw the wall basket in the LCD buffer
00041     
00042     lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);//wal
00043     lcd.drawCircle(6,20,5,FILL_TRANSPARENT);// kuang
00044     //score
00045     print_scores(lcd);
00046     // CXKs
00047    
00048     _cxkp.draw(lcd);
00049     int cxkp_score = _cxkp.get_score();
00050     // ball
00051     _ball.draw(lcd);
00052     if (cxkp_score >= 30) {
00053         lcd.printString("Congratulations",0,1);  
00054     lcd.printString(" u win CXK",0,2);
00055     lcd.printString("GAME OVER",0,4);
00056     lcd.refresh();
00057    }     
00058 }
00059 
00060 void cxkEngine::update(Gamepad &pad)
00061 {
00062     check_goal(pad);
00063     // important to update CXKs and ball before checking collisions so can
00064     // correct for it before updating the display
00065   
00066     _cxkp.update(_d,_vara);
00067     _ball.update();
00068 
00069     check_wall_collision(pad);
00070     check_CXK_collisions(pad);
00071 }
00072 
00073 void cxkEngine::check_wall_collision(Gamepad &pad)
00074 {
00075     // read current ball attributes
00076     Vector2D ball_pos = _ball.get_pos();
00077     Vector2D ball_velocity = _ball.get_velocity();
00078 
00079     // check if hit top wall
00080     if (ball_pos.y <= 1) {  //  1 due to 1 pixel boundary
00081         ball_pos.y = 1;  // bounce off ceiling without going off screen
00082         ball_velocity.y = -ball_velocity.y;
00083         // audio feedback
00084         pad.tone(650.0,0.1);
00085     }
00086     else if (ball_pos.x <= 1) {  //  1 due to 1 pixel boundary
00087         ball_pos.x = 1;  // bounce off ceiling without going off screen
00088         ball_velocity.x = -ball_velocity.x;
00089         // audio feedback
00090         pad.tone(750.0,0.1);
00091     }
00092     // check if hit right wall
00093     else if (ball_pos.x + _ball_size >= (WIDTH-1) ) { // bottom pixel is 47
00094         // hit bottom
00095         ball_pos.x = (WIDTH-1) - _ball_size;  // stops ball going off screen
00096         ball_velocity.x = -ball_velocity.x;
00097         // audio feedback
00098         pad.tone(750.0,0.1);
00099     }
00100     
00101     // check if hit bottom wall
00102     else if (ball_pos.y + _ball_size >= (HEIGHT-1) ) { // bottom pixel is 47
00103         // hit bottom
00104         ball_pos.y = (HEIGHT-1) - _ball_size;  // stops ball going off screen
00105         ball_velocity.y = -ball_velocity.y;
00106         // audio feedback
00107         pad.tone(650.0,0.1);
00108     }
00109 
00110     // update ball parameters
00111     _ball.set_velocity(ball_velocity);
00112     _ball.set_pos(ball_pos);
00113 }
00114 
00115 void cxkEngine::check_CXK_collisions(Gamepad &pad)
00116 {
00117     // read current ball attributes
00118     Vector2D ball_pos = _ball.get_pos();
00119     Vector2D ball_velocity = _ball.get_velocity();
00120 
00121   
00122     // see if ball has hit the
00123 
00124     // check cxkp next
00125     Vector2D cxkp_pos = _cxkp.get_pos();
00126 
00127     // see if ball has hit the CXK 
00128     if ((ball_pos.y - cxkp_pos.y >= -5) && 
00129         (ball_pos.y - cxkp_pos.y <= 5) &&
00130         (ball_pos.x - cxkp_pos.x >= -5) && 
00131         (ball_pos.x - cxkp_pos.x <=  5)
00132          ) //right
00133      {
00134         // if it has, change the position and reflect x,y  velocity
00135         ball_pos.x= ball_pos.x + 3;
00136         ball_pos.y = ball_pos.y + 1;
00137          ball_velocity.x = -ball_velocity.x ;
00138       ball_velocity.y = -ball_velocity.y;
00139         // audio feedback
00140         pad.tone(1000.0,0.1);
00141         pad.tone(900.0,0.1);
00142         pad.tone(800.0,0.1);
00143     }
00144     
00145         // write new attributes
00146     _ball.set_velocity(ball_velocity);
00147     _ball.set_pos(ball_pos);
00148 }
00149 
00150 void cxkEngine::check_goal(Gamepad &pad)
00151 {
00152     Vector2D ball_pos = _ball.get_pos();
00153     Vector2D cxkp_pos = _cxkp.get_pos();
00154     // cxkp has scored if the ball in the basket
00155     if (
00156         (ball_pos.y <= 25) &&
00157         (ball_pos.y >= 15) && 
00158         (ball_pos.x <= 6) && 
00159         (ball_pos.x >= 1)  
00160         
00161      ) {
00162         _cxkp.add_score();
00163         _ball.init(_ball_size,_speed,_direction);
00164         pad.tone(1500.0,0.5);
00165         pad.leds_on();
00166         wait(0.5); // wiat show the goal 
00167         pad.leds_off();
00168     }
00169    
00170 }
00171 
00172 void cxkEngine::print_scores(N5110 &lcd)
00173 {
00174     // get scores from CXKs
00175    
00176     int cxkp_score = _cxkp.get_score();
00177 
00178     // print to LCD i
00179     // font is 8 wide, so leave 4 pixel 2e from middle assuming two digits
00180     char buffer2[14];
00181     sprintf(buffer2,"%2d",cxkp_score);
00182     lcd.printString(buffer2,40 ,1);
00183    
00184 }
00185