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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
//object header
#include "objects.h"
//Allegro headers
#include <allegro5\allegro.h>
#include <allegro5\allegro_primitives.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_image.h>
//Globals===============================
//screen variables
int screenWidth = 640;
int screenHeight = 480;
int worldWidth = 3392;
int worldHeight = 960;
float camX = 0;
int camY = 0;
bool can_jump = true;
bool in_air = false;
int plyr_vel = 2;
int hang_time;
//player controls
enum KEYS {LEFT, RIGHT, DOWN, SPACE, P};
bool keys[5] = {false, false, false, false, false};
bool camAct = true;
//prototypes
//player
void InitPlayer(Player &plyr);
void DrawPlayer(Player &plyr);
void MoveLeft(Player &plyr);
void MoveRight(Player &plyr);
void MoveDown(Player &plyr);
void Jump(Player &plyr);
void InitGround(Ground &ground, Player &plyr, int x, int y);
void DrawGround(Ground &ground, Player &plyr, int x, int y);
void CollideGround(Ground &ground, Player &plyr, int x, int y);
//Camera
void cam(Player &plyr);
//main
int main(void)
{
//primitive variables
const int FPS = 60;
bool done = false;
bool redraw = true;
bool isGameOver = false;
int x = 0;
int y = 0;
int imageWidth = 0;
int imageHeight = 0;
//object variables
Player plyr;
Ground ground;
//Allegro variables
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_FONT *font18 = NULL;
ALLEGRO_BITMAP *bitmap = NULL;
//Initialization Functions
//initialize Allegro
if(!al_init())
return -1;
//create our display object
display = al_create_display(screenWidth, screenHeight);
//test display object
if(!display)
return -1;
//Allegro Module Init
al_init_primitives_addon();
al_install_keyboard();
al_init_font_addon();
al_init_ttf_addon();
al_init_image_addon();
bitmap = al_load_bitmap("world1-1.png");
//event creation
event_queue = al_create_event_queue();
timer = al_create_timer(1.0 / FPS);
//init random generator
srand(time(NULL));
//Game initilization
//player
InitPlayer(plyr);
InitGround(ground, plyr, 0, 0 );
//Font (TBA)
//--------------------------------
//Keyboard and other things initilization
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
//timer start
al_start_timer(timer);
//game loop
while (!done)
{
//event loader
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);//wait for an event.
//events for key strokes
if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
done = true;
}
else
if (ev.type == ALLEGRO_EVENT_TIMER)
{
redraw = true;
//keys for player
if (keys[LEFT])
MoveLeft(plyr);
if(keys[RIGHT])
MoveRight(plyr);
if(keys[DOWN])
MoveDown(plyr);
if(keys[SPACE])
Jump(plyr);
if(keys[P]);
//is game over checks
if(plyr.lives <= 0)
{
isGameOver = true;
}
}
//Is a key pressed
else if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch (ev.keyboard.keycode)
{
//close key
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
//Left arrow key
case ALLEGRO_KEY_LEFT:
keys[LEFT] = true;
break;
//Right arrow key
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = true;
break;
//Down arrow key
case ALLEGRO_KEY_DOWN:
keys[DOWN] = true;
break;
//Space bar
case ALLEGRO_KEY_SPACE:
keys[SPACE] = true;
Jump(plyr);
break;
}
}
//Is a key released
else if (ev.type == ALLEGRO_EVENT_KEY_UP)
{
switch (ev.keyboard.keycode)
{
//Left arrow key
case ALLEGRO_KEY_LEFT:
keys[LEFT] = false;
break;
//Right arrow key
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = false;
break;
//Down arrow key
case ALLEGRO_KEY_DOWN:
keys[DOWN] = false;
break;
//Space bar
case ALLEGRO_KEY_SPACE:
keys[SPACE] = false;
Jump(plyr);
break;
}
}
if (hang_time > 0)
{
plyr.y -= plyr_vel;
hang_time --;
} else
if (hang_time <= 0)
{
if (plyr.x < ground.boundX - 32 && plyr.y < ground.boundY - 32)
{
plyr.y += plyr_vel;
}
hang_time = 0;
}
//draw functions
if (redraw && al_event_queue_is_empty(event_queue))
{
redraw = false;
if (!isGameOver)
{
DrawPlayer(plyr);
cam(plyr);
for (int i = 0; i <= 1024; i += 16)
{
DrawGround(ground, plyr,i, screenHeight - 16);
}
al_draw_bitmap(bitmap, worldWidth / 2 - imageWidth , worldHeight / 2 - imageHeight , 0);
al_draw_tinted_bitmap(bitmap, al_map_rgba(255,255,255,.5),x - camX,y -camY,0);
//draw more things later
}
//add game over screen here.
//display flipping
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
}
}//end while loop
//Destroy all objects running program
al_destroy_event_queue(event_queue);
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_font(font18);
return 0;
} //end main
//functions for game
//player
void InitPlayer(Player &plyr)
{
plyr.x = 20;
plyr.y = worldHeight / 2;
plyr.IDS = PLAYER;
plyr.lives = 3;
plyr.speed = 4;
plyr.boundX = 6;
plyr.boundY = 7;
plyr.score = 0;
}
void DrawPlayer(Player &plyr)
{
al_draw_filled_rectangle(plyr.x - camX, plyr.y-32, plyr.x+10 - camX , plyr.y -48, al_map_rgb(0,255,0));
if(plyr.x <= camX)
plyr.x = camX;
if (plyr.x >= screenWidth + camX - 10)
plyr.x = camX + screenWidth - 11;
}
void MoveLeft(Player &plyr)
{
plyr.x -= plyr.speed;
if(plyr.x < 0)
plyr.x = 0;
if(plyr.x <= camX)
plyr.x = camX;
}
void MoveRight(Player &plyr)
{
plyr.x += plyr.speed;
if (plyr.x > worldWidth)
plyr.x = worldWidth;
if (plyr.x >= screenWidth + camX - 10)
plyr.x = camX + screenWidth - 11;
}
void MoveDown(Player &plyr)
{
//add code
}
void Jump(Player &plyr)
{
if (can_jump == true && in_air == false)
{
in_air = true;
can_jump = false;
hang_time = 10;
}
}
//camera
void cam(Player &plyr)
{
// making sure the camera stays within the boundary of the game world
if (camX < 0) camX = 0;
if (camY < 0) camY = 0;
if (camX > worldWidth - screenWidth) camX = worldWidth - screenWidth;
if (camY > worldHeight - screenHeight) camY = worldHeight - screenHeight;
// make the camera follow the player
if ( plyr.x > camX + screenWidth / 2 )
camX += plyr.speed;
else
if (plyr.x < camX + screenWidth/2)
camX -= plyr.speed;
}
void InitGround(Ground &ground, Player &plyr, int x, int y)
{
ground.x = x;
ground.y = y;
ground.boundX = 48;
ground.boundY = 48;
}
void DrawGround(Ground &ground, Player &plyr, int x, int y)
{
al_draw_filled_rectangle(x - camX, y + 16, x + 16 - camX, y - 16, al_map_rgb(255,0,255));
}
void CollideGround(Ground &ground, Player &plyr, int x, int y)
{
if (plyr.x < ground.boundX - 32 && plyr.y < ground.boundY - 32)
{
can_jump = true;
in_air = false;
plyr.y = ground.boundY - 32;
if (plyr.y < ground.boundY - 32)
plyr.y = ground.boundY-32;
}
}
|