Snake.h
1 #pragma once 2 #include <stdio.h> 3 #include <graphics.h> 4 #include <mmsystem.h> 5 #include <stdlib.h> 6 #include <time.h> 7 #pragma comment(lib,"winmm.lib") 8 9 #define WINDOW_WIDTH 640//窗口的宽 10 #define WINDOW_HEIGHT 640//窗口的高 11 #define MAXNUM 100//蛇的最大节数 12 #define SNAKESIZE 10 //蛇的大小 13 14 typedef enum CH{ up = 72, down = 80, left = 75, right = 77 }CH; 15 typedef struct Coor 16 { 17 int x; 18 int y; 19 }COOR; 20 //蛇的类型,当前有多少节,蛇的方向,蛇的坐标 21 typedef struct Snake 22 { 23 COOR sCor[MAXNUM];//蛇的坐标(包含100节) 24 int n;//当前的节数 25 CH ch;//蛇的方向 26 }SNAKE; 27 28 typedef struct Food 29 { 30 Coor fCor; 31 int isEat; 32 }FOOD; 33 34 SNAKE snake;//把蛇定义出来 35 FOOD food; 36 37 void GameInit(); 38 void SnakeMove(); 39 void SnakeDraw(); 40 void SnakeChange(); 41 void FoodDraw(); 42 void FoodCoor(); 43 void FoodEat(); 44 BOOL SnakeBreak();
main.cpp
/********************************************************** 文件名称:main.cpp 文件描述:C语言+图形界面库实现人工智能版的贪吃蛇 编译环境:VS2013 最后修改: <2017年1月15日> <创建工程> <池国维> ***********************************************************/ #include "Snake.h" #include "conio.h" //主函数 int main() { initgraph(WINDOW_HEIGHT,WINDOW_WIDTH);//初始化图形界面(窗口) GameInit(); while (1) { //如果没有键盘按下 while (!_kbhit()) { if (SnakeBreak()) { MessageBoxA(0, "游戏结束", "提示", 1); break; } cleardevice(); if (food.isEat == 0) { FoodCoor(); } FoodDraw(); FoodEat(); SnakeMove(); Sleep(100); } SnakeChange(); } getchar(); //卡屏 closegraph(); return 0; } void GameInit() { setbkcolor(LIGHTGRAY); //播放音乐 mciSendString(L"open ./甩葱歌.mp3 alias backmusic", 0, 0, 0); mciSendString(L"play backmusic repeat", 0, 0, 0); //初始化一条蛇 rectangle(0, 0, SNAKESIZE, SNAKESIZE); snake.ch = down;//方向 snake.n = 1;//节数 snake.sCor[0].x = 0;//第一节蛇的坐标 snake.sCor[0].y = 0; food.isEat = 0; //被吃掉 } //让蛇移动 void SnakeMove() { //不断交换相邻节的坐标 for (int i = snake.n - 1; i > 0; i--) { snake.sCor[i].x = snake.sCor[i - 1].x; snake.sCor[i].y = snake.sCor[i - 1].y; } switch (snake.ch) { case up: snake.sCor[0].y -= SNAKESIZE; break; case down: snake.sCor[0].y += SNAKESIZE; break; case left: snake.sCor[0].x -= SNAKESIZE; break; case right: snake.sCor[0].x += SNAKESIZE; break; } //蛇的绘制 SnakeDraw(); } //蛇的绘制 void SnakeDraw() { for (int i = 0; i < snake.n; i++) { rectangle(snake.sCor[i].x, snake.sCor[i].y, snake.sCor[i].x + SNAKESIZE, snake.sCor[i].y + SNAKESIZE); } } //改变方向 void SnakeChange() { char move; move = _getch(); //getchar()显示回文 enter键完成 getch()不显示回文 接受键盘的字符 switch (move) { case up: if (snake.ch != down) snake.ch = up; break; case down: if (snake.ch != up) snake.ch = down; break; case left: if (snake.ch != right) snake.ch = left; break; case right: if (snake.ch != left) snake.ch = right; break; } } //画食物 void FoodDraw() { roundrect(food.fCor.x, food.fCor.y, food.fCor.x + SNAKESIZE, food.fCor.y + SNAKESIZE, 10, 10); } //得到食物的坐标 void FoodCoor() { food.fCor.x = rand() % (WINDOW_WIDTH / SNAKESIZE) * SNAKESIZE; food.fCor.y = rand() % (WINDOW_HEIGHT / SNAKESIZE) * SNAKESIZE; food.isEat = 1; } //判断食物 void FoodEat() { if (snake.sCor[0].x == food.fCor.x && snake.sCor[0].y == food.fCor.y) { snake.n++; food.isEat = 0; } } //撞墙判断 //撞到自己 for去遍历数组 判定蛇头的坐标有没有和蛇的身上某一节蛇身重合 BOOL SnakeBreak() { if (snake.sCor[0].x<0 || snake.sCor[0].x > WINDOW_WIDTH || snake.sCor[0].y<0 || snake.sCor[0].y > WINDOW_HEIGHT) { //结束 //MessageBoxA(0, "游戏结束", "提示",1); return TRUE; } //撞到自己 for (int i = snake.n - 1; i > 0; i--) { if (snake.sCor[0].x == snake.sCor[i].x && snake.sCor[0].y == snake.sCor[i].y) { return TRUE; //MessageBoxA(0, "游戏结束", "提示", 1); } } return FALSE; }
原文地址:https://www.cnblogs.com/xiaochi/p/8295952.html
时间: 2024-11-13 11:15:02