c语言课程设计之贪吃蛇代码及思路 c语言课程设计报告之贪吃蛇

您所在的位置:网站首页 酒红色羽绒服配什么裤子好看 c语言课程设计之贪吃蛇代码及思路 c语言课程设计报告之贪吃蛇

c语言课程设计之贪吃蛇代码及思路 c语言课程设计报告之贪吃蛇

2024-05-09 19:44| 来源: 网络整理| 查看: 265

 

原文作者:aircraft

原文地址:https://www.cnblogs.com/DOMLX/p/8846529.html

 

注:本文档需与c语言课程设计之贪吃蛇文档配套使用。c语言实现贪吃蛇代码可随意下载 c语言课程设计报告也可随意下载1.本代码在VS2013下可正常运行,其他版本需根据版本需要进行调试。2.代码在Csnake-Csnake里,想直接打开整个工程直接进入Cnake,点击Csnake.sln打开  注意这里可能需要使用相关的IDE环境才能打开,没有相关环境的话,复制代码自己创建个工程就行了很简单的。3.代码注释为全英文注释,若有疑问可借助百度翻译,或者看文档报告。

 

贪吃蛇代码文档链接可直接百度云下载:链接:https://pan.baidu.com/s/1vnXkR8d9-R5lAYpxzcMYxw 密码:i1q6

 

后面在下的博客可能还会有大学常见的课设代码和报告发布,比如c++MFC课设,数据库课设,WEB课设,网络编程课设,linux课设,数据结构课设,python课设,matlab课设,,,想要的话就关注在下的博客吧嘿嘿-----

 

 

说到大学都要做的课程设计和报告我还是决定分享出来,为什么呢?(大学就是给这些无聊的课程,无聊报告和画图耽误的,才会有那么多大学生找不到工作)节省大家时间啦 嘿嘿。。。。。。请叫我雷锋先生。。。

说到贪吃蛇还是直接上代码吧,解释最后来。。。。。

首先是main.cpp文件,控制调用函数。

#include "snake.h" //Transposed structure object extern struct Food food; extern struct Snake snake; //main function int main() { ret = menu(); if (ret == 1) { while (finish) { initMap(); //Initialization map while (TRUE) { updataFood(); //updata food getSpeed(); //speed moveSnake(); //move snake Sleep(snake.speed); //Control the speed of the snake printFont(FONT_WINDOW_STARTX, FONT_WINDOW_STARTY, snake.speed); //Right interface if (!(isAlive())) break; } if (finish) //Death or customs clearance { system("cls"); gotoxy(22, 5); printf("Game Over!"); gotoxy(24, 5); printf("按(1)键退出游戏,按其他任意键重玩"); if (getch() == 49) break; system("cls"); } else { system("cls"); gotoxy(22, 5); printf("恭喜你通关了"); gotoxy(24, 5); printf("按(1)键退出游戏,按其他任意键重玩"); if (getch() == 49) break; system("cls"); } } } return 0; }

 

snake.h 主要声明函数和定义变量。

#pragma once #ifndef _SNAKE_H_ #define _SNAKE_H_ #include #include #include #include #include #pragma warning(disable: 4996) #define FONT_WINDOW_STARTY 41 //define font window #define FONT_WINDOW_STARTX 2 #define FRAME_HEIGHT 20 //define map size #define FRAME_WIDTH 40 #define UP 'w' //define operate key #define DOWN 's' #define LEFT 'a' #define RIGHT 'd' #define SNAKE_LEN 50 #define ADD 3 static int i, j, k; static char ch = UP; //initial first direction static char Direction = UP; static int grow = 0; //flag: if snake grow static int ret = 0; static int finish = 1; //Structure declaration struct Food{ int x; int y; }; struct Snake{ int x[SNAKE_LEN]; int y[SNAKE_LEN]; int len; int speed; }; //snake[0] is head //Initialization map void initMap(void); //updataFood void updataFood(void); //moveSnake void moveSnake(void); //Judge whether the snake is alive or not int isAlive(void); //get snake speed void getSpeed(void); //move cursor void gotoxy(int x, int y); //Right interface void printFont(int x, int y, int speed); //menu int menu(); #endif

snake.cpp文件 主要实现函数功能。

#include "snake.h" //Initialization of structure struct Food food; struct Snake snake; //menu int menu() { gotoxy(14, 45); printf("欢迎来到贪吃蛇小游戏...."); gotoxy(16, 45); printf("开始游戏请按(1)"); gotoxy(18, 45); printf("按其他任意键退出游戏QAQ"); int ch = getch(); while (TRUE) { if (ch == 49) //The ASCII value of 1 { ret = 1; break; } else { break; } } system("cls"); return ret; } //initialize Map void initMap(void) { ch = UP; Direction = UP; //initial food srand(time(NULL)); //Set random number seed food.x = rand() % (FRAME_HEIGHT - 2) + 1; food.y = rand() % (FRAME_WIDTH - 2) + 1; gotoxy(food.x, food.y); printf("!"); //initial snake snake.x[0] = FRAME_HEIGHT / 2; snake.y[0] = FRAME_WIDTH / 2; gotoxy(snake.x[0], snake.y[0]); printf("@"); snake.len = 3; snake.speed = 200; for (k = 1; k0; k--) { snake.x[k] = snake.x[k - 1]; snake.y[k] = snake.y[k - 1]; } if (kbhit()) { ch = getch(); if (ch == ' ') //Pause or continue by space while (getch() != ' '){}; if (ch != UP && ch != DOWN && ch != LEFT && ch != RIGHT) //Other keys to prevent interference { ch = Direction; goto sport; } if (Direction != ch) { if ((Direction == UP &&ch != DOWN) || (Direction == DOWN &&ch != UP) || (Direction == LEFT && ch != RIGHT) || (Direction == RIGHT && ch != LEFT)) //Prevent reverse movement { Direction = ch; goto sport; } else { ch = Direction; goto sport; } } else { goto sport; } } else { sport: switch (ch) //Move the snake's head { case UP: snake.x[0]--; break; case DOWN: snake.x[0]++; break; case LEFT: snake.y[0]--; break; case RIGHT: snake.y[0]++; break; default: break; } } gotoxy(snake.x[0], snake.y[0]); printf("@"); //print the snake's head grow = 0; gotoxy(FRAME_HEIGHT, 0); } //Judge whether the snake is alive or not int isAlive(void) { if (snake.x[0] == 0 || snake.x[0] == FRAME_HEIGHT - 1 || snake.y[0] == FRAME_WIDTH - 1 || snake.y[0] == 0) //When you touch a wall or eat your own body, you die return 0; for (k = 1; k


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3