博主晚上闲来无事,用opencv写了一个贪吃蛇程序,以前是用dos界面写的,不太好理解,所以这次把opencv代码贴出来,供大家娱乐,哈哈。
一、测试软硬件
测试硬件:PC-Windows
测试软件:VS2013
二、测试可执行程序
可执行程序下载地址:https://download.csdn.net/download/samylee/10802993
三、操作说明
界面1:选择困难度:1(简单)、2(适中)、3(困难)
界面2:操作说明:A(向左)、W(向上)、D(向右)、S(向下)
四、代码
头文件:snake.h
#ifndef SNAKE_H
#define SNAKE_H
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
class Snake
{
public:
Snake();
void Snake_Start();
private:
vector snake_info;
int snake_direction;
Mat snake_bg;
Mat snake_fg;
Rect snake_fg_rect;
Mat snake_square;
Rect snake_start;
Point snake_max;
Rect eat_foot;
bool eat_;
int difficult;
Rect Set_Random_Point();
int Snake_Move(Rect &random_snake);
};
#endif // !SNAKE_H
源文件:snake.cpp
#include "snake.h"
Snake::Snake()
{
snake_bg = Mat::zeros(Size(640, 480), CV_8UC3);
putText(snake_bg, "Score: 0", Point(150, 30), 0, 0.8, Scalar(0, 255, 0), 2);
putText(snake_bg, "Operation: A(left) W(up) D(right) S(down)", Point(60, 460), 0, 0.8, Scalar(0, 255, 0), 2);
snake_fg = Mat(Size(520, 380), CV_8UC3, Scalar(255,255,255));
snake_fg_rect.x = 60;
snake_fg_rect.y = 50;
snake_fg_rect.width = 520;
snake_fg_rect.height = 380;
snake_square = Mat(Size(10, 10), CV_8UC3, Scalar(255, 0, 0));
Mat middle_color(Size(8, 8), CV_8UC3, Scalar(0, 0, 255));
middle_color.copyTo(snake_square(Rect(1, 1, 8, 8)));
eat_ = false;
snake_direction = 100;
snake_max.x = snake_fg.cols - snake_square.cols;
snake_max.y = snake_fg.rows - snake_square.rows;
snake_start.x = 100;
snake_start.y = 100;
snake_start.width = snake_square.cols;
snake_start.height = snake_square.rows;
snake_info.push_back(snake_start);
snake_start.x = 90;
snake_start.y = 100;
snake_start.width = snake_square.cols;
snake_start.height = snake_square.rows;
snake_info.push_back(snake_start);
difficult = 200;//easy
}
Rect Snake::Set_Random_Point()
{
Rect snake_point;
srand(time(0));
snake_point.x = rand() % (snake_max.x / snake_square.cols);
snake_point.y = rand() % (snake_max.y / snake_square.rows);
snake_point.x *= snake_square.cols;
snake_point.y *= snake_square.rows;
snake_point.width = snake_square.cols;
snake_point.height = snake_square.rows;
return snake_point;
}
int Snake::Snake_Move(Rect &random_snake)
{
while (true)
{
Rect insert_snake;
switch (snake_direction)
{
case 97:
snake_info.pop_back();
insert_snake = snake_info[0];
insert_snake.x = snake_info[0].x - snake_square.cols;
snake_info.insert(snake_info.begin(), insert_snake);
break;
case 119:
snake_info.pop_back();
insert_snake = snake_info[0];
insert_snake.y = snake_info[0].y - snake_square.rows;
snake_info.insert(snake_info.begin(), insert_snake);
break;
case 100:
snake_info.pop_back();
insert_snake = snake_info[0];
insert_snake.x = snake_info[0].x + snake_square.cols;
snake_info.insert(snake_info.begin(), insert_snake);
break;
case 115:
snake_info.pop_back();
insert_snake = snake_info[0];
insert_snake.y = snake_info[0].y + snake_square.rows;
snake_info.insert(snake_info.begin(), insert_snake);
break;
default:
break;
}
if (eat_)
{
snake_info.push_back(eat_foot);
eat_ = false;
return 1;
}
if (snake_info[0].x < 0 || snake_info[0].y < 0 || snake_info[0].x > snake_max.x || snake_info[0].y > snake_max.y)
return -1;
for (int i = 3; i < snake_info.size(); i++)
{
if (snake_info[0] == snake_info[i])
return -2;
}
if (snake_info[0] == random_snake)
{
eat_foot = snake_info[snake_info.size() - 1];
eat_ = true;
}
Mat snake_fg_move = snake_fg.clone();
for (int i = 0; i < snake_info.size(); i++)
{
snake_square.copyTo(snake_fg_move(snake_info[i]));
snake_fg_move.copyTo(snake_bg(snake_fg_rect));
}
imshow("SNAKE", snake_bg);
char wait = waitKey(difficult);
switch (wait)
{
case 97:
if (snake_direction != 100)
snake_direction = wait;
break;
case 119:
if (snake_direction != 115)
snake_direction = wait;
break;
case 100:
if (snake_direction != 97)
snake_direction = wait;
break;
case 115:
if (snake_direction != 119)
snake_direction = wait;
break;
default:
break;
}
}
}
void Snake::Snake_Start()
{
Mat difficult_choice = Mat::zeros(Size(640, 480), CV_8UC3);
putText(difficult_choice, "SNAKE", Point(170, 170), 4, 3, Scalar(0, 255, 255), 2);
putText(difficult_choice, "Choice difficulty", Point(230, 220), 0, 0.8, Scalar(0, 255, 0), 2);
putText(difficult_choice, "1: easy", Point(230, 270), 0, 0.8, Scalar(0, 255, 0), 2);
putText(difficult_choice, "2: moderation", Point(230, 320), 0, 0.8, Scalar(0, 255, 0), 2);
putText(difficult_choice, "3: difficulty", Point(230, 370), 0, 0.8, Scalar(0, 255, 0), 2);
imshow("SNAKE", difficult_choice);
char wait = waitKey(0);
if (wait == 49)
difficult = 200;
else if (wait == 50)
difficult = 100;
else if (wait == 51)
difficult = 50;
else
difficult = 200;
switch (difficult)
{
case 200:
putText(snake_bg, "Phase: Easy", Point(320, 30), 0, 0.8, Scalar(0, 255, 0), 2);
break;
case 100:
putText(snake_bg, "Phase: moderation", Point(320, 30), 0, 0.8, Scalar(0, 255, 0), 2);
break;
case 50:
putText(snake_bg, "Phase: difficulty", Point(320, 30), 0, 0.8, Scalar(0, 255, 0), 2);
break;
default:
break;
}
int score_count = 0;
while (true)
{
Rect random_snake = Set_Random_Point();
snake_square.copyTo(snake_fg(random_snake));
int move_check = Snake_Move(random_snake);
if (move_check == -1 || move_check == -2)
break;
else if (move_check == 1)
{
putText(snake_bg, "Score: " + to_string(score_count), Point(150, 30), 0, 0.8, Scalar(0, 0, 0), 2);
score_count++;
putText(snake_bg, "Score: " + to_string(score_count), Point(150, 30), 0, 0.8, Scalar(0, 255, 0), 2);
}
Mat snake_square_merge = Mat(Size(10, 10), CV_8UC3, Scalar(255, 255, 255));
snake_square_merge.copyTo(snake_fg(random_snake));
}
putText(snake_bg, "GAME OVER!", Point(240, 240), 0, 1, Scalar(0, 0, 255), 4);
imshow("SNAKE", snake_bg);
waitKey(0);
}
主函数:main.cpp
#include "snake.h"
int main()
{
Snake snake_get;
snake_get.Snake_Start();
return 0;
}
打完收工!
任何问题请加唯一QQ2258205918(名称samylee)!