Need Criticism on coding style of my AI code

Started by
14 comments, last by Nicholas Kong 10 years, 6 months ago

I'd separate the movement logic into another class. It looks like you have some data-driven behaviour in there (mostXPositionThreshold, leastXPositionThreshold) which could be moved into a HorizontalPingPong movement class or something (maybe use one class for horizontal and vertical ping-pong movement).

Limiting movement speed to 1 pixel at a time (you check for exact pixel positions at least once, e.g.

if(position.getX() == leastXPositionThreshold)
{
runOnce = false;

movementRightFlag = true;

}

is going to cause problems later on. Always check >= or <=

Then you ruin everything by hardcoding the number 50 :(

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Advertisement

I'd separate the movement logic into another class. It looks like you have some data-driven behaviour in there (mostXPositionThreshold, leastXPositionThreshold) which could be moved into a HorizontalPingPong movement class or something (maybe use one class for horizontal and vertical ping-pong movement).

Limiting movement speed to 1 pixel at a time (you check for exact pixel positions at least once, e.g.

if(position.getX() == leastXPositionThreshold)
{
runOnce = false;

movementRightFlag = true;

}

is going to cause problems later on. Always check >= or <=

Then you ruin everything by hardcoding the number 50 sad.png

Doh! Yeah I should def change the condition. I see what you mean.

What do you mean me runing everything by hardcoding the number 50? Because it is a magic number?

Yes, it's a magic number. Is there only 1 bat in your game (checks code again: looks like it is a flying bad and not a pong refugee) and does it always appear in the same place? Presumably 50 is something to do with the height of the ceiling? In the particular place where the bat lives?

Try and imagine how to handle monster movement without any numbers from the program (so read from a level data file say). How would a bat be placed in a level editor, if one existed? They'd probably draw a box with the extents the bat could move between. That box and the fact it is a Bat (probably following ping-pong movement constrained to the height of the box) should be data, not code. (Static data tables are ok though if you haven't got an editor. It's better to read the data from a file though).

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Yes, it's a magic number. Is there only 1 bat in your game (checks code again: looks like it is a flying bad and not a pong refugee) and does it always appear in the same place? Presumably 50 is something to do with the height of the ceiling? In the particular place where the bat lives?

Try and imagine how to handle monster movement without any numbers from the program (so read from a level data file say). How would a bat be placed in a level editor, if one existed? They'd probably draw a box with the extents the bat could move between. That box and the fact it is a Bat (probably following ping-pong movement constrained to the height of the box) should be data, not code. (Static data tables are ok though if you haven't got an editor. It's better to read the data from a file though).

There is only one bat in the game.

50 is just a threshold variable for the bat's position y coordinate. It needs this variable for the bat movement state to be set to "down" otherwise the bat cannot move down in the game.

The bat moves just like the ball in the pong game.

What type of code should be read from a file? Just the movement data of the bat from the program?

Don't read code from a file read data.

Sounds like you have a bouncing object constrained to a 2D rectangle, with a start position, initial direction/velocity, that bounces off the edges of the rectangle. Read those values from a file (or a data table in the code) and you will be able to us the same logic for a lot of enemies, surely?

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

Don't read code from a file read data.

Sounds like you have a bouncing object constrained to a 2D rectangle, with a start position, initial direction/velocity, that bounces off the edges of the rectangle. Read those values from a file (or a data table in the code) and you will be able to us the same logic for a lot of enemies, surely?

Oh I see just put monster data in the text file and read them off. Sounds like a clean approach.

The bouncing object does not bounce off the edges of a rectangle object. It bounces off at certain threshold denoted by the variable names with "threshold as the name" in the Bat class in Java.

More specifically, the update method compares the bat's x o y position coordinates with the threshold variable every frame. If the coordinates lies on that threshold variable, the bat behaves differently.

This topic is closed to new replies.

Advertisement