コード例 #1
0
 private void MainForm_Load(object sender, EventArgs e)
 {
     _rendering = new MapRendering(boardPic);
     _rnd       = new Random();
     ResetGame();
     _rendering.Render(_map);
 }
コード例 #2
0
        private void gameTimer_Tick(object sender, EventArgs e)
        {
            // Move ghosts
            if (ghostRandomRadio.Checked)
            {
                randomGhosts();
            }
            else if (ghostProxyRadio.Checked)
            {
                proxyGhosts();
            }
            else if (ghostShortRadio.Checked)
            {
                shortGhosts();
            }
            else
            {
                randomGhosts();
            }

            _game.Trail = _map.Ghost;
            _map.Ghost  = _game.Ghost.ToArray();

            // Move pacman
            if (pacmanAutoChk.Checked)
            {
                // Find nearest osbtacle in a line of sight in all directions
                List <GameSight> rays = new List <GameSight>();
                lookRay(rays, -1, 0);
                lookRay(rays, 1, 0);
                lookRay(rays, 0, -1);
                lookRay(rays, 0, 1);
                rays.Sort((a, b) =>
                {
                    int diff = weighType(a) - weighType(b);
                    if (diff != 0)
                    {
                        return(diff);
                    }
                    if (a.Obstacle == MapTileType.GHOST1)
                    {
                        return(b.Distance - a.Distance);
                    }
                    else
                    {
                        return(a.Distance - b.Distance);
                    }
                });
                if (rays.Count > 0)
                {
                    if (rays[0].Obstacle == MapTileType.WALL && rays.Count(t => t.Obstacle == MapTileType.WALL) >= 2)
                    {
                        rays.RemoveAll(t => t.Obstacle != MapTileType.WALL || t.Position.Equals(_game.CameFrom));
                        _game.CameFrom = _map.Pacman;
                        if (rays.Count == 1)
                        {
                            _map.Pacman = rays[0].Position;
                        }
                        else
                        {
                            _map.Pacman = rays[_rnd.Next(rays.Count)].Position;
                        }
                    }
                    else
                    {
                        _game.CameFrom = _map.Pacman;
                        _map.Pacman    = rays[0].Position;
                    }
                }
            }
            else
            {
                _game.CameFrom = _map.Pacman;
                _map.Pacman    = _game.Pacman;
            }



            bool gameLost = false;

            // if ghost hits pacman, lose the game
            if (_map.Ghost.Any(g => g.Equals(_map.Pacman)))
            {
                // LOSE
                Text = "Codeman LOST";
                gameTimer.Enabled = false;
                gameLost          = true;
            }

            if (!gameLost)
            {
                // if pacman is on point, eat it
                if (_map[_map.Pacman] == MapTileType.POINT)
                {
                    _map[_map.Pacman] = MapTileType.EMPTY;
                    _game.PointCount--;
                }

                if (_game.PointCount == 0)
                {
                    // WIN
                    Text = "Codeman WON";
                    gameTimer.Enabled = false;
                }
            }

            _rendering.Render(_map);
        }