Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdlib.h>
- #include<stdio.h>
- #include<Windows.h>
- #include<time.h>
- #include<conio.h>
- void gotoxy(int x, int y) {
- COORD c;
- c.X = x;
- c.Y = y;
- HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleCursorPosition(h, c);
- }
- void textcolor(int color) {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
- }
- int MonRand(int min, int max) {
- int res;
- res = min + rand() % (max - min);
- return res;
- }
- float frand() {
- float f;
- f = (float)rand() / RAND_MAX;
- return f;
- }
- int main() {
- SetConsoleOutputCP(1252); //pour les accents dans la console
- int fin = 0, touche;
- const int TX = 40;
- const int TY = 20;
- // déclaration du joueur
- int x, y; // position
- int color; //couleur
- int lettre; // son apparence
- srand(time(NULL));
- //initialisation du joueur
- x = rand() % TX;
- y = rand() % TY;
- lettre = 'A' + rand() % 26;
- color = rand() % 256;
- //afficher le joueur
- textcolor(color);
- gotoxy(x, y);
- putchar(lettre);
- while (!fin) { // tant que fin == 0
- if (_kbhit()) { //si touche appuyée
- //effacer le joueur
- textcolor(0);
- gotoxy(x, y);
- putchar(lettre);
- //putchar(' ');
- //recuperer la touche
- touche = _getch();
- switch (touche) {
- case 72: y--; break; //fleche haut
- case 77: x++; break; //fleche droite
- case 80: y++; break; //fleche bas
- case 75: x--; break; //fleche gauche
- case 'a': color = rand() % 256; break; //changer la couleur du joueur en appuant sur a
- case 27: fin = 1; //quitter : ECHAP
- }
- //controle des bords
- if (x < 0)
- x = 0;
- if (x > TX - 1)
- x = TX - 1;
- if (y < 0)
- y = 0;
- if (y > TY - 1)
- y = TY - 1;
- //afficher le joueur
- textcolor(color);
- gotoxy(x, y);
- putchar(lettre);
- }
- }
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement