Johan
Antal inlägg : 47 Registreringsdatum : 08-09-03
| Rubrik: Uppg 7 - Pong ons nov 26, 2008 1:01 pm | |
| Ok, här är mitt Pong-exempel. Jag kallar det ”Space Phong”.
Förutom det som krävs för att få godkänt på den här uppgiften (En boll som studsar mot två ”paddar” och att poäng delas ut ifall bollen passerar en padd), så har jag lagt dit ett par funktionaliteter till som du kan välja att inspireras från, ha med i ditt spel eller låta bli om du vill.
Bakgrund En 2D array av sprites skapar en bakgrund som blir som ett mönster i bakgrunden.
Glidande paddar När man trycker styrknapparna så ändrar man endast paddarnas hastighet sedan låter jag paddarna ALLTID flyttas med den hastigheten vilket gör att paddarna verkar glida.
Eld animation När man vill göra animationer i spel brukar man ofta använda sig av så kallade sprites. Jag vet att Sprite-klassen i SFML är ett måste om man vill rita ut grafik på skärmen. Men oftast brukar man referera sprites till konsten att använda sig av bara delar av en större bild. Här har jag en bild som för varje 30:e pixel utgör en ny bild i min eld-animation och sedan använder jag mig av funktionen SetSubRect() för att bara visa en del av den större bilden.
Jag har valt att inte använda mig av objektorienterad kod därför att jag anser att den här uppgiften handlar om SFML grundläggande funktioner och inte objektorientering. Därför behöver inte du heller göra det. Men i fortsättningen, efter detta, vill jag att du kombinerar SMFL och objektorientering.
Som vanligt vill jag inte att ni bara kopierar min kod utan att ni använder den som studiematerial. Läs och jämför med din egen kod och fundera. Om jag anser att din kod är för lik min kod, kommer jag vilja att du går igenom koden med mig muntligt och förklarar hur den fungerar innan jag kan godkänna det.
Mycket nöjje med ditt Pong-spel och lycka till!
Senast ändrad av Johan den ons nov 26, 2008 5:21 pm, ändrad totalt 3 gånger | |
|
Johan
Antal inlägg : 47 Registreringsdatum : 08-09-03
| Rubrik: Sv: Uppg 7 - Pong ons nov 26, 2008 1:18 pm | |
| - Kod:
-
// SPACE PHONG // by: Johan Stenberg
////////////////////////////////////////////////////// //TABLE OF CONTENTS// /////////////////////
//INITIATION /////////////// //Players //Creating the main rendering window //Loading Images and creating Sprites //The ball //Pads //The left pad (controlled with w and s buttons) //The right pad (controlled with up and down arrow buttons) //Fire effect that will be showing when you controll the pads //Background //Loading Font and creating texts
//GAME-LOOP ///////////////////
// HANDLE EVENTS //////////////////////// //Shut down when closing window or pushing escape //Moving pads //Move fire (follow the pads) //Animate fire //Moving Ball
//COLLISION DETECTION ////////////////////////// //Ball Collisions //Against the top and bottom of the screen //Against the left side of the screen (Right player wins) //Against the right side of the screen (Left player wins) //Against the left pad //Against the right pad //Pad Collisions //Left pad //Right pad
// DRAW AND DISPLAY WINDOW CONTENTS /////////////////////////////////////// //Draw background //Draw fires //Draw ball //Draw pads //Draw points counters //Display window
//END /////////
///////////////////////////////////////////////////////
#include <iostream> #include <SFML/Graphics.hpp>
int main() {
//INITIATION /////////////// //Create controll variables int leftPoints = 0; int rightPoints = 0; char leftPointsTempText[8]; char rightPointsTempText[8]; bool leftThrough = 0; //remembers if the ball has passed the goalee bool rightThrough = 0;
// Create the main rendering window const int windowWidth = 800; const int windowHeight = 600; sf::RenderWindow myWindow(sf::VideoMode(windowWidth, windowHeight, 32), "Space Phong");
//Loading Images and creating Sprites //The ball float ballSpeedX = sf::Randomizer::Random(-400.f, 400.f); //Determines the speed of the ball holisontally in pixels per second. float ballSpeedY = sf::Randomizer::Random(-400.f, 400.f); //Determines the speed of the ball vertically in pixels per second. float ballAx = 0.01; float ballMaxSpeed = 1000;
sf::Image ballImage; if (!ballImage.LoadFromFile("ball.png")){ std::cout << "Error: Unable to load boll.png\n"; }
sf::Sprite ball; ball.SetPosition((windowWidth/2)-(ballImage.GetWidth()/2), (windowHeight/2)-(ballImage.GetHeight()/2)); ball.SetCenter(0, 0); ball.SetImage(ballImage);
//Pads float leftPadSpeed = 0; //Determines the speed of wich the pads moves in pixels per second. float rightPadSpeed = 0; float padAx = 0.7; //Determines how fast the pads will axelerate when controlled float padMaxSpeed = 300;
//The left pad (controlled with w and s buttons) sf::Image leftPadImage; if (!leftPadImage.LoadFromFile("leftPad.png")){ std::cout << "Error: Unable to load leftPad.png\n"; }
sf::Sprite leftPad; leftPad.SetPosition(0.f, 100.f); leftPad.SetCenter(0, 0); leftPad.SetImage(leftPadImage);
//The right pad (controlled with up and down arrow buttons) sf::Image rightPadImage; if (!rightPadImage.LoadFromFile("rightPad.png")){ std::cout << "Error: Unable to load rightPad.png\n"; }
sf::Sprite rightPad; rightPad.SetCenter(0, 0); rightPad.SetPosition((windowWidth - rightPadImage.GetWidth()), 100.f); rightPad.SetImage(rightPadImage);
//Fire effect that will be showing when you controll the pads //Fire Image sf::Image fireImage; if (!fireImage.LoadFromFile("fire.png")) { std::cout << "Error: Unable to load fire.png\n"; }
sf::Sprite fire[4]; //fire[0] = left pad top fire //fire[1] = left pad bottom fire //fire[2] = right pad top fire //fire[3] = right pad bottom fire
int fireFrame[4]; //Tells what frame in the fire animation is active. 0 to 9 bool fireSwitch[4]; //Tells if a fire is burning 1=on 0=off sf::Clock fireTimer;
for (int i=0;i<4;i++){ fire[i].SetCenter(15, 60); fire[i].SetImage(fireImage); fire[i].SetSubRect(sf::IntRect(30*5, 0, 30*6, 60)); fireFrame[i]=1; fireSwitch[i]=0; }
fire[0].SetPosition((leftPad.GetPosition().x + 32),(leftPad.GetPosition().y + 2)); fire[1].SetPosition((leftPad.GetPosition().x + 32),(leftPad.GetPosition().y + leftPadImage.GetHeight()- 2)); fire[2].SetPosition((rightPad.GetPosition().x + 18),(rightPad.GetPosition().y + 2)); fire[3].SetPosition((rightPad.GetPosition().x + 18),(rightPad.GetPosition().y + rightPadImage.GetHeight() - 2));
fire[1].SetRotation(180.f); fire[3].SetRotation(180.f);
//Background (stars) sf::Image backgroundImage; if (!backgroundImage.LoadFromFile("background.png")){ std::cout << "Error: Unable to load background.png\n"; }
int bgWidth = windowWidth/backgroundImage.GetWidth()+1; int bgHeight = windowHeight/backgroundImage.GetHeight()+1; sf::Sprite background[bgWidth][bgHeight];
for(int bgY=0;bgY<bgHeight;bgY++){ for(int bgX=0;bgX<bgWidth;bgX++){ background[bgX][bgY].SetCenter(0, 0); background[bgX][bgY].SetPosition((backgroundImage.GetWidth()*bgX), (backgroundImage.GetHeight()*bgY)); background[bgX][bgY].SetImage(backgroundImage); } }
//Loading Font and creating texts sf::Font MyFont; if (!MyFont.LoadFromFile("astrolyt.ttf")){ std::cout << "Error: Unable to load font; astrolyt.ttf\n"; }
sf::String leftPointText("0", MyFont, 30); leftPointText.SetColor(sf::Color(80, 80, 255)); leftPointText.SetPosition(10.f, 10.f);
sf::String rightPointText("0", MyFont, 30); rightPointText.SetColor(sf::Color(80, 80, 255)); rightPointText.SetPosition(windowWidth-40.f, 10.f);
//GAME-LOOP ///////////////////
while (myWindow.IsOpened()) { // HANDLE EVENTS ////////////////////////
//Shut down when closing window or pushing escape sf::Event myEvent; while (myWindow.GetEvent(myEvent)) { // Close window : exit if (myEvent.Type == sf::Event::Closed) myWindow.Close();
// Escape key pressed if ((myEvent.Type == sf::Event::KeyPressed) && (myEvent.Key.Code == sf::Key::Escape)) myWindow.Close();
}
//Moving pads if (myWindow.GetInput().IsKeyDown(sf::Key::W)){ leftPadSpeed -= padAx; fireSwitch[1]=1;}else{fireSwitch[1]=0; fireFrame[1]=0;} if (myWindow.GetInput().IsKeyDown(sf::Key::S)){ leftPadSpeed += padAx; fireSwitch[0]=1;}else{fireSwitch[0]=0; fireFrame[0]=0;} if (myWindow.GetInput().IsKeyDown(sf::Key::Up)){ rightPadSpeed -= padAx; fireSwitch[3]=1;}else{fireSwitch[3]=0; fireFrame[3]=0;} if (myWindow.GetInput().IsKeyDown(sf::Key::Down)){ rightPadSpeed += padAx; fireSwitch[2]=1;}else{fireSwitch[2]=0; fireFrame[2]=0;}
if (leftPadSpeed > padMaxSpeed){ leftPadSpeed = padMaxSpeed; }else if (leftPadSpeed < -padMaxSpeed){ leftPadSpeed = -padMaxSpeed; }
if (rightPadSpeed > padMaxSpeed){ rightPadSpeed = padMaxSpeed; }else if (rightPadSpeed < -padMaxSpeed){ rightPadSpeed = -padMaxSpeed; }
leftPad.Move(0, leftPadSpeed * myWindow.GetFrameTime()); rightPad.Move(0, rightPadSpeed * myWindow.GetFrameTime());
//Move fire (follow the pads) fire[0].SetPosition((leftPad.GetPosition().x + 32),(leftPad.GetPosition().y + 2)); fire[1].SetPosition((leftPad.GetPosition().x + 32),(leftPad.GetPosition().y + leftPadImage.GetHeight()- 2)); fire[2].SetPosition((rightPad.GetPosition().x + 18),(rightPad.GetPosition().y + 2)); fire[3].SetPosition((rightPad.GetPosition().x + 18),(rightPad.GetPosition().y + rightPadImage.GetHeight() - 2));
//Animate fire for(int i=0;i<4;i++){ if(fireSwitch[i]!=0){ fire[i].SetSubRect(sf::IntRect(30*fireFrame[i], 0, 30*(fireFrame[i]+1), 60)); if (fireTimer.GetElapsedTime() > 0.08){ fireFrame[i]++; if(fireFrame[i]>9){ fireFrame[i]=4; } }//if (fireTimer.GetElapsedTime() > 1) }//if(fireFrame[i]!=0) }//for(int i=0;i<4;i++) if (fireTimer.GetElapsedTime() > 0.08){ fireTimer.Reset(); }
//Moving Ball ball.Move(ballSpeedX * myWindow.GetFrameTime(), ballSpeedY * myWindow.GetFrameTime()); if (ballSpeedX >= 0){ballSpeedX+=ballAx;}else{ballSpeedX-=ballAx;} if (ballSpeedY >= 0){ballSpeedY+=ballAx;}else{ballSpeedY-=ballAx;} if (ballSpeedX > ballMaxSpeed){ballSpeedX = ballMaxSpeed;}else if(ballSpeedX < -ballMaxSpeed){ballSpeedX = -ballMaxSpeed;} if (ballSpeedY > ballMaxSpeed){ballSpeedY = ballMaxSpeed;}else if(ballSpeedY < -ballMaxSpeed){ballSpeedY = -ballMaxSpeed;}
//COLLISION DETECTION //////////////////////////
//Ball Collisions //Against the top and bottom of the screen if (ball.GetPosition().y <= 0 ){ ball.SetY(0.f); ballSpeedY = -ballSpeedY; } else if (ball.GetPosition().y >= (windowHeight-ballImage.GetHeight())){ ball.SetY(windowHeight-ballImage.GetHeight()); ballSpeedY = -ballSpeedY; }
//Against the left side of the screen (Right player wins) if (ball.GetPosition().x <= -30){ ball.SetPosition((windowWidth/2)-(ballImage.GetWidth()/2), (windowHeight/2)-(ballImage.GetHeight()/2)); ballSpeedX = sf::Randomizer::Random(-400.f, 400.f); ballSpeedY = sf::Randomizer::Random(-400.f, 400.f); rightPoints++; itoa(rightPoints, rightPointsTempText, 10); rightPointText.SetText(rightPointsTempText); leftThrough = 0; rightThrough = 0; }
//Against the right side of the screen (Left player wins) else if(ball.GetPosition().x >= windowWidth){ ball.SetPosition((windowWidth/2)-(ballImage.GetWidth()/2), (windowHeight/2)-(ballImage.GetHeight()/2)); ballSpeedX = sf::Randomizer::Random(-400.f, 400.f); ballSpeedY = sf::Randomizer::Random(-400.f, 400.f); leftPoints++; itoa(leftPoints, leftPointsTempText, 10); leftPointText.SetText(leftPointsTempText); rightThrough = 0; leftThrough = 0;
}
//Against the left pad if (ball.GetPosition().x < (leftPad.GetPosition().x + leftPadImage.GetWidth()) && leftThrough == 0) { leftThrough = 1; if((ball.GetPosition().y + ballImage.GetHeight()) >= leftPad.GetPosition().y && ball.GetPosition().y <= (leftPad.GetPosition().y + leftPadImage.GetHeight())) { ball.SetX(leftPad.GetPosition().x + leftPadImage.GetWidth()); ballSpeedX = -ballSpeedX; leftThrough = 0; } }
//Against the right pad else if ((ball.GetPosition().x + ballImage.GetWidth()) > rightPad.GetPosition().x && rightThrough == 0) { rightThrough = 1; if((ball.GetPosition().y + ballImage.GetHeight()) >= rightPad.GetPosition().y && ball.GetPosition().y <= (rightPad.GetPosition().y + rightPadImage.GetHeight())) { ball.SetX(rightPad.GetPosition().x - ballImage.GetWidth()); ballSpeedX = -ballSpeedX; rightThrough = 0; } }
//Pad Collisions //Left pad if(leftPad.GetPosition().y < 0){ leftPad.SetY(0.f); leftPadSpeed = 0; }else if ((leftPad.GetPosition().y+leftPadImage.GetHeight()) > windowHeight){ leftPad.SetY(windowHeight-leftPadImage.GetHeight()); leftPadSpeed = 0; }
//Right pad if(rightPad.GetPosition().y < 0){ rightPad.SetY(0.f); rightPadSpeed = 0; }else if ((rightPad.GetPosition().y+rightPadImage.GetHeight()) > windowHeight){ rightPad.SetY(windowHeight-rightPadImage.GetHeight()); rightPadSpeed = 0; }
// DRAW AND DISPLAY WINDOW CONTENTS /////////////////////////////////////// //Draw background for(int bgY=0;bgY<bgHeight;bgY++){ for(int bgX=0;bgX<bgWidth;bgX++){ myWindow.Draw(background[bgX][bgY]); } }
//Draw fires for(int i=0;i<4;i++){ if (fireSwitch[i]!=0){ myWindow.Draw(fire[i]); } } //Draw ball myWindow.Draw(ball);
//Draw pads myWindow.Draw(leftPad); myWindow.Draw(rightPad);
//Draw point counters myWindow.Draw(leftPointText); myWindow.Draw(rightPointText);
//Display window myWindow.Display(); }
//END /////////////////////
return EXIT_SUCCESS; }
Senast ändrad av Johan den ons nov 26, 2008 5:17 pm, ändrad totalt 1 gång | |
|
Johan
Antal inlägg : 47 Registreringsdatum : 08-09-03
| Rubrik: Sv: Uppg 7 - Pong ons nov 26, 2008 1:30 pm | |
| Bilderna som jag använder: döp om till "background.png" döp om till "ball.png" döp om till "leftPad.png" döp om till "rightPad.png" döp om till "fire.png" Här har jag valt att använda mig av png-format eftersom det kan hantera alpha-kanaler (genomskinlighet) | |
|
mjaoo
Antal inlägg : 27 Registreringsdatum : 08-09-06 Ort : Mjau?
| Rubrik: Tips! ons nov 26, 2008 10:22 pm | |
| Använd using namespace sf så blir allt mycket enklare! | |
|
Sponsored content
| Rubrik: Sv: Uppg 7 - Pong | |
| |
|