|
Collision Detection with pong game |
vivosmith
Member #14,071
February 2012
|
1#include <stdio.h>
2#include <allegro5/allegro.h>
3#include<allegro5/allegro_image.h>
4
5const float FPS = 60;
6const int SCREEN_W = 640;
7const int SCREEN_H = 560;
8const int BOUNCER_SIZE = 32;
9enum MYKEYS {
10 KEY_UP, KEY_DOWN
11};
12
13int main(int argc, char **argv)
14{
15 ALLEGRO_DISPLAY *display = NULL;
16 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
17 ALLEGRO_TIMER *timer = NULL;
18 ALLEGRO_BITMAP *bouncer = NULL;
19 ALLEGRO_BITMAP *background = NULL;
20 ALLEGRO_BITMAP *com1=NULL;
21 ALLEGRO_BITMAP *ball=NULL;
22 float bouncer_x = SCREEN_W /2.0 - BOUNCER_SIZE / 2.0 -290;
23 float bouncer_y = SCREEN_H /2.0 - BOUNCER_SIZE / 2.0 -200;
24 float ball_x=320;
25 float ball_y=240 ;
26 float ball_xd= -5.0;
27 float ball_yd= 4.0;
28 float play1_x=-5.0;
29 float play1_y=4.0;
30 float com1_pos_x=570;
31 float com1_pos_y=150;
32 bool key[4] = { false, false};
33 bool redraw = true;
34 bool doexit = false;
35
36
37 if(!al_init()) {
38 fprintf(stderr, "failed to initialize allegro!\n");
39 return -1;
40 }
41 if(!al_init_image_addon()){
42 fprintf(stderr,"Failed to Initialize Images.\n");
43 return -1;}
44
45 if(!al_install_keyboard()) {
46 fprintf(stderr, "failed to initialize the keyboard!\n");
47 return -1;
48 }
49
50
51 timer = al_create_timer(1.0 / FPS);
52 if(!timer) {
53 fprintf(stderr, "failed to create timer!\n");
54 return -1;
55 }
56
57 display = al_create_display(SCREEN_W, SCREEN_H);
58 if(!display) {
59 fprintf(stderr, "failed to create display!\n");
60 al_destroy_timer(timer);
61 return -1;
62 }
63
64 bouncer = al_load_bitmap("player1.png");
65 if(!bouncer) {
66 fprintf(stderr, "failed to create bouncer bitmap!\n");
67 al_destroy_display(display);
68 al_destroy_timer(timer);
69 return -1;
70 }
71 background=al_load_bitmap("background.png");
72 if(!background){
73 fprintf(stderr,"Failed to Initialized Background.\n");
74 return -1;}
75 com1=al_load_bitmap("player2.png");
76 if(!com1){
77 fprintf(stderr,"Failed to initialize opponent.\n");
78 return -1;
79 }
80 ball=al_load_bitmap("ball.png");
81 if(!ball){fprintf(stderr,"Failed to initialize ball.\n");
82 return -1;}
83
84 event_queue = al_create_event_queue();
85 if(!event_queue) {
86 fprintf(stderr, "failed to create event_queue!\n");
87 al_destroy_bitmap(bouncer);
88 al_destroy_display(display);
89 al_destroy_timer(timer);
90 return -1;
91 }
92
93 al_register_event_source(event_queue, al_get_display_event_source(display));
94
95 al_register_event_source(event_queue, al_get_timer_event_source(timer));
96
97 al_register_event_source(event_queue, al_get_keyboard_event_source());
98
99 al_clear_to_color(al_map_rgb(0,0,0));
100
101 al_flip_display();
102
103 al_start_timer(timer);
104
105 while(!doexit)
106{
107 ALLEGRO_EVENT ev;
108 al_wait_for_event(event_queue, &ev);
109
110 if(ev.type == ALLEGRO_EVENT_TIMER) {
111 if(key[KEY_UP] && bouncer_y >= 65.0){
112 bouncer_y -= 4.0;
113 }
114
115 if(key[KEY_DOWN] && bouncer_y <= 240.0){
116 bouncer_y += 4.0;}
117
118
119 redraw=TRUE;
120}
121
122 else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
123 break;
124 }
125 else if(ev.type == ALLEGRO_EVENT_KEY_DOWN) {
126 switch(ev.keyboard.keycode) {
127 case ALLEGRO_KEY_UP:
128 key[KEY_UP] = true;
129 break;
130
131 case ALLEGRO_KEY_DOWN:
132 key[KEY_DOWN] = true;
133 break;
134
135 }}
136 else if(ev.type == ALLEGRO_EVENT_KEY_UP) {
137 switch(ev.keyboard.keycode) {
138 case ALLEGRO_KEY_UP:
139 key[KEY_UP] = false;
140 break;
141
142 case ALLEGRO_KEY_DOWN:
143 key[KEY_DOWN] = false;
144 break;
145
146
147 case ALLEGRO_KEY_ESCAPE:
148 doexit = true;
149 break;
150 }
151
152 } int com_speed=5;
153 if(ev.type==ALLEGRO_EVENT_TIMER){
154
155 if(ev.type == ALLEGRO_EVENT_TIMER){
156 if(ball_x<0||ball_x>SCREEN_W-BOUNCER_SIZE||ball_x>=com1_pos_x-BOUNCER_SIZE){
157 ball_xd= -ball_xd;}
158 if(ball_y<0||ball_y>SCREEN_H-BOUNCER_SIZE){
159 ball_yd= -ball_yd;}
160 ball_y+= ball_yd;
161 ball_x+=ball_xd;
162 redraw=TRUE;
163 com1_pos_y=ball_y/2.0+20;
164 }
165 }
166
167
168
169
170
171
172
173 if(redraw && al_is_event_queue_empty(event_queue)) {
174 redraw = false;
175
176 al_clear_to_color(al_map_rgb(0,0,0));
177
178 al_draw_bitmap(background,0,0,0);
179 al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
180 al_draw_bitmap(com1,com1_pos_x,com1_pos_y,0);
181 al_draw_bitmap(ball,ball_x,ball_y,0);
182
183
184 al_flip_display();
185 }}
186
187
188
189 al_destroy_bitmap(bouncer);
190 al_destroy_bitmap(background);
191 al_destroy_bitmap(com1);
192 al_destroy_bitmap(ball);
193 al_destroy_timer(timer);
194 al_destroy_display(display);
195 al_destroy_event_queue(event_queue);
196
197 return 0;}
Hello, |
weapon_S
Member #7,859
October 2006
|
You have separated the logic from the input in the right way. You are using indentation in a horrible way. |
|