Hey guys,
this is my first game made with purely coding, I have made some games in the past with Blender G.E.
It is a Pong spin-off.
Anyways, I just wanted to show it off and get some tips to make it "Better" and "Efficient" , because I know that this program is horribly coded
[source='C++']
#define D_WIDTH 800
#define D_HEIGHT 600
int InitGame(){
std::cout<<"INITIALIZING GAME\n";
if(!al_init()){
return -1;
}
std::cout<<"COMPLETE\n";
std::cout<<"INITIALIZING ADDONS\n";
al_init_primitives_addon();
al_init_font_addon();
al_init_ttf_addon();
al_install_keyboard();
al_install_audio();
al_init_acodec_addon();
al_reserve_samples(1);
std::cout<<"COMPLETE\n";
}
enum KEYS{UP,DOWN};
ALLEGRO_DISPLAY *display;
bool gamerunning = true;
bool keys[2] ={false,false};
void DrawMap(){
al_draw_line(D_WIDTH-1,0,D_WIDTH-1,600,al_map_rgb(200,200,200),10.0);
al_draw_line(D_WIDTH/2,0,D_WIDTH/2,600,al_map_rgb(35,35,35),5.0);
al_draw_circle(D_WIDTH/2,D_HEIGHT/2,125,al_map_rgb(35,35,35),5.0);
}
const float FPS = 60.0;
int randomnum(){
return rand()%3 + 1;
}
int dif = 1;int velX =randomnum(),velY=randomnum();;
int main(void){
InitGame();
int pscore=0;
int ball_x = 400,ball_y = 300;
srand(time(NULL));
//display//
ALLEGRO_TIMER *timer = nullptr;
timer = al_create_timer(1.0/FPS);
al_set_new_display_flags(ALLEGRO_NOFRAME);
display = al_create_display(D_WIDTH,D_HEIGHT);
if(!display){ return -1;}
al_set_window_title(display,"Pong! by Junaid");
//buffer = al_create_bitmap(D_WIDTH,D_HEIGHT);
al_set_target_bitmap(al_get_backbuffer(display));
al_flip_display();
//display//
std::cout<<"Start screen loaded!\n";
//FONTS//
ALLEGRO_FONT *pongtitle = al_load_font("resources/font/ZIG.TTF",50,NULL);
ALLEGRO_FONT *smalltitle = al_load_font("resources/font/ZIG.ttf",25,NULL);
ALLEGRO_FONT *score = al_load_font("resources/font/HISCORE.ttf",20,NULL);
//FONTS_end//
//bgSOUND//
ALLEGRO_SAMPLE *sidecol = al_load_sample("resources/sound/sidecollision.wav");
ALLEGRO_SAMPLE *sound_bg_loop = al_load_sample("resources/sound/bg.wav");
al_play_sample(sound_bg_loop,1.0,0.0,1.0,ALLEGRO_PLAYMODE_LOOP,NULL);
//ENDBGSOUND//
//drawSTARTUPscreen//
al_clear_to_color(al_map_rgb(256,256,256));
al_draw_line(-1,500,801,500,al_map_rgb(55,55,55),4.0);
al_draw_line(-1,100,801,100,al_map_rgb(55,55,55),4.0);
al_draw_text(pongtitle,al_map_rgb(200,200,200),D_WIDTH/2,((D_HEIGHT/2)-100),ALLEGRO_ALIGN_CENTER,"PONG! beta_v1");
al_draw_text(pongtitle,al_map_rgb(150,150,150),D_WIDTH/2,((D_HEIGHT/2)-45),ALLEGRO_ALIGN_CENTER,"By Junaid");
al_draw_text(smalltitle,al_map_rgb(100,100,100),D_WIDTH/2+10,D_HEIGHT/2+55,ALLEGRO_ALIGN_CENTER,"Press any key to play...");
al_flip_display();
//startCONTROLS//
bool startgame = false;
ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
ALLEGRO_EVENT startevent;
al_register_event_source(event_queue,al_get_keyboard_event_source());
while(!startgame){
al_wait_for_event(event_queue,&startevent);
startgame=true;
}
//endSTARTCONTROLS//
std::cout<<"Game started!\n";
std::cout<<"Loading the game...\n";
al_rest(0.5);
std::cout<<"Loading complete!\n";
//drawSTARTUPscreen-end//
al_set_target_bitmap(al_get_backbuffer(display));
al_clear_to_color(al_map_rgb(256,256,256));
al_flip_display();
std::cout<<"\n\n\nDebug mode started.\n\n-------------------------------\n";
//gameloop//
int y1 = 300,y2 = 400;
al_destroy_sample(sound_bg_loop);
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_start_timer(timer);
while(gamerunning)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue,&ev);
if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_UP:
std::cout<<"UP";
keys[UP] = true;
break;
case ALLEGRO_KEY_DOWN:
std::cout<<"DOWN";
keys[DOWN] = true;
break;
}
}
else if(ev.type == ALLEGRO_EVENT_KEY_UP){
switch(ev.keyboard.keycode){
case ALLEGRO_KEY_UP:
keys[UP] = false;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = false;
break;
case ALLEGRO_KEY_ESCAPE:
if(al_show_native_message_box(display,"Quit game","Are you sure you want to exit the game?","",NULL,ALLEGRO_MESSAGEBOX_YES_NO) == true){
gamerunning = false;
}
break;
}
}
y1 -= keys[UP]*10;y1 += keys[DOWN]*10;
y2 -= keys[UP]*10;y2 += keys[DOWN]*10;
if(y1 == 0 || y1 <= 0){
y1 = 1; y2 = y1+100;
}
if(y2 == 600 || y2 >= 600){
y2 = 599; y1 = y2 - 100;
}
//ball//
char scor[10];
itoa(pscore, scor, 10);
///
ball_x+=velX;
ball_y+=velY;
if(ball_y-7 < 1 || ball_y+7 > 599){
al_play_sample(sidecol,1.0,0.0,5.0,ALLEGRO_PLAYMODE_ONCE,NULL);
velY = -velY;
}
if(ball_x+7 > 799){
al_play_sample(sidecol,1.0,0.0,5.0,ALLEGRO_PLAYMODE_ONCE,NULL);
velX = -velX;//pscore+=1;
if(rand()%100+1 < 50 && dif <= 10){dif++;velX = velX-=1; velY+=1;}
}
bool collisionpaddle = false;
if( ball_x >= 20 &&
ball_y >= y1 &&
ball_x <= 30 &&
ball_y <= y2){
collisionpaddle = true;
pscore+=1;
velX = -velX;
std::cout<<"\ncollison "<
}
collisionpaddle = false;
if(ball_x <= 0){
//al_play_sample(lost,1.0,0.0,1.0,ALLEGRO_PLAYMODE_ONCE,NULL);
//al_rest(2.0);
al_show_native_message_box(display,"GAME OVER!","Your total score was ",scor,NULL,NULL);
gamerunning = false;
}
//endball//
//increase difficulty//
//incdif();
char diflevel[10];
itoa(dif, diflevel, 10);
//
al_draw_text(smalltitle,al_map_rgb(50,50,50),600,400,ALLEGRO_ALIGN_CENTER,"Score");
al_draw_text(score,al_map_rgb(50,50,50),600,450,ALLEGRO_ALIGN_CENTER,scor);
al_draw_text(smalltitle,al_map_rgb(50,50,50),600,150,ALLEGRO_ALIGN_CENTER,"Difficulty");
al_draw_text(score,al_map_rgb(50,50,50),600,200,ALLEGRO_ALIGN_CENTER,diflevel);
al_draw_line(D_WIDTH,0,D_WIDTH,D_HEIGHT,al_map_rgb(150,150,150),5.0);
al_draw_line(0,0,D_WIDTH,0,al_map_rgb(150,150,150),5.0);
al_draw_line(0,D_HEIGHT,D_WIDTH,D_HEIGHT,al_map_rgb(150,150,150),5.0);
al_draw_filled_rectangle(20,y1,30,y2,al_map_rgb(100,100,100));
al_draw_filled_circle(ball_x,ball_y,7,al_map_rgb(175,175,175));
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
}
//ENDdrawmaingame//
//destruction//
al_destroy_sample(sidecol);
al_destroy_timer(timer);
al_destroy_event_queue(event_queue);
al_destroy_font(pongtitle);
al_destroy_font(score);
al_destroy_font(smalltitle);
//al_destroy_sample(sound_bg_loop);
al_destroy_display(display);
//end distruction//
}
[/source]
Controls :
Up Arrow : move paddle up
Down Arrow : move paddle down
Escape : quit