Unformatted Attachment Preview
BIT-2400, Assignment M3
Title
Object-Oriented Game
Functional Requirements
Use the base object-oriented game code developed in class and make a game with the
following features:
1. Player class
a. Data: x, y, shape, score, and health (all private)
b. Functions: constructor, required Get/Set functions for data, collision
detection with another object
2. Enemy class
a. Data: x, y, shape, speedx, speedy, type
b. Functions: constructor, Move
3. Render class
a. Data:
b. Functions:
void Draw(GameObject* obj);
//draw an object (use your own classes)
void Begin(ALLEGRO_BITMAP* bg);
//start a frame by drawing the background
void End();
//end the frame by moving to primary buffer
4. Game class
a. Data: background image, one player, an array of 10 enemies
b. Functions: constructor, Run, Update, Draw
5. The player object goes in 4 directions using arrow keys.
6. Any collision with an enemy will:
a. Destroy the enemy and give a score if space key is hold down
b. Decreases the health if the space key is not hold down
7. The player’s shape changes if the user holds down the space key (open mouth to
eat the enemy). Player will not move when space is down.
8. Enemies have random initial locations on screen and can have three different
types (each with a different shape) that is assigned randomly:
a. Move right and re-appear on the left side once reaching the far right
b. Move up and re-appear on the bottom side once reaching top
c. Follow player
Implementation Requirements
• This is an individual assignment.
• Your program should start with the base code. You may modify the base code or
add other classes.
• Add a comment section at the top of the source file with main function, for
Documentation with following parts:
Title: Assignment-1
Author: [your name]
Objective: [very short 1-02 sentence description of goal as you understand]
Object Description: [list of classes with short description of their members]
Submission Requirements
Submit your project as a zip file through cuLearn. The zip file should include all of you
project excluding Debug/Release folders and .SDF files.
Evaluation
• 1 point for each class
1
• General program structure and coding
0.5
• Documentation section
0.5
• Penalties
• Submission will get 0 if the code doesn’t compile or run, or if incorrect file is
submitted.
/*
Game Object class
Learning Objectives:
1. GameObject class
2. simple OO game
3. establishing a unique way of moving and drawing all objects so a game engine
can easily control the regardless of our specific game logic
Copyright (c) 2017, Ali Arya
*/
//Using Allegro
//1- create a Win32 Project (no Console)
//2- include "allegro5/allegro.h"
//3- add allegro-5.0.10-monolith-md-debug.lib to Additional Dependencies, in
Properties/Linker/Input
//4- In Linker/System set the SubSystem to Not Set if you want to see text
window
//http://alleg.sourceforge.net/a5docs/5.0.10/
//
#include
#include "allegro5/allegro.h"
//use image addon for loading images, dialog boxes, and primitive shapes
#include
//use primitives addon to draw primitive shapes like line
#include
//global variable for display
ALLEGRO_DISPLAY* g_display = NULL; //ALLEGRO_DISPLAY is a "user defined type" in
Allegro. * shows a pointer
//user-defined types
struct Vector2D //a 2D value corresponding to X and Y dimensions (position,
size, direction, etc)
{
int x;
int y;
};
struct Colour
{
int r; //red
int g; //green
int b; //blue
int a; //alpha
};
class GameObject
{
public:
Vector2D position;
Vector2D speed;
ALLEGRO_BITMAP* shape; //a pointer to bitmap
void Draw()
{
al_draw_bitmap(shape, position.x, position.y, 0);
}
void Move()
{
position.x += speed.x;
position.y += speed.y;
};
}
//initialzie Allegro components
void InitAllegro(int screenWidth, int screenHeight)
{
//initialize allegro
if (!al_init())
{
printf("failed to initialize allegro!\n");
}
//initialize display screen
g_display = al_create_display(screenWidth, screenHeight);
if (!g_display)
{
printf("failed to create display!\n");
exit(0);
}
else
{
printf("ok");
al_clear_to_color(al_map_rgb(0, 0, 0));
}
//initialize keyboard
if (!al_install_keyboard())
{
printf("failed to install keyboard!\n");
exit(0);
}
//initialize image addon
if (!al_init_image_addon())
{
printf("failed to initialize image addon!\n");
exit(0);
}
//initialize mouse
if (!al_install_mouse())
{
printf("failed to install mouse!\n");
exit(0);
}
//initialize primitive shapes
if (!al_init_primitives_addon())
{
printf("failed to initialize primitives!\n");
exit(0);
}
}
//End and clean up Allegro components
void EndAllegro()
{
al_destroy_display(g_display);
}
//main function
//an interactive application has two main parts
//1. Initialize that happens once
//2. Loop that repeats
//
2.1. Update (read inputs, do calculations, change variables, implement
program logic)
//
2.2. Draw (render all content to screen)
void main()
{
///////////////////////////////////
// INITIALIZE
///////////////////////////////////
//initialize allegro
int sw = 640;
int sh = 480;
InitAllegro(sw, sh);
//create and initialize all objects
GameObject obj1;
obj1.position.x = 200;
obj1.position.y = 200;
obj1.speed.x = obj1.speed.y = 0;
obj1.shape = al_load_bitmap("player.bmp");
GameObject obj2;
obj2.position.x = 300;
obj2.position.y = 300;
obj2.speed.x = obj2.speed.y = 0;
obj2.shape = al_load_bitmap("enemy.bmp");
//keyboard data is stored in a structure
ALLEGRO_KEYBOARD_STATE key_state; //ALLEGRO_KEYBOARD_STATE is a "user
define type" in Allegro
//main loop
bool quit = false;
while (quit == false) //no checking for Escape key here as we have a
forever loop
{
///////////////////////////////////
//UPDATE
///////////////////////////////////
//here we respond to events and change variables
//check user input
//this updates key_state
//used for obj1
al_get_keyboard_state(&key_state);
//end program
if (al_key_down(&key_state, ALLEGRO_KEY_ESCAPE)) //al_key_down
checks the status of a key in the key_state structure
{
quit = true;
}
//change player speed using arrow keys
obj1.speed.x = obj1.speed.y = 0;
if (al_key_down(&key_state, ALLEGRO_KEY_RIGHT))
{
obj1.speed.x = 1;
}
if (al_key_down(&key_state, ALLEGRO_KEY_LEFT))
{
obj1.speed.x = -1;
}
if (al_key_down(&key_state, ALLEGRO_KEY_UP))
{
obj1.speed.y = -1;
}
if (al_key_down(&key_state, ALLEGRO_KEY_DOWN))
{
obj1.speed.y = 1;
}
//try adding a logic to control enemy speed
//move all objects
//first we set (update) the speed values then we move all objects
//this way we can have all Move functions together
obj1.Move();
obj2.Move();
///////////////////////////////////
//DRAW
///////////////////////////////////
//all drawing will be here
//erase frame
al_clear_to_color(al_map_rgb(0, 0, 0));
//draw objects and other content (menus, etc)
obj1.Draw();
obj2.Draw();
//put on screen
al_flip_display();
//wait (to slow down)
al_rest(0.01); //0.1 seconds of wait
}
//clean up
EndAllegro();