Hey first post here so let me know if I'm doing something wrong.
I wanted to practice classes with a fun game aspect (shit pacman) but for some reason I can't properly remove a life when I hit a ghost. I'm far from done. I know there is a lot wrong but I'd like to focus on removing a life.
I use the line player1.setLives(player1.getLives() - 1); in pacmain.cpp
to remove a life from the player1 object created as a man object witch is kid class of the parent Entity class. the parent Has the get and set lives functions both defined as public inside the Entity.h file.
I run the line above when the ghost and man are on the same space. I know this if statement works because the other lines inside execute.
Is there an conceptual issue I'm not understanding? or maybe my method is poor?
----------------Inside my Entity.cpp---------------------------
#pragma once
#include <Windows.h>
#include <iostream>
#include <string>
#include "map.h"
class Entity
{
private:
int x, y, lives;
char symbol;
public:
Entity();
Entity(int, int, int, char);
void setX(int); //sets x value
void setY(int); //sets y value
void setLives(int); //sets lives variable
void setSymbol(char); //sets symbol variable
int getX() const; //returns x value
int getY() const; //returns y value
int getLives() const; //returns lives
char getSymbol() const; // returns symbol
void gotoxy(short, short) const;
void display() const;
void display(char) const;
void replace() const;
};
In at least one point the code has a minor error .. it assumes short is ok for lives where they are of size int. Keep the sizes the same unless you have a really good reason to change it, and in that case, you are usually promoting to a larger type. If going large type to small for a reason, this requires a comment as to why, for you are doing something weird (possibly valid bitwise logic by chopping the data, but say so!).
it can be int, just be consistent and not change types. If its like 3 lives per quarter arcade game stuff, it will even fit in a char, but saving a byte or two isn't really a big deal anymore.
Inheritance won't change your idea; if it isn't working, its not the getter/setter use that is breaking it.
I can make a micro example with the inheritance, but that won't fix whatever isn't working for you. It will look a lot like what you have, and you will still have to debug yours... This does exactly the same output:
Yeah this does the same thing mine does, thanks! Only error is rand is rand() for my IDE. I'd like to make the ghost smarter though. Currently the ghost only changes direction when it hits a wall rather than anytime it could change direction. Also, it will do the same thing over and over rather than 'learning'.
It's always rand(), actually. That's just a typo. As I said, I didn't test it (or even try to compile it) since I don't usually use Windows.
To make the ghost move a little more randomly, not just changing direction when it hits a wall, you could do something like this. It's still not "smart", though. (Still untested, though.)
the classic, they chased you down and ran away if you had activated the corner pellet. That may be fun to put in later... path finding is good to study.