Je débute en programmation C pour TI92+, mais je programmait déja en C/C++. Voici un jeu de tetris que j'ai fait :// C Source File
// Created 13/02/2003; 20:09:55
#define USE_TI89 // Compile for TI-89
#define USE_TI92PLUS // Compile for TI-92 Plus
#define USE_V200 // Compile for V200
// #define OPTIMIZE_ROM_CALLS // Use ROM Call Optimization
#define MIN_AMS 100 // Compile for AMS 1.00 or higher
#define SAVE_SCREEN // Save/Restore LCD Contents
#include <tigcclib.h> // Include All Header Files
typedef unsigned char uc;
typedef unsigned short us;
typedef unsigned long ul;
typedef unsigned long long ull;
//dimensions de l'Ècran:
#define MAXX 255
#define MAXY 127
//dimension des cases:
#define CX 8
#define CY 8
//dimension de la matrice:
#define MX 23
#define MY 16
//nombre d'ÈlÈments diffÈrents:
#define COUNTEL 19
//vitesse:
#define VIT 1500
//nombre de score retenus:
#define COUNTSC 7
//dimension des chaines de caractËres:
#define DIMNSCO 11
//dÈfinition des sprites:
static unsigned char rond[]={0x3C,0x7E,0xFF,0xFF,0xFF,0xFF,0x7E,0x3C};
static unsigned char carre[]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
unsigned char *cplein=rond;
static unsigned char cvide[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
//struct des scores:
typedef struct
{
char name[DIMNSCO];
us score;
uc mode;
us vit;
}Score;
//struct des ÈlÈments:
typedef struct
{
uc c[4][4];
}EL;
//dÈfinition des ÈlÈments:
EL el[COUNTEL];
/*
static unsigned char cplein[]={0xCC,0x00};//carrÈ
static unsigned char cplein[]={0xF0,0x00};//carrÈ
*/
//variables du jeu:
us score=0;
//matrice:
uc mat[MX][MY];
unsigned char mx=12;//MX;
unsigned short vit=3000;//VIT;
unsigned char transf=2;
Score sco[COUNTSC];
//l'ÈlÈment qui tombe:
uc cur;
uc posx=MX/2-2,posy=5;
uc nextel;
//
uc GetCode (void);
uc strequals (const char *text1,const char *text2)
{
uc i;
for (i=0;*text1 && *text2;i++)
{
if (*text1!=*text2)
return 0;
text1++;
text2++;
}
return 1;
}
ul GetNum ()
{
us c;
ul n=0;
ul p=1;
uc i;
char text[20];
char *tamp=text;
for (i=0;;i++)
{
c=ngetchx();
if (c>='0' && c<='9')
{
printf("%c",c);
*tamp=c;
tamp++;
}
if (c==KEY_ENTER)
break;
}
tamp--;
c=i;
for (i=0;i<c;i++)
{
n+=(*tamp-'0')*p;
p*=10;
tamp--;
}
return n;
}
void GetText (char *result,us dim)
{
uc i,c,ok=0;
for (i=0;i<dim-1;i++)
{
c=ngetchx();
if (c==KEY_ENTER)
break;
else
{
/*if (c==KEY_DEL)
{
if (ok)
{
}
}else
*/
{
*result=c;
printf("%c",*result);
result++;
ok++;
}
}
}
*result=0;
}
uc GetCode ()
{
ClrScr();
printf("\nCode ?\n");
char tamp[5];
GetText(tamp,5);
uc ok=strequals(tamp,"2004");
ClrScr();
return ok;
}
void NumToString (ul n,char *str)
{
uc i,j;
char *dest=str;
char *tamp=malloc(15);
if (!n)
{
*dest='0';
dest++;
}
for (i=0;n;i++)
{
*tamp=((n%10)+'0');
tamp++;
n/=10;
}
tamp--;
for (j=0;j<i;j++)
{
*dest=*tamp;
dest++;
tamp--;
}
*dest=0;
}
void DrawNum (uc x,uc y,ul n)
{
char tamp[16];
NumToString(n,tamp);
DrawStr(x,y,tamp,A_NORMAL);
}
uc getel (EL *ele,uc x,uc y)
{
if (x<mx && y<MY)
{
return ele->c[x][y];
}else
return 7;
}
uc getcase (uc x,uc y)
{
if (x<mx && y<MY)
{
return mat[x][y];
}else
return 7;
}
void DrawNext (uc n)
{
uc x,y;
DrawLine((mx+1)*CX-1,39,(mx+5)*CX+1,39,A_NORMAL);
DrawLine((mx+1)*CX-1,39,(mx+1)*CX-1,41+4*CY,A_NORMAL);
DrawLine((mx+5)*CX+1,39,(mx+5)*CX+1,41+4*CY,A_NORMAL);
DrawLine((mx+1)*CX-1,41+4*CY,(mx+5)*CX+1,41+4*CY,A_NORMAL);
for (y=0;y<4;y++)
{
for (x=0;x<4;x++)
{
uc p=GetPix((x+1+mx)*CX+1,y*CY+1+40);
if ((el[n].c[x][y] && !p) || (!el[n].c[x][y] && p))
{
Sprite8((x+mx+1)*CX, y*CY+40, 8, cplein, LCD_MEM, 0);
}
}
}
}
void DrawCase (uc x,uc y)
{
if (x<mx && y<MY)
{
uc n=GetPix(x*CX+1,y*CY+1);
if ((mat[x][y] && !n) || (!mat[x][y] && n))
{
Sprite8(x*CX, y*CY, 8, cplein, LCD_MEM, 0);
}
}
}
void DrawCur ()
{
uc x,y;
for (y=0;y<4;y++)
{
for (x=0;x<4;x++)
{
if (el[cur].c[x][y])
DrawCase(x+posx,y+posy-4);
}
}
}
void DrawScore (us score)
{
char tamp[20];
NumToString(score,tamp);
uc i;
for (i=0;i<3;i++)
{
if (CX*mx+3+(i+1)*8<MAXX)
{
Sprite8(CX*mx+3+i*8,12,8, cvide, LCD_MEM, SPRT_AND);
Sprite8(CX*mx+3+i*8,20,8, cvide, LCD_MEM, SPRT_AND);
}
}
DrawStr(CX*mx+3,12,tamp,A_NORMAL);
}
void DrawGame ()
{
DrawLine(CX*mx,0,CX*mx,MAXY,A_NORMAL);
DrawStr(CX*mx+3,0,"Score:",A_NORMAL);
DrawScore(score);
DrawStr(CX*mx+3,26,"next:",A_NORMAL);
DrawNext(nextel);
}
void DrawMat ()
{
uc x,y;
for (x=0;x<mx;x++)
{
for (y=0;y<MY;y++)
{
DrawCase(x,y);
}
}
}
uc getnextel ()
{
return random(COUNTEL);
}
void init ()
{
uc i,j,k;
posy=0;
posx=mx/2-2;
score=0;
//initialize le random:
randomize();
//
cur=getnextel();
//initialize la matrice:
for (i=0;i<MX;i++)
{
for (j=0;j<MY;j++)
{
mat[i][j]=0;
}
}
//initialize les ÈlÈments:
for (k=0;k<COUNTEL;k++)
{
for (j=0;j<4;j++)
{
for (i=0;i<4;i++)
{
el[k].c[i][j]=0;
}
}
}
//carrÈ:
el[0].c[0][0]=1;
el[0].c[1][0]=1;
el[0].c[0][1]=1;
el[0].c[1][1]=1;
//barre horiz:
el[1].c[0][0]=1;
el[1].c[1][0]=1;
el[1].c[2][0]=1;
el[1].c[3][0]=1;
//barre verti:
el[2].c[0][0]=1;
el[2].c[0][1]=1;
el[2].c[0][2]=1;
el[2].c[0][3]=1;
//__|
el[3].c[0][1]=1;
el[3].c[1][1]=1;
el[3].c[2][1]=1;
el[3].c[2][0]=1;
//--|
el[4].c[0][0]=1;
el[4].c[1][0]=1;
el[4].c[2][0]=1;
el[4].c[2][1]=1;
//|__
el[5].c[0][0]=1;
el[5].c[0][1]=1;
el[5].c[1][1]=1;
el[5].c[2][1]=1;
//|--
el[6].c[0][0]=1;
el[6].c[1][0]=1;
el[6].c[2][0]=1;
el[6].c[0][1]=1;
//2*|_
el[7].c[0][0]=1;
el[7].c[0][1]=1;
el[7].c[0][2]=1;
el[7].c[1][2]=1;
//2*|-
el[8].c[0][0]=1;
el[8].c[0][1]=1;
el[8].c[0][2]=1;
el[8].c[1][0]=1;
//_|*2
el[9].c[1][0]=1;
el[9].c[1][1]=1;
el[9].c[1][2]=1;
el[9].c[0][2]=1;
//-|*2
el[10].c[0][0]=1;
el[10].c[1][0]=1;
el[10].c[1][1]=1;
el[10].c[1][2]=1;
//|-
el[11].c[0][0]=1;
el[11].c[0][1]=1;
el[11].c[1][1]=1;
el[11].c[0][2]=1;
//-|
el[12].c[1][0]=1;
el[12].c[0][1]=1;
el[12].c[1][1]=1;
el[12].c[1][2]=1;
//|/-/|
el[13].c[0][0]=1;
el[13].c[0][1]=1;
el[13].c[1][1]=1;
el[13].c[1][2]=1;
//|\-\|
el[14].c[1][0]=1;
el[14].c[1][1]=1;
el[14].c[0][1]=1;
el[14].c[0][2]=1;
//_|_
el[15].c[1][0]=1;
el[15].c[0][1]=1;
el[15].c[1][1]=1;
el[15].c[2][1]=1;
//-|-
el[16].c[0][0]=1;
el[16].c[1][0]=1;
el[16].c[2][0]=1;
el[16].c[1][1]=1;
//_|-
el[17].c[1][0]=1;
el[17].c[2][0]=1;
el[17].c[0][1]=1;
el[17].c[1][1]=1;
//-|_
el[18].c[0][0]=1;
el[18].c[1][0]=1;
el[18].c[1][1]=1;
el[18].c[2][1]=1;
nextel=getnextel();
}
uc getnextelmt (uc c)
{//donne l'ÈlÈment tournÈ
uc r=0;
switch (c)
{
case 0:
//carrÈ
r=0;
break;
case 1:
//barre hori.
r=2;
break;
case 2:
//barre verti.
r=1;
break;
case 3:
//__|
r=7;//2*|_
break;
case 4:
//--|
r=9;//_|*2
break;
case 5:
//|__
r=8;//2*|-
break;
case 6:
//|--
r=10;//-|*2
break;
case 7:
//2*|_
r=6;//|--
break;
case 8:
//2*|-
r=4;//--|
break;
case 9:
//_|*2
r=5;//|__
break;
case 10:
//-|*2
r=3;//__|
break;
case 11:
//|-
r=16;//-|-
break;
case 12:
//-|
r=15;//_|_
break;
case 13:
//|/-/|
r=17;//_|-
break;
case 14:
//|\-\|
r=18;//-|_
break;
case 15:
//_|_
r=11;//|-
break;
case 16:
//-|-
r=12;//-|
break;
case 17:
//_|-
r=13;//|/-/|
break;
case 18:
//-|_
r=14;//|\-\|
break;
}
return r;
}
void pause ()
{
uc i;
for (i=0;i<30;i++)
{
if ((i+1)*8<MAXX)
{
Sprite8(i*8,CY*(MY-1),8, cvide, LCD_MEM, SPRT_AND);
Sprite8(i*8,CY*(MY-2),8, cvide, LCD_MEM, SPRT_AND);
}
}
DrawStr(0,115,"(appuie sur une touche)",A_NORMAL);
while (!kbhit());
ngetchx();
for (i=0;i<30;i++)
{
if ((i+1)*8<MAXX)
{
Sprite8(i*8,CY*(MY-1),8, cvide, LCD_MEM, SPRT_AND);
Sprite8(i*8,CY*(MY-2),8, cvide, LCD_MEM, SPRT_AND);
}
}
DrawLine(CX*mx,MAXY-16-1,CX*mx,MAXY,A_NORMAL);
}
void afterpause ()
{
uc i;
for (i=0;i<mx;i++)
{
DrawCase(i,MY-1);
DrawCase(i,MY-2);
}
}
void DelCur ()
{
uc x,y;
for (y=0;y<4;y++)
{
for (x=0;x<4;x++)
{
if (el[cur].c[x][y])
{
if (x+posx<mx && (uc)(y+posy-4)<MY)
mat[x+posx][y+posy-4]=0;
}
}
}
}
void PutCur ()
{
uc x,y;
for (y=0;y<4;y++)
{
for (x=0;x<4;x++)
{
if (el[cur].c[x][y])
{
if (x+posx<mx && (uc)(y+posy-4)<MY)
mat[x+posx][y+posy-4]=1;
}
}
}
}
void DelLigne (uc n)
{
uc x,y;
if (n>=MY)
return;
for (y=n;y>0;y--)
{
for (x=0;x<mx;x++)
{
mat[x][y]=mat[x][y-1];
DrawCase(x,y);
}
}
}
void Podium ()
{
uc i,j,pos=0,ok=0;
for (i=0;i<COUNTSC;i++)
{
if (score>=sco[i].score)
{
ok=1;
pos=i;
for (j=COUNTSC-1;j>pos;j--)
{
sco[j]=sco[j-1];
}
break;
}
}
if (ok)
{
uc y;
ClrScr();
printf("\n FÈlicitation !!!\n");
printf("Tu es parmi\nles %d meilleurs scores !\n",COUNTSC);
printf("\nEntre ton nom:\n(%d lettres)\n",DIMNSCO);
GetText(sco[pos].name,DIMNSCO);
sco[pos].score=score;
sco[pos].mode=transf;
sco[pos].vit=vit;
ClrScr();
//affiche les scores:
DrawStr(0,0,"Noms",A_NORMAL);
DrawLine(80,0,80,MAXY,A_NORMAL);
DrawStr(82,0,"Scores",A_NORMAL);
DrawLine(137,0,137,MAXY,A_NORMAL);
DrawStr(142,0,"modes",A_NORMAL);
DrawLine(190,0,190,MAXY,A_NORMAL);
DrawStr(195,0,"vit.",A_NORMAL);
for (i=0;i<COUNTSC;i++)
{
y=(i+1)*15;
if (*sco[i].name || sco[i].score)
{
DrawStr(0,y,sco[i].name,A_NORMAL);
DrawNum(85,y,sco[i].score);
switch (sco[i].mode)
{
case 0:
DrawStr(140,y,"fixe",A_NORMAL);
break;
case 1:
DrawStr(140,y,"change",A_NORMAL);
break;
case 2:
DrawStr(140,y,"tourne",A_NORMAL);
break;
default:
break;
}
DrawNum(195,y,sco[i].vit);
}
}
pause();
}
}
void Loop ()
{
us count=0;
uc x,y,i,j,gx,gy,ok,perdu=0,nokey=0;
for (;;)
{
if (nokey)
{
nokey--;
count+=vit;
}
count++;
if (kbhit() && !nokey)
{
us c=ngetchx();
count+=500;
if (c==KEY_ENTER && transf)
{
//test si le prochain ÈlÈment peut Ítre placÈ:
ok=1;
if (transf==1)//mode change
{
i=cur+1;
}else
{//mode tourne
i=getnextelmt(cur);
}
gx=0;//dÈcal de 0 par dÈfaut
if (i>=COUNTEL)
i=0;
uc x2;
for (x2=0;x2<4;x2++)
{
x=3-x2;
for (y=0;y<4;y++)
{
if (el[i].c[x][y] && !el[cur].c[x][y])
{
j=getcase(x+posx,y+posy-4);
if (j)
{
if (j==1)
{
ok=0;
break;
}else
{//si l'ÈlÈment sort de l'Ècran, on dÈcal
if (posx+x>=mx)
gx=mx-1-x;
if (posy+y-4>=MY)
ok=0;
//gx est la nouvelle position (valeur de posx)
break;
}
}
}
}
if (!ok)
break;
}
//ok=1;
if (ok)
{
DelCur();
DrawCur();
cur=i;
if (gx)
{
posx=gx;
}
PutCur();
DrawCur();
}
}
if (c==KEY_ESC)
{
ClrScr();
printf("\nEst-tu s?r de vouloir quitter ?\n");
printf("0 : non , annuler\n");
printf("1 : oui\n");
if ((char)ngetchx()=='1')
break;
else
{
ClrScr();
DrawGame();
DrawMat();
}
}
if (c==' ')
{
pause();
afterpause();
}
if (c=='e')
{
char ttamp[20];
ClrScr();
printf("\nQue veux-tu tu veux Èditer:\n");
printf("0 : rien , annuler\n");
printf("1 : largeur de la zone de jeu\n");
printf("2 : rapiditÈ\n");
printf("3 : recommencer une partie\n");
printf("4 : mode fixe/change/tourne\n");
printf("5 : motif\n");
uc n=ngetchx();
switch (n)
{
case '1':
printf("Largeur? (en cases,de 4 ‡ %d)\n",MX);
printf("Elle Ètait de %d\n",mx);
mx=GetNum();
printf("\n");
if (mx>MX || mx<4)
{
printf("Tu as entrÈ %d\n",mx);
mx=MX;
printf("Valeur incorrecte,\nje prend %d.\n",mx);
pause();
}
init();
break;
case '2':
printf("Vitesse? (de 20 ‡ 10000)\n");
printf("(+ c'est grand + c'est lent)\n");
printf("elle Ètait ‡ %d\n",vit);
vit=GetNum();
if (vit<20 || vit>10000)
{
printf("Tu as entrÈ %d\n",vit);
vit=VIT;
printf("Valeur incorrecte,\nje prend %d.\n",vit);
pause();
}
init();
break;
case '3':
init();
break;
case '4':
printf("Si tu veux pouvoir modifier\nun ÈlÈment quand il est\nen train de tomber : 1\nsinon :0\n");
printf("mode tourne : 2\nmode change : 1\nmode fixe: 0\n");
transf=(uc)(ngetchx()-'0');
init();
break;
case '5':
printf("ronds : 0\n");
printf("carrÈs : 1\n");
if (ngetchx()=='0')
{
cplein=rond;
}else
{
cplein=carre;
}
break;
case '9':
printf("Code ?\n");
GetText(ttamp,20);
if (strequals(ttamp,"ptacrem"))
{
printf("Score?\n");
score=(us)GetNum();
}
break;
case 's':
printf("Code ?\n");
GetText(ttamp,20);
if (strequals(ttamp,"ptacrem"))
{
printf("numero du score a modifier?\n");
i=(us)GetNum();
if (i<COUNTSC)
{//modifie le nom
printf("\nEntre ton nom:\n(%d lettres)\n",DIMNSCO);
GetText(sco[i].name,DIMNSCO);
}else
{//efface un score
printf("numero du score a modifier?\n");
i=(us)GetNum();
if (i<COUNTSC)
{
for (j=i;j<COUNTSC-1;j++)
{
sco[j]=sco[j+1];
}
*sco[COUNTSC-1].name=0;
sco[COUNTSC-1].score=0;
}
}
}
break;
case 'r':
case 'R':
//remet les scores ‡ 0
for (i=0;i<COUNTSC;i++)
{
sco[i].score=0;
for (j=0;j<DIMNSCO;j++)
{
sco[i].name[j]=0;
}
sco[i].vit=0;
sco[i].mode=0;
}
break;
default:
break;
}
ClrScr();
DrawGame();
DrawMat();
}
if (c==KEY_DOWN)
{
count+=vit;
nokey=2;
}
if (c==KEY_LEFT)
{
ok=1;
for (y=0;y<4;y++)
{
for (x=0;x<4;x++)
{
if (el[cur].c[x][y])
{
if (getcase(x+posx-1,y+posy-4) || !posx)
{
ok=0;
}
break;
}
}
if (!ok)
break;
}
if (ok)
{
DelCur();
DrawCur();
posx--;
PutCur();
DrawCur();
}
}
if (c==KEY_RIGHT)
{
ok=1;
for (y=0;y<4;y++)
{
for (i=0;i<4;i++)
{
x=3-i;
if (el[cur].c[x][y])
{
if (getcase(x+posx+1,y+posy-4) || posx+x+1>mx)
{
ok=0;
}
break;
}
}
if (!ok)
break;
}
if (ok)
{
DelCur();
DrawCur();
posx++;
PutCur();
DrawCur();
}
}
}
if (count>vit)
{
count=0;
ok=1;
for (x=0;x<4;x++)
{
for (j=0;j<4;j++)
{
y=3-j;
gx=posx+x;
gy=posy+y-4;
if (el[cur].c[x][y])
{
if (getcase(gx,gy+1))
{
if (posy>4)
{
ok=0;
}else
{
if (gx<mx && gy+1<MY)
{
perdu=1;
ok=0;
}
}
}
break;
}
}
if (!ok)
break;
}
if (ok)
{
DelCur();
DrawCur();
posy++;
PutCur();
DrawCur();
}else
{
if (perdu)
{
ClrScr();
for (i=0;i<7;i++)
DrawStr((uc)random(mx-50),i*12,"Perdu !",A_NORMAL);
DrawStr(30,8*12,"Ton score est :",A_NORMAL);
DrawNum(155,8*12,score);
pause();
Podium();
break;
}else
{
uc countl=0;
cur=nextel;
nextel=getnextel();
DrawNext(nextel);
ok=0;
for (y=3;y>0;y--)
{
for (x=0;x<4;x++)
{
if (el[cur].c[x][y])
{
ok=y;
break;
}
}
if (ok)
break;
}
posy=3-ok;
posx=mx/2-2;
//cherche les lignes rÈalisÈes:
for (y=0;y<MY;y++)
{
ok=1;
for (x=0;x<mx;x++)
{
if (!mat[x][y])
{
ok=0;
break;
}
}
if (ok)
{
DelLigne(y);
countl++;
}
}
score+=(countl*countl*10);
DrawScore(score);
}
}
}
}
}
// Main Function
void _main(void)
{
(pour TI92, TI89,...). Comment peut-on faire pour obtenir un pointeur sur l'écran, c'est-à-dire l'adresse du pixel haut-gauche? Cela me permettrai de faire des effets graphiques.
