I tested it for a few minutes, here are some comments:
- There are several places in the code were you're using double literals when you could use float. There is also some places where you are storing floating point values in integers or the inverse. The compiler gave me several warnings/errors about that.
| float a = 1.5f; // The 'f' at the end indicates that the literal is a 32 bits floating point value.
double b = 1.0; // Without the 'f' the value is a 64 bits floating point value (double precision).
float sum = a + 5.0; // If you don't put an 'f' at the end, you're adding a 32 bits value with a 64 bits value.
// The compiler will promote the float (32 bits) to a double (64 bits) for the computation,
// but since we want to store the result as a float, we either have to cast the result to a float or use a float literal.
float sum = a + 5.0f;
float sum = ( float ) ( a + 5.0 );
int c = ( int ) a; // Placing a type in parenthesis in front of a variable will indicates to the compiler
// that you want it to "interpret its type" differently (it's called casting).
// Here 'a' should be interpreted as an int. Since 'a' has a value of 1.5f, the CPU will truncate the value, so 'c' equals 1.
|
- You are doing collision detection by intersecting lines, but if a bullet move fast enough, it could enter an asteroid and be at its center without any line crossing and do the same to go out and there would be no collisions. If you're interested you can look at GJK (
a video of Casey explaining it in 3D but you can do it in 2D, you only need to find a triangle. And it's easier than it looks) or
Separating Axis Theorem (SAT).
The rest is about gameplay/features:
- The ship is too big and the screen area is too small (and maximizing the window doesn't work)
- The ship accelerates too fast, and it seems to go faster when you turn and thrust (I didn't check, but maybe you don't use the frame time in you movement computations and if the framerate is higher on my machine, the gameplay is faster than on your machine).
- In asteroids a big part of the game is the fact that if something goes of screen on one side it comes back the other side. In your version for the moment, you can go off screen and get lost.
- In asteroids there is no air friction (it's in space) and no brakes, if you don't turn around and thrust you keep going in the same direction forever.
- I use an azerty keyboard layout so the default layout doesn't work right away. I wrote a post about
keyboard input if you're interested.
I'm also working on a small game vastly inspired by asteroids that I hope I'll be able to show soon.
Good luck