I am writing a game for the TI-89 in C, but I have some problems…
/*
Starvak
By blue_bear_94
I worked my - off this, so don't you
take this as your own!
(That doesn't mean you have to press the big
red X, though. Feel free to read the source.)
*/
// Delete or comment out the items you do not need.
#define COMMENT_STRING "Place your comment here."
#define COMMENT_PROGRAM_NAME "Starvak 89, in C"
#define COMMENT_VERSION_STRING "0.0"
#define COMMENT_VERSION_NUMBER 0,0,0,0 /* major, minor, revision, subrevision */
#define COMMENT_BW_ICON \
{0b1111111111111111, \
0b0100000000000000, \
0b0010000000000000, \
0b0001000000000000, \
0b0000100000000000, \
0b0000010000000000, \
0b0000001000000000, \
0b0000000100000000, \
0b0000001000000000, \
0b0000010000000000, \
0b0000100000000000, \
0b0001000000000000, \
0b0010000000000000, \
0b0100000000000000, \
0b1111111111111111, \
0b0000000000000000}
#define COMMENT_GRAY_ICON \
{0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000}, \
{0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000, \
0b0000000000000000}
#include <tigcclib.h>
/* unsigned char i;
for(i=number+1;i<strlen(ex)+1;i++)
{
ex[i]=ex[i+1];
ey[i]=ey[i+1];
}
ex=realloc(ex,strlen(ex));
if (ex==NULL)
{
DlgMessage(merde,msg,BT_NONE,BT_CANCEL);
free(ex);
free(ey);
goto exit;
}
ey=realloc(ey,strlen(ey));
if (ey==NULL)
{
DlgMessage(merde,msg,BT_NONE,BT_CANCEL);
free(ex);
free(ey);
goto exit;
}
int k=strlen(ex)+2;
ex=realloc(ex,k);
if (ex==NULL)
{
DlgMessage(merde,msg,BT_NONE,BT_CANCEL);
free(ex);
free(ey);
goto exit;
}
ey=realloc(ey,k);
if (ey==NULL)
{
DlgMessage(merde,msg,BT_NONE,BT_CANCEL);
free(ex);
free(ey);
return;
}
ex[k]=x;
ey[k]=y;
*/
void drawEnemy(unsigned char x,unsigned char y)
{
/*Draws an enemy at the point (x,y).
Arrangement of pixels (X represents the point):
1 1
X
1 1
*/
DrawPix(x,y,A_XOR);
DrawPix(x-1,y-1,A_XOR);
DrawPix(x-1,y+1,A_XOR);
DrawPix(x+1,y-1,A_XOR);
DrawPix(x+1,y+1,A_XOR);
}
void drawShip(unsigned char x,unsigned char y)
{
/*Draws a ship at the point (x,y).
X is the point.
1
11
1 X
11
1
*/
DrawLine(x-2,y-2,x,y+2,A_XOR);
DrawPix(x-1,y-1,A_XOR);
DrawPix(x-1,y+1,A_XOR);
DrawPix(x,y,A_XOR);
}
inline char SMenu(void)
{
//Draws the main menu.
int choice=0;
ClrScr();
char array1[]={62,62,59,62};
char array2[]={92,92,95,92};
//0=play,1=help,2=about,3=quit
FontSetSys(F_8x10);
DrawStr(52,10,"Žtarvak",A_NORMAL);
FontSetSys(F_6x8);
DrawStr(62,24,">Play<",A_NORMAL);
DrawStr(68,32,"Help",A_NORMAL);
DrawStr(65,40,"About",A_NORMAL);
DrawStr(68,48,"Quit",A_NORMAL);
//wait for user to pick a choice
int key;
while ((key=ngetchx())!=KEY_ENTER)
{
DrawStr(array1[choice],24+8*choice," ",A_REPLACE);
DrawStr(array2[choice],24+8*choice," ",A_REPLACE);
if (key==KEY_UP && choice!=0) choice--;
if (key==KEY_DOWN && choice!=3) choice++;
DrawStr(array1[choice],24+8*choice,">",A_NORMAL);
DrawStr(array2[choice],24+8*choice,"<",A_NORMAL);
}
return (char) choice;
}
// Main Function
void _main(void)
{
//initialize vars
const char* merde="Oh shoot.";
const char* msg="Not enough memory";
const char* stv="Starvak";
unsigned char* ex=NULL;
unsigned char* ey=NULL;
int choice=0;
randomize();
char array1[]={62,62,59,62};
char array2[]={92,92,95,92};
while (choice!=3)
{
if ((ex=(unsigned char*) realloc(ex,1))==NULL)
{
DlgMessage(merde,msg,BT_NONE,BT_CANCEL);
return;
}
if ((ey=(unsigned char*) realloc(ey,1))==NULL)
{
DlgMessage(merde,msg,BT_NONE,BT_CANCEL);
free(ex);
return;
}
unsigned char health=100,shipx=50,shipy=5;
int score=0;
//bug starts here...
//Draws the main menu.
ClrScr();
//0=play,1=help,2=about,3=quit
FontSetSys(F_8x10);
DrawStr(52,10,"Žtarvak",A_NORMAL);
FontSetSys(F_6x8);
DrawStr(62,24,">Play<",A_NORMAL);
DrawStr(68,32,"Help",A_NORMAL);
DrawStr(65,40,"About",A_NORMAL);
DrawStr(68,48,"Quit",A_NORMAL);
//wait for user to pick a choice
int key;
while ((key=ngetchx())!=KEY_ENTER)
{
DrawStr(array1[choice],24+8*choice," ",A_REPLACE);
DrawStr(array2[choice],24+8*choice," ",A_REPLACE);
if (key==KEY_UP && choice!=0) choice--;
if (key==KEY_DOWN && choice!=3) choice++;
DrawStr(array1[choice],24+8*choice,">",A_NORMAL);
DrawStr(array2[choice],24+8*choice,"<",A_NORMAL);
}
//debugging
clrscr();
printf("%d \n",choice);
ngetchx();
if (choice==0)
{
//first enemy
ex[0]=158;
ey[0]=2+random(96);
//draw it
drawEnemy(ex[0],ey[0]);
//now draw the ship
drawShip(50,5);
//draw score
FontSetSys(F_4x6);
char* buff;
sprintf(buff,"Score: %d Health: %d",score,health);
DrawStr(0,0,buff,A_XOR);
/*
The rules are:
You can move up, down, left or right.
2<=y<=98
2<=x<=12
Enemies that pass through the left boundary
cost you 5 health each.
Enemies that collide with you cost you 10 health each.
Shoot with (key).
Enemies that get hit by the beam earn you
1 point each.
*/
int key;
while (health>0)
{
//get the key (1=up,2=left,4=down,8=right,16=2nd)
key=_rowread(126);
//move ship
drawShip(shipx,shipy);
if ((key&2) && shipx!=2) shipx--;
if ((key&8) && shipx!=12) shipx++;
if ((key&1) && shipy!=2) shipy--;
if ((key&4) && shipy!=98) shipy++;
drawShip(shipx,shipy);
//draw beam
if (key&16)
{
//ZAP!
DrawLine(shipx,shipy,shipx,159,A_XOR);
DrawLine(shipx,shipy,shipx,159,A_XOR);
}
//move enemies
unsigned char i;
for(i=0;i<strlen(ex)+1;i++)
{
drawEnemy(ex[i],ey[i]);
ex[i]--;
drawEnemy(ex[i],ey[i]);
//enemies passing bounds
if (ex[i]==1)
{
//erase
drawEnemy(ex[i],ey[i]);
//take health
health-=5;
//delete enemy
unsigned char j;
for(j=i+1;j<strlen(ex)+1;j++)
{
ex[j]=ex[j+1];
ey[j]=ey[j+1];
}
ex=realloc(ex,strlen(ex));
if (ex==NULL)
{
DlgMessage(merde,msg,BT_NONE,BT_CANCEL);
free(ex);
free(ey);
return;
}
ey=realloc(ey,strlen(ey));
if (ey==NULL)
{
DlgMessage(merde,msg,BT_NONE,BT_CANCEL);
free(ex);
free(ey);
return;
}
}
//collision
if (abs(shipy-ey[i])<=3 && abs(shipx-ex[i])<=1)
{
//erase
drawEnemy(ex[i],ey[i]);
//take health
health-=10;
//delete enemy
unsigned char j;
for(j=i+1;j<strlen(ex)+1;j++)
{
ex[j]=ex[j+1];
ey[j]=ey[j+1];
}
ex=realloc(ex,strlen(ex));
if (ex==NULL)
{
DlgMessage(merde,msg,BT_NONE,BT_CANCEL);
free(ex);
free(ey);
return;
}
ey=realloc(ey,strlen(ey));
if (ey==NULL)
{
DlgMessage(merde,msg,BT_NONE,BT_CANCEL);
free(ex);
free(ey);
return;
}
}
//hit by beam
if (abs(ey[i]-shipy)<=1 && (key&16))
{
//erase
drawEnemy(ex[i],ey[i]);
//give point
score++;
//delete enemy
unsigned char j;
for(j=i+1;j<strlen(ex)+1;j++)
{
ex[j]=ex[j+1];
ey[j]=ey[j+1];
}
ex=realloc(ex,strlen(ex));
if (ex==NULL)
{
DlgMessage(merde,msg,BT_NONE,BT_CANCEL);
free(ex);
free(ey);
return;
}
ey=realloc(ey,strlen(ey));
if (ey==NULL)
{
DlgMessage(merde,msg,BT_NONE,BT_CANCEL);
free(ex);
free(ey);
return;
}
}
}
//now new enemies!
if (random(1))
{
unsigned char x=158;
unsigned char y=2+random(96);
int k=strlen(ex)+2;
ex=realloc(ex,k);
if (ex==NULL)
{
DlgMessage(merde,msg,BT_NONE,BT_CANCEL);
free(ex);
free(ey);
return;
}
ey=realloc(ey,k);
if (ey==NULL)
{
DlgMessage(merde,msg,BT_NONE,BT_CANCEL);
free(ex);
free(ey);
return;
}
ex[k]=x;
ey[k]=y;
}
}
ClrScr();
FontSetSys(F_8x10);
DrawStr(10,10,"GAME OVER",A_NORMAL);
FontSetSys(F_6x8);
sprintf(buff,"Score: %d",score);
DrawStr(10,24,buff,A_NORMAL);
break;
}
}
//We hate memory leaks, so we do this.
free(ex);
free(ey);
}When I compile and run the program, I get a menu but I can't play the game. When I get to the buggy code, the program acts as if it were:
while (choice!=3)
{
//menu, w/o game
}I don't know what I'm doing wrong. Could anyone please help me?
Sunrise 3 Progress: 30%
Size: around 20 KB, not including the save lists and in-game RAM.