Exemple #1
0
        public void generateFood(ClsSnake snake1, ClsSnake snake2, int width, int height)
        {
            Random random = new Random();

            this.isBigFood = (random.Next(0, 100) < ClsParameter.PercentBigFood);
            bool check;

            do
            {
                this._Coor.X = random.Next(0, width / snake1.Size) * snake1.Size;
                this._Coor.Y = random.Next(0, height / snake1.Size) * snake1.Size;
                check        = true;
                for (int i = 0; i < snake1.lengh; i++)
                {
                    if (snake1.Coor[i].X == this._Coor.X && snake1.Coor[i].Y == this._Coor.Y)
                    {
                        check = false;
                        break;
                    }
                }
                for (int i = 0; i < snake2.lengh; i++)
                {
                    if (snake2.Coor[i].X == this._Coor.X && snake2.Coor[i].Y == this._Coor.Y)
                    {
                        check = false;
                        break;
                    }
                }
            }while (check == false);            // check duplication
        }
Exemple #2
0
        public void processEventKey(ClsSnake snake, ClsKeyCode key)
        {
            Direction direction = this.getKeyCode(snake, key);

            if (direction == Direction.NONE)
            {
                return;
            }
            snake.updateCorner(direction);
        }
 private bool collideSnake(ClsSnake snake1, ClsSnake snake2)
 {
     for (int i = 0; i < snake2.lengh; i++)
     {
         if (snake1.Coor[0].X == snake2.Coor[i].X && snake1.Coor[0].Y == snake2.Coor[i].Y)
         {
             return(true);
         }
     }
     return(false);
 }
Exemple #4
0
        public FrmMode2(ClsMapWidthHeight winsize,
                        bool hasborder,
                        string color,
                        int lengh,
                        int snakesize,
                        string snakecolor1,
                        string snakecolor2,
                        string foodcolor,
                        bool isspeedup,
                        ClsKeyCode playerkey1,
                        ClsKeyCode playerkey2)
        {
            InitializeComponent();
            this.MapSizeWidth  = winsize.Width;
            this.MapSizeHeight = winsize.Height;
            this.hasBorder     = hasborder;

            this.BackgroundImage                 = new Bitmap(Application.StartupPath + ClsParameter.LinkBackground + ClsParameter.Extension);
            this.BackgroundImageLayout           = ImageLayout.Stretch;
            this.ptbGame.BackgroundImage         = new Bitmap(Application.StartupPath + ClsParameter.LinkBGGame + color + ClsParameter.Extension);
            this.ptbGame.BackgroundImageLayout   = ImageLayout.Stretch;
            this.ptbBorder.BackgroundImage       = new Bitmap(Application.StartupPath + ClsParameter.LinkBDGame + ClsParameter.Extension);
            this.ptbBorder.BackgroundImageLayout = ImageLayout.Stretch;
            this.ptbBorder.Visible               = (this.hasBorder);

            this.snake1 = new ClsSnake(lengh, snakesize, snakecolor1, 1, this.MapSizeWidth, this.MapSizeHeight);
            this.snake2 = new ClsSnake(lengh, snakesize, snakecolor2, 3, this.MapSizeWidth, this.MapSizeHeight);
            this.food   = new ClsFood(foodcolor);

            this.isSpeedup = isspeedup;
            this.key1      = playerkey1;
            this.key2      = playerkey2;

            this.increaceFree1 = false;
            this.increaceFree2 = false;

            this.process = new ClsProcessLogic();
            this.press   = new ClsEventKey();

            this.playerName1 = "";
            this.playerName2 = "";
            this.score1      = 0;
            this.score2      = 0;

            this.setWindow();

            this.timerDelay1.Interval = ClsParameter.DelayDefault;
            this.timerDelay1.Start();
            this.timerDelay2.Interval = ClsParameter.DelayDefault;
            this.timerDelay2.Start();

            this.food.generateFood(snake1, snake2, MapSizeWidth, MapSizeHeight);
        }
 public void logicBorderEnable(ClsSnake snake1, ClsSnake snake2, ref bool increacefree, ClsFood food, int width, int height, bool ischangespeed, Timer time)
 {
     if (increacefree)
     {
         active.afterConlideBigFood(snake1, snake2, ref increacefree, food, width, height);
     }
     else if (this.collideFood(snake1, food))
     {
         active.afterCollideFood(snake1, snake2, ref increacefree, food, width, height, ischangespeed, time);
     }
     if (this.collideSnake(snake1) || this.collideSnake(snake1, snake2) || this.collideBorder(snake1, width, height))
     {
         active.afterCollideSnakeOrBorder(snake1);
     }
     else
     {
         snake1.moveSnake();
     }
 }
Exemple #6
0
 private Direction getKeyCode(ClsSnake snake, ClsKeyCode key)
 {
     if (ClsInput.KeyPressed(key.UpCode) && snake.Direct != Direction.UP && snake.Direct != Direction.DOWN)
     {
         return(Direction.UP);
     }
     if (ClsInput.KeyPressed(key.DownCode) && snake.Direct != Direction.UP && snake.Direct != Direction.DOWN)
     {
         return(Direction.DOWN);
     }
     if (ClsInput.KeyPressed(key.LeftCode) && snake.Direct != Direction.LEFT && snake.Direct != Direction.RIGHT)
     {
         return(Direction.LEFT);
     }
     if (ClsInput.KeyPressed(key.RightCode) && snake.Direct != Direction.LEFT && snake.Direct != Direction.RIGHT)
     {
         return(Direction.RIGHT);
     }
     return(Direction.NONE);
 }
 private bool collideFood(ClsSnake snake, ClsFood food)
 {
     return(food.Coor.X == snake.Coor[0].X && food.Coor.Y == snake.Coor[0].Y);
 }
 private bool collideBorder(ClsSnake snake, int width, int height)
 {
     return(snake.Coor[0].X <0 || snake.Coor[0].X> width - 1 || snake.Coor[0].Y <0 || snake.Coor[0].Y> height - 1);
 }