gotta post mah basecode…

#include <allegro.h>
#include <iostream>
#include <string>
using namespace std;

const int PLAYER_H = 10;
const int PLAYER_W = 10;
const int NUM_PLATS = 200;
const float GRAVITY = 0.2;
const int STRENGTH = 6;

class Rect {
public:
int x, y, width, height;
BITMAP* image;
Rect(int _x, int _y, int _width, int _height, BITMAP* _image) {
x = _x;
y = _y;
width = _width;
height = _height;
if(_image != NULL)
image = _image;
else {
cout << “Image was not loaded properly.” << endl;
exit(1);
}
}

Rect(void) {

}

void draw() {
//blit(image, screen, 0, 0, x-1, y-1, width+2, height+2);
rect(screen, x-1, y-1, x+width+1, y+height+1, makecol(255, 255, 255));
}

protected:
bool collision(Rect rect) {
if(x > rect.x+rect.width || x+width < rect.x)
return FALSE;
if(y > rect.y+rect.height || y+height < rect.y)
return FALSE;
return TRUE;
}
};

class Player : public Rect {
public:
float dy; //delta x/y
int dx;
bool up, down, left, right;
bool jumping, can_jump;

Player(int _x, int _y, int _width, int _height, BITMAP* _image) {
x = _x;
y = _y;
width = _width;
height = _height;
image = _image;
}

Player(void) {

}

void draw() {
blit(image, screen, 0, 0, x, y, width, height);
}

void move(Rect plats[]) {
if(key[KEY_UP] && can_jump) {
dy = -STRENGTH;
can_jump = FALSE;
jumping = TRUE;
}
if(key[KEY_RIGHT]) dx = 2*right;
else if(key[KEY_LEFT]) dx = -2*left;
else if(key[KEY_HOME]) {
width–;
height–;
}
else if(key[KEY_END]) {
width++;
height++;
}
else dx = 0;
down = TRUE;
up = TRUE;
right = TRUE;
left = TRUE;
can_jump = FALSE;

y += dy;
for(int i = 0; i < NUM_PLATS; i++) {
if(collision(plats[i])) {
if(dy > 0) {
y = (plats[i].y – height) – 1;
down = FALSE;
can_jump = TRUE;
}
if(dy < 0) {
y = (plats[i].y + plats[i].height) + 1;
up = FALSE;
}
dy = 0;
}
}

x += dx;
for(int i = 0; i < NUM_PLATS; i++) {
if(collision(plats[i])) {
if(dx > 0) {
x = (plats[i].x – width) -1;
right = FALSE;
}
if(dx < 0) {
x = (plats[i].x + plats[i].width) + 1;
left = FALSE;
}
dx = 0;
}
}
if(y > SCREEN_H) {
y = -height;
}
dy += GRAVITY;
}
};

int main() {
allegro_init();
install_keyboard();
install_timer();
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
set_window_title(“Platform”);

srand(time(NULL));

BITMAP* loading;
loading = load_bitmap(“Loading.pcx”, NULL);

bool regenerate = FALSE;
do {
blit(loading, screen, 0, 0, 0, 0, loading->w, loading->h);

Player player((SCREEN_W/2)-PLAYER_W/2, (SCREEN_H/2)-PLAYER_H/2, PLAYER_W, PLAYER_H, load_bitmap(“Player.pcx”, NULL));
Rect plats[NUM_PLATS];
for(int i = 0; i < NUM_PLATS; i++) {
plats[i].x = (((rand() % (SCREEN_W-PLAYER_W)*2)/(PLAYER_W*2))*(PLAYER_W))+1;
plats[i].y = (((rand() % (SCREEN_H-PLAYER_H)+PLAYER_H)*2/(PLAYER_H*2))*(PLAYER_H))+1;
int num = rand() % 4;
if(num == 1) {
plats[i].width = (PLAYER_W*2)-2;
plats[i].height = PLAYER_H-2;
}
if(num == 2) {
plats[i].width = (PLAYER_W*3)-2;
plats[i].height = PLAYER_H-2;
}
if(num == 3) {
plats[i].width = (PLAYER_W*4)-2;
plats[i].height = PLAYER_H-2;
}
else {
plats[i].width = PLAYER_W-2;
plats[i].height = (PLAYER_H*3)-2;
}
plats[i].image = load_bitmap(“Platform.pcx”, NULL);

//string text;
//textout_ex(screen, font, text, 0, 0, makecol(0, 255, 0), int bg);
}
clear_bitmap(screen);

regenerate = FALSE;
while(!key[KEY_ESC]) {
clear_keybuf();
player.move(plats);

clear_bitmap(screen);
player.draw();
for(int i = 0; i < NUM_PLATS; i++) {
plats[i].draw();
}
if(key[KEY_R]) {
regenerate = TRUE;
break;
}
rest(10);
}
} while(regenerate);

return 0;
}

END_OF_MAIN()

Comments

Flyboy
18. Dec 2010 · 00:42 UTC
Pastebin that.