Coding a rogue-like game for practicing c++
Hey guys,
A few days ago I started coding in C++ so I've created an own task for me: Coding a rogue-like game.
Today I created a square where you can walk in:
Code:
#include <cstdio>
#include <ncurses.h>
#include <iostream>
using namespace std;
const int border_x = 20; //border for the square
const int border_y = 20;
string Field[border_y][border_x]; //the field with your character/monsters..., global so every function can change it
void CreateStartField() //Create the field without anything
{
for (int i=0; i < border_y; i++)
{
for(int j=0; j < border_x; j++)
{
if ( (i==0) || (j==0) || (i==border_x-1) || (j==border_y-1) ) //Borders of the field
{
Field[i][j] = "*"; //signs for border
}
else
{
Field[i][j] = "."; //signs for field
}
}
}
}
void KeyPress(int &act_x, int &act_y) //reads the keys in you press
{
int Direct; // the direction
initscr();
keypad(stdscr, TRUE);
Direct = getch();
switch (Direct) // checks if your keypress is "useful"
{
case KEY_RIGHT:
if (act_x < border_x-2)
{
Field[act_y][act_x] = ".";
act_x++;
Field[act_y][act_x] = "A";
break;
}
else
break;
case KEY_LEFT:
if (act_x > 1)
{
Field[act_y][act_x] = ".";
act_x--;
Field[act_y][act_x] = "A";
break;
}
else
break;
case KEY_UP:
if (act_y > 1)
{
Field[act_y][act_x] = ".";
act_y--;
Field[act_y][act_x] = "A";
break;
}
else
break;
case KEY_DOWN:
if (act_y < border_y-2)
{
Field[act_y][act_x] = ".";
act_y++;
Field[act_y][act_x] = "A";
break;
}
else
break;
}
}
void PrintField() //prints the actual field
{
for (int y=0; y < border_y; y++)
{
for (int x=0; x < border_x; x++)
{
cout << Field[y][x] << " ";
}
cout << "\n\r";
}
}
int main()
{
CreateStartField();
int act_x = 1; //actual coordinates of your character
int act_y = 1;
Field[act_y][act_x] = "A";
PrintField();
while (1)
{
KeyPress(act_x, act_y);
PrintField();
}
}
My idea is, that at the end of the game, everything like your character, monsters and so on you will find at the "Field Array".
Before I continue with the next steps:
Can you give me any hints what I can do better?
And: Before the "game" starts, you have to press any key. How do I fix it, that the display shows it immediately at startup?
How can I refresh the window, so that the old display will be deleted (I hope you understand what I want to say)
PS: Sorry for my bad English, but it is not my mother tongue.
Re: Coding a rogue-like game for practicing c++
As Field only stores one char and the size of the array is known at compile time, then instead of Field being an array of string, it could be just an array of char.
You could simplify CreateStartField by using the tertiary ? operator
Code:
char Field[border_y][border_x];
...
void CreateStartField() //Create the field without anything
{
for (int i=0; i < border_y; i++)
for(int j=0; j < border_x; j++)
Field[i][j] = ( (i==0) || (j==0) || (i==border_x-1) || (j==border_y-1) ) ? '*' : '.';
}
You could also simply the key switch as to
Code:
case KEY_RIGHT:
if (act_x < border_x-2) {
Field[act_y][act_x] = '.';
act_x++;
Field[act_y][act_x] = 'A';
}
break;
Good luck with your game:wave: