SFML Pong Help

Started by
4 comments, last by BeerNutts 12 years, 2 months ago
I need some advice for a loop in my pong game.

#include "Play.h"
void Play::Show(sf::RenderWindow& window3)
{
sf::Event GEvent;
//load background image
sf::Image image4;
image4.LoadFromFile("PlayBack.jpg");
//load grey bar
sf::Image image5;
image5.LoadFromFile("GreyBar.jpg");
//setup grey bar
sf::Sprite GBar(image5);
GBar.Resize(800,50);
//setup background image
sf::Sprite PlayBack(image4);
PlayBack.Resize(800,600);
//load paddle
sf::Image image6;
image6.LoadFromFile("paddle.png");
//setup paddle
sf::Sprite ppaddle(image6);
ppaddle.Rotate(90);
//load ball
sf::Image image7;
image7.LoadFromFile("ball.png");
//setup ball
sf::Sprite ball(image7);
ball.SetPosition(350, 300);
while(window3.GetEvent(GEvent))
{
int x = GEvent.MouseMove.Y;
if(x < 140)
{
x = 140;
}
if(GEvent.MouseMove.Y || GEvent.MouseMove.X)
{
ppaddle.SetPosition(15, x );
}
}
window3.Draw(PlayBack);
window3.Draw(GBar);
window3.Draw(ppaddle);
window3.Draw(ball);
window3.Display();
}

The Paddle moves fine, but if the cursor is in certain spots, the sprite will disappear idk why. Is that the right loop to update the game? Also, I need help with ball movement. I like to have the user click for the ball to start moving. I couldn't get the ball to move and I'm guessing i will need a different loop or maybe even a whole new function.Thanks for any advice you can give me.
Advertisement
I'm not sure exactly how your functions are defined, but It looks to me like the drawing functions should be inside the game loop. The way you have them, nothing will get drawn until the game is over.

I would try doing this:

while(window3.GetEvent(GEvent))
{
int x = GEvent.MouseMove.Y;
if(x < 140)
{
x = 140;
}
if(GEvent.MouseMove.Y || GEvent.MouseMove.X)
{
ppaddle.SetPosition(15, x );
}
window3.Draw(PlayBack);
window3.Draw(GBar);
window3.Draw(ppaddle);
window3.Draw(ball);
window3.Display();
}


You can ignore me if ppaddle.SetPosition contains drawing functions, but I suspect it doesn't. I'm not sure what your other issue is, but this change should help get things moving. I'll edit this if I see anything else.

I'm not sure exactly how your functions are defined, but It looks to me like the drawing functions should be inside the game loop. The way you have them, nothing will get drawn until the game is over.

I would try doing this:
...

You can ignore me if ppaddle.SetPosition contains drawing functions, but I suspect it doesn't. I'm not sure what your other issue is, but this change should help get things moving. I'll edit this if I see anything else.


Eridan, that's not correct. That while loops will exit when there's not any input to be read. it doesn't draw when game over. And, this Show function isn't the main game loop.

Soulheart, ignore what Eridan said. Here are a few suggestions I have for you.

#1, you should not be loading the image every time you call show. SFML image loading should be done once per image. So, you should load the image, then assign the Sprite. You can have the Sprites as member variable, or pass them in the function, or have them grouped in their own class and pass in the class.

#2, You're Show should be separate from checking User input. So, you should check user input, and adjust the location of your paddle, and then, in a different function, draw the images. By having the sprites defined outside these functions, they can be updated in different functions.

As for the cursor, I'm not exactly sure what the deal is with that.

If you want the game to start with a click, then, you could have different States. So, you have a WAITING_FOR_START state. In your CheckUserInput function, if you're in the WAITING_FOR_START state, and the user clicks the mouse, you shoot the ball out. You would then change the State to GAME_PLAYING. When the Game is over, you can change it to GAME_OVER, and in your CheckUserInput, you would ask if the user wants to play again. that's the general idea.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

ok thanks. I'll see what I can work up
So is this a good way to set up the game loop?

Main.cpp

//include....
//Make The game state
enum GameState

int main()
{
//Load images, fonts, etc.....
//Starts the Game
Game::Start();
//shouldn't get here but if it does return 0
return 0;
}


Game.cpp

//include........
//Starts the game
void Start()
{
//GState = anything but Uninitialized, return
if(GState != Uninitialized)
return;
//Creates game window
GameW.Create(sf::VideoMode(800, 600, 32),"Pang!", sf::Style::Fullscreen);
//show Splashscreen
GState = FirstState;
//while the game isn't exiting run the game loop
while(!IsExiting())
{
GameLoop();
}
GameW.Close();
}
//exiting loop
bool IsExiting()
{
if(GState == Exiting)
return true;
else
return false;
}
//Game loop
void GameLoop()
{
//GState Switch
switch(GState)
{
case //GState
{
//Run certain function
break;
}
}
}
void Play()
{
//show/update the game
Show();
//get input and set changes(but not show them)
Input();
}


Is this a good foundation?
Please help me out if it isn't.
It's a start. You'll want to call Input(), and then Show() somewhere within your Game Loop. You will probably want an Update(), which updates all the objects in your game.

So, basically, Input() get the input from the user, and applies the proper settings to the paddle (ie, if used is pressing LEFT, then the velocity of the Paddle is set to a negative X value).
Update updates the ball, and the paddles, based on the velocities, and if collision checks (ball hits paddle or off screen, or paddle hits edge of screen).

And, Show draws all the objects on the screen. All the State stuff can be handle multiple ways, just find one that you are comfortable with. I won't say what you've done is the best solution, but, it's a good start, and, as you go, you'll learn bettre ways of doing things.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement