INTRODUCTION

We were asked to create a program based on C++ codes and the specifications given. Based on these, we had decided to create a game entitled Space Racer. Space Racer is a gaming type program that allows a user to play the game at a time. This game basically let the player/user to control a space ship and he/she needs to avoid the obstacles that were prepared in this game. The obstacles will get harder from time to time. Other than that, the speed of this space ship will also increase from 1 to 10 as the time goes on. The way to play this game can easily be learned as you just need to only use the left and right cursor to let the space ship avoid the obstacles prepared. The player will be given 3 lives and every time the space ship hits an obstacle, the lives will be deducted by 1 and after all the lives are gone, the score of the player will be displayed and saved. This will be fun as another player can compete to beat that score to have his/her name on the highscores list. We had created this program using all the informations that we had learned in the class and we also used a few codes and libraries that we learned from the internet. Functions, Strings and Structures are a few of the things that we had learned in the class while Vector, Conio and Try-catch are a few of the things that we had learned on the web. We used all these informations in order to ease the management of our program and to make this program a big success. So come on, give a try to this game and you would not regret!

Files.

Multiple files are combined using #include. Linker does not need to link several object file, as only one will be generated.

Name / Description
Main.cpp / The only file that compile correctly. It include common.cpp , startmenu.cpp, ongame.cpp, endgame.pp in that order. Also contain the basic program state handling.
Common.cpp / Include the headers required,
declare variables and function that will be used used by more than one file. In another word contain variable and function declaration common the the program. These variables are usually settings.
Also have some utils function.
Startmenu.cpp / Code for the fancy startmenu.
Ongame.cpp / Code for the actual game logic, animation, etc... Also define the Buffer structure. #include gamedata.cpp
Gamedata.cpp / Contain the animation frames of the game and obstacle, and also obstacle factors, such as probability, also have some constant for game animation.
Endgame.cpp / Code that is called by ongame.cpp that handles what happen after the game, including score saving in hiscore.txt
Highscore.txt / File that contain the score. First line is the number of score contained. Each score has two line, first line is the use name, and the second line is the score.

Ongame.cpp

struct Buffer.

Represent a two dimension array of ascii character. But implemented in one dimensional string array to make it easier to manage.Also store height and width.

Functions in ongame.cpp

Name / Return Type / Arguments / Description
createBufferDef / Buffer* / intwidth,intheight,const char defaultc=' ' / returns a new buffer of size width and height filled with defaultc.
createBuffer / Buffer* / intwidth,intheight,const char** data / returns a new buffer of size width and height, with data from data.
fillBuffer / void (correction, should be void) / Buffer* bu,intx,inty,intwidth,intheight,chartofill=' '
or
Buffer* bu,chartofill=' ' / Fill the buffer with tofill in the square range denoted by x,y,width,height.
For the second overload, fill the whole buffer with tofill.
deleteBuffer / void / Buffer* b / delete b and data from memory.
drawBuffer / void / const Buffer* b,ostreamout,int ox=0,int oy=0 / draw buffer to the ostream given. ox and oy is the offset used to center the displaybuffer.
drawBuffer / void / const Buffer* b,int ox=0,int oy=0 / Call above function but using cout as the output.
drawBufferToString / string / const Buffer* b,int ox=0,int oy=0 / Unused. Originally used to experiment reducing the 'blink' by first output the buffer into a string. This basically call the first drawBuffer with an ostringstream as the ostream then return the string.
composeBuffer / void / Buffer* target,const Buffer* source,intx,inty,char ignore=0 / compose, basically copy the data of a Buffer into another buffer on a particular position. Its like a draw image on another image but instead of image, it use Buffer which is array of char.
hasCollide / bool / const Buffer* target,const Buffer* source,intx,int y / Check if source will collide with target if composed on x,y position.
putToBuffer / void / Buffer* target,stringstr,intx,int y / Put a string into the buffer. Like compose but use string.
is_shadow / bool / return true, if the ship is currently invincible to the obstacle due to recent collition.
loop / bool / the main game loop. Executed TARGETFPS times per second.
updatefactors / void / ranevery loop. It update the speed and counter and pullfactor.
updatestatus / void / draw to statusbuffer about the current lives, speed and score.
showdisplaybuffer / void / compose the buffers and draw it on the terminal.
prepareBuffer / void / ran once to clear the buffers.
prepareGame / void / ran once to reset game variables.
play / void / theentrypoint to the game. The menu call this function to start the game.
keyinput / bool / get the keyboard input. return false if 'q' is pressed signaling quit. Otherwise return true.
pulldown / void / move the obstacle in the mapbuffer one line down. Additionally call generateObstacle(), and checkCollide()
generateObstacle / void / Randomly check if to put an obstacle into the mapbuffer. The obstacle is always put at the top of the map.
onceinit / void / Called once in the program lifetime to initialize the buffer from gamedata.cpp
oncedestruct / void / Called once before the program exit to remove the buffer from memory. This function is there just for being nice, as the OS will recover the memory back anyway.

Global Variables in ongame.cpp

name / type / description
mapbuffer / Buffer* / themapbuffer contain the obstacle and also the fences.
displaybuffer / Buffer* / themapbuffer and shipbuffer will be composed into this buffer.
realdisplaybuffer / Buffer* / displaybuffer and statusBuffer will be composed into this buffer. Additionally why this buffer is different that displaybuffer is that originally, I want to put some more fancy frame on the game window.
statusBuffer / Buffer* / a buffer with the status.
shipbuffer / Buffer*[SHIPFRAME] / contain the frames for the ship.
shipdeadbuffer / Buffer* / contain the frame for the ship when the ship is dead. Used when gameover.
obstacles / Buffer** / array of Buffer* that contain the frame for the obstacles. Will be composed to the mapbuffer on generateObstacle()
curpos / int / thecurrentposition of the ship.
counter / int / how many time the loop has been run. Used on various calculation for animation, speed etc.
gamelife / int / How many life you have left?
lastcollition / int / when is the last collision in unit counter. Used by is_shadow()
pullfactor / unsigned int / This one is used to denote when to pull the map. When this is greater than pullcounter*TARGETFPS, then pull. pullcounter is in common.cpp because it is also the score.
curmilis / unsigned long. / Used to more accurately calculate the amount of delay required to reach TARGETFPS.
speed / int / the current speed. This number will be added to pullfactor every loop.

Gamedata.cpp variables.

SHIPWIDTH / constint / the width of the ship.
SHIPHEIGHT / constint / the height of the ship.
EXTRAMAPHEIGHT / constint / extra height of mapbuffer. Used to put the obstacle. In another word, the maximum obstacle height.
STATUSWIDTH / constint / the width of status buffer
STATUSHEIGT / constint / the height of status buffer
LASTCOLLITIONDURATION / constint / the duration in second from the last collitionwhere the ship is invincible.
SHIPDEAD / char*[] / frame for shipdead
SHIPRAW1,SHIPRAW2,SHIPRAW3 / char*[] / frames of the ship
SHIPRAW / char**[] / same as above, but collected in one variable.
obst1,obst2,obst3,obst4 / char*[] / obstaclesframe.
obstaclenum / constint / number of obstacle
obstaclesdata / char**[] / the obstacle frames in one variable.
obstsizefactor / int[][4] / contain the factors for each obstacle. The first column is the width, the second column is the height, the third column is the probability of the obstacle to appear, as in one over the value probability, which means the lower the more frequent, the forth column is the minimum pullcounter before the obstacle can appear.

Common.cpp

Functions.

Name / return type / parameters / description
gotoXY / void / intx,int y / copy paste from internet, this function will reposition the cursor. Used to reposition the cursor back to 0,0 eliminating the need for cls and blinking.
fetchTerminalSize / void / half copy paste, it will fill TERMINAL_COL and TERMINAL_ROW. Used to center the game.
getMilis / long unsigned / Used to get current OS milisecond.

Variables

name / type / description
MAXHIGHSCORE / constint / this set the maximum number of score recorded in highscore.txt
TARGETFPS / constint / targeted frame per second.
MAPWIDTH / constint / the width of obstacle map.
MAPHEIGHT / constint / the height of displaybuffer.
SPEEDFACTOR / constint / this set how fast the speed rise. In this case, the speed increase every SPEEDFACTOR/TARGETFPS second.
SPEEDLIMIT / constint / the maximum speed.
INITIALLIFE / constint / initial life
MINSPEED / constint / minimum speed. The used will see 1 but it is actually this.
SPEEDEXPONENTIAL / const float / basically set where the speed increase is linear or not. if set to 1, it is linear. If set to greater than on the time taken to increase speed will be lower and lower the next time.

main.cpp , endgame.cpp and startmenu.cpp variables and functions.

Variables/Functions/
Files/Structures / Data type / Return type(if any) / Arguments(if any) / Description
showHighscore / None / None / highscore h / Display name of player and his/her highscore alone right after game over
printList() / None / None / None / Display all the name and highscores that had been saved
sendtofile() / None / None / ofstream &file, highscore h / Send the names and highscores to file
getfromfile() / highscore / Highscore / Ifstream &file / Get the names and highscores from file
sortlist / None / None / vector<highscore> &list / Sort the highscores according to ascending orders
savelist() / None / None / None / Save all the highscores in a file named “hiscore.txt”
loadlist() / None / None / None / Load all the previous highscores from a file named “hiscore.txt”
showgameover() / None / None / None / Display all the name and highscores saved
showwhenquit() / None / None / None / Display “You quitted” when the user quit the game before finish playing
hiscore.txt / Ifstream / None / None / Stores the highest scores of the game
Intro() / None / None / None / Display the introduction picture and title of our program
menu() / Int / Int / None / Display the menu which has ‘Play game,Instruction,Highscores,Credits and Exit’ selection
instruction() / None / None / None / Display the instruction needed on how to play the game

FlowCharts