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
|
#include <allegro5/allegro.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_audio.h>
#include<allegro5/allegro_acodec.h>
#include <cstdlib>
#include <ctime>
#include "objects.h"
#define ScreenWidth 800
#define ScreenHeight 600
//Prototpyes
void initPlayer(Player &player);
void initCoin(Coin &coin1);
void drawCoin(Coin &coin1, ALLEGRO_BITMAP *image);
bool coinCollide(Coin &coin1,Player &player ,int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, bool live, int coinCount);
void initCoin2(Coin &coin2);
void drawCoin2(Coin &coin2, ALLEGRO_BITMAP *image);
bool coinCollide2(Coin &coin2,Player &player ,int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, bool live, int coinCount);
void initArrow(Arrow arrow[], int size);
void drawArrow(Arrow arrow[], int size);
void fireArrow(Arrow arrow[], int size, Player &player, ALLEGRO_SAMPLE *bowShot);
void updateArrow(Arrow arrow[], int size);
bool collision(Player &player, int ex, int ey, int eWidth, int eHeight, int pWidth, int pHeight);
void cameraUpdate(float *cameraPosition,Player &player, int width, int height);
//Global Variables
enum direction {LEFT, RIGHT};
int dir = LEFT;
int arrowCount = 10;
//const int NUM_ARROW = 10;
const int NUM_ENEMY = 10;
int groundHeight = 545;
static bool fired = false;
bool coinLive = false;
int main()
{
srand(static_cast<unsigned int>(time(0)));
al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 24, ALLEGRO_REQUIRE);
ALLEGRO_DISPLAY *display;
if(!al_init())
{
al_show_native_message_box(NULL, NULL, "Error", "Falied to initialize allegro", NULL, NULL);
return -1;
}
display = al_create_display(ScreenWidth, ScreenHeight);
if(!display)
{
al_show_native_message_box(NULL, NULL, "Error", "Falied to initialize the display", NULL, NULL);
return -1;
}
Player player;
Coin coin1;
Coin coin2;
Arrow arrow[10];
bool done = false, active = false, draw = true;
bool jump = false;
int sourceX = 32, sourceY = 0;
int coinCount = 0;
const float FPS = 55.0;
float jumpSpeed = 16.5;
float velX = 0, velY = 0;
const float gravity = 1;
float cameraPosition[2] = {0,0};
al_init_image_addon();
al_init_primitives_addon();
al_install_keyboard();
al_init_font_addon();
al_init_ttf_addon();
al_install_audio();
al_init_acodec_addon();
initPlayer(player);
initArrow(arrow, 10);
initCoin(coin1);
initCoin2(coin2);
ALLEGRO_KEYBOARD_STATE keystate;
ALLEGRO_TRANSFORM camera;
ALLEGRO_SAMPLE *coinCollect = al_load_sample("coin collect sound.wav");
ALLEGRO_SAMPLE *bowShot = al_load_sample("bow sound effect.wav");
ALLEGRO_BITMAP *character = al_load_bitmap("spritesheet(Bow & left + right).png");
ALLEGRO_BITMAP *background = al_load_bitmap("ayers rock.png");
ALLEGRO_BITMAP *coin = al_load_bitmap("coin2.png");
ALLEGRO_BITMAP *ground = al_load_bitmap("Ground.png");
ALLEGRO_BITMAP *platform = al_load_bitmap("platform(long).png");
ALLEGRO_BITMAP *platform2 = al_load_bitmap("platform(short).png");
ALLEGRO_FONT *numArrow = al_load_font("JUNGBN__.TTF", 23, 0);
ALLEGRO_FONT *numCoin = al_load_font("JUNGBN__.TTF", 23, 0);
ALLEGRO_TIMER *timer = al_create_timer(1.0/FPS);
ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
al_reserve_samples(2);
al_set_window_title(display, "Australian Outback");
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_display_event_source(display));
al_start_timer(timer);
while(!done)
{
ALLEGRO_EVENT event;
al_wait_for_event(event_queue, &event);
al_get_keyboard_state(&keystate);
if(al_key_down(&keystate, ALLEGRO_KEY_ESCAPE))
{
done = true;
}
else if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
done = true;
}
else if(event.type == ALLEGRO_EVENT_TIMER)
{
active = true;
updateArrow(arrow, 10);
if(al_key_down(&keystate, ALLEGRO_KEY_D))
{
velX = player.speed;
dir = RIGHT;
}
else if(al_key_down(&keystate, ALLEGRO_KEY_A))
{
velX = -player.speed;
dir = LEFT;
}
else
{
velX = 0;
active = false;
}
if(al_key_down(&keystate, ALLEGRO_KEY_W) && jump)
{
velY = -jumpSpeed;
jump = false;
}
if(active)
{
sourceX += al_get_bitmap_width(character) / 3;
}
else
{
sourceX = 32;
}
if(sourceX >= al_get_bitmap_width(character))
{
sourceX = 0;
}
sourceY = dir;
draw = true;
if(!jump)
velY += gravity;
else
velY = 0;
player.x += velX;
player.y += velY;
jump = (player.y >= groundHeight);
if(jump)
{
player.y = groundHeight;
}
cameraUpdate(cameraPosition, player, 32, 32);
al_identity_transform(&camera);
al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]);
al_use_transform(&camera);
}
if(al_key_down(&keystate, ALLEGRO_KEY_SPACE))
{
fireArrow(arrow, 10, player, bowShot);
}
else
{
fired = false;
}
if(collision(player, 360, 447, 351, 30, 32, 32))
{
groundHeight = 435;
player.y = groundHeight;
}
else if(collision(player, 70, 377, 214, 38, 32, 32))
{
groundHeight = 365;
player.y = groundHeight;
}
else
{
groundHeight = 545;
}
if(coinCollide(coin1, player, coin1.x, coin1.y, coin1.width, coin1.height, 32, 32, coinLive, coinCount))
{
coin1.live = false;
++coinCount;
coin1.x = 0;
coin1.y = 0;
al_play_sample(coinCollect, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
}
if(coinCollide2(coin2, player, coin2.x, coin2.y, coin2.width, coin1.height, 32, 32, coinLive, coinCount))
{
coin2.live = false;
++coinCount;
coin2.x = 0;
coin2.y = 0;
al_play_sample(coinCollect, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
}
if(draw && al_is_event_queue_empty(event_queue))
{
draw = false;
al_draw_bitmap_region(character, sourceX, sourceY * al_get_bitmap_height(character) / 2, 32, 32, player.x, player.y, NULL);
drawArrow(arrow,10);
drawCoin(coin1, coin);
drawCoin2(coin2, coin);
al_draw_textf(numArrow, al_map_rgb(252,209, 22), 10, 10, 0, "Arrows: %i", arrowCount);
al_draw_textf(numCoin, al_map_rgb(252, 209, 22), 10, 50, 0, "Coins: %i", coinCount);
al_flip_display();
al_clear_to_color(al_map_rgb(255, 255, 255));
al_draw_bitmap(background, 0,0, 0);
al_draw_bitmap(ground, 0, 549, 0);
al_draw_bitmap(platform, 360, 447, NULL);
al_draw_bitmap (platform2, 70, 377, NULL);
}
}
al_destroy_bitmap(character);
al_destroy_bitmap(ground);
al_destroy_bitmap(coin);
al_destroy_bitmap(platform);
al_destroy_sample(coinCollect);
al_destroy_font(numArrow);
al_destroy_display(display);
al_destroy_event_queue(event_queue);
al_destroy_timer(timer);
return 0;
}
void initPlayer(Player &player)
{
player.x = 10;
player.y = 545;
player.ID = PLAYER;
player.lives = 3;
player.speed = 5;
player.score = 0;
}
bool collision(Player &player, int ex, int ey, int eWidth, int eHeight, int pWidth, int pHeight)
{
if((player.x > ex + eWidth - 10) || (player.y > ey + eHeight - 10) ||
(ex > player.x + pWidth - 10) || (ey > player.y + pHeight - 10))
{
return false;
}
return true;
}
void cameraUpdate(float *cameraPosition, Player &player, int width, int height)
{
cameraPosition[0] = -(ScreenWidth / 2) + (player.x + width / 2);
cameraPosition[1] = -(ScreenHeight / 2) + (player.y + height / 2);
if(cameraPosition[0] < 0)
cameraPosition[0] = 0;
if(cameraPosition[1] < 0)
cameraPosition[1] = 0;
}
|