Beispiel #1
0
 public Form1()
 {   
    // this.FormBorderStyle
     InitializeComponent();
     food = new Food(randFood,20,20);
     bigFood = new Food(randBigFood,50,50);
     boom = new Boom(randBoom);
     MaximizeBox = false;//không cho phóng to form
 
 }
Beispiel #2
0
        //Khai báo timer để cho rắn di chuyển
        private void timerRun_Tick(object sender, EventArgs e)
        {
            if (countRandBoom ==2)
            {
                countRandBoom = 0;
                fakeCountRandBoom =2 ;
               // boom.boomLocation(randBoom);
                boom = new Boom(randBoom);
            }
            if (fakeCountRandBoom ==2)
            {
                boom.moveDown();
            }
         
           
            boom.moveDown();
            if (down == true)//nhấn phím down
            {
                snake.moveDown();
               
            }
            if (up == true)//nhấn phím up
            {
                snake.moveUp();
            }
            if (left == true)//nhấn phím left
            {
                snake.moveLeft();

            }
            if (right == true) //nhấn phím right              
            {
                snake.moveRight();
            }
            //Kiểm tra nếu rắn ăn thực phẩm
            for (int i = 0; i < snake.SnakeRec.Length; i++)
            {
                if (snake.SnakeRec[i].IntersectsWith(food.foodRec))//nếu snake va chạm với food
                {  //Play music khi ăn mồi nhỏ
                    playSmallFoodSound();
                    //Nếu như va chạm thì sẽ gán foodCount=0 để cho mất bigFood
                   // foodCount = fakeFoodCount;
                    Score += 5;
                  //  diemSo.Text = Score.ToString();
                    diemSo.Text = "Score: " + Score.ToString();
                    snake.growSnake();//phát triển rắn
                    //Tạo ra thực phẩm lớn nếu con rắn ăn đủ 5 thực phẩm  nhỏ
                    fakeFoodCount++;
                    check = false;
                  //thay đổi vị trí food
                    if (fakeFoodCount ==3)
                    {
                        //check = true;
                        foodCount =fakeFoodCount;
                         bigFood.foodLocation(randBigFood);
                         fakeFoodCount = 0;
                         bigFoodTime = 0;
                    }
                    
                        food.foodLocation(randFood);                                         
                }


                if (snake.SnakeRec[i].IntersectsWith(bigFood.foodRec))//nếu snake va chạm với bigFood
                {
                    if (check == true)
                    {
                    
                        playBigFoodSound();//play âm thanh khi ăn bigFood
                        Score += 10;
                        //diemSo.Text = Score.ToString();
                        diemSo.Text = "Score: "+Score.ToString();
                        foodCount = fakeFoodCount;
                        check = false;
                    }

                }

                //nếu rắn va chạm với vật cản

                if (snake.SnakeRec[i].IntersectsWith(boom.boomRec[0]))
                {
                  /*  if (check == true)
                    {

                        playBigFoodSound();//play âm thanh khi ăn bigFood
                        Score += 10;
                        //diemSo.Text = Score.ToString();
                        diemSo.Text = "Score: " + Score.ToString();
                        foodCount = fakeFoodCount;
                        check = false;
                    }
                   */
                    playDiedSound();//Play sound khi chết
                    Thread.Sleep(2000);
                    Restart();

                }
              
             
            }
            //nếu sau 10s mà ko ăn thực phẩm lớn thì sẽ biến mất
            if (bigFoodTime == 10)
            {
                foodCount = 0;
                bigFoodTime = 0;
                fakeFoodCount = 0;
                check = false;
            }

                //Kiểm tra va chạm
                collission();
            this.Invalidate();//cập nhật lại con rắn
        }