Hi! Long time no see huh! Well I've been working on a little something that I'm going to show you now. I've been working on a program that makes a little spaceship video game! You drive a little spaceship around a black screen and it bounces off the walls. You can adjust the speed (speed = magnitude),and direction (magnitude + direction = velocity) of the spaceship anddddddd, it will never leave the screen!
Here's my program:
#include <Core.h>
#include <Vector.h>
Vector position;
Vector velocity;
void checkin ( )
{
if(Core::Input::IsPressed(Core::Input::KEY_LEFT))
{
velocity.x = velocity.x - 1;
}
if(Core::Input::IsPressed(Core::Input::KEY_RIGHT))
{
velocity.x = velocity.x + 1;
}
if(Core::Input::IsPressed(Core::Input::KEY_UP))
{
velocity.y = velocity.y - 1;
}
if(Core::Input::IsPressed(Core::Input::KEY_DOWN))
{
velocity.y = velocity.y + 1;
}
}
bool MyUpdateFn( float dt )
{
checkin ();
position = position + velocity * dt;
if (position.x > 800)
{
velocity.x = velocity.x * -1;
}
if (position.x < 0)
{
velocity.x = velocity.x * -1;
}
if (position.y > 800)
{
velocity.y = velocity.y * -1;
}
if (position.y < 0)
{
velocity.y = velocity.y * -1;
}
return false;
}
void drawship (Core::Graphics& graphics)
{
Vector a(-50,50);
Vector b(-50,-50);
Vector c(50,-50);
Vector d(50,50);
Vector e(0,-100);
Vector aPrime = position + a;
Vector bPrime = position + b;
Vector cPrime = position + c;
Vector dPrime = position + d;
Vector ePrime = position + e;
graphics.DrawLine(aPrime.x,aPrime.y,bPrime.x,bPrime.y);// 1
graphics.DrawLine(aPrime.x,aPrime.y,dPrime.x,dPrime.y);// 4
graphics.DrawLine(bPrime.x,bPrime.y,cPrime.x,cPrime.y);// 2
graphics.DrawLine(cPrime.x,cPrime.y,dPrime.x,dPrime.y);// 3
graphics.DrawLine(ePrime.x,ePrime.y,cPrime.x,cPrime.y);// 5
graphics.DrawLine(ePrime.x,ePrime.y,bPrime.x,bPrime.y);// 6
}
void MyDrawFn( Core::Graphics& graphics)
{
// drawship(graphics);
}
int main()
{
Core::Init("Collin", 800, 800);
Core::RegisterUpdateFn(MyUpdateFn);
Core::RegisterDrawFn(MyDrawFn);
Core::GameLoop();
}
As you can see, it's a ton of code. So I'll start explaining it so we're not here for a century or 2. At the top we make our 2 vectors: position, and velocity. Next we make our void checkin procedure. After that we make our bool MyUpdateFn . Then we type in the void drawship procedure,and after that we type in void MyDrawFn,and finally.........last but not least...........int........main!! Once you've gotten all that typed in we'll take a peek at how it fits together to make a game.
HI THERE!! REMEMBER ME!!?? OF COURSE YOU DO!! So the reason we make our 2 vectors is because we're gonna need vectors right? The reason we call them position and velocity is because one of them represents the ships' position and the other represents the ships' velocity. The position of the ship is where it's at on the screen and the velocity of the ship is what direction the ship is going in and how fast it's going in that direction,so whenever you change the ships' velocity you indirectly change the ships position so together velocity and position make the ship move. But that doesn't mean that you have to change velocity to change position, in fact position has to be updated every single frame or else the ship stops when you let go of the arrow keys and then you have to start again at the lowest speed. The top procedure: void checkin controls which direction the ship is going in and how fast it's going in that direction,those little bits of code each control one of the arrow keys, you see how on left we're taking away from velocity.x and on right we are adding to it? Plus when we do up, we subtract from velocity.y,and when we do down we add to it. Then it's off to see bool MyUpdateFn! In MyUpdateFn we make the ship bounce off the "walls" of the screen,here's how we do it. The screen is 800 by 800 pixels right? Well how we make the ship bounce off the walls is this. We say "if position.x is less than 0 or bigger than 800 multiply it by -1 and the same thing with position.y. The reason we multiply them by -1 is because whenever you multiply a number by -1 if it's a positive number it becomes a negative number,and if it's a negative number it becomes a positive number which flips the direction of the ship so whenever the ship hits a wall it goes in the opposite direction at the same speed. Now we're going to talk about my drawship procedure. This procedure as it's name suggests draws the ship on our screen using vectors a,b,c,d, and e and then sets them to the x,and y positions that we want to put the lines that make up the ship between. Now, you're probably wondering, if we set the ship to one position how is it supposed to move? How we do that is we take these vectors and add them to position so that they will move, and name those new vectors aPrime,bPrime etc. etc. Then we take those Prime vectors and build the ship with them. And everything else after that is stuff my dad put in that I don't know how to explain.
WELL TTFN TA TA FOR NOW!!!!
No comments:
Post a Comment