I am working on my first scrolling game.
I am pretty new to game programming in general, but have so far successfully made a couple very simple games. (Pong, etc.)
I am using Dev-C++ and the Allegro library for this game.
It's basically going to be a cat that runs around and kills mice...and whatever other little things I can think to add later.
I have the can animated and running around, but I run into trouble with the background. I've tried some different combinations, but can't seem to get both the background and the cat on the screen at the same time. It's also not yet set up to scroll. I have been researching it for a while, but am stumped on how to get it going.
The game's resolution is 640x580, and my background is 5144x480.
For now, I just want it to continuously scroll...either by itself or preferably as the cat is moved forward/backward.
Please help!
Here is my code so far:
#include <stdio.h>
#include <allegro.h>
#define WIDTH 640
#define HEIGHT 480
#define MODE GFX_AUTODETECT_WINDOWED
#define WHITE makecol(255,255,255)
#define BLACK makecol(0,0,0)
BITMAP *kitty[7], *kittyb[7], *scene;
char s[20];
int curframe=0, framedelay=5, framecount=0;
int x=50, y=340, volume=128, n, b;
int main(void)
{
BITMAP *buffer;
BITMAP *bg;
MIDI *music;
//int pos, length;
//initialize the program
allegro_init();
install_keyboard();
install_timer();
set_color_depth(16);
set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
textout_ex(screen, font, "AnimSprite Program (ESC to quit)",
0, 0, WHITE,0);
//create the back buffer
buffer = create_bitmap(WIDTH,HEIGHT);
//load background
bg = load_bitmap("background.bmp", NULL);
if (!bg) {
allegro_message("Error loading background image\n%s", allegro_error);
return 1;
}
//install the sound driver
if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0) {
allegro_message("Error initializing sound system\n%s\n", allegro_error);
return 1;
}
//load the MIDI file
music = load_midi("Head.mid");
if (!music) {
allegro_message("Error loading MIDI file!");
return 1;
}
//play the music
if (play_midi(music, 0) != 0) {
allegro_message("Error playing MIDI\n%s", allegro_error);
return 1;
}
//load the animated sprites
for (n=0; n<6; n++)
{
sprintf(s,"cat%d.bmp",n+1);
kitty[n] = load_bitmap(s, NULL);
}
for (n=0; n<6; n++)
{
sprintf(s,"cat%db.bmp",n+1);
kittyb[n] = load_bitmap(s, NULL);
}
/* Create a scene buffer to draw into. */
scene = create_bitmap(screen->w, screen->h);
/***************************************************** -THE GAME LOOP!- ***/
while(!key[KEY_ESC])
{
//fill screen with background image
blit(bg, buffer, 0, 0, 0, 0, WIDTH, HEIGHT);
if (key[KEY_RIGHT])
{
b = 0;
//update the position of cat
x += 5;
if (x > SCREEN_W - kitty[0]->w)
{
x = SCREEN_W - kitty[0]->w;
}
//update the frame
if (framecount++ > framedelay)
{
framecount = 0;
curframe++;
if (curframe > 5)
curframe = 0;
}
/* Clear the drawing buffer. */
clear_to_color(scene, BLACK);
//draw the sprite
draw_sprite(scene, kitty[curframe], x, y);
}
if (key[KEY_LEFT])
{
b = 1;
//update the position of cat
x -= 5;
if (x < 0)
{
x = 0;
}
//update the frame
if (framecount++ > framedelay)
{
framecount = 0;
curframe++;
if (curframe > 5)
curframe = 0;
}
//clear the drawing buffer
clear_to_color(scene, BLACK);
//draw the sprite
draw_sprite(scene, kittyb[curframe], x, y);
}
if (key[KEY_UP])
{
//update the position of cat
y -= 5;
if (y < 220)
{
y = 220;
}
//update the frame
if (framecount++ > framedelay)
{
framecount = 0;
curframe++;
if (curframe > 5)
curframe = 0;
}
//clear the drawing buffer
clear_to_color(scene, BLACK);
//draw the sprite
if (b == 0)
{
draw_sprite(scene, kitty[curframe], x, y);
}
else if (b == 1)
{
draw_sprite(scene, kittyb[curframe], x, y);
}
}
if (key[KEY_DOWN])
{
//update the position of cat
y += 5;
if (y > SCREEN_H-100)
{
y = SCREEN_H-100;
}
//update the frame
if (framecount++ > framedelay)
{
framecount = 0;
curframe++;
if (curframe > 5)
curframe = 0;
}
//clear the drawing buffer
clear_to_color(scene, BLACK);
//draw the sprite
if (b == 0)
{
draw_sprite(scene, kitty[curframe], x, y);
}
else if (b == 1)
{
draw_sprite(scene, kittyb[curframe], x, y);
}
}
if (key[KEY_SPACE])
{
//update the position of cat
y -= 5;
if (y < 220)
{
y = 220;
}
//update the frame
if (framecount++ > framedelay)
{
framecount = 0;
curframe++;
if (curframe > 5)
curframe = 0;
}
//clear the drawing buffer
clear_to_color(scene, BLACK);
//draw the sprite
if (b == 0)
{
draw_sprite(scene, kitty[curframe], x, y);
}
else if (b == 1)
{
draw_sprite(scene, kittyb[curframe], x, y);
}
}
if (key[KEY_EQUALS] && volume<255) {
volume++;
set_volume(0,volume);
}
else if (key[KEY_MINUS] && volume>0) {
volume--;
set_volume(0,volume);
}
//DISPLAY SOME INFO
textprintf_ex(/*screen*/scene,font,0,0,WHITE,0,
"-,+ - Volume Down,Up");
textprintf_ex(/*screen*/scene,font,0,10,WHITE,0,
"Arrow Keys - Movement");
textprintf_ex(/*screen*/scene,font,0,30,WHITE,0,
"Sprite X,Y: %3d,%3d", x, y);
textprintf_ex(/*screen*/scene,font,0,40,WHITE,0,
"Frame,Count,Delay: %2d,%2d,%2d",
curframe,framecount,framedelay);
/* Put buffer to display. */
acquire_screen();
blit(scene, screen, 0, 0, 0, 0, WIDTH, HEIGHT);
release_screen();
rest(10);
}
stop_midi();
destroy_midi(music);
remove_sound();
allegro_exit();
return 0;
}
END_OF_MAIN()