The way my code is laid out is I have my main.cpp file with all the main() function, display() function and things like keyboard input functions etc. Then I have my lander.cpp and lander.h files with the definition and implementation of my lander class. I've created two functions of type bool called IsLanded() and IsColliding()but to be honest I'm unsure what to put in these and what parameters whey should be taking to get my game to work. I'm not a very good programmer( even for a begginer) but I'm trying to understand(hence being here asking for help) so please keep the critisism constructive and focused on the code. As long as its about the code then critisize away, pick at anything you consider bad practice etc and suggest better ways of doing things. Its the only way i'll learn.
If anyone prefers seeing it in action rather than just reading the code from here then I can send you a link to my dropbox where you can download the full project(VC++ project file and all the files including bitmaps)
And thank you for taking the time to read this. I appreciate it
Main.cpp
#include"Bitmap.h"
#include<gl/glut.h>
#include "lander.h"
#include <iostream>
#include <cmath>
//#include "random.h"
const float gravity = -0.005;
const float engine_thrust = 0.010;
const float x_pad = 258;
const float y_pad = 459;
const float pad_length = 87;
#define windowWidth 800
#define windowHeight 600
bool left_key;
bool right_key;
bool up_key;
float dx;
float dy;
bool IsLanded();
Bitmap *bitmap;
Bitmap *bitmap2 = NULL;
Lander *lander;
void display(){
glClear(GL_COLOR_BUFFER_BIT);
// Draw the background...
bitmap->draw();
if(bitmap2)
bitmap2->drawAt(0, 0);
if(lander)
lander->show();
glFlush();
}
void init(){
// Just a bitmap file, with transparency enabled (true)...
bitmap = new Bitmap("background.bmp", false);
bitmap2 = new Bitmap("ground.bmp", true);
lander = new Lander("lander.bmp",400, 550, 0, 0);
}
void update()
{
float x_thrust=0;
float y_thrust=gravity;
if(left_key){
x_thrust = -0.1;
}
if (right_key){
x_thrust += 0.1;
}
if(up_key){
y_thrust += engine_thrust;
}
lander->update(x_thrust, y_thrust);
glutPostRedisplay();
}
void timer(int value)
{
update();
glutTimerFunc(40, timer, 0);
}
void special_keys(int value, int x, int y)
{
switch (value) {
case GLUT_KEY_LEFT:
left_key = true;
dx += 0.8;
break;
case GLUT_KEY_RIGHT:
right_key = true;
dx += -0.8;
break;
case GLUT_KEY_UP:
up_key = true;
dy += 0.8;
break;
break;
//case GLUT_KEY_ESC:
//end program
//break;
}
}
void special_up_keys(int value, int x, int y)
{
switch (value) {
case GLUT_KEY_LEFT:
left_key = false;
break;
case GLUT_KEY_RIGHT:
right_key = false;
break;
case GLUT_KEY_UP:
up_key = false;
break;
//case GLUT_KEY_ESC:
//end program
//break;
}
}
bool IsLanded()
{
float x;
float y;
if ( x+1 < x_pad || x > x_pad + 87 && y > y_pad+1)
return false;
else
//
return true;
}
bool IsColliding(Bitmap, float Ground1)
{
//I don't have a clue what goes here
}
int main(int argc,char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("Mars Lander by Michael McGuire and Patrick Stevenson");
glClearColor(1.0, 1.0, 0.0, 0.0);
gluOrtho2D(0, windowWidth, 0, windowHeight);
init();
glutDisplayFunc(display);
glutSpecialFunc(special_keys);
glutSpecialUpFunc(special_up_keys);
glutTimerFunc(0, timer, 0);
glutMainLoop();
delete bitmap;
return 0;
}
Lander.h
#include "Bitmap.h"
class Lander
{
public:
Lander::Lander (char* filename, float ix, float iy,float idx, float idy);
void move();
void show();
void update(float dx, float dy);
float get_Altitude();
float set_Altitude();
float get_Fuel();
float set_Fuel();
private:
Bitmap *bitmap;
float gravity, terminal_velocity, x, y, fuel, altitude;
float dx, dy;
};
//float Lander::get_Altitude{ return altitude}
Lander.cpp
#include "lander.h"
#include <GL/glut.h>
#include "Bitmap.h"
Lander::Lander (char* filename, float ix, float iy, float idx, float idy)
{
bitmap = new Bitmap(filename, true);
x = ix; y = iy;
dx=idx; dy=idy;
show();
}
void Lander::move()
{
x += dx;
y += dy;
show();
}
void Lander::show()
{
bitmap->drawAt(x,y);
}
void Lander::update(float dx, float dy)
{
this->dy += dy;
this->dx += dx;
move();
}

New Topic/Question
Reply



MultiQuote





|