-
April 23rd, 2013, 08:48 AM
#1
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.
-
April 23rd, 2013, 09:54 AM
#2
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
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|