From 046b9e0cb5a616e5d9f302a6faf97235eff1f351 Mon Sep 17 00:00:00 2001 From: pjvrmisk8 Date: Mon, 17 Jun 2024 17:45:29 +0800 Subject: [PATCH] ADD file via upload --- 贪吃蛇.cpp | 282 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 贪吃蛇.cpp diff --git a/贪吃蛇.cpp b/贪吃蛇.cpp new file mode 100644 index 0000000..0b6cf35 --- /dev/null +++ b/贪吃蛇.cpp @@ -0,0 +1,282 @@ +#include +using namespace std; +#include //ͼοͷļ +#include +#include + +#define WIDTH 1040 //峡С +#define HEIGHT 640 +#define SIZE 20 //ʳԼߵĴС һӵĴС +#define UP 1 //ߵij +#define DOWN -1 +#define LEFT 2 +#define RIGHT -2 +#define MAX 2000 //ߵ󳤶 + + +typedef struct +{ + int x; + int y; +}SnakeNode; + +SnakeNode temp[MAX]; //һ洢ԭλ +int speed = 150; //ߵٶ + +//ߵ +class Snake +{ + friend class Food; +public: + Snake(); //ʼ + void Move(); //ƶ + void Draw(); // + bool Eat(Food food); //ʳ + bool Defeat(); //ʧж +private: + int dirt; // + int length; // + SnakeNode node[MAX]; //ߵ +}; + +//ʳ +class Food +{ + friend class Snake; +public: + Food(Snake snake); //ʳʼ + void Draw(); //ʳ +private: + int x, y; // +}; + +//ߵijʼ +Snake::Snake() +{ + dirt = RIGHT; + length = 3; + //±0λΪߵͷ + for (int i = 0; i < length; i++) + { + node[i].x = 60 - ((i + 1) * SIZE);//20 + node[i].y = 0; + } //̶տʼߵλ + //һʼĻϽdz +} + +//ƶ +void Snake::Move() +{ + for (int i = 0; i < length; i++) //ԭ߽㿽һ + { + temp[i].x = node[i].x; + temp[i].y = node[i].y; + } + int status = 0; //жǷת򰴼 + if (dirt == RIGHT) //ĸڵķ򣬷ֱжǷת򣬲ܷ + { + //жǷת + if (GetAsyncKeyState('W') && status == 0) //׽Ϊweasyx жת + { + dirt = UP; //ȥif(dirt==up)else + status = 1; + } + else if (GetAsyncKeyState('S') && status == 0) + { + dirt = DOWN; + status = 1; + } + else + { + node[0].x = node[0].x + SIZE; + } + } + if (dirt == DOWN) + { + //жǷת + if (GetAsyncKeyState('A') && status == 0) + { + dirt = LEFT; + status = 1; + } + else if (GetAsyncKeyState('D') && status == 0) + { + node[0].x = node[0].x + SIZE; + dirt = RIGHT; + status = 1; + } + else + { + node[0].y = node[0].y + SIZE; + } + } + if (dirt == LEFT) + { + //жǷת + if (GetAsyncKeyState('W') && status == 0) + { + dirt = UP; + status = 1; + } + else if (GetAsyncKeyState('S') && status == 0) + { + node[0].y = node[0].y + SIZE; + dirt = DOWN; + status = 1; + } + else + { + node[0].x -= SIZE; + } + } + if (dirt == UP) + { + //жǷת + if (GetAsyncKeyState('A') && status == 0) + { + node[0].x = node[0].x - SIZE; + dirt = LEFT; + status = 1; + } + else if (GetAsyncKeyState('D') && status == 0) + { + node[0].x = node[0].x + SIZE; + dirt = RIGHT; + status = 1; + } + else + { + node[0].y = node[0].y - SIZE; + //ı + } + } + + //ƶ + for (int i = 1; i < length; i++) + { + node[i].x = temp[i - 1].x; + node[i].y = temp[i - 1].y; + } + Sleep(speed); //ôʱ䣬ͣһ +} + +// +void Snake::Draw() +{ + cleardevice(); //ԭȵĻͼ + srand((unsigned)time(NULL)); //ɫsetfillcolor)ʱ + for (int i = 0; i < length; i++) + { + setfillcolor(RGB(rand() % 256, rand() % 256, rand() % 256)); //ɫ + fillrectangle(node[i].x, node[i].y, node[i].x + SIZE, node[i].y + SIZE); + } +} + +//ʳ +bool Snake::Eat(Food food) +{ + if (food.x == node[0].x && food.y == node[0].y) //ͷûʳ + { + if (node[length - 1].x - node[length - 2].x == 0 && node[length - 1].y - node[length - 2].y == -20) + { + //жβ͵ķλùϵµβͺ + length++; + node[length - 1].x = node[length - 2].x; + node[length - 1].y = node[length - 2].y - SIZE; + } + else if (node[length - 1].x - node[length - 2].x == 0 && node[length - 1].y - node[length - 2].y == 20) + { + length++; + node[length - 1].x = node[length - 2].x; + node[length - 1].y = node[length - 2].y + SIZE; + } + else if (node[length - 1].x - node[length - 2].x == 20 && node[length - 1].y - node[length - 2].y == 0) + { + length++; + node[length - 1].x = node[length - 2].x + SIZE; + node[length - 1].y = node[length - 2].y; + } + else if (node[length - 1].x - node[length - 2].x == -20 && node[length - 1].y - node[length - 2].y == 0) + { + length++; + node[length - 1].x = node[length - 2].x - SIZE; + node[length - 1].y = node[length - 2].y; + } + + return true; + } + return false; +} + +//ʧж +bool Snake::Defeat() +{ + //߽ + if (node[0].x < 0 || node[0].x >= WIDTH || node[0].y < 0 || node[0].y >= HEIGHT) + return true; + + //Լ + for (int i = 1; i < length; i++) + { + if (node[0].x == node[i].x && node[0].y == node[i].y) + return true; + } + return false; +} + +//ʳijʼ +Food::Food(Snake snake) +{ +table: + srand((unsigned)time(NULL)); //ӣ + x = (rand() % (WIDTH / SIZE)) * SIZE; + y = (rand() % (HEIGHT / SIZE)) * SIZE; + + for (int i = 0; i < snake.length; i++) + { + if (snake.node[i].x == x && snake.node[i].y == y) + goto table; //ʳϣ³ʼ + } +} + +//ʳ +void Food::Draw() +{ + setfillcolor(GREEN); //ʳɫ + fillrectangle(x, y, x + SIZE, y + SIZE); //ʳΪΣĸꡣ +} + +int main() +{ + initgraph(WIDTH, HEIGHT); + Snake snake; + +table: + Food food(snake); + while (1) + { + BeginBatchDraw(); + FlushBatchDraw(); //˫壬ֹĻһһ + snake.Draw(); + food.Draw(); + FlushBatchDraw(); //˫壬ֹĻһһ + EndBatchDraw(); + /*˫壬ֹĻһһ*/ + if (snake.Eat(food)) + { + goto table; //Եʳһʳ + } + if (snake.Defeat()) + { + break; + } + snake.Move(); + } + + HWND window = GetHWnd(); //ʾϷʧ + SetWindowText(window, L"ʾ"); //޸Ĵڱ⣬óʾ + MessageBox(window, L"Ϸʧ", L"ʾ", MB_OKCANCEL); + //ʾʧϢ MB_OKCANCELȷȡ + + return 0; +} \ No newline at end of file