Last active
December 23, 2023 12:31
-
-
Save SnowyYANG/b993813653e16b2153c258f75f3a6f34 to your computer and use it in GitHub Desktop.
Simple C++ RPG
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Simple C++ RPG | |
//by ChatGPT, Copilot and SnowyYANG | |
//https://snowyyang.itch.io/simple-cpp-rpg | |
#include <iostream> | |
#include <cstdlib> | |
#include <ctime> | |
#include <conio.h> | |
#define MAP_SIZE 10 | |
// Define variables | |
int player_hp = 100, player_atk = 15, player_def = 10, player_gold = 0; | |
// Define monster | |
typedef struct { | |
int x, y, hp, atk, def, gold; | |
} Monster; | |
Monster monsters[MAP_SIZE]; | |
// Player position | |
int player_x = 0, player_y = 0; | |
// Game map | |
char map[10][10]; | |
// Initialize monster | |
void init_monster(Monster& monster) { | |
while(true) { | |
monster.x = rand() % MAP_SIZE; | |
monster.y = rand() % MAP_SIZE; | |
if (map[monster.x][monster.y] == '.'&&!(monster.x==player_x&&monster.y==player_y)) break; | |
} | |
monster.hp = rand() % 100 + 50; | |
monster.atk = rand() % 10 + 10; | |
monster.def = rand() % 5 + 5; | |
monster.gold = rand() % 49 + 1; | |
} | |
int lastgold; | |
// Battle with monster | |
void battle(Monster& monster) { | |
while (player_hp > 0 && monster.hp > 0) { | |
int damage = player_atk - monster.def; | |
if (damage < 0) damage = 0; | |
monster.hp -= damage; | |
if (monster.hp <= 0) { | |
player_gold += monster.gold; | |
lastgold=monster.gold; | |
return; | |
} | |
damage = monster.atk - player_def; | |
if (damage < 0) damage = 0; | |
player_hp -= damage; | |
} | |
} | |
Monster& get_monster(int x, int y) { | |
for(int i=0;i<MAP_SIZE;i++) | |
{ | |
if(monsters[i].x==x && monsters[i].y==y) | |
{ | |
return monsters[i]; | |
} | |
} | |
} | |
void show_monster_info(int x, int y, const char* direction) { | |
if (x >= 0 && x < MAP_SIZE && y >= 0 && y < MAP_SIZE && map[x][y] == 'M') { | |
Monster& monster = get_monster(x, y); | |
std::cout << direction << " HP: " << monster.hp << ", atk: " << monster.atk << ", def: " << monster.def << ", gold: " << monster.gold << std::endl; | |
} | |
} | |
bool game_start; | |
// Show map | |
void show_map() { | |
for (int i = 0; i < MAP_SIZE; i++) { | |
for (int j = 0; j < MAP_SIZE; j++) { | |
std::cout << map[i][j] << ' '; | |
} | |
std::cout << '\n'; | |
} | |
std::cout << "Your HP: " << player_hp << ", atk: " << player_atk << ", def: " << player_def << ", gold: " << player_gold << std::endl; | |
show_monster_info(player_x, player_y+1, "Right monster"); | |
show_monster_info(player_x+1, player_y, "Monster below"); | |
show_monster_info(player_x, player_y-1, "Left monster"); | |
show_monster_info(player_x-1, player_y, "Upper monster"); | |
if (game_start) { | |
std::cout << "Press WASD to move, ESC to restart." << std::endl; | |
game_start = false; | |
} | |
if (lastgold!=0) | |
{ | |
std::cout << "You defeated a monster and got " << lastgold << " gold." << std::endl; | |
lastgold=0; | |
} | |
} | |
int main() { | |
GAME_START: | |
game_start = true; | |
srand(time(0)); // Initialize random seed | |
lastgold = 0; | |
// Initialize game map | |
for (int i = 0; i < MAP_SIZE; i++) | |
for (int j = 0; j < MAP_SIZE; j++) | |
map[i][j] = '.'; | |
// Place obstacles | |
for (int i = 0; i < MAP_SIZE; i++) | |
map[rand() % MAP_SIZE][rand() % MAP_SIZE] = '#'; | |
// Initialize monsters | |
for (int i = 0; i < MAP_SIZE; i++) { | |
init_monster(monsters[i]); | |
map[monsters[i].x][monsters[i].y] = 'M'; | |
} | |
// Place player | |
map[player_x][player_y] = 'P'; //always start at 0,0 | |
while (player_hp > 0) { | |
system("cls"); // Clear screen | |
show_map(); | |
char command = _getch(); | |
map[player_x][player_y] = '.'; // Clear previous position | |
int x0 = player_x; | |
int y0 = player_y; | |
if ((command == 'a' || command == 'A') && player_y > 0) player_y--; | |
if ((command == 'd' || command == 'D') && player_y < MAP_SIZE - 1) player_y++; | |
if ((command == 'w' || command == 'W') && player_x > 0) player_x--; | |
if ((command == 's' || command == 'S') && player_x < MAP_SIZE - 1) player_x++; | |
if (command == 27) goto GAME_START; // Restart game | |
// Check if player encounters a monster | |
if (map[player_x][player_y] == 'M') { | |
battle(get_monster(player_x, player_y)); | |
} else if (map[player_x][player_y] == '#') { | |
player_x=x0; | |
player_y=y0; | |
} | |
map[player_x][player_y] = 'P'; // Update player position | |
} | |
std::cout << "You die. Got " << player_gold << " gold in total." << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment