制作简单的蛇游戏
making simple snake game
我有问题,我正在用opencv制作一个简单的蛇游戏。现在我只想让我的蛇动,但它没有。我的代码怎么了?我认为我创建的snakemove()不适用于我的snake。我该怎么解决这个问题?提前谢谢。这是我的代码
#include<opencv2corecore.hpp>
#include<opencv2highguihighgui.hpp>
#include<opencv2imgproc.hpp>
#include<stdio.h>
#include<conio.h>
#include<iostream>
bool gameover;
using namespace cv;
const int width = 500;
const int height = 500;
int x, y;
enum eDirection
{
STOP = 0,
LEFT,
RIGHT,
UP,
DOWN
};
eDirection dir;
void Setup()
{
gameover = false;
dir = STOP;
x = width / 2;
y =height / 2;
}
void DrawAsnake(Mat &img)
{
const int HEAD_SIZE = 10;
const int BODY_SIZE = 10;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if (i == y && j == x)
{
circle(img, Point(x, y), HEAD_SIZE, Scalar(255, 0, 0), 2);
rectangle(img, Point(x - 2 * HEAD_SIZE, y), Point(x - HEAD_SIZE, y + HEAD_SIZE / 2), Scalar(255, 0, 0), 2);
}
}
}
}
void SnakeMove()
{
if (_kbhit())
{
switch (_getch())
{
case 'a'://75
dir = LEFT;
break;
case'w' ://72
dir = UP;
break;
case 'd'://77
dir = RIGHT;
break;
case 's'://80
dir = DOWN;
break;
}
}
}
void GameLogic()
{
switch (dir)
{
case LEFT:
x--;
break;
case RIGHT:
x--;
break;
case UP:
y--;
break;
case DOWN:
y++;
default:
break;
}
}
void main()
{
Mat img(500, 800, CV_32FC3);
Setup();
while(!gameover)
{
DrawAsnake(img);
SnakeMove();
GameLogic();
imshow("main window", img);
waitKey(20);
img = Mat::zeros(img.rows, img.cols, CV_32FC3);
imshow("main window", img);
x++;
}
}
Try this.
void main()
{
Mat img(500, 800, CV_32FC3);
Setup();
while(!gameover)
{
imshow("main window", img);
DrawAsnake(img);
SnakeMove();
GameLogic();
waitKey(20);
img = Mat::zeros(img.rows, img.cols, CV_32FC3);
imshow("main window", img);
x++;
}
}
相关文章:
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 简单的游戏数据库问题
- X 变量在最后一次尝试简单猜谜游戏时发生变化,C++
- 简单的比赛游戏与 C++ 中的动态数组
- 简单的游戏,同时循环和检查字符串
- 生活的游戏邻居检查(更简单的方法可能吗?)
- 如何简单地管理我的游戏状态?C++
- 循环麻烦.C 简单游戏
- 简单的单词猜测游戏
- 我简单的 c++ 文本游戏不起作用。我正在使用随机数生成,我很困惑
- OpenGL - 简单3D游戏的低FPS
- 简单的战舰游戏建议
- 我需要哪种语言/课程来制作简单的纸牌游戏
- 如何创建一个简单的侧滚动游戏
- 为什么c++游戏与unity3d相比如此简单
- 变量的值在使用C++的简单文本 RPG 游戏中不会更改
- 制作简单的蛇游戏
- 一个简单的猜谜游戏,继续使用除声明变量Y/N之外的任何其他字母
- C++游戏编程帮助.超级简单
- 如何简单检查游戏是否已重新编译