void Pong::ballPaddleCollide(int i) { if (ball[i].collideRightLast) //dont collide again with the same paddle. sometimes balls collide { //with the same paddle the next frame that they did previously. do not want. if (ball[i].pos.x + 5 > left.pos.x - padWidthHalf && ball[i].pos.x - 5 < left.pos.x + padWidthHalf) //close enough, x-wise? { if (ball[i].pos.y - 5 < left.pos.y + padHeightHalf && ball[i].pos.y + 5 > left.pos.y - padHeightHalf) //close close enough, y-wise? { ball[i].velocity.x = ball[i].velocity.x * -1; //reverse direction ball[i].collideRightLast = false; //switch the last collided paddle newBall(ball[i]); //make a new ball } } } else if (!ball[i].collideRightLast) { if (ball[i].pos.x + 5 > right.pos.x - padWidthHalf && ball[i].pos.x - 5 < right.pos.x + padWidthHalf) { if (ball[i].pos.y - 5 < right.pos.y + padHeightHalf && ball[i].pos.y + 5 > right.pos.y - padHeightHalf) { ball[i].velocity.x = ball[i].velocity.x * -1; ball[i].collideRightLast = true; newBall(ball[i]); } } } }
void gameStep() { uchar nx1,ny1,nx2,ny2,o; // Ищем готовые линии if(!check()) { // Добавляем три шарика newBall(newBall1); nx1=nx; ny1=ny; newBall(newBall2); nx2=nx; ny2=ny; newBall(newBall3); // Рисуем анимацию for(o=0; o<5; o++) { if(nx1!=-1) drawSpriteNew(nx1, ny1, newBall1, o); if(nx2!=-1) drawSpriteNew(nx2, ny2, newBall2, o); if(nx !=-1) drawSpriteNew(nx, ny, newBall3, o); delay(ANIMATION_SPEED); } // Загадываем три новых шарика randNewBall(); redrawNewBalls1(); if(!gameOver) { // Ищем готовые линии check(); // Считаем кол во свободных клеток if(calcFreeCell()==0) gameOver=1; } } drawCursor(); }
int main(int argc, char * argv[]) { int i, j, m, n, wall_cost, num_bumper, x, y, value, cost, direction, lifetime, sum = 0; if (DEBUG) printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"); scanf("%d %d\n", &m, &n); struct Obstacle * surface[51][51]; scanf("%d\n", &wall_cost); /** * initiate surface */ for (i = 1; i <= m; i++) { for (j = 1; j <= n; j++) { if (i == 1 || i == m || j == 1 || j == n) { surface[i][j] = newObstacle(0, wall_cost); } else { surface[i][j] = NULL; } } } scanf("%d\n", &num_bumper); for (i = 0; i < num_bumper; i++) { scanf("%d %d %d %d\n", &x, &y, &value, &cost); surface[x][y] = newObstacle(value, cost); } while (scanf("%d %d %d %d\n", &x, &y, &direction, &lifetime) != EOF) { struct Ball *ball = newBall(x, y, direction, lifetime); int points = runBall(surface, m, n, ball); printf("%d\n", points); sum += points; } printf("%d\n", sum); return 0; }
Elevator* Elevator::setBalls(int newBalls) { while(getNumOfBalls() > newBalls) doShoot(); while(getNumOfBalls() < newBalls) newBall(); return this; }
/***************************************************************** * Initialize the mode *****************************************************************/ void RetroMode::setup() { cout << "Setting up RETRO mode ... "; // Set up the Box2d environment box2d.init(); box2d.setGravity(0,0); box2d.setFPS(30); gameOver = false; ACTIVE = true; MAX_SCORE = 5; INITIAL_SPEED = 3; // Setup the top and bottom walls in Box2d upperWall.setup(box2d.getWorld(), 0, 0, ofGetWidth(), 10,false); lowerWall.setup(box2d.getWorld(), 0, ofGetHeight() - 10, ofGetWidth(), ofGetHeight(),false); strLeftGoal = "leftGoal"; strRightGoal = "rightGoal"; strBall = "baller"; // Start listening for collisions //ofxBox2dContactListener* listener = new pongContactListener(this); //box2d.setContactListener(listener); // Load the sounds hitLeft.loadSound("sounds/retro/hitPaddle.wav"); hitRight.loadSound("sounds/retro/hitPaddle.wav"); hitWall.loadSound("sounds/retro/hitWall.wav"); // Load the sprites leftPaddleGraphic.loadImage("images/retro/paddle.png"); rightPaddleGraphic.loadImage("images/retro/paddle.png"); gameBallGraphic.loadImage("images/retro/ball.png"); // Set up the left paddle leftPaddle.setPhysics(1000.0, 1.0, 1.0); leftPaddle.setup(box2d.getWorld(), 0, 0, 32, 155,false); leftPaddle.body->SetUserData( strLeftGoal ); // Set up the right paddle rightPaddle.setPhysics(1000.0, 1.0, 1.0); rightPaddle.setup(box2d.getWorld(), 0, 0, 32, 155,false); rightPaddle.body->SetUserData( strRightGoal ); // Setup the game ball gameBall.setPhysics(1, 1, 0); gameBall.setup(box2d.getWorld(), ofGetWidth()/2, ofGetHeight()/2, 35); gameBall.body->SetUserData( strBall ); // Spawn a new ball and get the game started! newBall(true); cout << "READY" << endl; }
/***************************************************************** * Update all variables *****************************************************************/ void RetroMode::update() { if(ACTIVE) { box2d.update(); // Move the left and right paddles to the mouse leftPaddle.moveTo(100, playerLeftY); rightPaddle.moveTo(ofGetWidth() - 100, playerRightY); // Game ball goes off left edge of screen if(gameBall.getPosition().x < 0) { if(gameOver != true) { newBall(true); leftScore++; } } // Game ball goes off right edge of screen if(gameBall.getPosition().x > ofGetWidth()) { if(gameOver != true) { newBall(false); rightScore++; } } // If the left player's score hit the max, they win if(leftScore == MAX_SCORE) { winner = "left"; endGame(winner); } // If the right player's score hit the max, they win if(rightScore == MAX_SCORE) { winner = "right"; endGame(winner); } } }
Elevator::Elevator(SpeedController *top, SpeedController *bottom, Encoder *eTop, Encoder *eBottom, DigitalInput *iTop, DigitalInput *iIn, DigitalInput *iEnter, int numOfBalls ) { init(); this->top = top; this->bottom = bottom; this->eTop = eTop; this->eBottom = eBottom; this->iTop = iTop; this->iIn = iIn; this->iEnter = iEnter; limboBall = false; while ( numOfBalls-- ) newBall(); }
int main(){ int i; fp = fopen("output.txt", "w+"); fb = (FILE** )malloc(nballs*sizeof(FILE*)); nevent = 0; //fprintf(fp, "This is testing for fprintf...\n"); char colour; double rx,ry,vx,vy,radius; printf("Enter number of balls:"); scanf("%d",&nballs); balls = (Ball**)malloc(nballs*sizeof(Ball*)); rand(); rand(); char carr[] = {'r','b','g','o','y','f','g','h','i','j'}; for (i = 0; i < nballs; i++) { rx = rand()%80 + 10; ry = (rand()%80) + 10; vx = rand() % 20 + 1; vy = rand() % 20 + 1; radius = (rand() % 10) + 1; colour = carr[i%10]; char filename[20]; sprintf(filename,"%c.txt",colour); fb[i] = fopen(filename,"w+"); fprintf(fb[i],"x\ty\n"); fprintf(fb[i],"%0.2f\t%0.2f\n",rx,ry); balls[i] = newBall(rx, ry, vx, vy, radius, colour); printball(balls[i]); } simulate(balls); for(i = 0;i<nballs;i++){ fclose(fb[i]); } fclose(fp); return 0; }
Ball::Ball(int timeTicks) { newBall(timeTicks); }
bool Elevator::calculateBalls(int ballAdjustment) { bool handled = false; bool iEnterOn = !iEnter->Get(); bool iInOn = iIn->Get(); bool iTopOn = iTop->Get(); for(; ballAdjustment > 0; ballAdjustment--) newBall(); for(; ballAdjustment < 0; ballAdjustment++) doShoot(); //bool limboBallBefore = limboBall; if( iInOn && tIIn.Get() == 0) { tIIn.Start(); } else if( !iInOn && tIIn.Get() > Constants::elevatorBallSpeediIn) { limboBall = false; handled = false; //if(limboBallBefore) //cerr<<"Ball Has now Passed out of limbo"<<endl; } if( !iInOn ) { tIIn.Stop(); tIIn.Reset(); } if( iEnterOn && tIEnter.Get() == 0) { tIEnter.Start(); } else if( !iEnterOn && tIEnter.Get() > Constants::elevatorBallSpeediEnter) { if ( speed > 0 ) { newBall(); } } if( !iEnterOn ) { tIEnter.Stop(); tIEnter.Reset(); } if ( iEnterOn || limboBall ) { speed = 1; limboBall = true; handled = true; //if(!limboBallBefore) //cerr<<"Ball is now in limbo"<<endl; } iEnterOnBefore = iEnterOn; if( iTopOn && tITop.Get() == 0) // Ball was not previously in sensor. { tITop.Start(); } else if( !iTopOn && tITop.Get() > Constants::elevatorBallSpeed ) { // Ball went through sensor. if ( speed > 0 ) { cerr << tITop.Get() << endl; doShoot(); } } if (!iTopOn) // If the sensor is off, { tITop.Stop(); // Reset the timer. tITop.Reset(); // } return handled; }
//-------------------------------------------------------------- void testApp::draw() { ofBackground(0); ofSetColor(255); ofSetRectMode(OF_RECTMODE_CORNER); backgroundimg.draw(0,0, ofGetWidth(), ofGetHeight()); ofSetColor(0, 173, 238, 125); if(startScreen){ ofSetColor(236, 28, 36); visitor82.drawString("VFA PONG", bounds.x + bounds.width/2 - visitor82.stringWidth("VFA PONG")/2, bounds.y + bounds.height/2); visitor82.drawString("Press button to start a new game", bounds.x + bounds.width/2 - 775,bounds.y + bounds.height/2 + 100); printf("startscreen \n"); } if(!user1load && players && !startScreen){ // printf("alpha: %d: \n", alpha); ofSetColor(0, 173, 238, alpha); ofSetRectMode(OF_RECTMODE_CORNER); ofRect(bounds.x,bounds.y + 86, bounds.width/2, bounds.height - 86); ofSetColor(255); visitor82.drawString("Left Player Tap!", bounds.x + 80, bounds.y + bounds.height/2); visitor42.drawString("Press button to play as guest!", bounds.x + 30, bounds.y + bounds.height/2 + 100); } if(user1load && !user2load && players){ ofSetColor(0, 173, 238, alpha); ofSetRectMode(OF_RECTMODE_CORNER); ofRect(bounds.x + bounds.width/2, bounds.y + 86, bounds.width/2, bounds.height - 86); ofSetColor(255); user1.draw(bounds.x + bounds.width/4 - 50, bounds.height/2, 100, 100); visitor82.drawString(username1, bounds.x + bounds.width/4 - visitor82.stringWidth(username1)/2, bounds.y + bounds.height/2 + 120); ofSetColor(255); visitor82.drawString("Right Player Tap!", bounds.x + bounds.width/2 + 80, bounds.y + bounds.height/2); visitor42.drawString("Press button to play as guest", bounds.x + 30 + bounds.width/2, bounds.y + bounds.height/2 + 100); } if(user1load && user2load){ countdownnumbool = true; ofSetColor(255); user1.draw(bounds.x + bounds.width/4 - 50, bounds.height/2, 100, 100); user2.draw(bounds.x + bounds.width/4 - 50 + bounds.width/2, bounds.height/2, 100, 100); visitor82.drawString(username1, bounds.x + bounds.width/4 - visitor82.stringWidth(username1)/2, bounds.y + bounds.height/2 + 120); visitor82.drawString(username2, bounds.x + bounds.width/4 - visitor82.stringWidth(username2)/2 + bounds.width/2, bounds.y + bounds.height/2 + 120); if(countdownnum > 300) visitor82.drawString("3", bounds.x + bounds.width/2, bounds.y + bounds.height/2); if(countdownnum > 200 &&countdownnum < 300 ) visitor82.drawString("2", bounds.x + bounds.width/2, bounds.y + bounds.height/2); if(countdownnum > 100 && countdownnum < 200) visitor82.drawString("1", bounds.x + bounds.width/2, bounds.y + bounds.height/2); if(countdownnum > 0 && countdownnum < 100) visitor82.drawString("GO!", bounds.x + bounds.width/2, bounds.y + bounds.height/2); if (countdownnum == 0 && countdownnumbool){ countdownnumbool = false; countdownnum = 400; startGame(); drawusers = true; players = false; user1load = false; user2load = false; newBall(); } } if(startGameBool){ for(int i=0; i<circles.size(); i++) { Data * data = (Data*)circles[i].getData(); ofSetColor(255); ofEnableAlphaBlending(); // ofRectMode(OF_RECTMODE_CORNER); vimeologo.draw(circles[i].getPosition().x, circles[i].getPosition().y, circles[i].getRadius() * 2, circles[i].getRadius() * 2); } for(int i=0; i<paddles.size(); i++) { ofFill(); Data * data = (Data*)paddles[i].getData(); ofSetColor(236, 28, 36); paddles[i].draw(); } for(int i=0; i<winningvideos.size(); i++) { winningvideos[i].draw(); } for(int i=0; i<nomvideos.size(); i++) { nomvideos[i].draw(); } if (score1 > 4 && counter < 200) { ofSetColor(255,221,21); visitor82.drawString("YOU WIN!", bounds.x + bounds.width/4 - visitor82.stringWidth("YOU WIN!")/2, bounds.y + bounds.height/2 - 120); visitor82.drawString("YOU LOSE!", bounds.x + bounds.width/4 - visitor82.stringWidth("YOU LOSE!")/2 + bounds.width/2, bounds.y + bounds.height/2 - 120); ofSetColor(255); ofSetRectMode(OF_RECTMODE_CORNER); user1.draw(bounds.x + bounds.width/4 - 50, bounds.height/2 + 20, 100, 100); user2.draw(bounds.x + bounds.width/4 - 50 + bounds.width/2, bounds.height/2 + 20, 100, 100); ofSetColor(255,221,21); visitor82.drawString(username1, bounds.x + bounds.width/4 - visitor82.stringWidth(username1)/2, bounds.y + bounds.height/2 + 120); visitor82.drawString(username2, bounds.x + bounds.width/4 - visitor82.stringWidth(username2)/2 + bounds.width/2, bounds.y + bounds.height/2 + 120); counter++; if (!user1photo.compare("images/guest.jpg")){ ofxOscMessage m; m.setAddress( "/user" ); m.addStringArg( user1photo ); sender.sendMessage( m ); } } if (score2 > 4 && counter < 200) { ofSetColor(255,221,21); visitor82.drawString("YOU LOSE!", bounds.x + bounds.width/4 - visitor82.stringWidth("YOU LOSE!")/2, bounds.y + bounds.height/2 - 120); visitor82.drawString("YOU WIN!", bounds.x + bounds.width/4 - visitor82.stringWidth("YOU WIN!")/2 + bounds.width/2, bounds.y + bounds.height/2 - 120); ofSetColor(255); ofSetRectMode(OF_RECTMODE_CORNER); user1.draw(bounds.x + bounds.width/4 - 50, bounds.height/2 + 20, 100, 100); user2.draw(bounds.x + bounds.width/4 - 50 + bounds.width/2, bounds.height/2 + 20, 100, 100); ofSetColor(255,221,21); visitor82.drawString(username1, bounds.x + bounds.width/4 - visitor82.stringWidth(username1)/2, bounds.y + bounds.height/2 + 120); visitor82.drawString(username2, bounds.x + bounds.width/4 - visitor82.stringWidth(username2)/2 + bounds.width/2, bounds.y + bounds.height/2 + 120); counter++; if (!user2photo.compare("images/guest.jpg")){ ofxOscMessage m; m.setAddress( "/user" ); m.addStringArg( user2photo ); sender.sendMessage( m ); } } ofSetColor(236, 28, 36); if (drawusers){ visitor82.drawString("SCORE " + ofToString(score1, 1), bounds.x+120, bounds.y +65); visitor82.drawString("SCORE " + ofToString(score2, 1), bounds.x + bounds.width/2 + 120, bounds.y +65); ofSetColor(255); user1.draw(bounds.x+ 30, bounds.y+35, 80,80); user2.draw(bounds.x+ 30 + bounds.width/2, bounds.y+35, 80,80); } } }
//-------------------------------------------------------------- void testApp::update() { box2d.update(); //OSC STUFF while( receiver.hasWaitingMessages() ) { // get the next message ofxOscMessage m; receiver.getNextMessage( &m ); if ( m.getAddress() == "/joysticks" ) { // both the arguments are int32's joystick1 = m.getArgAsInt32( 0 ); joystick2 = m.getArgAsInt32( 1 ); } if ( m.getAddress() == "/user" && players && !startScreen) { user = m.getArgAsString( 0 ); username = m.getArgAsString( 1 ); printf("got a user!"); loaduser = true; } if ( m.getAddress() == "/leftshoot") { if(!startScreen && players){ user = "******"; username = "******"; loaduser = true; } if(startScreen){ startScreen = false; players = true; break; } } if ( m.getAddress() == "/rightshoot") { if(!startScreen && players){ user = "******"; username = "******"; loaduser = true; } if(startScreen){ startScreen = false; players = true; break; } } } if(loaduser && players && whichuser == 0){ user1.loadImage(user); user1photo = user; username1 = username; whichuser = 1; user1load = true; loaduser = false; if (!user1photo.compare("images/guest.jpg")){ ofxOscMessage m; m.setAddress( "/user" ); m.addStringArg( user1photo ); sender.sendMessage( m ); } } if(loaduser && players && whichuser == 1){ user2.loadImage(user); user2photo = user; username2 = username; whichuser = 0; user2load = true; loaduser = false; if (!user2photo.compare("images/guest.jpg")){ ofxOscMessage m; m.setAddress( "/user" ); m.addStringArg( user2photo ); sender.sendMessage( m ); } } alpha = alpha + alphaincrement; if(alpha > 200 || alpha < 100){ alphaincrement = alphaincrement * -1; } if(countdownnumbool){ printf("counting down: %d \n", countdownnum); countdownnum = countdownnum -1; } if(startGameBool){ for(int i=0; i<wvideos.size(); i++){ wvideos[i]->idleMovie(); } for(int i=0; i<nvideos.size(); i++){ nvideos[i]->idleMovie(); } //MOVE THE PADDLES mapped_joystick1 = int(ofMap(joystick1, 0, 360, bounds.y + 77 + paddles[0].getHeight()/2 , bounds.y+ bounds.height - 77 - paddles[0].getHeight()/2)); mapped_joystick2 = int(ofMap(joystick2, 0, 360, bounds.y + 77 + paddles[1].getHeight()/2, bounds.y+ bounds.height - 77 - paddles[1].getHeight()/2)); b2Vec2 pos1 = paddles[0].body->GetPosition(); b2Vec2 target1 = b2Vec2(pos1.x, mapped_joystick1/OFX_BOX2D_SCALE); // b2Vec2 target1 = b2Vec2(pos1.x, mouseY/OFX_BOX2D_SCALE); b2Vec2 diff1 = b2Vec2(target1.x-pos1.x,target1.y-pos1.y); diff1.operator*=(2); paddles[0].body->SetLinearVelocity(diff1); b2Vec2 pos2 = paddles[1].body->GetPosition(); // b2Vec2 target2 = b2Vec2(pos2.x, mouseY/OFX_BOX2D_SCALE); b2Vec2 target2 = b2Vec2(pos2.x, mapped_joystick2/OFX_BOX2D_SCALE); b2Vec2 diff2 = b2Vec2(target2.x-pos2.x,target2.y-pos2.y); diff2.operator*=(2); paddles[1].body->SetLinearVelocity(diff2); //FOR WHEN THE BALL GETS TOO FAST OR SLOW for(int i=0; i<circles.size(); i++) { int maxSpeed = 50; int minSpeed = 10; b2Vec2 velocity = circles[i].body->GetLinearVelocity(); float32 speed = velocity.Length(); if (speed > maxSpeed) { circles[i].body->SetLinearDamping(0.5); } else if (speed < maxSpeed) { circles[i].body->SetLinearDamping(0.0); } if (speed < 10){ b2Vec2 veloc = circles[i].body->GetLinearVelocity(); veloc.operator*=(1.5); circles[i].body->SetLinearVelocity(veloc); } } for(int i=0; i<circles.size(); i++) { b2Vec2 p = circles[i].body->GetLocalCenter(); //KEEP THE BALL GOING UP circles[i].body->ApplyForce(b2Vec2( 0, -3 ), p); //IF THE CIRCLE GOES OFFSCREEN, UP THE PLAYER SCORE if(circles[i].getPosition().x > bounds.x + bounds.width - 40){ score2 ++; box2d.getWorld()->DestroyBody(circles[i].body); circles.erase(circles.begin() + i); if (score1 < 5 && score2 < 5) newballbool = true; } if(circles[i].getPosition().x < bounds.x + 40){ score1 ++; box2d.getWorld()->DestroyBody(circles[i].body); circles.erase(circles.begin() + i); if (score1 < 5 && score2 < 5) newballbool = true; } } if(newballbool){ newballcount++; if (newballcount > 100){ newBall(); newballbool = false; newballcount = 0; } } for(int i=0; i<winningvideos.size(); i++) { Data * theData = (Data*)winningvideos[i].getData(); if(theData->hit == true){ //load the nominated videos & make their bodies for(int j=0; j< 4; j++){ ofVideoPlayer * vid = new ofVideoPlayer(); vid->loadMovie("movies/smingers" + ofToString(j) +".mov"); vid->play(); nvideos.push_back(vid); NominatedVideo v; v.setPhysics(1.0, 0.0, 0.5); v.setup(box2d.getWorld(), winningvideos[i].getPosition().x, winningvideos[i].getPosition().y, 30, 30, b2_dynamicBody); v.setupTheCustomData(); v.setVelocity(int(ofRandom(0, 5)), int(ofRandom(0, 5))); v.movie = vid; nomvideos.push_back(v); } //delete winning videos after they've been hit theData->hit = false; winningvideos[i].movie->stop(); delete winningvideos[i].movie; box2d.getWorld()->DestroyBody(winningvideos[i].body); wvideos.erase(wvideos.begin()+i); winningvideos.erase(winningvideos.begin()+i); //HOW TO DELETE FROM VECTOR LIST? } } //delete nominated videos after their lifespan ends for(int i=0; i<nomvideos.size(); i++) { Data * theData = (Data*)nomvideos[i].getData(); if(theData->lifespan < 0){ box2d.world->DestroyBody(nomvideos[i].body); nomvideos[i].movie->stop(); delete nomvideos[i].movie; nvideos.erase(nvideos.begin()+i); nomvideos.erase(nomvideos.begin()+i); } } //GAME OVER- RESET if (counter == 199) { score1 = 0; score2 = 0; counter = 0; for(int i=0; i<nomvideos.size(); i++) { box2d.world->DestroyBody(nomvideos[i].body); delete nomvideos[i].movie; } for(int i=0; i<winningvideos.size(); i++) { box2d.world->DestroyBody(winningvideos[i].body); delete winningvideos[i].movie; } nomvideos.clear(); winningvideos.clear(); wvideos.clear(); nvideos.clear(); vector <ofxBox2dCircle>::iterator iter3 = circles.begin(); while (iter3 != circles.end()) { iter3->draw(); box2d.world->DestroyBody(iter3->body); iter3 = circles.erase(iter3); } startGameBool = false; startScreen = true; drawusers = false; } } }