int main( int argc , char*argv[] ) { int start = 0 ; XInfo xInfo; initX(argc, argv, xInfo); while(true) { int i = eventLoop(xInfo, 0); ship.reset(); pad.reset(); pad2.reset(); leftx = pad.getx()+pad.getl(); // the right x-ixs of the first pad lefty = pad.gety(); //the right y-ixs of the first pad rightx = pad2.getx(); // the left x-ixs of the second pad righty = pad2.gety(); // the left y-ixs of the second pad mountain1.reset(0,500, pad.getx(), pad.gety()); mountain3.reset(leftx,lefty,rightx,righty); mountain2.reset( pad2.getx()+pad2.getl(),pad2.gety(),800,0); while(dList.size() > 0 ) { dList.pop_front(); } if( i == 0 ) { break; } start = 0; } XCloseDisplay(xInfo.display); }
/* function to repaint a display list Checking collisions with mountain NOTE: all the conditions for mountains are different cannot use helper function to reduce the code */ int repaint(XInfo &xinfo) { list<Displayable *>::const_iterator begin = dList.begin(); list<Displayable *>::const_iterator end = dList.end(); int status = 1; //1: nothing happened 0:landed 2: crushed float slop; int ship_point; int i; float length; float height; float expecty; int extra; int real_y; vector<int> x; vector<int> y; //big black rectangle to clear background XFillRectangle(xinfo.display, xinfo.pixmap, xinfo.gc[0], 0 , 0 , xinfo.width, xinfo.height); //draw display list while (begin != end) { Displayable *d = *begin; d->paint(xinfo); begin++; } //check if the ship landed if ( (ship.getY() >= lefty && pad.getx() <= ship.getX()+40 && ship.getX() <= leftx ) || (ship.getY() >= righty && rightx <= ship.getX()+40 && ship.getX() < rightx+pad2.getl()) ) { if(ship.getspeed() > 2) { status = 2; } else { status = 0; } } if(ship.getX() < pad.getx() ) { /*mountain 1*/ x = mountain1.getx(); y = mountain1.gety(); vector<int> indexs = findPoints(ship.getX(), ship.getX()+40, x); vector<int> points; for(int j = 0; j < indexs.size(); j ++ ) { points.push_back(y[indexs[j]]); } real_y = ship.getY(); // right point if(ship.getX() +40 < pad.getx()) { ship_point = ship.getX()+40; i = FindInterval( x, ship_point); if ( i == 0 ) { length = x[i] - 0; height = y[i] - 500; slop = height / length; extra = 500; } else if( i == x.size() ) { length = pad.getx() - x[i-1]; height = pad.gety() - y[i-1]; slop = height / length; extra = y[i-1] - x[i-1]*slop; } else { length = x[i] - x[i-1]; height = y[i] - y[i-1]; slop = height / length; extra = y[i] - x[i]*slop; } expecty = ship_point * slop +extra; if ( real_y >= expecty) { status = 2; } } // left point if( status == 1) { ship_point = ship.getX(); i = FindInterval( x, ship_point); if ( i == 0 ) { length = x[i] - 0; height = y[i] - 500; slop = height / length; extra = 500; } else if( i == x.size() ) { length = pad.getx() - x[i-1]; height = pad.gety() - y[i-1]; slop = height / length; extra = y[i-1] - x[i-1]*slop; } else { length = x[i] - x[i-1]; height = y[i] - y[i-1]; slop = height / length; extra = y[i] - x[i]*slop; } expecty = ship_point * slop +extra; if ( real_y >= expecty) { status = 2; } } //line if(lower(points, ship.getY()) && status == 1) { status = 2; } } else if( ship.getX() < pad2.getx() && ship.getX()+40 > pad.getx() + pad.getl() ) { //mountain 3 x = mountain3.getx(); y = mountain3.gety(); vector<int> indexs = findPoints(ship.getX(), ship.getX()+40, x); vector<int> points; for(int j = 0; j < indexs.size(); j ++ ) { points.push_back(y[indexs[j]]); } real_y = ship.getY(); //right point if(ship.getX() +40 > pad.getx() +pad.getl() && ship.getX() +40 < pad2.getx()) { ship_point = ship.getX()+40; i = FindInterval( x, ship_point); if ( i == 0 ) { length = x[i] - pad.getx()+pad.getl(); height = y[i] - pad.gety(); slop = height / length; extra = y[i] - x[i] *slop; } else if( i == x.size() ) { length = pad2.getx() - x[i-1]; height = pad2.gety() - y[i-1]; slop = height / length; extra = y[i-1] - x[i-1]*slop; } else { length = x[i] - x[i-1]; height = y[i] - y[i-1]; slop = height / length; extra = y[i] - x[i]*slop; } expecty = ship_point * slop +extra; if ( real_y >= expecty) { status = 2; } } //left point if(ship.getX() < pad2.getx() && ship.getX() > pad.getx() + pad.getl()) { if(status == 1) { ship_point = ship.getX(); i = FindInterval( x, ship_point); if ( i == 0 ) { length = x[i] - pad.getx()+pad.getl(); height = y[i] - pad.gety(); slop = height / length; extra = pad.gety() - (pad.getx()+pad.getl()) *slop; } else if( i == x.size() ) { length = pad2.getx() - x[i-1]; height = pad2.gety() - y[i-1]; slop = height / length; extra = y[i-1] - x[i-1]*slop; } else { length = x[i] - x[i-1]; height = y[i] - y[i-1]; slop = height / length; extra = y[i] - x[i]*slop; } expecty = ship_point * slop +extra; cout << "real_y " << real_y << endl; cout << "expecty" << expecty << endl; if ( real_y >= expecty) { status = 2; } } } //line if(lower(points,ship.getY()) && status == 1) { status = 2; } } else if( ship.getX() +40< 800 && ship.getX()+40 > pad2.getx() + pad2.getl() ) { x = mountain2.getx(); y = mountain2.gety(); vector<int> indexs = findPoints(ship.getX(), ship.getX()+40, x); vector<int> points; for(int j = 0; j < indexs.size(); j ++ ) { points.push_back(y[indexs[j]]); } real_y = ship.getY(); //right point ship_point = ship.getX()+40; i = FindInterval( x, ship_point); if ( i == 0 ) { length = x[i] - pad2.getx()+pad.getl(); height = y[i] - pad2.gety(); slop = height / length; extra = y[i] - x[i] *slop; } else if( i == x.size() ) { length = 0 - x[i-1]; height = 800 - y[i-1]; slop = height / length; extra = y[i-1] - x[i-1]*slop; } else { length = x[i] - x[i-1]; height = y[i] - y[i-1]; slop = height / length; extra = y[i] - x[i]*slop; } expecty = ship_point * slop +extra; if ( real_y >= expecty) { status = 2; } //left point if(ship.getX() > pad2.getx() &&ship.getX() < 800) { if(status == 1) { ship_point = ship.getX(); i = FindInterval( x, ship_point); if ( i == 0 ) { length = x[i] - pad2.getx()+pad2.getl(); height = y[i] - pad2.gety(); slop = height / length; extra = pad2.gety() - (pad2.getx()+pad2.getl()) *slop; } else if( i == x.size() ) { length = 0 - x[i-1]; height = 800 - y[i-1]; slop = height / length; extra = y[i-1] - x[i-1]*slop; } else { length = x[i] - x[i-1]; height = y[i] - y[i-1]; slop = height / length; extra = y[i] - x[i]*slop; } expecty = ship_point * slop +extra; if ( real_y >= expecty) { status = 2; } } } //line if(lower(points, ship.getY()) && status == 1) { status = 2; } } XCopyArea(xinfo.display , xinfo.pixmap, xinfo.window , xinfo.gc[0], 0,0,xinfo.width,xinfo.height,(xinfo.width-800)/2,(xinfo.height-600)/2); XFlush(xinfo.display); return status; }
Text(int x, int y, string s):x(x), y(y), s(s) {} private: XPoint p; int x; int y; string s; // string to show }; //=============================================================================== list<Displayable *> dList; Ship ship(40,0,0); Pad pad(50,100,200); Pad pad2(50, 500, 600); int leftx = pad.getx()+pad.getl(); // the right x-ixs of the first pad int lefty = pad.gety(); //the right y-ixs of the first pad int rightx = pad2.getx(); // the left x-ixs of the second pad int righty = pad2.gety(); // the left y-ixs of the second pad //draw mountains Mountain mountain1(10,0,500, pad.getx(), pad.gety()); Mountain mountain3(10,leftx,lefty,rightx,righty); Mountain mountain2(10, pad2.getx()+pad2.getl(),pad2.gety(),800,0); Bomb bomb(ship.getX(), ship.getY(), 40); void initX(int argc, char* argv[], XInfo &xInfo) { XSizeHints hints; unsigned long white, black; xInfo.display = XOpenDisplay( "" );