What can be improved in game code w/ SFML?

Hi, I've made a pong game. It's very generic as it's just the core of the game. My next step is making everything an object that's an object and taking everything out of main etc.

I'm going to be looking to optimize it(don't tell me optimization for when I do the next project -- just the code you see).

Anyway, I'm looking to see what's wrong with my code. What can be improved... If I overdid things etc.


Also, can anyone tell me what an engine is?

I want to make an object, ball, that can be used for a plethora of things. It will be fully customizable in shape, direction, etc. I want to use it for my next game which is some 2d arcade style starship blasting meteors type thing. If I'm able to reuse this ball class that I make for the pong game, will that be a piece of an engine?

Well, here's the code...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <SFML/Graphics.hpp>
#include <iostream>
#define BOARD sf::Shape::Rectangle(0.f, 0.f,10.f, 80.f, sf::Color::Blue)


int dir;
int paddleTouch;

int main()
{

	sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Pong Revision 3");
	sf::Vector2f ballCenter(1.f, 1.f);	
	sf::Shape Ball = sf::Shape::Circle(ballCenter, 5.f, sf::Color::Blue);
	sf::Shape Board1 = BOARD;
	sf::Shape Board2 = BOARD;

	sf::Vector2f ballPosition(400.f,300.f);
	Ball.SetPosition(ballPosition);
	sf::Vector2f boardPosition(0.f, 260.f);
	Board1.SetPosition(boardPosition);
	sf::Vector2f boardPosition2(790.f, 260.f);
	Board2.SetPosition(boardPosition2);
	
	
	while(App.IsOpened())
	{
		
	
 		sf::Event ev1;
		while(App.GetEvent(ev1))
			if (ev1.Type == sf::Event::Closed)
				App.Close();

		float elapsedTime = App.GetFrameTime();
		
		if (dir == 0 && int(Ball.GetPosition().x) == int(Board1.GetPosition().x + 10) && int(Ball.GetPosition().y) > int(Board1.GetPosition().y) && int(Ball.GetPosition().y) < int(Board1.GetPosition().y + 80))
		{	
			dir=3;
			paddleTouch = 1;

		}
		
		if (dir == 1 && int(Ball.GetPosition().x) == int(Board1.GetPosition().x + 10) && int(Ball.GetPosition().y) > int(Board1.GetPosition().y) && int(Ball.GetPosition().y) < int(Board1.GetPosition().y + 80))
		{
			dir = 2;
			paddleTouch = 1;
		}

		
		if (dir == 2 && int(Ball.GetPosition().x) == int(Board2.GetPosition().x -10) && int(Ball.GetPosition().y) > int(Board2.GetPosition().y) && int(Ball.GetPosition().y) < int(Board2.GetPosition().y + 80))
		{
			dir = 1;
			paddleTouch =1;
		}
		
		if (dir == 3 && int(Ball.GetPosition().x) == int(Board2.GetPosition().x -10) && int(Ball.GetPosition().y) > int(Board2.GetPosition().y) && int(Ball.GetPosition().y) < int(Board2.GetPosition().y + 80))
		{
			dir = 0;
			paddleTouch =1;
		}

		//Doesn't hit paddle
		if(dir == 0 && int(Ball.GetPosition().y) == 595 && paddleTouch == 0)
			dir = 1;
		if(dir == 1 && int(Ball.GetPosition().y) == 5 && paddleTouch == 0)
			dir = 0;
	
		if(dir == 2 && int(Ball.GetPosition().y) == 5 && paddleTouch ==0)
			dir = 3;
	
		if(dir == 3 && int(Ball.GetPosition().y) == 595 && paddleTouch ==0)
			dir = 2;
		

		if( dir == 0 && Ball.GetPosition().x > 0 && Ball.GetPosition().y < 600)
				Ball.Move(-.1f, .1f);
			if(dir == 1 && Ball.GetPosition().x > 0 && Ball.GetPosition().y > 0)
				Ball.Move(-.1f, -.1f);
			if(dir == 2 && Ball.GetPosition().x < 800 && Ball.GetPosition().y > 0)
				Ball.Move(.1f, -.1f);
			if(dir == 3 && Ball.GetPosition().x < 800 && Ball.GetPosition().y < 600)
				Ball.Move(.1f, .1f);
	
		if (App.GetInput().IsKeyDown(sf::Key::Up) && Board1.GetPosition().y > 0.f) 
			Board1.Move(0, -2000 * elapsedTime);
		if (App.GetInput().IsKeyDown(sf::Key::Down) && Board1.GetPosition().y < 520.f) 				Board1.Move(0, 2000 * elapsedTime);

		if (App.GetInput().IsKeyDown(sf::Key::W) && Board2.GetPosition().y > 0.f)
			Board2.Move(0, -2000 *elapsedTime);
		if (App.GetInput().IsKeyDown(sf::Key::S) && Board2.GetPosition().y < 520.f)
			Board2.Move(0, 2000 * elapsedTime);
		

		App.Clear();
		App.Draw(Board1);
		App.Draw(Board2);
		App.Draw(Ball);
		App.Display();
		
		paddleTouch = 0;
	}
	return EXIT_SUCCESS;
}


		
Last edited on
just bumping :)
Well I'd say you could improve the movement of your paddle quite easily, Just make a simple little class with a velocity, and move your sprite based on that velocity with an update method or something.

You could derive the class from the sf::Sprite class.

You could add scoring...
You could make interesting effects, like variable gravity based on vectors or something.
You could have upgrades available based on the rally of the game.
You could add sounds...

=]

BTW, don't call it "Pong". The Suits at Magnavox Atari are still applying legal action to people who make games called "Pong".

http://www.plasmapong.com/
http://indygamer.blogspot.com/2007/09/plasma-pong-removed.html
http://forum.grasscity.com/general/189844-plasma-pong-forced-shut-down.html
http://arun.chagantys.org/blog/?p=18

Alas.
ultifinitus wrote:
You could add scoring... [skip]
You could have upgrades available based on the rally of the game.
You could add sounds...


That sounds exactly like my first graphical game! pong with more interesting gfx/sfx and powerups/scoring. The code was atrocious, but hey, it was a first for me.

As for your pong, its fine, provided that you change your coding style (stuff everything into main, global vars, formatting your super long if statements differently, etc)
Topic archived. No new replies allowed.