Author Topic: Basic game programming concepts  (Read 19399 times)


guest509

  • Guest
Re: Basic game programming concepts
« Reply #16 on: October 14, 2013, 03:40:58 AM »
Assembly? That's sort of like Gamemaker, right?  :P

Beelzebob

  • Newcomer
  • Posts: 2
  • Karma: +0/-0
    • View Profile
    • Email
Re: Basic game programming concepts
« Reply #17 on: October 14, 2013, 06:47:55 AM »
I thought Assembly was for Noobs ? LOL

chasm

  • Newcomer
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: Basic game programming concepts
« Reply #18 on: October 15, 2013, 02:15:32 PM »
Quote from: Quendus
To get help with problems in code and software, it's best to provide specific information about what went wrong - no-one can say much about something that "doesn't work", but if you say what doesn't work, when, whether there was an error message at runtime or compile time, what the error messages were, etc., then often there will be someone who's had a similar problem in the past.

You're totally right. I was just thinking it might not be an ncurses thing, but rather a problem of the implementation itself. So maybe someone who never used ncurses might be able to help as well.

This is the actual error that I don't understand:
I used getch() to capture the keyboard input, however it will only return "Press 'q' to quit", even when a key is pressed that I defined to do something else (like the arrow keys or u, h, n, j).
Even though I implemented while (key != 'q') in the function Action(), the program terminates after the second key is pressed.


Quote from: Vanguard
Making "hero" a tile type is a Real Bad Idea.  It's confusing and it'll lead to more than a few problems.  Instead you should have heroX and heroY integers to track their location.  Better yet, make arrays of integers to store the x and y locations of every man, animal, and monster in the dungeon.

I worried about that, too. Would it make sense to write a function like GetLocation() that returns both coordinates? I actually wanted to do this at first, but I didn't really know how to do that (if it makes sense, I will again try to implement it by myself), so I chose the simplest thing I could think of.
What do you mean by arrays of integers? I would have rather thought about a list or a 2-dim. vector. Could you give an example?


Quote from: Vanguard
Even though programming a game is about manipulating abstract data, imo the best way to do it is to think about everything in terms of concrete nouns and verbs (with objects and variables being nouns and functions/methods being verbs).  The tile array describes different segments of the building the game is taking place in.  That description doesn't fit the hero, so he or she shouldn't be tracked by that variable.

This is very interesting.
I've never thought like that. I admit that I rather have a hard time to actually imagine variables and functions like this as I still think of (tile) arrays as tensors rather than actual maps.
But as I'm still a beginner with game programming, I'm not beyond hope, I think.

Quendus

  • Rogueliker
  • ***
  • Posts: 447
  • Karma: +0/-0
  • $@ \in \{1,W\} \times \{1,H\}$
    • View Profile
    • Klein Roguelikes
Re: Basic game programming concepts
« Reply #19 on: October 15, 2013, 05:32:28 PM »
Quote from: Quendus
To get help with problems in code and software, it's best to provide specific information about what went wrong - no-one can say much about something that "doesn't work", but if you say what doesn't work, when, whether there was an error message at runtime or compile time, what the error messages were, etc., then often there will be someone who's had a similar problem in the past.

You're totally right. I was just thinking it might not be an ncurses thing, but rather a problem of the implementation itself. So maybe someone who never used ncurses might be able to help as well.

This is the actual error that I don't understand:
I used getch() to capture the keyboard input, however it will only return "Press 'q' to quit", even when a key is pressed that I defined to do something else (like the arrow keys or u, h, n, j).
Even though I implemented while (key != 'q') in the function Action(), the program terminates after the second key is pressed.


That error sounds a lot like the errors I get when I forget to put breaks in a switch statement and indeed, searching for "press" in your code I found just that:
Code: [Select]
...
    case 'n':
      OpenDoor(x,y+1);
      break;
    case 'q':
      endwin();
    /* add "break;" here */
    default:
      mvaddstr(0,0,"Press 'q' to quit");
break;
...

Vanguard

  • Rogueliker
  • ***
  • Posts: 1112
  • Karma: +0/-0
    • View Profile
Re: Basic game programming concepts
« Reply #20 on: October 16, 2013, 07:18:56 AM »
Good catch, Quendus.

I worried about that, too. Would it make sense to write a function like GetLocation() that returns both coordinates?

It would be easier to make one function for each coordinate instead.  Make a getLocationX() and a getLocationY() and just call them one after the other to get a pair of coordinates.

What do you mean by arrays of integers? I would have rather thought about a list or a 2-dim. vector. Could you give an example?

The arrays I'm thinking of would look something like this:

int actorLocationX[MAX_ACTORS];
int actorLocationY[MAX_ACTORS];

And then just assign everything a number when they spawn.  The hero will probably spawn first, so they're number zero.  Personally, I'd reserve the number zero slot for them just so you can always easily interact with the hero's data if you need to.

Anyway, in that system you could have something like this:

Code: [Select]
// We want to know where a given character is.
void printActorLocation(int actorNumber) {
     int targetX = getLocationX(actorNumber);
     int targetY = getLocationY(actorNumber);
     std::cout<<"Actor number " <<actorNumber<<" is located at "<<targetX<<", "<<targetY";
}

// Returns the x coordinate of a given character
int getLocationX(int targetNumber) {
     return xLocation[targetNumber];
}

// Returns the y coordinate of a given character
int getLocationY(int targetNumber) {
     return yLocation[targetNumber];
}

Something like that.

A vector should be able to accomplish the same thing, my understanding is that they're more or less the same thing, but the size of an array is constant and the size of a vector can change.  I could be wrong.

Another method, and probably a better one, would be to make a class containing the hero and all the monsters and NPCs and such and then referring to them through that.

Something like this:

Code: [Select]
class Actor {

// Whether the actor is alive and active
bool alive;

// The actor's name
string actorName;

// The actor's health
int maxHealth;
int health;

// The actor's location
int xLocation;
int yLocation;

void displayHeroData();
void displayActorData(Actor);
};

// I'm not going to assign the hero a location or any stats in this example, but pretend we did that at some point
Actor hero;

// Prints data for the hero specifically
Actor::displayHeroData(){
     std::cout<<"The hero has "<<hero.health<<"/"<<hero.maxHealth<<" hit points.\n";
     std::cout<<"They are located at "<<hero.xLocation<<", "<<hero.yLocation<<".";
}

// Prints data for any actor.  This could also be used to display the hero's data
// In a real program, you would never want to include both this function and the one above it.  It's redundant.
Actor::displayActorData(Actor currentActor){
     std::cout<<currentActor.actorName<<" has "<<currentActor.health<<"/"<<currentActor.maxHealth<<" hit points.\n";
     std::cout<<"They are located at "<<currentActor.xLocation<<", "<<currentActor.yLocation<<".";
}

Hopefully those examples are useful.  I didn't bother to specify which parts of the class are private, protected, or public to keep the example simple, but you'll want to practice encapsulation in your own programs.