void GWindow::messageLoop(GWindow* dialogWnd /* = 0 */) { XEvent event; //+++ // printf("Message loop...\n"); //+++ while ( m_NumCreatedWindows > 0 && (dialogWnd == 0 || dialogWnd->m_Window != 0) ) { //... XNextEvent(m_Display, &event); if (!getNextEvent(event)) { // Sleep a bit timeval dt; dt.tv_sec = 0; dt.tv_usec = 10000; // 0.01 sec select(1, 0, 0, 0, &dt); // sleep... continue; } // printf("got event: type=%d\n", event.type); dispatchEvent(event); } while (getNextEvent(event)) { dispatchEvent(event); } }
void* sdEntity::getNextValue(double time, EDescriptor descriptor){ sdEvent *evt = getNextEvent(time, descriptor); if(evt){ return evt->getValue(); } return NULL; }
StatusWith<ClusterQueryResult> RouterStageMerge::awaitNextWithTimeout(ExecContext execCtx) { invariant(_params->tailableMode == TailableMode::kTailableAndAwaitData); // If we are in kInitialFind or kGetMoreWithAtLeastOneResultInBatch context and the ARM is not // ready, we don't block. Fall straight through to the return statement. while (!_arm.ready() && execCtx == ExecContext::kGetMoreNoResultsYet) { auto nextEventStatus = getNextEvent(); if (!nextEventStatus.isOK()) { return nextEventStatus.getStatus(); } auto event = nextEventStatus.getValue(); // Block until there are further results to return, or our time limit is exceeded. auto waitStatus = _executor->waitForEvent( getOpCtx(), event, awaitDataState(getOpCtx()).waitForInsertsDeadline); if (!waitStatus.isOK()) { return waitStatus.getStatus(); } // Swallow timeout errors for tailable awaitData cursors, stash the event that we were // waiting on, and return EOF. if (waitStatus == stdx::cv_status::timeout) { _leftoverEventFromLastTimeout = std::move(event); return ClusterQueryResult{}; } } // We reach this point either if the ARM is ready, or if the ARM is !ready and we are in // kInitialFind or kGetMoreWithAtLeastOneResultInBatch ExecContext. In the latter case, we // return EOF immediately rather than blocking for further results. return _arm.ready() ? _arm.nextReady() : ClusterQueryResult{}; }
bool StarTrekEngine::popNextEvent(TrekEvent *e, bool poll) { if (!getNextEvent(e, poll)) return false; removeNextEvent(); return true; }
bool MasterConfig::run( co::Object* frameData ) { LBASSERT( _objects ); if( frameData ) LBCHECK( _objects->register_( frameData, OBJECTTYPE_FRAMEDATA )); _objects->setFrameData( frameData ); seq::Application* const app = getApplication(); while( isRunning( )) { startFrame(); if( getError( )) LBWARN << "Error during frame start: " << getError() << std::endl; finishFrame(); while( !needRedraw( )) // wait for an event requiring redraw { if( app->hasCommands( )) // execute non-critical pending commands { app->processCommand(); handleEvents(); // non-blocking } else // no pending commands, block on user event { const eq::EventCommand& event = getNextEvent(); if( !handleEvent( event )) LBVERB << "Unhandled " << event << std::endl; } } handleEvents(); // process all pending events } finishAllFrames(); return true; }
int main(void) { // instantiate window GWindow window = newGWindow(320, 240); // instantiate circle GOval circle = newGOval(0, 0, 50, 50); // x, y, width, height // add circle to window add(window, circle); // follow mouse forever while (true) { // check for mouse event GEvent event = getNextEvent(MOUSE_EVENT); // if we heard one if (event != NULL) { // if the event was movement if (getEventType(event) == MOUSE_MOVED) { // ensure circle follows top cursor double x = getX(event) - getWidth(circle) / 2; // Subtract off the radius to center the circle double y = getY(event) - getWidth(circle) / 2; setLocation(circle, x, y); } } } }
void System::waitForBack() { while (!isBack() && !isClosing() && !isRestart()) { MAEvent event = getNextEvent(); if (event.type == EVENT_TYPE_KEY_PRESSED && event.key == SB_KEY_BACKSPACE) { break; } } }
A_INT16 hwGetNextEvent ( A_UINT16 devIndex, void *pBuf ) { MDK_WLAN_DEV_INFO *pdevInfo; pdevInfo = globDrvInfo.pDevInfoArray[devIndex]; return getNextEvent(pdevInfo, (EVENT_STRUCT *)pBuf); }
void VisualizeSquareMazeRunner::waitForMouseClick (void) { eventRecord e; int type; do { type = getNextEvent(&e); if (type == EXPOSE) { flush(); } } while (type != MOUSE); }
void Scheduler::run() { for (;;) { for(std::list<Checker>::iterator it = _list.begin(); it != _list.end(); it++) { if (it->timesUp()) it->start(); } Sleep(getNextEvent()); } }
unsigned long MidiFileIn :: getNextMidiEvent( std::vector<unsigned char> *midiEvent, unsigned int track ) { // Fill the user-provided vector with the next MIDI event in the // specified track (default = 0) and return the event delta time in // ticks. Meta-Events preceeding this event are skipped and ignored. if ( track >= nTracks_ ) { errorString_ << "MidiFileIn::getNextMidiEvent: invalid track argument (" << track << ")."; handleError( StkError::FUNCTION_ARGUMENT ); } unsigned long ticks = getNextEvent( midiEvent, track ); while ( midiEvent->size() && ( midiEvent->at(0) >= 0xF0 ) ) { //for ( unsigned int i=0; i<midiEvent->size(); i++ ) //std::cout << "event byte = " << i << ", value = " << (int)midiEvent->at(i) << std::endl; ticks = getNextEvent( midiEvent, track ); } //for ( unsigned int i=0; i<midiEvent->size(); i++ ) //std::cout << "event byte = " << i << ", value = " << (int)midiEvent->at(i) << std::endl; return ticks; }
int selectMode(GWindow window) { int mode = 0; //button for 1 player mode GImage single = newGImage("single.png"); add(window, single); setLocation(single, WIDTH/2 - getWidth(single)/2, HEIGHT/2 - 45); //button for 2 player mode GImage two = newGImage("two.png"); add(window, two); setLocation(two, WIDTH/2 - getWidth(two)/2, HEIGHT/2 + 45); while (true) { // check for mouse event GEvent event = getNextEvent(MOUSE_EVENT); // if we heard one if (event != NULL) { // if the event was movement if (getEventType(event) == MOUSE_CLICKED) { // print click's coordinates //printf("%.0f,%.0f\n", getX(event), getY(event)); GObject object = getGObjectAt(window, getX(event), getY(event)); if(object != NULL && object == single ) { mode = 1; break; } if(object != NULL && object == two ) { mode = 2; break; } } } } // remove the buttons removeGWindow(window, single); removeGWindow(window, two); return mode; }
Event* addFileEvent(EventLoop *loop, int sfd, void (*callback)(EventLoop*, void*), void *data, int op) { Event *evt=getNextEvent(loop); PRINT("add file event %p", evt); assert(evt!=NULL); evt->fd=sfd; evt->callback=callback; evt->type=FILE_EVT; evt->data=data; addEvent(loop, evt, op); return evt; }
int main() { printf("Enter Time Limit : "); scanf("%f",&TIME_LIMIT); int i, j; srand(time(NULL)); Heap *heap; Ball **ball; Interaction *nextCollision; heap = initHeap(); ball = (Ball **)malloc(PARTICLE_COUNT * sizeof(Ball *)); gnuplotPipe = initPipe(); for(i = 0; i < PARTICLE_COUNT; i++) { logFile[i] = initGraph(i); gnuplotGraphPipe[i] = initGraphPipe(i); } for(i = 0; i < PARTICLE_COUNT; i++) { ball[i] = initBallRandom(i); insertToHeap(heap, eventWallCollideX(heap, ball[i])); insertToHeap(heap, eventWallCollideY(heap, ball[i])); for(j = 0; j < i; j++) insertToHeap(heap, eventBallCollide(heap, ball[i], ball[j])); } while(sim_time < TIME_LIMIT) { nextCollision = getNextEvent(heap); simulateTo(ball, nextCollision->tstamp); printf("Collision at t=%lf\n", sim_time); resolveCollision(nextCollision); removeFromHeap(heap, nextCollision->interactee->id); scheduleEvent(ball, heap, nextCollision->interactee->id); if(nextCollision->interactor != NULL) { removeFromHeap(heap, nextCollision->interactor->id); scheduleEvent(ball, heap, nextCollision->interactor->id); } } fprintf(gnuplotPipe, "quit\n"); saveGraph(ball); showGraph(ball); return 0; }
int main(int argc, char* argv[]) { printf("hello world\n"); X11_data xd = initialise_x11(1000, 100); printf("hello world\n"); TextBox tb = TextBox(xd, 12, 12, "HEllo world!", "9*15"); tb.setId("textbox1"); addGraphicItem(tb); //TODO: Create a reference system that will allow events to be passed to and from //using a really easy function like OGetNextEvent(xd, [XEventMask]) while((getNextEvent(xd, StructureNotifyMask | KeyPressMask)) != NULL) { //printf("test\n"); tb.Draw(false); //At this point I have realised that there are critical failures in the //underlying infastructure that this software will eventually rely on //a render pipeline and a stack of discriptive objects which is passed into a //rendering function. } return 0; }
Event* addTimerEvent(EventLoop *loop, u16 timeout, bool cycle, void (*callback)(EventLoop*, void*), void *data) { Event *evt=getNextEvent(loop); /* assert(evt!=NULL); */ /* */ /* evt->fd=timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC); */ /* */ /* struct timespec to={.tv_sec=timeout, .tv_nsec=0}; */ /* struct timespec nulltimeout={.tv_sec=0, .tv_nsec=0}; */ /* struct itimerspec timer={.it_interval=nulltimeout, .it_value=to};; */ /* if(cycle) */ /* timer.it_interval=to; */ /* if(timerfd_settime(evt->fd, 0, &timer, NULL) == -1) */ /* printf("settimeerror\n"); */ /* evt->callback=callback; */ /* evt->type=TIMER_EVT; */ /* evt->data=data; */ /* */ /* addEvent(loop, evt, EV_READ); */ PRINT("add timer evt"); return evt; }
/** * Detects the paddle based on the location of the mouse pointer */ void checkForEvent(Paddle *bat) { GEvent event = getNextEvent(MOUSE_EVENT); if (event != NULL) { if (getEventType(event) == MOUSE_MOVED) { double xCoordinate = getX(event) - getWidth(bat->paddle) / 2; setLocation(bat->paddle, xCoordinate, HEIGHT - PADDLE_HEIGHT - 100); // mouse moving right if (xCoordinate > bat->x) { strcpy(bat->direction, "right\0"); } else if(xCoordinate < bat->x) { strcpy(bat->direction, "left\0"); } bat->x = xCoordinate; } } }
int main(void) { // instantiate window GWindow window = newGWindow(320, 240); // listen forever while (true) { // check for mouse event GEvent event = getNextEvent(MOUSE_EVENT); // if we heard one if (event != NULL) { // if the event was movement if (getEventType(event) == MOUSE_CLICKED) { // print click's coordinates printf("%.0f,%.0f\n", getX(event), getY(event)); } } } }
int main(void) { GWindow window = newGWindow(320, 240); GOval circle = newGOval(0,0,50,50); setColor(circle, "RED"); setFilled(circle, true); add(window, circle); while ( true ) { GEvent event = getNextEvent( MOUSE_EVENT ); if( event != NULL ) { if( getEventType(event) == MOUSE_MOVED ) { double x = getX(event) - getWidth(circle) / 2; double y = getY(event) - getWidth(circle) /2; setLocation(circle, x, y); } } } }
//----------------------------------------------------------------------------- // // VMotionEvent::onTrigger( pDelta, pDelta ); // // The path object is told to move to the next node. If this event corresponds // to Node 0, the object will move to Node 1. If the object reaches the node // before the next event is triggered, then the object will stop moving. // // The object's position is only reset when the track is reset and not when an // event is triggered. // //----------------------------------------------------------------------------- void VMotionEvent::onTrigger( const S32 &pTime, const S32 &pDelta ) { Parent::onTrigger( pTime, pDelta ); // Fetch Parent Track. VMotionTrack *track; if ( !getTrack( track ) ) { // Invalid Track. return; } // Fetch Path & Reference Object. VTorque::PathObjectType *path = track->getPath(); VTorque::SceneObjectType *object = getSceneObject(); if ( !path || !object ) { // Invalid. return; } // Valid Destination Node? if ( !isControllerLooping() && !getNextEvent() ) { // Clear Active. VTorque::setPathObjectActive( path, object, false ); // Quit. return; } // Set Active. VTorque::setPathObjectActive( path, object, true ); // Apply Speed. VTorque::setPathObjectSpeed( path, object, getObjectSpeed() ); }
int main(void) { // seed pseudorandom number generator srand48(time(NULL)); // instantiate window GWindow window = newGWindow(WIDTH, HEIGHT); // instantiate bricks initBricks(window); // instantiate ball, centered in middle of window GOval ball = initBall(window); // instantiate paddle, centered at bottom of window GRect paddle = initPaddle(window); // instantiate scoreboard, centered in middle of window, just above ball GLabel label = initScoreboard(window); // number of bricks initially int bricks = COLS * ROWS; // number of lives initially int lives = LIVES; // number of points initially int points = 0; // initial velocity double Xvelocity = drand48(); double Yvelocity = 2.0; // keep playing until game over while (lives > 0 && bricks > 0) { // move circle along y-axis move(ball, Xvelocity, Yvelocity); // bounce off top edge of window if (getY(ball) <= 0) { Yvelocity = -Yvelocity; } // ball hits bottom edge, lose life if (getY(ball) + getHeight(ball) >= getHeight(window)) { lives = lives - 1; removeGWindow(window, ball); waitForClick(); ball = initBall(window); pause(20); Yvelocity = -Yvelocity; } // bounce off right edge of window if (getX(ball) + getWidth(ball) >= getWidth(window)) { Xvelocity = -Xvelocity; } // bounce off left edge of window if (getX(ball) <= 0) { Xvelocity = -Xvelocity; } // linger before moving again pause(10); // check for mouse event GEvent event = getNextEvent(MOUSE_EVENT); // if we heard one if (event != NULL) { // if the event was movement if (getEventType(event) == MOUSE_MOVED) { // ensure grect follows top cursor double x = getX(event) - pWIDTH / 2; double y = 500; setLocation(paddle, x, y); } } //check for ball collision GObject object = detectCollision(window, ball); //if there was a collision if (object != NULL) { //if it collided with the paddle if (object == paddle) { Yvelocity = -Yvelocity; } //removes brick, updates scores else if (strcmp(getType(object), "GRect") == 0) { Yvelocity = -Yvelocity; removeGWindow(window, object); points = points + 1; updateScoreboard(window, label, points); } //Do not detect scoreboard if (strcmp(getType(object), "GLabel") == 0) { return 0; } } } // wait for click before exiting waitForClick(); // game over closeGWindow(window); return 0; }
int main(int argv, string args[] ) { int isGod= -1; if (argv ==2) { if (strcmp(args[1] ,"GOD")==0) { isGod = 1;; //printf("args %s \n",args[1]); } } // seed pseudorandom number generator srand48(time(NULL)); // instantiate window GWindow window = newGWindow(WIDTH, HEIGHT); // instantiate bricks initBricks(window); // instantiate ball, centered in middle of window GOval ball = initBall(window); // instantiate paddle, centered at bottom of window GRect paddle = initPaddle(window); // instantiate scoreboard, centered in middle of window, just above ball GLabel label = initScoreboard(window); // number of bricks initially int bricks = COLS * ROWS; // number of lives initially int lives = LIVES; // number of points initially int points = 0; // Variable declation for whiel double velocityX = drand48()+5; double velocityY = drand48()+1; // keep playing until game over while (lives > 0 && bricks > 0) { // ball beyond paddle if( (getY(ball) + getWidth(ball)) > getHeight(window) ) { lives--; removeGWindow(window, ball); freeGObject(ball); if(lives <= 0) { break; } initBall(window); } waitForClick(); // paddle moment if (isGod == -1) { GEvent event = getNextEvent(MOUSE_EVENT); if(event !=NULL) { //printf("before MOUSE_MOVED event\n"); if(getEventType(event) == MOUSE_MOVED) { double x = getX(event); // repositioning paddle from going beyond the window's width if(x + getWidth(paddle) > getWidth(window)) { x = getWidth(window) - getWidth(paddle); } double y = getHeight(window)-SPACE - getHeight(paddle); setLocation(paddle, x , y ); } //printf("After MOUSE_MOVED event\n"); } } else if(isGod == 1)//Its GOD mode { double ballX = getX(ball); double ballY = getX(ball); double x = ballX; if(x + getWidth(paddle) > getWidth(window)) { x = getWidth(window) - getWidth(paddle); } double y = getHeight(window)-SPACE - getHeight(paddle); setLocation(paddle, x , y ); } // Accelerate x cordinates of the ball i.e. left and right if( getX(ball) + getWidth(ball) > getWidth(window) ) { velocityX = - velocityX ; } else if( getX(ball) <= 0) { velocityX = - velocityX; } // Acceleration control check for top of the window if( getY(ball) < 0) { velocityY = - velocityY; } // Acceleration control if ball collided with paddle or brick GObject object = detectCollision(window,ball); if( object != NULL) { // Acceleration control if ball collided with paddle if( object == paddle) { velocityY = - velocityY; //move(ball, velocityX, velocityY); } // Ignore collision if (strcmp(getType(object), "GLabel") == 0) { //do nothing } // Acceleration, ScoreBoard, brick control if ball collided with brick if( (strcmp(getType(object), "GRect") == 0) && getY(ball) < HEIGHT / 2) { velocityY = - velocityY; //update points string colorB = getColorGObject(object); // return type is "#rrggbb" if(strcmp(colorB,"RED")==0 ) { points = points+5; } else if(strcmp(colorB,"ORANGE")==0) { points = points+4; } else if(strcmp(colorB,"YELLOW")==0 ) { points = points+3; } else if(strcmp(colorB,"GREEN")==0) { points = points+2; } else if(strcmp(colorB,"CYAN")==0) { points++; } // shrink bat with points increased if( points % 10 == 0 ) { double x = getX(paddle); double y = getY(paddle); paddleL -= 5; removeGWindow(window, paddle); paddle = newGRect(x, y, paddleL, paddleH); setFilled(paddle, true); setColor(paddle,"BLACK"); add(window,paddle); //paddle = initPaddle(window) } //update Scoreboard updateScoreboard(window, label, points); //brick removeGWindow(window, object); bricks--; if(bricks < 0) { break; } } } // move ball move(ball, velocityX, velocityY); pause(10); } // wait for click before exiting waitForClick(); // game over closeGWindow(window); return 0; }
int main(void) { // instantiate window GWindow window = newGWindow(WIDTH, HEIGHT); // instantiate bricks initBricks(window); // instantiate ball, centered in middle of window GOval ball = initBall(window); // instantiate paddle, centered at bottom of window GRect paddle = initPaddle(window); // instantiate scoreboard, centered in middle of window, just above ball GLabel label = initScoreboard(window); // number of bricks initially int bricks = COLS * ROWS; // number of lives initially int lives = LIVES; // number of points initially int points = 0; // wait for click before starting waitForClick(); velocityX = 1.0; velocityY = 2.5; // keep playing until game over while (lives > 0 && bricks > 0) { // Scoreboard updateScoreboard(window, label, points); // move ball move(ball, velocityX, velocityY); pause(10); // check for mouse event. GEvent event = getNextEvent(MOUSE_EVENT); // Lock the paddle X to the cursor. if (event != NULL) { // if the event was movement if (getEventType(event) == MOUSE_MOVED) { // ensure paddle follows top cursor double x = getX(event) - getWidth(paddle) / 2; double y = 500; setLocation(paddle, x, y); } } GObject object = detectCollision(window, ball); if (object != NULL) { // If the ball hits the paddle. if (object == paddle) { velocityY = -velocityY; } // If the ball hits a block. Remove block, add a point, decrement count and bounce. else if (strcmp(getType(object), "GRect") == 0) { removeGWindow(window, object); velocityY = -velocityY; points++; bricks--; } } // If the ball hits the right wall. if (getX(ball) + getWidth(ball) >= getWidth(window)) { velocityX = -velocityX; } // If the ball hits the left wall. if (getX(ball) <= 0) { velocityX = -velocityX; } // If the ball hits the top wall. if (getY(ball) <= 0) { velocityY = -velocityY; } // Remove a life. Start over. if (getY(ball) + getHeight(ball) >= getHeight(window)) { lives--; //move ball to start setLocation(ball, 190, 200); //move paddle to start setLocation(paddle, 160, 500); waitForClick(); } } // You Lose Label Message for kicks. if (bricks > 0) { GLabel game_over = newGLabel("YOU LOSE!"); setFont(game_over, "SansSerif-70"); setColor(game_over, "RED"); add(window, game_over); setLocation(game_over, 15, 300); } else { GLabel game_over = newGLabel("YOU WIN!"); setFont(game_over, "SansSerif-70"); setColor(game_over, "GREEN"); add(window, game_over); setLocation(game_over, 15, 300); } // wait for click before exiting waitForClick(); // game over closeGWindow(window); return 0; }
// first event sdEvent* sdEntity::getFirstEvent(const EDescriptor descriptor) const{ return getNextEvent(-1.0, descriptor); }
int main(void) { // seed pseudorandom number generator srand48(time(NULL)); // instantiate window GWindow window = newGWindow(WIDTH, HEIGHT); // instantiate bricks initBricks(window); // instantiate ball, centered in middle of window GOval ball = initBall(window); // instantiate paddle, centered at bottom of window GRect paddle = initPaddle(window); // instantiate scoreboard, centered in middle of window, just above ball GLabel label = initScoreboard(window); // number of bricks initially int bricks = COLS * ROWS; // number of lives initially int lives = LIVES; // number of points initially int points = 0; // make the ball’s initial velocity along its (horizontal) x-axis random. srand48((long int) time(NULL)); double velocityX = -BALL_VELOCITY + 2 * BALL_VELOCITY * drand48(); double velocityY = BALL_VELOCITY; while (true) { GEvent event = getNextEvent(MOUSE_EVENT); if (event != NULL) { if (getEventType(event) == MOUSE_CLICKED) { break; } } } // keep playing until game over while (lives > 0 && bricks > 0) { // TODO move(ball, velocityX, velocityY); GEvent event = getNextEvent(MOUSE_EVENT); if (event != NULL) { if (getEventType(event) == MOUSE_MOVED) { double x = 0; if (getX(event) - PADDLE_WIDTH / 2 <= 0) { x = 0; } else if (getX(event) + PADDLE_WIDTH / 2 >= WIDTH) { x = WIDTH - PADDLE_WIDTH; } else { x = getX(event) - PADDLE_WIDTH / 2; } double y = getY(paddle); setLocation(paddle, x, y); } } if (getX(ball) + 2 * RADIUS >= WIDTH) { velocityX = -velocityX; } else if (getX(ball) <= 0) { velocityX = -velocityX; } /*// the bottom edge condition else if (getY(ball) + 2 * RADIUS >= HEIGHT) { velocityY = -velocityY; } */ else if (getY(ball) > getY(paddle) + PADDLE_WIDTH / 2) { lives--; if (lives > 0) { while (true) { GEvent event = getNextEvent(MOUSE_EVENT); if (event != NULL) { if (getEventType(event) == MOUSE_CLICKED) { break; } } } setLocation(ball, (getWidth(window) - 2 * RADIUS) / 2, (getHeight(window) - 2 * RADIUS) / 2); move(ball, velocityX, velocityY); } } else if (getY(ball) <= 0) { velocityY = -velocityY; } GObject object = detectCollision(window, ball); if (object != NULL) { if (object == paddle) { // TODO velocityY = -velocityY; } /*// More generally with the same functionality if (strcmp(getType(object), "GRect") == 0) { // TODO } */ if (strcmp(getType(object), "GLabel") == 0) { // TODO sendToBack(object); } if (strcmp(getType(object), "GRect") == 0 && object != paddle) { // TODO bricks--; removeGWindow(window, object); updateScoreboard(window, label, ++points); velocityY = -velocityY; } } pause(8); } // wait for click before exiting waitForClick(); // game over closeGWindow(window); return 0; }
int main(void) { // seed pseudorandom number generator srand48(time(NULL)); // instantiate window GWindow window = newGWindow(WIDTH, HEIGHT); // instantiate bricks initBricks(window); // instantiate life counter GOval lifeball1 = newGOval(WIDTH - 30, 20, DIAMETER, DIAMETER); setFilled(lifeball1, true); setColor(lifeball1, "#000000"); add(window, lifeball1); GOval lifeball2 = newGOval(WIDTH - 50, 20, DIAMETER, DIAMETER); setFilled(lifeball2, true); setColor(lifeball2, "#000000"); add(window, lifeball2); // instantiate ball, centered in middle of window GOval ball = initBall(window); // instantiate paddle, centered at bottom of window GRect paddle = initPaddle(window); // instantiate scoreboard, centered in middle of window, just above ball GLabel scoreboard = initScoreboard(window); // number of bricks initially int bricks = COLS * ROWS; // number of lives initially int lives = LIVES; // number of points initially int points = 0; //initialize ball velocities double xvelocity = drand48(); double yvelocity = 2.0; // keep playing until game over if (lives > 0) { waitForClick(); } while (lives > 0 && bricks > 0) { // compel paddle to follow mouse along x-axis by listening for mouse_event GEvent paddlemove = getNextEvent(MOUSE_EVENT); // if there was an event if (paddlemove != NULL) { // if the event was movement if (getEventType(paddlemove) == MOUSE_MOVED) { // make paddle follow mouse pointer's motion along the x axis only double paddlex = getX(paddlemove) - getWidth(paddle) / 2; double paddley = 565; setLocation(paddle, paddlex, paddley); } } // move ball along both x and y move(ball, xvelocity, yvelocity); GObject collided = detectCollision(window, ball); // Correct code to make ball bounce - this does not work, despite working exactly as so in bounce.c //perhaps a switch is in order using detect collision for bouncing if (getX(ball) + DIAMETER >= WIDTH) { xvelocity = -xvelocity; } // bounce off left edge of window else if (getX(ball) <= 0) { xvelocity = -xvelocity; } else if (getY(ball) <= 0) { yvelocity = -yvelocity; } else if (collided == paddle) { yvelocity = -yvelocity; xvelocity = xvelocity + (drand48() - drand48()); } else if ((((collided != NULL) && (collided != paddle)) && (collided != scoreboard)) && (strcmp(getType(collided), "GOval") != 0)) { yvelocity = -yvelocity; xvelocity = xvelocity + (drand48() - drand48()); removeGWindow(window, collided); points++; bricks = bricks - 1; updateScoreboard(window, scoreboard, points); } else if (getY(ball) + DIAMETER >= HEIGHT) { yvelocity = 2.0; xvelocity = drand48(); lives = lives - 1; if (lives == 2) { removeGWindow(window, lifeball2); } else if (lives == 1) { removeGWindow(window, lifeball1); } setLocation(ball, (WIDTH / 2) - (DIAMETER / 2), (HEIGHT / 2) - (DIAMETER / 2)); setLocation(paddle, (WIDTH / 2) - (PADDLEWIDTH / 2), HEIGHT - 35); if (lives > 0) { waitForClick(); } } // linger before moving again pause(10); } // game over if (lives > 0 && bricks == 0) { GLabel winner = newGLabel("YOU WON!"); setFont(winner, "SansSerif-22"); double x = (WIDTH / 2) - (getWidth(winner) / 2); double y = HEIGHT - 250; setLocation(winner, x, y); setColor(winner, "#0033FF"); add(window, winner); } else { GLabel gameover = newGLabel("GAME OVER"); setFont(gameover, "SansSerif-22"); double x = (WIDTH / 2) - (getWidth(gameover) / 2); double y = HEIGHT - 250; setLocation(gameover, x, y); setColor(gameover, "#990000"); add(window, gameover); } waitForClick(); return 0; }
int main(void) { // seed pseudorandom number generator srand48(time(NULL)); // instantiate window GWindow window = newGWindow(WIDTH, HEIGHT); // instantiate bricks initBricks(window); // instantiate ball, centered in middle of window GOval ball = initBall(window); // instantiate paddle, centered at bottom of window GRect paddle = initPaddle(window); // instantiate scoreboard, centered in middle of window, just above ball GLabel label = initScoreboard(window); // number of bricks initially int bricks = COLS * ROWS; // number of lives initially int lives = LIVES; // number of points initially int points = 0; // keep playing until game over double velocity = drand48()*2; double angle = drand48()*3; while (lives > 0 && bricks > 0) { // check for mouse event GEvent event = getNextEvent(MOUSE_EVENT); // if we heard one if (event != NULL) { // if the event was movement if (getEventType(event) == MOUSE_MOVED) { double x = getX(event)-PADDLEW/2; double y = HEIGHT - PADDLEH; setLocation(paddle, x, y); } } move(ball, velocity, angle); GObject object = detectCollision(window, ball); // bounce off right edge of window if (getX(ball) + getWidth(ball) > 400) { velocity = -velocity; } // bounce off left edge of window else if (getX(ball) <= 0) { velocity = -velocity; } // Dtects if the ball hit the paddle then reverses angle else if(object == paddle) { angle = -angle; } //Detects if it hits the bottom of the screen else if(getY(ball) + getHeight(ball) > HEIGHT) { removeGWindow(window, ball); waitForClick(); ball = initBall(window); lives -= 1; } else if (getY(ball) <= 0) { angle = -angle; } else if (strcmp(getType(object), "GRect") == 0) { angle = -angle; removeGWindow(window, object); initScoreboard(window); updateScoreboard(window, label, points); points += 1; if(points == 50) { break; } } // linger before moving again pause(5); } // wait for click before exiting waitForClick(); // game over closeGWindow(window); return 0; }
int main(void) { // seed pseudorandom number generator srand48(time(NULL)); // instantiate window GWindow window = newGWindow(WIDTH, HEIGHT); // instantiate bricks initBricks(window); // instantiate ball, centered in middle of window GOval ball = initBall(window); // instantiate paddle, centered at bottom of window GRect paddle = initPaddle(window); // instantiate scoreboard, centered in middle of window, just above ball GLabel label = initScoreboard(window); // number of bricks initially int bricks = COLS * ROWS; // number of lives initially int lives = LIVES; // number of points initially int points = 0; // initial velocity double standardReduction = 0.07; double velocity = drand48() - standardReduction; double velocityY = 2; waitForClick(); // keep playing until game over while (lives > 0 && bricks > 0) { // Get the next mouse event GEvent event = getNextEvent(MOUSE_EVENT); // If some move if (event != NULL) { if (getEventType(event) == MOUSE_MOVED) { double x = getX(event) - (PADDLE_WIDTH / 2); double y = 500; setLocation(paddle, x, y); } } move(ball, velocity, velocityY); // bounce off right edge of window if (getX(ball) + RADIUS >= WIDTH) { velocity = -velocity; } // bounce off left edge of window else if (getX(ball) <= 0) { velocity = -velocity; } // bounce off top edge else if (getY(ball) <= 0) { velocityY = -velocityY; } // bottom edge else if (getY(ball) + RADIUS >= HEIGHT) { velocityY = 0; velocity = 0; lives--; removeGWindow(window, ball); removeGWindow(window, paddle); ball = initBall(window); paddle = initPaddle(window); velocity = drand48() - standardReduction; velocityY = 2; waitForClick(); } GObject obj = detectCollision(window, ball); if (obj != NULL) { if (obj == paddle) { velocityY = -velocityY; } else if (strcmp(getType(obj), "GRect") == 0) { removeGWindow(window, obj); bricks--; points++; updateScoreboard(window, label, points); velocityY = -velocityY; } } pause(3); } if (bricks == 0) { updateScoreboard(window, label, 51); } else { updateScoreboard(window, label, 0); } // wait for click before exiting waitForClick(); // game over closeGWindow(window); return 0; }
int main(void) { // seed pseudorandom number generator srand48(time(NULL)); // instantiate window GWindow window = newGWindow(WIDTH, HEIGHT); // instantiate bricks initBricks(window); // instantiate ball, centered in middle of window GOval ball = initBall(window); // randomize x velocity of ball double xvelocity = drand48()/50 + 0.05; double yvelocity = 0.1; // instantiate paddle, centered at bottom of window GRect paddle = initPaddle(window); // instantiate scoreboard, centered in middle of window, just above ball GLabel label = initScoreboard(window); // number of bricks initially int bricks = COLS * ROWS; // number of lives initially int lives = LIVES; // number of points initially int points = 0; while (lives > 0 && bricks > 0) { GEvent event = getNextEvent(MOUSE_EVENT); // if we heard one if (event != NULL) { // if the event was movement if (getEventType(event) == MOUSE_MOVED) { // ensure paddle follows mouse double x = getX(event) - WIDTHPADDLE/2; // makes sure paddle stays within screen if (x >= 0 || x <= WIDTH) { setLocation(paddle, x, 9*HEIGHT/10); } if (x < 0) { setLocation(paddle, 0, 9*HEIGHT/10); } if (x > WIDTH - WIDTHPADDLE) { setLocation(paddle, WIDTH - WIDTHPADDLE, 9*HEIGHT/10); } } } // compels ball to move move(ball, xvelocity, yvelocity); // bounce off edge of window if (getX(ball) + getWidth(ball) >= WIDTH || getX(ball) <= 0) { xvelocity = -xvelocity; } // bouce off top edge of window if (getY(ball) <= 0) { yvelocity = -yvelocity; } // detect collision GObject object = detectCollision(window, ball); // bounce off paddle if (object == paddle) { yvelocity = -yvelocity; } // bounce off brick else if (object != NULL) { if (strcmp(getType(object), "GRect") == 0) { yvelocity = -yvelocity; bricks--; removeGWindow(window, object); points++; } } // update scoreboard updateScoreboard(window, label, points); // ball at bottom edge of window if (getY(ball) + 2*RADIUS >= HEIGHT) { lives--; if (lives >= 1) { waitForClick(); } setLocation(ball, WIDTH/2 - RADIUS, HEIGHT/2 - RADIUS); } } // wait for click before exiting waitForClick(); // game over closeGWindow(window); return 0; }
int main(void) { // seed pseudorandom number generator srand48(time(NULL)); // instantiate window GWindow window = newGWindow(WIDTH, HEIGHT); // instantiate bricks initBricks(window); // instantiate ball, centered in middle of window GOval ball = initBall(window); // instantiate paddle, centered at bottom of window GRect paddle = initPaddle(window); // instantiate scoreboard, centered in middle of window, just above ball GLabel label = initScoreboard(window); // number of bricks initially int bricks = COLS * ROWS; updateScoreboard(window, label, 0); // number of lives initially int lives = LIVES; // number of points initially int points = 0; // define ball's velocity double xvelocity = 2.5 * drand48(); double yvelocity = 2.5; waitForClick(); // keep playing until game over while (lives > 0 && bricks > 0) { // TODO //set paddle to follow mouse GEvent event = getNextEvent(MOUSE_EVENT); if (event != NULL) { if (getEventType(event) == MOUSE_MOVED) { double x = getX(event) - 25; setLocation(paddle, x, 500); } } // move ball move(ball, xvelocity, yvelocity); // bounce from side walls if (getX(ball) + getWidth(ball) >= getWidth(window)) { xvelocity = -xvelocity; } else if (getX(ball) <= 0) { xvelocity = -xvelocity; } // bounce from ceiling or floor if (getY(ball) + getHeight(ball) >= getHeight(window)) { lives--; removeGWindow(window, ball); ball = initBall(window); waitForClick(); continue; } else if (getY(ball) <= 0) { yvelocity = -yvelocity; } // detect collision between objects GObject object = detectCollision(window, ball); if (object != NULL) { // bounce off paddle if (object == paddle) { if (yvelocity > 0.0) { yvelocity = -yvelocity; } } // bounce off and remove block else if (strcmp(getType(object), "GRect") == 0) { yvelocity = -yvelocity; bricks--; updateScoreboard(window, label, 50 - bricks); removeGWindow(window, object); } } pause(10); } // wait for click before exiting waitForClick(); // game over closeGWindow(window); return 0; }