0% found this document useful (0 votes)
9 views

Juego

Uploaded by

spilcos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Juego

Uploaded by

spilcos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIVERSIDAD NACIONAL JORGE BASADRE GROHMANN

FACULTAD DE INGENIERÍA – FAIN

ESCUELA PROFESIONAL DE INGENIERÍA METALÚRGICA

PRÁCTICA N°12

Juego de la serpiente minera

Estudiante: Sebastean Cristian Pilco Salamanca

Código: 2024-103061

Asignatura: Fundamentos de la programación

Docente: Miguel Anguel Laquiahuanaco

Año: 1er año B

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

using namespace std;

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;

// Posiciones de los alimentos buenos


int goodFoodX[3], goodFoodY[3];
// Posiciones de los alimentos malos
int badFoodX[3], badFoodY[3];
// Caracteres y puntuaciones de los alimentos
string goodFoodChar[3] = {"Au", "Fe", "Cu"};
string badFoodChar[3] = {"Cl", "S", "As"};
int goodFoodScore[3] = {10, 20, 30};
int badFoodScore[3] = {-10, -20, -30};

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;

for (int i = 0; i < height; i++) {


for (int j = 0; j < width; j++) {
if (j == 0)
cout << "#";
if (i == y && j == x)
cout << "O";
else {
bool print = false;
for (int k = 0; k < nTail; k++) {
if (tailX[k] == j && tailY[k] == i) {
cout << "o";
print = true;
}
}
for (int k = 0; k < 3; k++) {
if (i == goodFoodY[k] && j == goodFoodX[k]) {
cout << goodFoodChar[k];
j++; // Move cursor forward since food is two characters wide
print = true;
} else if (i == badFoodY[k] && j == badFoodX[k]) {
cout << badFoodChar[k];
j++; // Move cursor forward since food is two characters wide
print = true;
}
}
if (!print)
cout << " ";
}

if (j == width - 1)
cout << "#";
}
cout << endl;
}

for (int i = 0; i < width + 2; i++)


cout << "#";
cout << endl;
cout << "Score: " << score << 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;

for (int i = 0; i < nTail; i++)


if (tailX[i] == x && tailY[i] == y)
gameOver = true;

for (int i = 0; i < 3; i++) {


if (x == goodFoodX[i] && y == goodFoodY[i]) {
score += goodFoodScore[i];
goodFoodX[i] = rand() % width;
goodFoodY[i] = rand() % height;
nTail++;
}
if (x == badFoodX[i] && y == badFoodY[i]) {
score += badFoodScore[i];
badFoodX[i] = rand() % width;
badFoodY[i] = rand() % height;
if (score <= 0) {
gameOver = true;
}
}
}
}

int main() {
srand(time(0));
Setup();
while (!gameOver) {
Draw();
Input();
Logic();
Sleep(100); // para hacer el juego mas lento
}
return 0;
}

You might also like