C++贪吃蛇错误
#include <iostream>#include <cstdio>
#include <graphics.h>
#include <Windows.h>
#include <conio.h>
#include <time.h>
/********************************
* 初始化蛇 intiSnake
* 画蛇 drawSnake
* 移动蛇 moveSnake
* 按键处理 keyDown
* 初始化食物 initFood
* 画食物 drawFood
* 吃食物 eatFood
* 蛇什么时候死亡 SnakeDie
*********************************/
void initSnake();
void drawSnake();
void moveSnake();
void keyDown();
void initFood();
void drawFood();
void eatFood();
void snakeDie();
//void showGrade();
//坐标
struct point_XY {
int x;
int y;
};
//蛇属性
struct {
int num; //蛇的节数
point_XY snakeXY;//最多100节
char postion; //方向
int flag; //判断是否蛇死亡
}snake;
//食物的属性
struct {
point_XY foodXY; //食物坐标(X和Y)
int eatGrade; //吃掉食物所得分数
int flag; //食物是否存在
}food;
//枚举方向模拟键码值
enum movPostion {
right = 72, left = 75, down = 77, up = 80 //对应上下左右键的键码值
};
HWND hwnd;
int speed = 50;
int main() {
using namespace std;
srand((unsigned int)time(NULL));
hwnd = initgraph(640, 480);
setbkcolor(WHITE); //窗口颜色为白White
cleardevice(); //刷新
initSnake(); //创建蛇
snake.flag = 0;
initFood(); //创建食物
//showGrade(); //显示分数
while (true) {
cleardevice(); //刷新背景
drawSnake(); //画蛇
moveSnake(); //移动蛇
//snakeDie();
//if (snake.flag == 1) break;
if (food.flag != 1) initFood(); //初始化食物
if (food.flag == 1) drawFood(); //画食物
eatFood();
while (_kbhit()) {
keyDown(); //如果有键盘输入,检测输入
}
Sleep(speed); //间隔时间50ms
}
getchar();
return 0;
}
//初始化蛇
void initSnake() {
snake.num = 3; //初始化时为三节
snake.snakeXY.x = 0;
snake.snakeXY.y = 0;
snake.snakeXY.x = 10; //间隔10像素
snake.snakeXY.y = 0;
snake.snakeXY.x = 20;
snake.snakeXY.y = 0;
snake.postion = right; //初始化方向为右
food.flag = 0;
}
//画蛇
void drawSnake() {
for (int i = 0; i < snake.num; i++) {
setlinecolor(BLACK); //矩形边框颜色为黑Black
setfillcolor(BLUE); //矩形内部颜色为蓝blue
fillrectangle(snake.snakeXY.x, snake.snakeXY.y, snake.snakeXY.x + 10, snake.snakeXY.y + 10);
}
}
//移动蛇
void moveSnake() {
//除蛇第一节以外的移动坐标处理
for (int i = snake.num - 1; i > 0; i--) {
snake.snakeXY.x = snake.snakeXY.x;
snake.snakeXY.y = snake.snakeXY.y;
}
//第一节坐标处理
switch (snake.postion) {
case right:
snake.snakeXY.x += 10;
break;
case left:
snake.snakeXY.x -= 10;
break;
case down:
snake.snakeXY.y += 10;
break;
case up:
snake.snakeXY.y -= 10;
break;
default:
break;
}
}
//按键处理
void keyDown() {
char userKey;
userKey = _getch();
switch (userKey) {
case right:
if (snake.postion != down) snake.postion = up;
break;
case left:
if (snake.postion != right) snake.postion = left;
break;
case down:
if (snake.postion != left) snake.postion = right;
break;
case up:
if (snake.postion != up) snake.postion = down;
break;
case 112:
speed = 10;
break;
case 113:
speed = 20;
break;
case 114:
speed = 30;
break;
case 115:
speed = 40;
break;
case 116:
speed = 50;
break;
case 117:
speed = 60;
break;
case 118:
speed = 70;
break;
case 119:
speed = 80;
break;
case 120:
speed = 90;
break;
case 121:
speed = 100;
break;
case 122:
speed = 110;
break;
case 123:
speed = 120;
break;
default:
break;
}
}
//初始化食物
void initFood() {
food.foodXY.x = rand() % 65 * 10; //坐标一定要是10的倍数
food.foodXY.y = rand() % 48 * 10;
food.flag = 1;
for (int i = 0; i < snake.num; i++) {
//如果坐标重复,重新生成
if (food.foodXY.x == snake.snakeXY.x and food.foodXY.y == snake.snakeXY.y) {
food.foodXY.x = rand() % 65 * 10;
food.foodXY.y = rand() % 48 * 10;
}
}
}
//画食物
void drawFood() {
fillrectangle(food.foodXY.x, food.foodXY.y, food.foodXY.x + 10, food.foodXY.y + 10);
setlinecolor(BLACK); //矩形边框颜色为黑Black
setfillcolor(BLUE); //矩形内部颜色为蓝blue
}
//吃食物
void eatFood() {
//蛇变长了
if (snake.snakeXY.x == food.foodXY.x and snake.snakeXY.y == food.foodXY.y) {
snake.num++;
food.eatGrade += 10;
food.flag = 0;
//重新画食物
void drawFood();
}
}
//判断蛇死亡
void snakeDie() {
if (snake.snakeXY.x > 640 or snake.snakeXY.x < 0 or snake.snakeXY.y > 480 or snake.snakeXY.y < 0) {
outtextxy(200, 200, "你撞南墙了!");
MessageBox(hwnd, "GameOver!!!", "你撞墙了!!!", MB_OK);
snake.flag = 1; //蛇死亡,游戏结束
}
for (int i = 1; i < snake.num; i++) {
if (snake.snakeXY.x == snake.snakeXY.x and snake.snakeXY.y == snake.snakeXY.y) {
outtextxy(200, 200, "你撞自己了!");
MessageBox(hwnd, "GameOver!!!", "你自杀了!!!", MB_OK); //蛇死亡,游戏结束
snake.flag = 1; //蛇死亡,游戏结束
}
}
}
照视频写了一个C++ easyX 贪吃蛇,作了一点修改,不加死亡判断函数就正常,加了就直接显示死亡,改了好久都不行(头大),希望有dalao帮忙看看。
没 graphics.h,看不了。 永恒的蓝色梦想 发表于 2020-7-5 22:57
没 graphics.h,看不了。
就去easyX官网下一个就可以了呀 换一个不要 graphics.h的版本看看呗 jhanker 发表于 2020-7-6 08:45
换一个不要 graphics.h的版本看看呗
先把这个问题解决了再说吧,而且我是觉得EasyX挺好的,和我之前学的turtle有点像 livcui 发表于 2020-7-6 08:50
先把这个问题解决了再说吧,而且我是觉得EasyX挺好的,和我之前学的turtle有点像
我以前写的版本的代码参考看看
https://fishc.com.cn/thread-173032-1-1.html 初始蛇的坐标不对,改一下initSnake()函数,把3个点的坐标反一下,再试试正常不
void initSnake() {
snake.num = 3; //初始化时为三节
snake.snakeXY.x = 20;
snake.snakeXY.y = 0;
snake.snakeXY.x = 10;//间隔10像素
snake.snakeXY.y = 0;
snake.snakeXY.x = 0;
snake.snakeXY.y = 0;
snake.postion = right; //初始化方向为右
food.flag = 0;
} superbe 发表于 2020-7-6 10:07
初始蛇的坐标不对,改一下initSnake()函数,把3个点的坐标反一下,再试试正常不
谢了,一切正常了{:10_254:}
页:
[1]