//Hàm tạo mới trò chơi public void newGame() { hideButton(); // left = false; right = true; up = false; down = false; hide = 1;//biến này để check ẩn button lbInfor.Text = "";//gán thông tin ="" timerRun.Enabled = true;//bắt đầu time timer1.Enabled = true;//bắt đầu thời gian ch once = 0;//kiểm tra để vẽ con rắn bên phải lần đầu(trường hợp once=0 thì sẽ ko vẽ pause = 0; Score = 0; time = 0; thoiGian.Text = "Time: 0";//gán label thời gian ="" diemSo.Text = "Score: 0";//gán label điểm số ="" snake = new Snake(3,350,200); }
//Hàm tiếp tục chơi game public void continueGame() { //load thông tin từ file if (Check_Continue == true)//nếu đây là lần đầu vào game thì lấy thông tin từ file ra { string direction = readFile("checkRun.txt");//lấy hướng di chuyển lần cuối đã lưu trong file int X_Coordinate = Int16.Parse(readFile("xCoordinate.txt").ToString());//lấy tọa độ X lần cuối của rắn đã lưu trong file int Y_Coordinate = Int16.Parse(readFile("yCoordinate.txt").ToString());//lấy tọa độ Y lần cuối của rắn đã lưu trong file int Snake_Length = Int16.Parse(readFile("snakeLength.txt").ToString());//lấy độ dài của rắn lần cuối đã lưu trong file int SCORE = Int16.Parse(readFile("score.txt").ToString());//lấy điểm số đã lưu lần cuối trong file int TIME = Int16.Parse(readFile("time.txt").ToString());//lấy thời gian đã lưu lần cuối trong file if (direction == "left") { left = true; once = 0;//chỉ vẽ rắn một lần right = false; up = false; down = false; } else if (direction == "right") { right = true; once = 0;//chỉ vẽ rắn một lần left = false; up = false; down = false; } else if (direction == "up") { up = true; once = 0;//chỉ vẽ rắn một lần left = false; right = false; down = false; } else if (direction == "down") { down = true; once = 0;//chỉ vẽ rắn một lần left = false; right = false; up = false; } //khởi tạo rắn giống như lần chơi cuối snake = new Snake(Snake_Length, X_Coordinate, Y_Coordinate); Score = SCORE; diemSo.Text = "Score: " + Score.ToString(); foodCount = 0; time = TIME; thoiGian.Text = "Time: " + time.ToString(); } else//ngược lại thì lấy thông tin đang chơi { snake = new Snake(Int16.Parse(snakeLength), Int16.Parse(toaDoX), Int16.Parse(toaDoY)); if (checkRun == "left") { left = true; once = 0;//chỉ vẽ rắn một lần right = false; up = false; down = false; } else if (checkRun == "right") { right = true; once = 0;//chỉ vẽ rắn một lần left = false; up = false; down = false; } else if (checkRun == "up") { up = true; once = 0;//chỉ vẽ rắn một lần left = false; right = false; down = false; } else if (checkRun == "down") { down = true; once = 0;//chỉ vẽ rắn một lần left = false; right = false; up = false; } Score = Int16.Parse(saveScore); diemSo.Text = "Score: " + Score.ToString(); foodCount = 0; time = Int16.Parse(timePlay); thoiGian.Text = "Time: " + time.ToString(); } // playSelectSound(); timerRun.Enabled = true; timer1.Enabled = true; hideButton(); hide = 1;//hiện rắn vs food pauseCheck = true;// biến pause hoặt động escCheck = true;//ESC hoặt động hideContinueButton();//ẩn continue button }
//Hàm restart lại con rắn khi chết public void Restart() { string score = Score.ToString();//gán điểm của người chơi string playTime = time.ToString();//lưu thời gian chơi //Tiến hành lấy điểm số từ trong file data.txt và so sánh để chọn ra điểm cao int hightScore = Int16.Parse(readFile("data.txt").ToString()); //Tiến hành lưu điểm cao vào file data.txt if (Score > hightScore)//nếu điểm số hiện tại cao hơn điểm số trong file data.txt thì lưu vào { writeFile(score, @"data.txt"); } timerRun.Enabled = false; timer1.Enabled = false; //diemSo.Text = "0"; diemSo.Text = "Score: 0"; Score = 0; foodCount = 0; time = 0; thoiGian.Text = "Time: 0"; hide = 0;//ẩn rắn vs food pause = 1;//để khi nhắn lại phím cách sẽ play again lbInfor.Top = 90; lbInfor.Left = 300; lbInfor.Text = "Game Over!"+"\n"+"Score: "+score+"\n"+"Time: "+playTime+"\n"+"Level: 1"; checkContinue = false; right = false; left = false; down = false; up = false; playGameOverSound(); //Hiện button chơi lại khi thua ShowPlayAgainButton(); //Hiện button Main Menu ShowMainMenuButton(); snake = new Snake(3,350,200); pauseCheck = false;//ngường biến pause escCheck = false;//ESC ko hoặt động hideContinueButton();//ẩn continue button boomCheck = false;//boom ngừng rơi }