samedi 25 avril 2015

incrementing values in if loop (in C)

I am writing a self-controlled Pacman game (Pacman moves by itself by algorithms which decide it's movement, needs to chase ghosts, etc.) in C. Keep in mind that I am very new to programming in C so the method I use may not be a good one at all.

Anyways, I stumbled upon a problem.

my Pacman moves in an Array map with it's placement at

Map[yPacman][xPacman] == place of Pacman;

Now, when Pacman is in "rage mode"; after picking up a sweet, I would like him to find the ghosts he seeks. The ghost being a char 'G' in the array.

Now, I would like Pacman to see ahead of him to catch and follow the ghost when it reaches a distance near him. Let's say he's moving to the left; I would him to look 5 "X-Coords" left of him, including the X-coords "1,2,3,4,5". When any of these coordinates equal to a char 'G', it'd have to follow him.

Now, I would like to implement it, but I can't seem to find a way how to.

if (Map[yPacman][xPacman-i] == 'G')  { }

With "i" having a value from 1-5. Incrementing it during the process itself. I would not like to use

if (Map[yPacman][xPacman-1] == 'G' || Map[yPacman][xPacman-2] == 'G'|| Map[yPacman][xPacman-3] == 'G'|| Map[yPacman][xPacman-4] == 'G'|| Map[yPacman][xPacman-5] == 'G')  { }

Even though this is the exact result I would need.

I personally think it seems very easy to solve how to do this. But at the moment I have no clue. Thanks in advance

Aucun commentaire:

Enregistrer un commentaire