www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/10/21/18:25:07

Message-Id: <199610212154.RAA06637@shore.intercom.net>
Comments: Authenticated sender is <jeremyfo AT shore DOT intercom DOT net>
From: "Jeremy Ford" <jeremyfo AT shore DOT intercom DOT net>
To: djgpp AT delorie DOT com
Date: Mon, 21 Oct 1996 05:53:55 +0000
Subject: Allegro: How do you move sprites around with the arrow keys?

Hello,

I was wondering how you moved a sprite around using the arrow keys in 
allegro?

I wanted to draw the background and then move another sprite on top 
of it using the arrow keys.  I also don't want the background to get 
erased while the sprite is being moved.

Below I have included my code..,so everybody can see that I actually 
tried and that I don't just want someone to do it for me :-)

If anybody can give me some sample code or show me  what I'm doing wrong
please help me :-)
thanks
Jeremy Ford
p.s. I know the code below probaly isn't very good but I'm just 
learning..everybody has to start somewhere :-)

/* this code will not compile without my header and data file
 * if your interested in compiling this just -mail me and I will send 
  *you the files*/
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <dir.h>
#include <pc.h>
#include "rifle.h"
#include "allegro.h"

char *datafile_name = "rifle.dat";
DATAFILE *rifle_data;
BITMAP *sprite_buffer;
int *file_attr;
int next;
void animate(void);
void scope_move();

void main()
{
   if((file_exists(datafile_name, 32, file_attr))==0)
   {
      printf("Datafile %s not found..Exiting program",datafile_name);
      exit(0);
   }
   allegro_init();
   install_keyboard();
   install_mouse();
   clear_keybuf();
   set_gfx_mode(GFX_VGA,320,200,0,0);
   rifle_data = load_datafile(datafile_name);
   set_pallete(rifle_data[PALLETE_001].dat);
   sprite_buffer = create_bitmap(320,200);
   clear(sprite_buffer);
   do
   {
      draw_sprite(sprite_buffer,rifle_data[INTRO].dat,0,0);
      animate();
   }
   while(next == 1);
   readkey();
   fade_out(1);
   set_pallete(rifle_data[PALLETE_001].dat);  
   clear_keybuf(); rectfill(screen,0,0,320,200,0);
   do
   {
      draw_sprite(sprite_buffer,rifle_data[MAIN].dat,0,0);
      animate();
   }
   while(next == 1);
   clear_keybuf();
   scope_move();
   unload_datafile(rifle_data);
   allegro_exit();
   exit(0);
}
void animate()
{
  vsync(); vsync();
  blit(sprite_buffer,screen,0,0,0,0,320,200);
  clear(sprite_buffer);
  next = 1;
  if(keypressed)
     next = 0;
}

void scope_move()
{
   int pos_x;
   int pos_y;
   int c;
   pos_x = pos_y = 30;
   c = getch();
   switch(c)
   {
      case KEY_UP:
         pos_y++;
         vsync();
         draw_rle_sprite(screen,rifle_data[SCOPE].dat,pos_x,pos_y);
         break; 
      case KEY_DOWN:
         pos_y--;
         vsync();
         draw_rle_sprite(screen,rifle_data[SCOPE].dat,pos_x,pos_y);
         break;
      case KEY_LEFT:
         pos_x--;  
         vsync();
         draw_rle_sprite(screen,rifle_data[SCOPE].dat,pos_x,pos_y);
         break;
      case KEY_RIGHT:
         pos_x++;
         vsync();
         draw_rle_sprite(screen,rifle_data[SCOPE].dat,pos_x,pos_y);
         break;
      case 'Q'|| 'q':
         fade_out(1);
         allegro_exit();
         exit(0);
         break;
      default:
         scope_move();
         break;
   }
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019