|
Help with scrolling platforms (without transformation, or maybe if you can help) |
DJLad16
Member #14,857
January 2013
|
I'm trying to do screen scrolling but only with platforms I have created. But it turning out to be a real pain in the f***ing a*s, nothing will go right for me. I've tried using transformations, but it didn't work. So I think it will be easier to just displace every platform manually. (Unless you can help me with transformations). 1#include <allegro5/allegro.h>
2#include <allegro5/allegro_native_dialog.h>
3#include <allegro5/allegro_font.h>
4#include <allegro5/allegro_ttf.h>
5#include <allegro5/allegro_primitives.h>
6#include <allegro5/allegro_image.h>
7#include <allegro5/allegro_audio.h>
8#include <allegro5/allegro_acodec.h>
9#include "objects.h"
10
11#define ScreenWidth 800
12#define ScreenHeight 600
13
14//Prototpyes
15void initPlayer(Player &player);
16
17void initCoin(Coin &coin1);
18void drawCoin(Coin &coin1, ALLEGRO_BITMAP *image);
19bool coinCollide(Coin &coin1,Player &player ,int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, bool live, int coinCount);
20
21void initCoin2(Coin &coin2);
22void drawCoin2(Coin &coin2, ALLEGRO_BITMAP *image);
23bool coinCollide2(Coin &coin2,Player &player ,int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, bool live, int coinCount);
24
25void initArrow(Arrow arrow[], int size);
26void drawArrow(Arrow arrow[], int size, ALLEGRO_BITMAP *image);
27void fireArrow(Arrow arrow[], int size, Player &player, ALLEGRO_SAMPLE *bowShot);
28void updateArrow(Arrow arrow[], int size);
29
30bool collision(Player &player, int ex, int ey, int eWidth, int eHeight, int pWidth, int pHeight);
31
32//Global Variables
33enum direction {LEFT, RIGHT};
34int dir = LEFT;
35int arrowCount = 10;
36//const int NUM_ARROW = 10;
37const int NUM_ENEMY = 10;
38int groundHeight = 545;
39static bool fired = false;
40bool coinLive = false;
41
42int main()
43{
44
45 al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 24, ALLEGRO_REQUIRE);
46 ALLEGRO_DISPLAY *display;
47
48 if(!al_init())
49 {
50 al_show_native_message_box(0, 0, "Error", "Falied to initialize allegro", 0, 0);
51 return -1;
52 }
53
54 display = al_create_display(ScreenWidth, ScreenHeight);
55
56 if(!display)
57 {
58 al_show_native_message_box(0, 0, "Error", "Falied to initialize the display", 0, 0);
59 return -1;
60 }
61
62 Player player;
63 Coin coin1;
64 Coin coin2;
65 Arrow arrow[10];
66 bool done = false, active = false, draw = true;
67 bool jump = false;
68 int sourceX = 32, sourceY = 0;
69 int coinCount = 0;
70 const float FPS = 60.0;
71 float jumpSpeed = 16.5;
72 float velX = 0, velY = 0;
73 const float gravity = 1;
74 float cameraPosition[2] = {0,0};
75
76
77 al_init_image_addon();
78 al_init_primitives_addon();
79 al_install_keyboard();
80 al_init_font_addon();
81 al_init_ttf_addon();
82 al_install_audio();
83 al_init_acodec_addon();
84
85 initPlayer(player);
86 initArrow(arrow, 10);
87 initCoin(coin1);
88 initCoin2(coin2);
89
90 ALLEGRO_KEYBOARD_STATE keystate;
91 ALLEGRO_SAMPLE *coinCollect = al_load_sample("coin collect sound.wav");
92 ALLEGRO_SAMPLE *bowShot = al_load_sample("bow sound effect.wav");
93 ALLEGRO_SAMPLE *bgSong = al_load_sample("Background song.wav");
94 ALLEGRO_SAMPLE_INSTANCE *songInstance = al_create_sample_instance(bgSong);
95 ALLEGRO_BITMAP *character = al_load_bitmap("spritesheet(Bow & left + right).png");
96 ALLEGRO_BITMAP *bArrow = al_load_bitmap("Arrow.png");
97 ALLEGRO_BITMAP *background = al_load_bitmap("ayersrock.png");
98 ALLEGRO_BITMAP *coin = al_load_bitmap("coin2.png");
99 ALLEGRO_BITMAP *ground = al_load_bitmap("Ground.png");
100 ALLEGRO_BITMAP *platform = al_load_bitmap("platform(long).png");;
101 ALLEGRO_BITMAP *platform2 = al_load_bitmap("platform(short).png");
102 ALLEGRO_FONT *numArrow = al_load_font("JUNGBN__.TTF", 23, 0);
103 ALLEGRO_FONT *numCoin = al_load_font("JUNGBN__.TTF", 23, 0);
104 ALLEGRO_TIMER *timer = al_create_timer(1.0/FPS);
105 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
106
107 al_reserve_samples(3);
108 al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP);
109 al_attach_sample_instance_to_mixer(songInstance, al_get_default_mixer());
110 al_set_window_title(display, "Australian Outback");
111 al_register_event_source(event_queue, al_get_keyboard_event_source());
112 al_register_event_source(event_queue, al_get_timer_event_source(timer));
113 al_register_event_source(event_queue, al_get_display_event_source(display));
114
115 al_play_sample_instance(songInstance);
116
117 al_start_timer(timer);
118 while(!done)
119 {
120 ALLEGRO_EVENT event;
121 al_wait_for_event(event_queue, &event);
122 al_get_keyboard_state(&keystate);
123
124 if(al_key_down(&keystate, ALLEGRO_KEY_ESCAPE))
125 {
126 done = true;
127 }
128 else if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
129 {
130 done = true;
131 }
132
133
134 else if(event.type == ALLEGRO_EVENT_TIMER)
135 {
136 active = true;
137 updateArrow(arrow, 10);
138
139 if(al_key_down(&keystate, ALLEGRO_KEY_D))
140 {
141 velX = player.speed;
142 dir = RIGHT;
143
144 }
145 else if(al_key_down(&keystate, ALLEGRO_KEY_A))
146 {
147 velX = -player.speed;
148 dir = LEFT;
149 }
150 else
151 {
152 velX = 0;
153 active = false;
154 }
155 if(al_key_down(&keystate, ALLEGRO_KEY_W) && jump)
156 {
157 velY = -jumpSpeed;
158 jump = false;
159 }
160 if(active)
161 {
162 sourceX += al_get_bitmap_width(character) / 3;
163 }
164 else
165 {
166 sourceX = 32;
167 }
168 if(sourceX >= al_get_bitmap_width(character))
169 {
170 sourceX = 0;
171 }
172 sourceY = dir;
173 draw = true;
174 if(!jump)
175 velY += gravity;
176 else
177 velY = 0;
178 player.x += velX;
179 player.y += velY;
180
181 jump = (player.y >= groundHeight);
182
183 if(jump)
184 {
185 player.y = groundHeight;
186 }
187 }
188
189 if(al_key_down(&keystate, ALLEGRO_KEY_SPACE))
190 {
191 fireArrow(arrow, 10, player, bowShot);
192 }
193 else
194 {
195 fired = false;
196 }
197 if(collision(player, 360, 447, 351, 30, 32, 32))
198 {
199 groundHeight = 435;
200 player.y = groundHeight;
201 player.y += velY;
202
203 }
204 else if(collision(player, 70, 377, 214, 38, 32, 32))
205 {
206 groundHeight = 365;
207 player.y = groundHeight;
208 player.y += velY;
209 }
210 else
211 {
212 groundHeight = 545;
213 }
214
215 if(coinCollide(coin1, player, coin1.x, coin1.y, coin1.width, coin1.height, 32, 32, coinLive, coinCount))
216 {
217 coin1.live = false;
218 ++coinCount;
219 coin1.x = 0;
220 coin1.y = 0;
221 al_play_sample(coinCollect, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
222 }
223 if(coinCollide2(coin2, player, coin2.x, coin2.y, coin2.width, coin1.height, 32, 32, coinLive, coinCount))
224 {
225 coin2.live = false;
226 ++coinCount;
227 coin2.x = 0;
228 coin2.y = 0;
229 al_play_sample(coinCollect, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
230 }
231
232 if(draw && al_is_event_queue_empty(event_queue))
233 {
234 draw = false;
235 al_draw_bitmap(platform, 360, 447, 0);
236 al_draw_bitmap (platform2, 70, 377, 0);
237 al_draw_bitmap_region(character, sourceX, sourceY * al_get_bitmap_height(character) / 2, 32, 32, player.x, player.y, 0);
238 drawArrow(arrow, 10, bArrow);
239 drawCoin(coin1, coin);
240 drawCoin2(coin2, coin);
241 al_draw_textf(numArrow, al_map_rgb(252,209, 22), 10, 10, 0, "Arrows: %i", arrowCount);
242 al_draw_textf(numCoin, al_map_rgb(252, 209, 22), 10, 50, 0, "Coins: %i", coinCount);
243 al_flip_display();
244 al_clear_to_color(al_map_rgb(255, 255, 255));
245 al_draw_bitmap(background, 0,0, 0);
246 al_draw_bitmap(ground, 0, 549, 0);
247 }
248 }
249
250 al_destroy_bitmap(character);
251 al_destroy_bitmap(ground);
252 al_destroy_bitmap(coin);
253 al_destroy_bitmap(platform);
254 al_destroy_sample(coinCollect);
255 al_destroy_sample(bowShot);
256 al_destroy_sample(bgSong);
257 al_destroy_sample_instance(songInstance);
258 al_destroy_font(numArrow);
259 al_destroy_display(display);
260 al_destroy_event_queue(event_queue);
261 al_destroy_timer(timer);
262 return 0;
263}
264void initPlayer(Player &player)
265{
266 player.x = 10;
267 player.y = 545;
268 player.ID = PLAYER;
269 player.lives = 3;
270 player.speed = 5;
271 player.score = 0;
272}
273
274void initCoin(Coin &coin1)
275{
276 coin1.ID = COIN;
277 coin1.width = 16;
278 coin1.height = 16;
279 coin1.live = true;
280 coin1.x = 550;
281 coin1.y = 440;
282}
283void drawCoin(Coin &coin1, ALLEGRO_BITMAP *image)
284{
285 if(coin1.live)
286 {
287 if(image == NULL)
288 al_draw_filled_circle(coin1.x - 10, coin1.y - 10, 20, al_map_rgb(255,0,255));
289 else
290 al_draw_bitmap(image, coin1.x, coin1.y, 0);
291 }
292}
293void initCoin2(Coin &coin2)
294{
295 coin2.ID = COIN;
296 coin2.width = 16;
297 coin2.height = 16;
298 coin2.live = true;
299 coin2.x = 120;
300 coin2.y = 365;
301}
302void drawCoin2(Coin &coin2, ALLEGRO_BITMAP *image)
303{
304 if(coin2.live)
305 {
306 if(image == NULL)
307 al_draw_filled_circle(coin2.x - 10, coin2.y - 10, 20, al_map_rgb(255,0,255));
308 else
309 al_draw_bitmap(image, coin2.x, coin2.y, 0);
310 }
311}
312bool coinCollide(Coin &coin1,Player &player, int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, bool live, int coinCount)
313{
314 if((player.x > cX + cWidth) || (player.y > cY + cHeight) ||
315 (cX > player.x + pWidth) || (cY > player.y + pHeight) && (coin1.live))
316 {
317 return false;
318 }
319 return true;
320}
321bool coinCollide2(Coin &coin1,Player &player, int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, bool live, int coinCount)
322{
323 if((player.x > cX + cWidth) || (player.y > cY + cHeight) ||
324 (cX > player.x + pWidth) || (cY > player.y + pHeight) && (coin1.live))
325 {
326 return false;
327 }
328 return true;
329}
330
331void initArrow(Arrow arrow[], int size)
332{
333 for(int i = 0; i < size; i++)
334 {
335 arrow[i].ID = ARROW;
336 arrow[i].speed = 4.0;
337 arrow[i].live = false;
338 }
339}
340void drawArrow(Arrow arrow[], int size, ALLEGRO_BITMAP *image)
341{
342 for(int i = 0; i < size; i++)
343 {
344 if(arrow[i].live)
345 {
346 al_draw_bitmap(image, arrow[i].x, arrow[i].y, 0);
347 }
348 }
349}
350void fireArrow(Arrow arrow[], int size, Player &player, ALLEGRO_SAMPLE *bowShot)
351{
352 for(int i = 0; i < size; i++)
353 {
354 if(!arrow[i].live && !fired)
355 {
356 --arrowCount;
357 --size;
358 arrow[i].live = true;
359 arrow[i].x = player.x + 17;
360 arrow[i].y = player.y + 22;
361 fired = true;
362 al_play_sample(bowShot, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
363 }
364 }
365}
366void updateArrow(Arrow arrow[], int size)
367{
368 for(int i = 0; i < size; i++)
369 {
370 if(arrow[i].live)
371 {
372 arrow[i].x += arrow[i].speed;
373 if(arrow[i].x >= ScreenWidth - 5 && size <= 0)
374 {
375 arrow[i].live = false;
376 }
377 }
378 }
379}
380
381bool collision(Player &player, int ex, int ey, int eWidth, int eHeight, int pWidth, int pHeight)
382{
383 if((player.x > ex + eWidth - 10) || (player.y > ey + eHeight - 10) ||
384 (ex > player.x + pWidth - 10) || (ey > player.y + pHeight - 10))
385 {
386 return false;
387 }
388 return true;
389}
|
Jeff Bernard
Member #6,698
December 2005
|
Maybe I'm misunderstanding your question but the general idea is... float cameraX, cameraY; //... //update // set cameraX and cameraY to whatever // for instance, such that player.x-cameraX, player.y-cameraY puts the player in the center of the screen //... //draw al_draw_bitmap(platform, 360-cameraX, 447-cameraY, 0); //etc with all of your other draw operations
-- |
Schyfis
Member #9,752
May 2008
|
Yup, what you want is a camera. Also, do you really expect to continue doing this on a large scale? Coin coin1; Coin coin2; .... Coin coin324; void initCoin(){...} void initCoin2(){...} .... void initCoin324(){...} You're going to have to find a more convenient way to do it, for your own sake. ________________________________________________________________________________________________________ |
DJLad16
Member #14,857
January 2013
|
Yes I know schyfis, I will also need find a more effectient way to draw the platforms as well. |
|