马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#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]; //最多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[0].x = 0;
snake.snakeXY[0].y = 0;
snake.snakeXY[1].x = 10; //间隔10像素
snake.snakeXY[1].y = 0;
snake.snakeXY[2].x = 20;
snake.snakeXY[2].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[i].x, snake.snakeXY[i].y, snake.snakeXY[i].x + 10, snake.snakeXY[i].y + 10);
}
}
//移动蛇
void moveSnake() {
//除蛇第一节以外的移动坐标处理
for (int i = snake.num - 1; i > 0; i--) {
snake.snakeXY[i].x = snake.snakeXY[i - 1].x;
snake.snakeXY[i].y = snake.snakeXY[i - 1].y;
}
//第一节坐标处理
switch (snake.postion) {
case right:
snake.snakeXY[0].x += 10;
break;
case left:
snake.snakeXY[0].x -= 10;
break;
case down:
snake.snakeXY[0].y += 10;
break;
case up:
snake.snakeXY[0].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[i].x and food.foodXY.y == snake.snakeXY[i].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[0].x == food.foodXY.x and snake.snakeXY[0].y == food.foodXY.y) {
snake.num++;
food.eatGrade += 10;
food.flag = 0;
//重新画食物
void drawFood();
}
}
//判断蛇死亡
void snakeDie() {
if (snake.snakeXY[0].x > 640 or snake.snakeXY[0].x < 0 or snake.snakeXY[0].y > 480 or snake.snakeXY[0].y < 0) {
outtextxy(200, 200, "你撞南墙了!");
MessageBox(hwnd, "GameOver!!!", "你撞墙了!!!", MB_OK);
snake.flag = 1; //蛇死亡,游戏结束
}
for (int i = 1; i < snake.num; i++) {
if (snake.snakeXY[0].x == snake.snakeXY[i].x and snake.snakeXY[0].y == snake.snakeXY[i].y) {
outtextxy(200, 200, "你撞自己了!");
MessageBox(hwnd, "GameOver!!!", "你自杀了!!!", MB_OK); //蛇死亡,游戏结束
snake.flag = 1; //蛇死亡,游戏结束
}
}
}
照视频写了一个C++ easyX 贪吃蛇,作了一点修改,不加死亡判断函数就正常,加了就直接显示死亡,改了好久都不行(头大),希望有dalao帮忙看看。
初始蛇的坐标不对,改一下initSnake()函数,把3个点的坐标反一下,再试试正常不 void initSnake() {
snake.num = 3; //初始化时为三节
snake.snakeXY[0].x = 20;
snake.snakeXY[0].y = 0;
snake.snakeXY[1].x = 10; //间隔10像素
snake.snakeXY[1].y = 0;
snake.snakeXY[2].x = 0;
snake.snakeXY[2].y = 0;
snake.postion = right; //初始化方向为右
food.flag = 0;
}
|