Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C++ to realize the game of gluttonous snake

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use C++ to achieve the game of gluttonous Snake. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

In 1976, the Gremlin platform launched a classic arcade game Blockade. In the game, two players control a character to move on the screen and build a fence where they pass. The character can only make a 90-degree turn to the left and right, and the goal of the game is to make sure that the opponent hits the screen or fence first. It sounds a little complicated, but it looks like this: basically, the two gluttonous snakes that grow up with each step are doomed, and what players have to do is to avoid hitting obstacles and growing bodies. For more photos and videos, please see the introduction of GamesDBase. Blockade is very popular, and similar games appeared on early game consoles and computers, such as Atari 2600, TRS-80, Apple 2 and so on. But what really makes this form of game popular all over the world is the gluttonous snake game-Snake, which is introduced to the world with Nokia phones 21 years later.

Let's write our own game "gluttonous Snake" today.

In fact, I wanted to share this project with you a long time ago, but in view of the simplicity of this project, I didn't think it was necessary at that time, but recently, a small friend sent me a private letter saying that I wanted the project source code of the gluttonous snake, so I came to meet the requirements of my friends. Everyone must be careful and eager to learn!

All right, let's start now!

First of all, or our old friend structure (our food and snakes)

Typedef struct pointXY {int x; int y;} MYPOINT;HWND hwnd = NULL; / / Snake struct mySnake {int num; MYPOINT xy [MAX]; char postion; / / indicates direction, marked} snake; / / Food struct myFood {MYPOINT foodxy; int flag; int eatGrade;} food

The next step is initialization, which is also our old buddy, and then there is the function of drawing the snake and the food.

/ / 1. Initialize snake void initSnake () {snake.xy [2] .x = 0; snake.xy [2] .y = 0; snake.xy [1] .x = 10; snake.xy [1] .y = 0; snake.xy [0] .x = 20; snake.xy [0] .y = 0; snake.num = 3; snake.postion = right; food.flag = 0 } / / 2. Draw snake void drawSnake () {for (int I = 0; I)

< snake.num; i++) { setlinecolor(RED); setfillcolor(GREEN); fillrectangle(snake.xy[i].x, snake.xy[i].y, snake.xy[i].x + 10, snake.xy[i].y + 10); }}//初始化食物void initFood(){ //0,63 630 640 //0,47 470 480 food.foodxy.x = rand() % 64 * 10; //只能是10的整数倍,蛇头才能对齐食物 food.foodxy.y = rand() % 48 * 10; food.flag = 1; //食物不能出现在蛇身上 for (int i = 0; i < snake.num; i++) { if (food.foodxy.x == snake.xy[i].x&&food.foodxy.y == snake.xy[i].y) { food.foodxy.x = rand() % 64 * 10; //只能是10的整数倍,蛇头才能对齐食物 food.foodxy.y = rand() % 48 * 10; } }}//绘制食物void drawFood(){ fillrectangle(food.foodxy.x, food.foodxy.y, food.foodxy.x + 10, food.foodxy.y + 10);} 现在是蛇的移动函数,蛇要怎么走,千万不要一下头朝前,一下屁股朝前,不要闹笑话了 //3.移动蛇void moveSnake(){ //除了第一节之外,后面的坐标都是前一节坐标 for (int i = snake.num - 1; i >

0; iMub -) {snake.xy.x = snake.xy [i1] .x; snake.xy.y = snake.xy [i1] .y } / / move switch (snake.postion) {case right: snake.xy [0] .x + = 10; break; case left: snake.xy [0] .x-= 10 according to the direction sign Case down: snake.xy [0] .y + = 10; break; case up: snake.xy [0] .y-= 10; break;}}

The focus of the project, how to control our snake, before it was mouse control, then how to write keyboard control? check it out.

/ / 4. Button users: void keyDown () {static char userkey='\ 0users; / / VC getch does not need to be underlined / / userkey=_getch () / / invisible input / / generate your own key switch (snake.postion) {case right: case left: if (food.foodxy.y > = snake.xy [0] .y) {userkey = 80 } else if (food.foodxy.y= snake.xy [0] .x) {userkey = 77;} else if (food.foodxy.x

< snake.xy[0].x) { userkey = 75; } break; } switch (userkey) { case 'w': case 'W': case 72: //上 if (snake.postion != down) { //蛇往上走,你要排除掉蛇本来是朝下 snake.postion = up; } break; case 's': case 'S': case 80: //下 if (snake.postion != up) { snake.postion = down; } break; case 'a': case 'A': case 75: //左 if (snake.postion != right) { snake.postion = left; } break; case 'd': case 'D': case 77: //右 if (snake.postion != left) { snake.postion = right; } }} 接下来是我们的老判断函数了,蛇吃到食物会怎么样以及怎么判断蛇的死亡,理清逻辑 //蛇吃食物void eatFood(){ if (snake.xy[0].x == food.foodxy.x && snake.xy[0].y == food.foodxy.y) { snake.num++; food.eatGrade += 10; food.flag = 0; }}//蛇死亡判断int snakeDie(){ //显示分数 char grade[100] = { 0 }; sprintf(grade, "%d", food.eatGrade); setbkmode(TRANSPARENT); settextcolor(RED); outtextxy(580, 20, "分数:"); //loadimage outtextxy(620, 20, grade); // //判断蛇是否死亡 //撞墙 if (snake.xy[0].x >

| | snake.xy [0] .x480 | | snake.xy [0] .y < 0) {MessageBox (hwnd, "Game over!", "hit the Wall!", 0); return 1;} / / hit yourself for (int I = 1; I < snake.num) Snake.xy +) {if (snake.xy [0] .x = = snake.xx] .x & & snake.xy [0] .y = = snake.xy.y) {MessageBox (hwnd, "Game over!", "hit yourself!", 0); return 1 } return 0;}

Finally, there is our main function, which I will not talk about.

Int main () {srand ((unsigned int) time (NULL)); hwnd=initgraph (640,480); setbkcolor (WHITE); cleardevice (); initSnake (); while (1) {cleardevice () / / wipe out the path if (food.flag = = 0) {initFood ();} drawFood (); drawSnake (); if (snakeDie ()) {break } eatFood (); moveSnake (); Sleep (100); / / Control speed / / while (_ kbhit ()) / / kbhit () there is a keystroke operation that returns non-zero / / {/ / keyDown () / /} keyDown ();} closegraph (); printf ("GameOverqualified customers!"); system ("pause"); return 0;} Thank you for reading! This is the end of the article on "how to realize the Snake Game with C++". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report