C++:经典控制台游戏贪吃蛇 起始动画 改写

C++:控制台贪吃蛇 起始动画 改写

动画效果

在这里插入图片描述

贪吃蛇 起始动画

在这里插入图片描述

原工程代码

这篇博客写的:https://blog.csdn.net/silence1772/article/details/55005008
很详细,适合学习。
参考到的文件是point.h、tools.h、startinterface.h、point.cpp、tools.cpp、startinterface.cpp、main.cpp

改写

startaction.h头文件。这里没有像原工程,将函数声明和分开写,而是声明和定义写在了这个startaction.h文件中。需要打印的图标应事先定义好其起始坐标。使用原工程的部分实现自己需要的效果。

#ifndef STRATACTION_H
#define STRATACTION_H

#include <deque>
#include <vector>
#include "point.h"
#include<windows.h>

class Startaction
{
    
    
public:
    Startaction() : speed(35) {
    
    //speed=35;
        dot.emplace_back(Point(-25, 11));//图标的各点坐标
        dot.emplace_back(Point(-25, 10));
        dot.emplace_back(Point(-25, 9));
        dot.emplace_back(Point(-25, 7));
        dot.emplace_back(Point(-25, 6));
        dot.emplace_back(Point(-25, 5));
        
        dot.emplace_back(Point(-24, 11));
        dot.emplace_back(Point(-24, 8));
        dot.emplace_back(Point(-24, 5));

        dot.emplace_back(Point(-23, 11));
        dot.emplace_back(Point(-23, 8));
        dot.emplace_back(Point(-23, 5));

        dot.emplace_back(Point(-22, 10));
        dot.emplace_back(Point(-22, 9));
        dot.emplace_back(Point(-22, 8));
        dot.emplace_back(Point(-22, 7));
        dot.emplace_back(Point(-22, 6));

        dot.emplace_back(Point(-21, 10));
        dot.emplace_back(Point(-21, 9));
        dot.emplace_back(Point(-21, 8));
        dot.emplace_back(Point(-21, 7));
        dot.emplace_back(Point(-21, 6));

        dot.emplace_back(Point(-20, 11));
        dot.emplace_back(Point(-20, 8));
        dot.emplace_back(Point(-20, 5));

        dot.emplace_back(Point(-19, 11));
        dot.emplace_back(Point(-19, 8));
        dot.emplace_back(Point(-19, 5));

        dot.emplace_back(Point(-18, 11));
        dot.emplace_back(Point(-18, 10));
        dot.emplace_back(Point(-18, 9));
        dot.emplace_back(Point(-18, 7));
        dot.emplace_back(Point(-18, 6));
        dot.emplace_back(Point(-18, 5));

    }
    void PrintThird();
    void PrintText();
    void ClearText();
    void Action();
private:
    std::vector<Point> dot;//开始动画中的点
    int speed;//动画刷新移动的速度
};

void Startaction::PrintThird()
{
    
        
    while ( dot.back().GetX() < 23)//23是希望图标最后停止的位置
    {
    
    
        ClearText();//清除已有文字
        PrintText();//绘制更新位置后的文字
        Sleep(speed);//休眠时间,单位是ms
    }
}void Startaction::PrintText()//打印SNAKE
{
    
    
    for (auto& point : dot)//c++11新版本"for循环的使用"和"auto的使用"
    {
    
    
        if (point.GetX() >= 0)
            point.Print();
    }
}void Startaction::ClearText()//清除snake
{
    
    
    for (auto& point : dot)
    {
    
    
        if (point.GetX() >= 0)
            point.Clear();
        point.ChangePosition(point.GetX() + 1, point.GetY());//将最后清除坐标的 x+1,y不变 作为当前坐标
    }
}
void Startaction::Action()//行为
{
    
    
    PrintThird();
}
#endif // STRATACIION_H

point.h头文件

#ifndef POINT_H
#define POINT_H
#include <windows.h>
#include <stdio.h>
class Point
{
    
    
public:
	Point() {
    
    };
	Point(const int x, const int y) :x(x), y(y) {
    
    };
	/*
	Point(const int x, const int y) :x(x), y(y) {};此处定义方式相当于
	Point(const int x, const int y) 
		{x=x ;y=y;} 
	*/
	void Print();
	void Clear();
	void ChangePosition(const int x, const int y);
	int GetX() {
    
     return this->x; }
	int GetY() {
    
     return this->y; }
private:
	int x, y;
};

void Point::ChangePosition(const int x, const int y)
{
    
    
	this->x = x;
	this->y = y;
}

void SetWindowSize(int cols, int lines)//设置窗口大小
{
    
    
	system("title 贪吃蛇");//设置窗口标题
	char cmd[30];
	sprintf_s(cmd, "mode con cols=%d lines=%d", cols * 2, lines);//一个图形■占两个字符,故宽度乘以2
	system(cmd);//system(mode con cols=88 lines=88)设置窗口宽度和高度
}

void SetCursorPosition(const int x, const int y)//设置光标位置
{
    
    
	COORD position;
	position.X = x * 2;
	position.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
}

void SetColor(int colorID)//设置文本颜色
{
    
    
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colorID);
}

void SetBackColor()//设置文本背景色
{
    
    
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
		FOREGROUND_BLUE |
		BACKGROUND_BLUE |
		BACKGROUND_GREEN |
		BACKGROUND_RED);
}

void Point::Print()
{
    
    
	SetCursorPosition(x, y);
	std::cout << "\002";//笑脸符号的
}
void Point::Clear()
{
    
    
	SetCursorPosition(x, y);
	std::cout << " ";
}
#endif

程序入口文件test.cpp

#include <iostream>
#include"point.h"
#include"startaction.h"
using namespace std;
int main()
{
    
    
	Startaction obj1;
	Point obj2;
	obj1.PrintThird();
    SetCursorPosition(13, 13);
    std::cout << "Press any key to start... "<<endl;
    SetCursorPosition(13, 14);
    system("pause");
}

以上。

猜你喜欢

转载自blog.csdn.net/liangzixx/article/details/106925003