Juego
Juego
PRÁCTICA N°12
Código: 2024-103061
Tacna-Perú
2024
#include <iostream>
#include <conio.h> // Para _kbhit() y _getch()
#include <windows.h> // Para Sleep()
#include <ctime>
#include <cstdlib>
#include <string> // Para usar std::string
bool gameOver;
const int width = 20;
const int height = 20;
int x, y, score;
int tailX[100], tailY[100];
int nTail;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;
void Setup() {
gameOver = false;
dir = STOP;
x = width / 2;
y = height / 2;
for (int i = 0; i < 3; i++) {
goodFoodX[i] = rand() % width;
goodFoodY[i] = rand() % height;
badFoodX[i] = rand() % width;
badFoodY[i] = rand() % height;
}
score = 0;
}
void Draw() {
system("cls");
for (int i = 0; i < width + 2; i++)
cout << "#";
cout << endl;
if (j == width - 1)
cout << "#";
}
cout << endl;
}
void Input() {
if (_kbhit()) {
switch (_getch()) {
case 75:
dir = LEFT;
break;
case 77:
dir = RIGHT;
break;
case 72:
dir = UP;
break;
case 80:
dir = DOWN;
break;
case 27:
cout<<"gracias por jugar";
}
}
}
void Logic() {
int prevX = tailX[0];
int prevY = tailY[0];
int prev2X, prev2Y;
tailX[0] = x;
tailY[0] = y;
for (int i = 1; i < nTail; i++) {
prev2X = tailX[i];
prev2Y = tailY[i];
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (dir) {
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
case UP:
y--;
break;
case DOWN:
y++;
break;
default:
break;
}
if (x >= width) x = 0; else if (x < 0) x = width - 1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;
int main() {
srand(time(0));
Setup();
while (!gameOver) {
Draw();
Input();
Logic();
Sleep(100); // para hacer el juego mas lento
}
return 0;
}