Skip to content

Instantly share code, notes, and snippets.

@Yi-Tseng
Created May 7, 2013 08:34
Show Gist options
  • Save Yi-Tseng/5531127 to your computer and use it in GitHub Desktop.
Save Yi-Tseng/5531127 to your computer and use it in GitHub Desktop.
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_audio.h>
#include <allegro5/allegro_acodec.h>
#include <allegro5/allegro_image.h>
#include <stdio.h>
struct bar{
int y;
}bar1, bar2;
struct Ball{
int x;
int y;
int v_x;
int v_y;
}ball;
bool finish = false;
int sc1, sc2;
ALLEGRO_FONT* font;
ALLEGRO_SAMPLE *soundEffect;
ALLEGRO_SAMPLE *soundEffect2;
ALLEGRO_BITMAP* ballpng;
ALLEGRO_BITMAP* bgpng;
void move_ball(){
ball.x += ball.v_x;
ball.y += ball.v_y;
if(ball.x<5){
ball.v_x *= -1;
sc2++;
al_play_sample(soundEffect2, 1.0, 0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
}else if(ball.x>635){
ball.v_x *= -1;
sc1++;
al_play_sample(soundEffect2, 1.0, 0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
}
if(ball.y<5 || ball.y>475){
ball.v_y *= -1;
al_play_sample(soundEffect, 1.0, 0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
}
}
void draw_ball(){
// al_draw_circle(ball.x, ball.y, 10, al_map_rgb(0, 0, 0), 10);
// draw bitmap
al_draw_bitmap(ballpng, ball.x, ball.y, 0);
}
void draw_bg(){
al_draw_bitmap(bgpng, 0, 0, 0);
}
void draw_bar(){
// bar1
al_draw_rectangle(0, bar1.y, 20, bar1.y+100, al_map_rgb(255, 0, 0), 5);
// bar2
al_draw_rectangle(620, bar2.y, 640, bar2.y+100, al_map_rgb(255, 0, 0), 5);
}
void keyboard(){
ALLEGRO_KEYBOARD_STATE state;
al_get_keyboard_state(&state);
if(al_key_down(&state, ALLEGRO_KEY_UP)){
bar2.y -= 3;
}else if(al_key_down(&state, ALLEGRO_KEY_DOWN)){
bar2.y += 3;
}
if(al_key_down(&state, ALLEGRO_KEY_A)){
bar1.y -= 3;
}else if(al_key_down(&state, ALLEGRO_KEY_Z)){
bar1.y += 3;
}
if(al_key_down(&state, ALLEGRO_KEY_ESCAPE)){
finish = true;
}
}
void hit(){
if(ball.x < 20 && bar1.y < ball.y && ball.y < bar1.y+100 && ball.v_x < 0){
printf("hit");
ball.v_x *= -1;
al_play_sample(soundEffect, 1.0, 0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
}
if(ball.x+10 > 620 && bar2.y < ball.y && ball.y < bar2.y+100 && ball.v_x > 0){
printf("hit");
ball.v_x *= -1;
al_play_sample(soundEffect, 1.0, 0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
}
}
void drawScore(){
// char score[4];
//
// score[0] = '0' + sc1;
// score[1] = ':';
// score[2] = '0' + sc2;
// score[3] = '\0';
//
char score[10];
sprintf(score, "%d : %d", sc1, sc2);
al_draw_text(font, al_map_rgb(0, 0, 0), 320, 200, ALLEGRO_ALIGN_CENTRE, score);
}
int main(int argc, char **argv) {
ALLEGRO_DISPLAY* display;
if(!al_init()){
return -1;
}
display = al_create_display(640, 480);
if(!display){
return -1;
}
// init addon and keyboard
al_init_primitives_addon();
al_init_font_addon();
al_init_ttf_addon();
al_install_keyboard();
al_init_acodec_addon();
al_install_audio();
al_init_image_addon();
// init ball
ball.x = 30;
ball.y = 40;
ball.v_x = 1;
ball.v_y = 1;
ballpng = al_load_bitmap("ball.png");
// init font
font = al_load_ttf_font("font.ttf", 48, 0);
// init score
sc1 = 0;
sc2 = 0;
// init sound effect
soundEffect = al_load_sample("effect.wav");
soundEffect2 = al_load_sample("goal.wav");
al_reserve_samples(4);
// init bg
bgpng = al_load_bitmap("bg.png");
while(!finish){
al_clear_to_color(al_map_rgb(255, 255, 255));
keyboard();
hit();
move_ball();
printf("%d %d\n", ball.x, ball.y);
draw_bg();
draw_ball();
draw_bar();
drawScore();
al_flip_display();
al_rest(0.001);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment