bool detect(Spaceship spaceship,Pattern pattern){ XPoint x1; XPoint x2; x1.x=spaceship.getX(); x2.x=spaceship.getX()+spaceship.getWidth(); x1.y=spaceship.getY()+spaceship.getHeight(); x2.y=spaceship.getY()+spaceship.getHeight(); line_function bottom(x1,x2); cout<<"spaceship at ("<<x1.x<<" , "<<x1.y<<endl; for(int i=0;i<pattern.lines.size();i++){ if(pattern.lines[i].intersect(bottom)){ return true; } } return false; }
// EVENT LOOP!!!!!!!!!!!! void eventloop(XInfo& xinfo){ // add stuff to pain. listofd.push_front(&apolo); listofd.push_front(&Starter); listofd.push_front(&pattern); listofd.push_back(&Restarter); listofd.push_back(&Winner); listofd.push_back((&vertical)); listofd.push_back(&Horizontal); listofd.push_back(&leftHorizontal); listofd.push_back(&TopMeter); listofd.push_back((&EngineStats)); //listofd.push_front((&pattern)); XEvent event; KeySym key; char text[BufferSize]; unsigned long lastRepaint = 0; int xDiff=0; int yDiff=0; //infinite loop, enter q to exit. while (true) { if (XPending(xinfo.display)>0){ XNextEvent(xinfo.display, &event); //These are engine status. if(yDiff>=0){ vertical.currentSpeed=yDiff; } if(xDiff>=0){ Horizontal.currentSpeed=xDiff; } bool d = pattern.detect(apolo,pattern); if (xDiff<=0){ leftHorizontal.currentSpeed=abs(xDiff); } if (yDiff<=0){ TopMeter.currentSpeed=abs(yDiff); } //Here to make the driver complain. if (GameIsPlaying){ if(abs(xDiff)>=3||yDiff>4){ isGoingTooFast=true; } else {isGoingTooFast=false;} } //Here after the collision happen. if(d&&GameIsPlaying){ GameIsPlaying=!GameIsPlaying; if(yDiff>5){ IsSpeadTooMuch=true; } xDiff=0; yDiff=0; cout<<"position "<<apolo.getX()<<" "<<apolo.getY()<<endl; if(IsLandingOnPad&&(!IsSpeadTooMuch)){ Winner.hide=false; } else { Restarter.hide=false; } } switch (event.type) { /* * Exit when 'q' is typed. * Arguments for XLookupString : * event - the keyboard event * text - buffer into which text will be written * BufferSize - size of text buffer * key - workstation independent key symbol * 0 - pointer to a composeStatus structure */ case Expose: if(event.xexpose.count ==0){ printf("Expose event recieved. Redrawing\n"); repaint(xinfo); } break; case KeyPress: int i =XLookupString((XKeyEvent*)&event, text, BufferSize, &key, 0 ); cout << "KeySym " << key << " text='" << text << "'" << " at " << event.xkey.time << endl; //press q to exit if ( i == 1 && text[0] == 113 ) { cout << "Terminated normally." << endl; // XCloseDisplay(xinfo.display); return; } //UP key if ( key == XK_Up ) { cout << "[Up]" << endl; if(GameIsPlaying&&yDiff>=-2){ yDiff-=1; } } //Down Key if ( key == XK_Down ) { cout << "[D]" << endl; if (GameIsPlaying&&(yDiff<=10)){ yDiff=yDiff+1; } } //Left key if ( key == XK_Left ) { cout << "[L]" << endl; if (GameIsPlaying&&(xDiff>=-10)){ xDiff=xDiff-1; } } //Right key if ( key == XK_Right ) { cout << "[D]" << endl; if((xDiff<=10)&&GameIsPlaying){ xDiff+=1; } } //Space if ( i == 1 && text[0] == 32 ) { cout << "[ ]" << endl; GameIsPlaying=!GameIsPlaying; Starter.hide=true; if(d){ apolo=Spaceship(10,10); Restarter.hide=true; if(IsLandingOnPad){ Winner.hide=true; } if(IsSpeadTooMuch){ IsSpeadTooMuch=false; } IsLandingOnPad=false; } } break; } } //cout<<"aaaa"<<endl; //Handle key press done, now do the drawing unsigned long end = now(); if (end - lastRepaint > 1000000/FPS) { if (GameIsPlaying){ handleDoubleBuffer(xinfo, xDiff, yDiff); } repaint(xinfo); lastRepaint = now(); } else if (XPending(xinfo.display) == 0) { usleep(1000000/FPS - (end - lastRepaint)); } } }