Jump to content
  • Advertisement
Sign in to follow this  
Nicholas Kong

Need Criticism on coding style of my AI code

This topic is 2130 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :(

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

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?

Edited by warnexus

Share this post


Link to post
Share on other sites

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?

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

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.

Edited by warnexus

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!