Last active
December 15, 2015 16:39
-
-
Save Yi-Tseng/5290774 to your computer and use it in GitHub Desktop.
20130402 社課
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <allegro5/allegro.h> | |
#include <allegro5/allegro_primitives.h> | |
#include <allegro5/allegro_ttf.h> | |
#include <allegro5/allegro_font.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; | |
void move_ball(){ | |
ball.x += ball.v_x; | |
ball.y += ball.v_y; | |
if(ball.x<5){ | |
ball.v_x *= -1; | |
sc2++; | |
}else if(ball.x>635){ | |
ball.v_x *= -1; | |
sc1++; | |
} | |
if(ball.y<5 || ball.y>475){ | |
ball.v_y *= -1; | |
} | |
} | |
void draw_ball(){ | |
al_draw_circle(ball.x, ball.y, 10, al_map_rgb(0, 0, 0), 10); | |
} | |
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; | |
} | |
if(ball.x+10 > 620 && bar2.y < ball.y && ball.y < bar2.y+100 && ball.v_x > 0){ | |
printf("hit"); | |
ball.v_x *= -1; | |
} | |
} | |
void drawScore(){ | |
char score[4]; | |
score[0] = '0' + sc1; | |
score[1] = ':'; | |
score[2] = '0' + sc2; | |
score[3] = '\0'; | |
al_draw_text(font, al_map_rgb(0, 0, 0), 320, 240, 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(); | |
// init ball | |
ball.x = 30; | |
ball.y = 40; | |
ball.v_x = 1; | |
ball.v_y = 1; | |
// init font | |
font = al_load_ttf_font("font.ttf", 48, 0); | |
// init score | |
sc1 = 0; | |
sc2 = 0; | |
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_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