Chào mọi người .
Hiện tại em đang thử làm game snake huyền thoại trên console. Em biết console không thích hợp với việc làm game nhưng hiện tại em muốn làm để chạy được trên console rồi mới thêm thư viện đồ họa vào mặc dù mỗi khi cls
nó nháy khá khó chịu
Hiện em đang làm phần ăn thức ăn thì không hiểu sao chương trình chạy lại bị crash
Ý tưởng của em cho phần ăn thức ăn là:
- Tăng độ dài của rắn.
- Lấy tọa độ của thức ăn làm tọa độ đầu rắn, tọa độ các đốt lưng của rắn thì lấy của đốt trước đó.
- Random tọa độ thức ăn mới.
Tuy nhiên sau khi chạy xong hàm eat_food
rồi tiếp tục đến khi quay lại vòng lặp mới thì lại phát sinh lỗi và bị crash:
Em mới chỉ làm bản test nên code không hoàn chỉnh lắm. Khi demo được trên console em sẽ chia file rồi thêm các thứ vào sau.
Code của em
#include <iostream>
#include <conio.h>
#include <ctime>
#include <windows.h>
#include <cstdbool>
#include <vector>
#include <cmath>
using namespace std;
class Point{
public:
vector<int> x;
vector<int> y;
int lenght;
Point(){
//Phần đầu
x.push_back(10 );
y.push_back(10);
//Phần thân
x.push_back(9);
y.push_back(10);
//Cái đuôi
x.push_back(8);
y.push_back(10);
//Độ dài
lenght = 3;
}
};
class Food{
public:
int x ;
int y;
Food(){
x = rand() % 20 ;
y = rand() % 20;
}
};
bool checkPoint(vector<int> x , vector<int> y , int i , int j){//check vi tri cua snake
for(unsigned int k = 0 ; k < x.size() ; k++){
if(x[k] == j && y[k] == i) return true;
}
return false;
}
void draw(vector<int> x , vector <int> y, Food food){//draw screen
for(int i = 0 ; i < 20 ; i ++){
cout << '|';
for(int j = 0 ; j < 20 ; j++ ){
if(checkPoint(x , y , i , j)) cout << " A ";
else if (food.x == j && food.y == i) cout <<" O ";
else cout << " ";
}
cout << '|';
cout << endl;
}
}
void move_when_controled(Point &snake){
for(int i = snake.x.size() ; i > 0 ; i-- ){
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
}
}
void eat_food(Food &food , Point &snake){
if(snake.x[0] == food.x && snake.y[0] == food.y ){
snake.x.push_back(1);//Tang do dai
snake.y.push_back(1);
for(int i = snake.x.size() ; i > 0 ; i-- ){
snake.x[i] = snake.x[i - 1];
snake.y[i] = snake.y[i - 1];
}
snake.x[0] = food.x;
snake.y[0] = food.y;
food.x = rand() % 20;
food.y = rand() % 20;
snake.lenght++;
}
}
void control(Point &snake){
char a = getch();
system("cls");
switch(a){
case 72:{//di len
move_when_controled(snake);
snake.y[0]--;
if(snake.y[0] < 0) snake.y[0] = 19;
break;
}
case 80:{//di xuong
move_when_controled(snake);
snake.y[0]++;
if(snake.y[0] > 19) snake.y[0] = 0;
break;
}
case 77:{//sang phai
move_when_controled(snake);
snake.x[0]++;
if(snake.x[0] > 19) snake.x[0] = 0;
break;
}
case 75:{//sang trai
move_when_controled(snake);
snake.x[0]--;
if(snake.x[0] < 0) snake.x[0] = 19;
}
}
}
void auto_move(){
}
void Game_over(){
//Snake eats itself
}
void MENU(){
}
int main()
{
srand(time(0));
Point snake;
Food food;
while(1){
control(snake);
eat_food(food , snake);//???
draw(snake.x , snake.y , food);
cout << snake.lenght;
fflush(stdin);
}
return 0;
}
Mong mọi người có thể chỉ lỗi giúp em. Cảm ơn mọi người đã đọc ạ.