Remake de :
http://www.cppfrance.com/code.aspx?ID=22245
Cans le cadre d'un cours de correction de programme, j'ai choisi cette petite application en DOS et j'ai simplement...simplifié le code.
Source / Exemple :
/******************************************
- Programmed by Squallgamer
- Modified by Neterim 10-11-2010
- ZELDA
-
#include <iostream>
#include <math.h>
#include <windows.h>
#include <ctime>
#include <conio.h>
using namespace std;
char tabl[20][20]; //Tableau de jeu
int vie=1;
int heroX;
int heroY;
int heroStartPositionX;
int heroStartPositionY;
int levelEndX;
int levelEndY;
char* levelMessage;
bool attacked = false; //Dit si le personnage a attaqué dans sa dernière action
void clearLevel(); //Vide le tableau de jeu
int niveauCourant = 1;
bool levelStarted = false;
int niveau1();
int niveau2();
int niveau3();
int keyHandler(); //Fonction gérant les commandes
int attackHandler(int x, int y); //Fonction gérant l'attaque
void showWeapon(int direction); //Fonction affichant l'arme : 0 - droite | 1 - bas | 2 - gauche | 3 - haut
int heroCollider(); // Fonction gérant le collision du héros avec les ennemies et les objets
int moveHero(int direction); //Fonction s'occupant de gérer le mouvement du hero
void moveEnemies(); //Gère les ennemies
////////////////////////////////////////////////////////////////////
int main() //Fonction principale
{
while(niveauCourant != -1)
{
// Gestion des niveaux
clearLevel();
switch(niveauCourant)
{
case 1: niveau1();
break;
case 2: niveau2();
break;
case 3: niveau3();
break;
default: niveauCourant = -1;
break;
}
if(niveauCourant == -1)
{
break;
}
//Fin de la gestion des niveaux
//Gestion du jeu
for(;;)
{
if(heroX == levelEndX && heroY == levelEndY)
{
niveauCourant++;
break;
}
if(levelStarted)
{
system("cls");
}
else
{
cout << levelMessage;
levelStarted = true;
}
for(int y=0;y<20;y++) //Création du tableau (La carte du jeu)
{
for(int x=0;x<20;x++)
{
if((tabl[x][y] == '|' || tabl[x][y] == '-') && !attacked)
tabl[x][y] = ' ';
cout << tabl[x][y];
}
cout << endl;
}
cout << "\n\nNombre de vies : " << vie << endl;
attacked = false;
switch (keyHandler())
{
case 0 : break;
case 1 : vie--;
return main();
break;
case -1: niveauCourant = 1;
vie = 1;
return main();
break;
}
}
//Fin de la gestion du jeu
}
return 0; //Quitte le programme
}
////////////////////////////////////////////////////////////////////
void clearLevel()
{
system("cls");
for(int x = 0; x < 20; x++)
{
for(int y = 0; y < 20; y++)
{
tabl[x][y] = ' ';
}
}
}
int niveau1()
{
int static r=0;
if(r==0)
{
levelMessage = "Bienvenue dans le jeu ZELDA niveau1!!!\nLe but est de tuer le petit nuckers qui a envahi la foret magique et emprisoner ton amie.\nPour cela bouger le \02 pour cela utiliser les touches 2,6,4,8.\n";r++;
}
heroY = 2;
heroX = 1;
heroStartPositionX = 1;
heroStartPositionY = 2;
levelEndX = 19;
levelEndY = 18;
tabl[18][18]='\01'; // \01 est un nuckers
tabl[17][16]='\03'; // \03 est une vie
tabl[heroX][heroY]='\02'; // \02 est le héros
for(int y=0;y<20;y++) //Création du tableau (La carte du jeu)
{
for(int x=0;x<20;x++)
{
if(x==0)
tabl[x][y]='\06'; //Création du décor (Les arbres qui ne ressemble pas à des arbres)
if(y==0)
tabl[x][y]='\06';
if(x==19&&y!=18)
tabl[x][y]='\06';
if(y==19)
tabl[x][y]='\06';
if(y==13&&x!=7&&x!=8&&x!=17)
tabl[x][y]='\06';
if(y==17&&x!=5)
tabl[x][y]='\06';
if(x==15&&y!=5&&y!=18)
tabl[x][y]='\06';
}
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
int niveau2() //Pour le niveau 2 c'est le même principe
{
int static r=0;
if(r==0)
{
levelMessage = "Bienvenue dans le jeu ZELDA niveau2!!!\nVous arrivez plus profond dans la foret il faut sauver votre compagnon\01 et detruire les derniers petits \02\n";r++;
}
heroY=2;
heroX=1;
heroStartPositionY = 2;
heroStartPositionX = 1;
levelEndX = 18;
levelEndY = 18;
tabl[18][18]='\02';
tabl[17][16]='\01';
tabl[14][16]='\01';
tabl[6][16]='\01';
tabl[heroX][heroY]='\02';
for(int y=0;y<20;y++)
{
for(int x=0;x<20;x++)
{
if(x==0)
tabl[x][y]='\06';
if(y==0)
tabl[x][y]='\06';
if(x==19)
tabl[x][y]='\06';
if(y==19)
tabl[x][y]='\06';
if(y==15&&x!=7)
tabl[x][y]='\06';
if(y==17&&x!=15)
tabl[x][y]='\06';
if(x==10&&y!=4&&y!=18&&y!=16&&y!=12&&y!=9)
tabl[x][y]='\06';
if(x==3&&y!=2)
tabl[x][y]='\06';
if(y==10&&x!=16)
tabl[x][y]='\06';
if(x==1&&y!=2&&y!=1&&y!=3)
tabl[x][y]='\06';
if(x==2&&y!=2&&y!=1&&y!=3)
tabl[x][y]='\06';
if(y==5&&x!=17)
tabl[x][y]='\06';
}
}
return 0;
}
int niveau3()
{
int static r=0;
if(r==0)
{
levelMessage = "Bienvenue dans le jeu ZELDA niveau3!!!\n";
}
heroY=18;
heroX=1;
heroStartPositionY = 18;
heroStartPositionX = 1;
levelEndX = 18;
levelEndY = 1;
tabl[18][1]='O';
tabl[1][1]='\01';
tabl[1][4]='\01';
tabl[1][8]='\01';
tabl[2][13]='\01';
tabl[1][16]='\01';
tabl[2][9]='\01';
tabl[3][18]='\01';
tabl[3][1]='\01';
tabl[14][16]='\01';
tabl[6][16]='\01';
tabl[heroX][heroY]='\02';
for(int y=0;y<20;y++)
{
for(int x=0;x<20;x++)
{
if(x==0)
tabl[x][y]='\06';
if(y==0)
tabl[x][y]='\06';
if(x==19)
tabl[x][y]='\06';
if(y==19)
tabl[x][y]='\06';
if(y==4&&x!=0&&x!=1)
tabl[x][y]='\06';
if(y==12&&x!=18&&x!=17)
tabl[x][y]='\06';
}
}
return 0;
}
void moveEnemies()
{
for(int x = 0; x < 20; x++)
{
for(int y = 0; y < 20; y++)
{
if(tabl[x][y] == '\01')
{
int direction = (double)rand() / RAND_MAX *4;
switch(direction)
{
case 0: if(y+1 < 20 && tabl[x][y+1] != '\06' && tabl[x][y+1] != '\01' && tabl[x][y+1] != 'm')
{
tabl[x][y] = ' ';
tabl[x][y+1] = 'm';
}
break;
case 1: if(x+1 < 20 && tabl[x+1][y] != '\06' && tabl[x+1][y] != '\01' && tabl[x+1][y] != 'm')
{
tabl[x][y] = ' ';
tabl[x+1][y] = 'm';
}
break;
case 2: if(y-1 >= 0 && tabl[x][y-1] != '\06' && tabl[x][y-1] != '\01' && tabl[x][y-1] != 'm')
{
tabl[x][y] = ' ';
tabl[x][y-1] = 'm';
}
break;
case 3: if(x-1 >= 0 && tabl[x-1][y] != '\06' && tabl[x-1][y] != '\01' && tabl[x-1][y] != 'm')
{
tabl[x][y] = ' ';
tabl[x-1][y] = 'm';
}
break;
}
}
}
}
for(int x = 0; x < 20; x++)
{
for(int y = 0; y < 20; y++)
{
if(tabl[x][y] == 'm')
{
tabl[x][y] = '\01';
}
}
}
}
int attackHandler(int x, int y)
{
if(tabl[x][y] == '\01')
{
tabl[x][y] == NULL;
return 1;
}
return 0;
}
void showWeapon(int direction)
{
switch (direction)
{
case 0: if(tabl[heroX+1][heroY] != '\06')
tabl[heroX+1][heroY]='-';
if(tabl[heroX+1][heroY]=='-' && tabl[heroX+2][heroY] != '\06')
{
tabl[heroX+2][heroY]='-';
}
break;
case 1: if(tabl[heroX][heroY+1] != '\06')
tabl[heroX][heroY+1]='|';
if(tabl[heroX][heroY+1]=='|' && tabl[heroX][heroY+2] != '\06')
{
tabl[heroX][heroY+2]='|';
}
break;
case 2: if(tabl[heroX-1][heroY] != '\06')
tabl[heroX-1][heroY]='-';
if(tabl[heroX-1][heroY]=='-' && tabl[heroX-2][heroY] != '\06')
{
tabl[heroX-2][heroY]='-';
}
break;
case 3: if(tabl[heroX][heroY-1] != '\06')
tabl[heroX][heroY-1]='|';
if(tabl[heroX][heroY-1]=='|' && tabl[heroX][heroY-2] != '\06')
{
tabl[heroX][heroY-2]='|';
}
break;
}
attacked = true;
}
int heroCollider()
{
if(tabl[heroX][heroY] == '\01')
{
return 1;
}
else
{
if(tabl[heroX][heroY] == '\03')
{
return 2;
}
}
return 0;
}
int moveHero(int direction)
{
bool moved = false;
switch (direction)
{
case 0: if(tabl[heroX+1][heroY] != '\06')
{
tabl[heroX][heroY]=NULL;
heroX++;
moved = true;
}
break;
case 1: if(tabl[heroX][heroY+1] != '\06')
{
tabl[heroX][heroY]=NULL;
heroY++;
moved = true;
}
break;
case 2: if(tabl[heroX-1][heroY] != '\06')
{
tabl[heroX][heroY]=NULL;
heroX--;
moved = true;
}
break;
case 3: if(tabl[heroX][heroY-1] != '\06')
{
tabl[heroX][heroY]=NULL;
heroY--;
moved = true;
}
break;
}
return moved;
}
int keyHandler()
{
unsigned int g = getch(); //Getch() permet de taper sur une touche sans devoir appuyé sur Entrée
if(g==54) //Si on appuie sur 6 le héros sort son épée à droite
{
for(int t=0;t<1;t++)
{
showWeapon(0);
if(attackHandler(1+heroX, heroY) == 1 || attackHandler(2+heroX, heroY) == 1)
{
cout<<"Bien jouez!!!\n";Sleep(1000);
}
}
}
if(g==56) //Si on appuie sur 8 le héros sort son épée en haut
{
for(int t=0;t<1;t++)
{
showWeapon(3);
if(attackHandler(heroX, heroY-1) == 1 || attackHandler(heroX, heroY-2) == 1)
{
cout<<"Bien jouez!!!\n";Sleep(1000);
}
}
}
if(g==53) //Si on appuie sur 5 le héros sort son épée en bas
{
for(int t=0;t<1;t++)
{
showWeapon(1);
if(attackHandler(heroX, heroY+1) == 1 || attackHandler(heroX, heroY+2) == 1)
{
cout<<"Bien jouez!!!\n";Sleep(1000);
}
}
}
if(g==52) //Si on appuie sur 4 le héros sort son épée à gauche
{
for(int t=0;t<1;t++)
{
showWeapon(2);
if(attackHandler(heroX-1, heroY) == 1 || attackHandler(heroX-1, heroY) == 1)
{
cout<<"Bien jouez!!!\n";Sleep(1000);
}
}
}
moveEnemies();
//Si on appuie sur d le héros va à droite
if(g==100)
{
moveHero(0);
}
//Si on appuie sur w le héros va en haut
if(g==119)
{
moveHero(3);
}
//Si on appuie sur a le héros va à gauche
if(g==97)
{
moveHero(2);
}
//Si on appuie sur s le héros va en bas
if(g==115)
{
moveHero(1);
}
if(heroCollider() == 1)
{
vie--;
cout<<"tu a ete toucher il te reste "<<vie<<"\n";
if(vie<1){return -1;}
if(vie>=1)
{
tabl[heroX][heroY]=NULL;
heroY=heroStartPositionY;
heroX=heroStartPositionX;
return 1;
}
}
else
{
if(heroCollider() == 2)
{
cout << "Vous avez obtenu une vie!" << endl;
vie++;
tabl[heroX][heroY]='\02';
}
else
tabl[heroX][heroY]='\02';
}
return 0;
}
Conclusion :
Je ne demande pas vraiment de feedback, c'était dans l'énoncé du travail de le reposter.
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.