Scanners - C# Game


I am a PhD student at NUI Galway in Ireland researching energy efficiency in data centres. In the course of my early research I was writing some code to scan a heat map of a data centre floor plan to identify hot and cold spots in the room. The more I developed the code the more it began to look like a version of the original pacman game. So, late one evening (as is often the case with programmers) I decided to take the code further. Below is an interesting section from the first evolution of the game which I have called Scanners - based on what I was doing when I started writing the code.

Collision Detection

In pacman (and most other games since) the player must be kept within a game area which has well defined boundaries. Keeping the player within those boundaries is achieved using collision detection. Scanners includes a collision detection function to identify when a player hits one of the outer walls (inner boundaries are being added to version 2).

Thinking about hitting outer walls identifies the fact that a player will bounce back into the play area differently depending on where the collsion takes place. For example – if you detect a collision in the centre of the upper wall you can bounce the competitor back into the game in any of 5 directions (left, right, down,down-left and down-right), whereas if the competitor hits the top wall at either the left or right corner then the number of directions to bounce the player back into the game is limited to 3. Players are bounced in a random direction when a collision takes place:

int iNextMove = random.Next(1, 6);

The code for detecting any collision with the top wall follows. Comments explain each section:

#region topwall
            //top wall
            if (competitor.iYPosition == 1)
            {

                //not intersection with left or right walls – 16 squares in the play area
                if (competitor.iXPosition != 1 && competitor.iXPosition != 16)
                {
                    //bounce competitor off wall in any direction except up
                    int iNextMove = random.Next(1, 6);
                    //track the direction the player is bounced
                    switch (iNextMove)
                    {
                        //left
                        case 1: competitor.iXPosition = competitor.iXPosition - 1;
                            iLeftMoves++;
                            break;
                        //right
                        case 2: competitor.iXPosition = competitor.iXPosition + 1;
                            iRightMoves++;
                            break;
                        //down
                        case 3: competitor.iYPosition = competitor.iYPosition + 1;
                            iDownMoves++;
                            break;
                        //down-right
                        case 4: competitor.iXPosition = competitor.iXPosition + 1;
                            competitor.iYPosition = competitor.iYPosition + 1;
                            iDownRightMoves++;
                            break;
                        //down-left
                        case 5: competitor.iXPosition = competitor.iXPosition - 1;
                            competitor.iYPosition = competitor.iYPosition + 1;
                            iDownLeftMoves++;
                            break;
                        default: break;
                    }
                }
                else
                {
                    //left corner
                    if (competitor.iXPosition == 1)
                    {
                        int iNextMove = random.Next(1, 4);
                        switch (iNextMove)
                        {
                            //right
                            case 1: competitor.iXPosition = competitor.iXPosition + 1;
                                iRightMoves++;
                                break;
                            //down
                            case 2: competitor.iYPosition = competitor.iYPosition + 1;
                                iDownMoves++;
                                break;
                            //down-right
                            case 3: competitor.iXPosition = competitor.iXPosition + 1;
                                competitor.iYPosition = competitor.iYPosition + 1;
                                iDownRightMoves++;
                                break;
                            default: break;
                        }
                    }
                    //right corner
                    else
                    {
                        int iNextMove = random.Next(1, 4);
                        switch (iNextMove)
                        {
                            //left
                            case 1: competitor.iXPosition = competitor.iXPosition - 1;
                                iLeftMoves++;
                                break;
                            //down
                            case 2: competitor.iYPosition = competitor.iYPosition + 1;
                                iDownMoves++;
                                break;
                            //down-left
                            case 3: competitor.iXPosition = competitor.iXPosition - 1;
                                competitor.iYPosition = competitor.iYPosition + 1;
                                iDownLeftMoves++;
                                break;
                            default: break;
                        }
                    }
                }
            }
            #endregion


With up to 8 competitors in the game it is clear that a lot of processing is taking place to detect collisions. On my own PC I was able to run the Timer_Tick (which controls the game) at 1/10 of a second. With a faster PC you might be able to speed the game up even more. Game speed is available to the player as an option before starting.

I would be interested to hear any ideas C#Corner readers have for further development of the game or send me your edits :)


Similar Articles