 |
Hey, do you want to make a side scrolling game, such as Super Mario, but haven't found a good tutorial about how to make it? Search no more! This tutorial covers the basic of creating a side scrolling engine. Enjoy!
The Code
#define OPTIMIZE_ROM_CALLS // Use ROM Call Optimization
#define SAVE_SCREEN // Save/Restore LCD Contents
#include // Include All Header Files
short _ti89; // Produce .89Z File
short _ti92plus; // Produce .9XZ File
// One for Dark, and one for Light gray mode..
static unsigned char ground1_dark[] = {0xFF,0x08,0x82,0x28,0x00,0x40,0x00,0x00};
static unsigned char ground1_light[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
// Oh yeah.. this one comes from JesJump ;)
static unsigned char block1_dark[] = {0x7E,0xC3,0x81,0x81,0x81,0x81,0xC3,0x7E};
static unsigned char block1_light[] = {0x7E,0xC3,0x9F,0xBF,0xBF,0xFF,0xFF,0x7E};
void Draw_Map(int map_x_location,int map_array[9][61])
{
int x=map_x_location;
int y=0;
SetPlane (LIGHT_PLANE);
ClrScr ();
SetPlane (DARK_PLANE);
ClrScr ();
// Well.. the map_x_location will keep track of where we are on the X-plane on the map array
// This way we mask out the stuff in the array that we use.. So we will only read 30x8 elements of the
// array (instead of 60x8 which it is at the moment.. ).. This might sound
unuseful, and it sort of is
// with a small array as this one.. but i'm guessing you will create worlds that are much much bigger..
// so.. to gain more speed.. we mask it out ;)
while (x<(map_x_location+30)) // Again, we can't view more than 30 blocks.. so.. don't calculate more than 30 blocks
{
if ((map_array[y][x]==1)) { // If the array at this location is equal to 1, place a ground peace
Sprite8((x-map_x_location)*8, y*8+64, 8, ground1_dark, GetPlane (DARK_PLANE), SPRT_XOR);
Sprite8((x-map_x_location)*8, y*8+64, 8, ground1_light, GetPlane (LIGHT_PLANE), SPRT_XOR);
}
if ((map_array[y][x]==2)) { // .. and if it's 2, place block1
Sprite8((x-map_x_location)*8, y*8+64, 8, block1_dark, GetPlane (DARK_PLANE), SPRT_XOR);
Sprite8((x-map_x_location)*8, y*8+64, 8, block1_light, GetPlane (LIGHT_PLANE), SPRT_XOR);
}
y++; // Now.. we're increasing the Y value.. note that i'm drawing the world from LEFT to RIGHT
// I've seen people who draw their world TOP to DOWN or otherwise, but i fint LEFT to RIGHT
// the best way to draw the world..
if (y>7) { // We've reached the limit.. restore the Y value, and move 1 block row forward..
y=0;
x++;
}
}
}
// Main Function
void _main(void)
{
// 8 blocks height.. offcause that can be increased =)
// 60 blocks wide.. (that's 2xTI92's screen size which is 240 pixels.. so 60 blocks = 480 pixels)
int map1[9][61] = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,2,2,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}};
int key=0;
int map_x_location=0;
if (!GrayOn ()) return;
Draw_Map(map_x_location,map1);
while ((key=ngetchx())!=KEY_ESC)
{
if (key==KEY_RIGHT) {
map_x_location++;
if (map_x_location>30) map_x_location=30;
Draw_Map(map_x_location,map1);
}
if (key==KEY_LEFT) {
map_x_location--;
if (map_x_location<0) map_x_location=0;
Draw_Map(map_x_location,map1);
}
}
GrayOff();
}
What's this? Read out the code
I've tried to comented as much as possible in the code, but I'll sort some stuff out anyway.
First we take a look at those lines:
// One for Dark, and one for Light gray mode..
static unsigned char ground1_dark[] = {0xFF,0x08,0x82,0x28,0x00,0x40,0x00,0x00};
static unsigned char ground1_light[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
// Oh yeah.. this one comes from jesjump ;)
static unsigned char block1_dark[] = {0x7E,0xC3,0x81,0x81,0x81,0x81,0xC3,0x7E};
static unsigned char block1_light[] = {0x7E,0xC3,0x9F,0xBF,0xBF,0xFF,0xFF,0x7E};
Those are for the sprites we will use. Note that i use one for the Dark plane, and one
for the Light plane, this way I can get the color I want. If you haven't learned how to use
sprites, there's a very good tutorial about it over at Techno-Plaza
So be sure to check that out.
If we look at the engines map reading. Take a closer look at the initialization of map1 in the main function.
Note the pattern there, all areas having a "1" inside it will place a ground1 sprite on the screen,
all "0" will be empty, and the "2" will have the block1 sprite.
If you have any comments or questions about this tutorial, please e-mail them to jon80@home.se
|
| |