How to make graphics? And physics?

I am thinking of making a platformer, but have absolutely no experience with graphics of any kind: OpenGL, etc.
Also, how to put physics in?

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
  #include <iostream.h>
  #include <stdio.h>
using namespace std;
//I know it's really memory-wasting, but I don't want to put std:: everywhere.

struct user()//am I supposed to put parentheses?
{
string username;
int jumpheight = 20;
int speed = 10;
int hp = 100;
int regeneration = 0.25;
}

struct enemy
{
string name;
int attack = (rand()100)/5;
int jump = 10;
int speed = 5;
}
bool onplatform()
{
if //the character hits the platform going down
return true;
else
return false;
}
struct physics
{
int gravity = 9.8;
//how to make the graphics for it?

}
bool hitenemy()
{
if //the character hits the enemy
{
user.hp = user.hp - enemy.attack;
return true;
if (user.hp <= 0)
{
//take out the graphics and revert to text
cout << "GAME OVER";
//end the game. I would use a goto, but I don't know how to define labels.
}
}
else
return false;
}
int main()
{
cout << "What is your name?" << endl;
cin >> user.username;
cout << user.username << ", to jump, press the spacebar. To move, use the A and"
cout << " D keys (from WASD)." << endl;
cout << "GAME START";
clrscreen()
//graphics and game

return 0;     
}
Forget everything about cin/cout. They are useless for making graphical games (other than maybe embedding some debugging tools in your game).


A traditional simplistic game loop works like this:

1
2
3
4
5
6
7
8
9
10
while( game_is_running )
{
    ProcessUserInputAndMessages();

    UpdateAllObjects();

    ClearTheScreen();
    DrawTheScene();
    Display();
}


Each time this loop happens, the game world "updates". This means you have the player's object respond to the input the user is providing... and you "update" all objects, which involves:

- moving them a tiny bit
- seeing if there are any collisions with other objects or anything else that needs to be done
- advance any animations


This loop is simple because it combines the concept of "updating" with drawing. So if you set a fixed drawing rate (like 60 FPS), then you will also have a fixed update rate (60 updates per second).. which ensures the game will run at a fixed pace no matter how fast the user's computer is.


To do things like platformer physics, the basics are simple:

1) Keep track of an object's position (typically in the form of a vector: an X,Y coordinate)
2) Keep track of the object's velocity (ie speed) -- also in the form of a vector
3) Every update, simply add their velocity to their position to move them
4) Every update, adjust their velocity according to their input, gravity, and other forces.

So if you want to employ gravity, you would simply add downward velocity every update unless the user is standing on the ground (in which case their vertical velocity would be 0. To have them jump, you just give them a burst of upward velocity... and then leave it to gravity to push them back down.



As for things like drawing... get a lib like SFML. It makes this really easy. There are tutorials which show how to get a basic program up and running, and a lot of people here will be able to answer questions about it.

http://sfml-dev.org/


I do not recommend starting with OpenGL, as it is significantly more complex and confusing. Start with SFML -- and once you get a grip on it, if you want to move to OpenGL, SFML has OpenGL bindings so the transition is relatively easy.
Are vectors basically ints or numbers? Are they 2D arrays?
Thanks for everything else!
With the cin/cout, could I put that in a subprogram or something, and then do

subprogram()
{
what are your stats?
cin >> stats
difficulty?
cin >> difficulty
cls
}

int main()
{
subprogram()
while(game_is_running)
{
input
update
draw
}
}
You need to learn WAAAY MORE c++ before diving into this kind of stuff.
EssGeEich wrote:
You need to learn WAAAY MORE c++ before diving into this kind of stuff.


No you don't. Do it out of the gates. This is not nearly as hard as newbies think.

Though maybe he should start with a simple vertical shooter rather than a platformer. Platformers can get tricky.

AIa43 wrote:
Are vectors basically ints or numbers? Are they 2D arrays?


I was referring to a mathematical vector, which in its simplest form is just a struct with x,y coordinates:

1
2
3
4
5
6
7
8
9
10
struct Vector
{
    double x;
    double y;
};

//...
Vector position;
position.x = 150;
position.y = 40;


Though most vectors... like the ones in SFML... have a bunch of extra goodies which make them easier to use.


With the cin/cout, could I put that in a subprogram or something, and then do


If you really wanted to you could, but that would be extremely awkward.

The console is not your window... it's the console. If you want a graphical window it's going to be a separate window. So if you want to do this with cin/cout, you'll have to have 2 windows:

- The window which you're actually drawing graphics to
- The console

And the user would have to switch between the two.

So yes... it can be done... but it'd be absurd and weird.



Seriously -- forget about cin/cout. They're all but useless for game programming.


A better approach would be to have a graphical menu where the user can input their stats and stuff -- rather than using the console to do it.
Last edited on
Topic archived. No new replies allowed.