Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Auto Scrolling like gradius or raden trad

This thread is locked; no one can reply to it. rss feed Print
Auto Scrolling like gradius or raden trad
Rich Romer
Member #6,734
December 2005

I'm making a game thats like has auto scrolling like gradius or raden trad. The background updates constantly and makes it look like your moving. I dont know how to do this though since I'm new to game programming. So far i built something that scrolls the level but when i add in the ship and movement controlls it doesn't work.

I know alot of people have done this before but i cant find code or a tutorial that makes sense. ???????????????

---- code ------------------------------

int map_x = 0;
int map_y = 1490;
int player_x = 200;
int player_y = 1800;
int scroll = 0;
void scroll_screen() {
scroll++;
}
END_OF_FUNCTION(scroll_screen);

int main(int argc, char *argv[])
{
allegro_init();
install_timer();
install_keyboard(); // Initialize keyboard routines
install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL);
set_color_depth(32); // Set the color depth
set_gfx_mode(GFX_AUTODETECT, 640,480,0,0); // Change our graphics mode to 640x480
MIDI *music;
music = load_midi("midi/level_1.mid");
play_midi(music, 1);

BITMAP* level = load_bitmap("art/level_1.bmp", NULL);

BITMAP* buffer = create_bitmap(640,1919);

BITMAP* player = load_bitmap("art/Ship_TOP.bmp", NULL);

install_int_ex(scroll_screen, BPS_TO_TIMER(60));//Set our BPS

while ( !key[KEY_ESC] ) {
while( scroll > 0 ) {
if ( key[KEY_UP]){
player_y = map_y--;
}
if ( key[KEY_DOWN]){
player_y = map_y++;
}
if ( key[KEY_LEFT] ){
player_x = map_x--;
}
if ( key[KEY_RIGHT] ){
player_x = map_x++;
}
map_y--;
scroll--;
}
draw_sprite(buffer, level, 0,0);
draw_sprite(buffer, player, player_x, player_y);
blit(buffer, screen, 0,map_y,0,0,640,1919);
clear_bitmap(buffer);
}
return 0; // Exit with no errors

BAF
Member #2,981
December 2002
avatar

Wait, what do you want? A parallaxed background?

Rich Romer
Member #6,734
December 2005

i dont know what a parralex background is.

what i want to do is have the map auto scroll like in games like gradius, r-type, radien trad.

tapir
Member #6,629
November 2005

I never heard of these games

Christopher Bludau
Member #5,401
January 2005

Quote:

blit(buffer, screen, 0,map_y,0,0,640,1919);

should that not be something like:

blit(buffer, screen, map_x,map_y,0,0,640,480);

Onewing
Member #6,152
August 2005
avatar

[EDIT] OOps, guess I should learn how to read. ;D

Quote:

should that not be something like:

blit(buffer, screen, map_x,map_y,0,0,640,480);

I think he's trying to scroll vertically.

[EDIT2]

Quote:

So far i built something that scrolls the level but when i add in the ship and movement controlls it doesn't work.

What exactly isn't working?

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Christopher Bludau
Member #5,401
January 2005

but in his blitt the blitted image is BIGGER than the screen. this makes no sense.

And the map_x var is not used in his blit.

Quote:

BITMAP* buffer = create_bitmap(640,1919);

But as the buffer is the same size as the screen this should be no problem.

Hmm. Sorry. Just had a quick look at the code.

Onewing
Member #6,152
August 2005
avatar

Quote:

but in his blitt the blitted image is BIGGER than the screen. this makes no sense.

It's bigger in the y direction, not the x direction.

Quote:

And the map_x var is not used in his blit.

He's keeping the x-cood of the map stationary, like one of those arcade top-down view plane games that scroll up. Perhaps he's planning on having map_x elsewhere...

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Christopher Bludau
Member #5,401
January 2005

i Would do it like this:

BITMAP* buffer = create_bitmap(640,1919);
BITMAP* temp = create_bitmap(640,480);

blit(buffer, temp, camera_x, camera_y, 0, 0, 640, 480);
draw_sprite(temp, player, player_x, player_y);


blit(temp, screen, 0,0,0,0,640,480);

Everytime the player/ship moves change the player_x, player_y vars.
(make sure player_x is between 0 and 639, player_y between 0 and 479)

To scroll the background change the camera_x or camera_y vars.

Only a small part of the code. Hope this makes things clearer. If not please ask.
I'm tired and to foul to write more ;)

EDIT

Quote:

It's bigger in the y direction, not the x direction.

yea. Thats why I changed this part of the blit. Makes no sense to blit a 1000+ image to a screen which is only 480 in size. The most parts of the images would not be visable.

Quote:

He's keeping the x-cood of the map stationary, like one of those arcade top-down view plane games that scroll up. Perhaps he's planning on having map_x elsewhere...

Yes, I did oversee this at the first quick look. Sorry. ;)

Onewing
Member #6,152
August 2005
avatar

Quote:

i Would do it like this:

So would I, moreorless, except I'd switch the buffer and temp names (personal preference, just like the buffer to mimic what's going on the screen).

Quote:

I'm tired and to foul to write more

That's okay, you can't strike out with just fouls. ;D

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Rich Romer
Member #6,734
December 2005

Christopher, could you go more into what you where talking about in terms of using the camera_x and camera_y method.

Christopher Bludau
Member #5,401
January 2005

Try this code and see if it does what you want.
Than you can refine it ;)

It's very simple code. You can fly with your ship out of the screen.
But it will show you what i meant.

1#include <allegro.h>
2 
3 volatile int camera_x = 0;
4 volatile int camera_y = 1919-479;
5 int player_x = 30;
6 int player_y = 200;
7 
8void scroll_level()
9{
10 
11 
12 if (camera_y > 0) camera_y--;
13
14}
15END_OF_FUNCTION(scroll_level);
16 
17int main()
18{
19 allegro_init();
20
21 install_keyboard();
22 install_timer();
23
24 
25
26 /* set a 15 or 16 bpp video mode */
27 set_color_depth(16);
28 if (set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0) != 0) {
29 set_color_depth(15);
30 if (set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480, 0, 0) != 0) {
31 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
32 allegro_message("Error setting a 15 or 16 bpp 800x600 video mode\n%s\n", allegro_error);
33 return 1;
34 }
35 }
36
37
38
39 BITMAP* temp = create_bitmap(640,480);
40
41 BITMAP* player = load_bitmap("art/Ship_TOP.bmp", NULL);
42 BITMAP* level = load_bitmap("art/level_1.bmp", NULL);
43
44
45 install_int(scroll_level, 20);
46 
47 while (!key[KEY_ESC]) {
48
49 
50 blit(level, temp, camera_x, camera_y, 0, 0, 640, 480);
51 draw_sprite(temp, player, player_x, player_y);
52 blit(temp, screen, 0,0,0,0,640,480);
53
54 if (key[KEY_UP]) player_y--;
55 if (key[KEY_DOWN]) player_y++;
56 if (key[KEY_LEFT]) player_x--;
57 if (key[KEY_RIGHT]) player_x++;
58 
59 }
60
61 
62 
63 
64 
65
66 
67
68 return 0;
69 
70}
71END_OF_MAIN();

If something is unclear feel free to ask.

Rich Romer
Member #6,734
December 2005

ok i thought about what chris said for a while now and i think i understand the whole idea. I put it into a sample program and it seems to work. Thank you very much for posting that code snippet. ill try out that new code you put up just now and see how that goes.

Christopher Bludau
Member #5,401
January 2005

No problem. I hope it helped you.
If something is unclear, just ask.

Go to: