HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Full clone of the game Pengo

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
fullthepengogameclone

Problem

Use these links to understand the code I'm posting below since although it looks like PHP it is not:

  • Function Reference



  • Language Reference



I have been making a programming language for some time now as a good learning experience etc and improving my programming skills.

I'm making a full clone of the original Pengo. I'm currently doing the AI there is a few Ghosts.

Here is a video of the game I'm making Pengo Demo Video.

Source code to the game: Pengo Source Code

Here is the website for the game and it's download Pengo wiki which will be helpful in seeing how the AI works etc in order to help me improve it. Also i wouldn't mind improving any aspect let me know your opinions.

  • Yellow ghost will just cruise around the map going clockwise when it hits a wall however it will randomly decide to move in another direction.



  • Blue ghost is similar to Red but if it seeds the player it will begin moving towards the player.



  • Green ghost is similar to Blue but if it gets within 3 blocks of the player it will go frenzy and chase player non-stop.



  • Pink ghost uses A* pathfinder to immediately chase the player and hunt them down no matter where they are.



  • Red ghost is one I'm having problems with I need it to avoid the player at all costs even move out of the way of pushed obstacles.



This post will focus on the AI for the Ghosts improvements and the best way to do it also I want to add an avoid AI for the Red ghost so it will avoid players and even dodge blocks pushed at it not sure how making that work perfect.

Forgive the lengthy post; I just wanted to make sure you had all the AI that is needed available

I don't really see a way to get the AI posted since if I leave out any parts it might cause more confusion

This is the constructor for the Green ghost AI this is how it creates it's AI and sets it up:

```
// AI
$AI = new AI($this); // Creates a CORE AI class
$AI->$Enabled = false;
my $AIWanderAStar = new AIWanderAStar($this); // Uses a AIWanderAStar
$AIW

Solution

I am not familiar with this language (no one is), but I will make some comments about the readability of your code. These are sort of general blanket statements that could be applied to any language, and definitely can be applied here.

The only time you ever wrap an if, else, or loop's body in curly braces is when it's more than one line. I know that these are apparently optional in this language, and I know that they're optional in many languages. However, optional curly braces aren't really that great of an idea, and some modern languages have recognized this and made them non-optional.

The problem with optional braces is that we run into constructs like this:

if(!$Move)
    return false;
return true;


And this:

foreach($AIList as my $AICls)
    if($AICls->DoUpdate())
        return;


And unless your language enforces indentation like Python, it's extraordinarily easy for these bits of code to get messed up really easily.

A confused maintainer may comment out return true;, especially if it were ever accidentally indented another level. Or someone looking to add to the foreach loop may completely break the code.

You should definitely include curly braces here.

if(Utils::ChanceOf(0, 100, 0, 70)) // 70% chance to continue on path
if($GoRandomMove || Utils::ChanceOf(0, 100, 20, 40)) // 20% chance to make a totally random move


This Utils::ChanceOf function is really confusing.

I don't see the function anywhere in the code, so I'm assuming it's a language level function. It's nice that you provided comments on what they do... but a language level function shouldn't NEED comments.

Some of understanding language level functions comes with an understanding of the language... but a function which apparently returns a bool value a given percentage of the time should probably only need to take that percentage as an argument. Why are there 4 arguments? What do they all mean?

Why can't this just be:

if (Utils::ChanceOf(70))


And

if (Utils::ChanceOf(20))


}
                }
            }
        }
    }
    return false;
}


This is a pretty good sign that you're nested way too deeply.

First,

if(!$Enabled)
        return false;
    if($Disposed)
        return false;
    if(!AI::$AIEnabled)
        return false;


Why not just:

if (!$Enabled || $Disposed || !AI::$AIEnabled) {
    return false;
}


The if's within this foreach:

foreach($Entity->$Game->$Map->Players() as my $V)


Can probably be moved out into their own functions (there's repeated code here even) and that will help cut down some of the nesting.

Code Snippets

if(!$Move)
    return false;
return true;
foreach($AIList as my $AICls)
    if($AICls->DoUpdate())
        return;
if(Utils::ChanceOf(0, 100, 0, 70)) // 70% chance to continue on path
if($GoRandomMove || Utils::ChanceOf(0, 100, 20, 40)) // 20% chance to make a totally random move
if (Utils::ChanceOf(70))
if (Utils::ChanceOf(20))

Context

StackExchange Code Review Q#59737, answer score: 2

Revisions (0)

No revisions yet.