Coder Social home page Coder Social logo

greedysnake's Introduction

GreedySnake

贪吃蛇——C++练手

参考:https://github.com/silence1772/GreedySnake

更新:

  • Pointgetx()gety()Print()
  • tools的设置窗口大小SetWindowsSize(),设置光标位置SetCursorPosition()

踩坑:

  • 创建tools.cpp的时候没有勾选Debug、Release

    error:ld returned 1 exit status

    在这里插入图片描述

    可以通过右键tools.cpp,点击properties,点build,再勾选

总结:

  • 知道了COODR,表示一个字符在控制台屏幕上的坐标

  • SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);

    • SetConsoleCursorPosition:设置控制台光标位置
    • GetStdHandle:获取指定的标准设备的句柄,return HANDLE(句柄)
  • 控制台API函数

更新

  • Point的清除功能Clear()
  • Snake.cpp
    • InitSnake():初始化打印snake
    • move():根据方向移动
    • ChangeDirection():通过键盘的上下左右改变蛇的移动方向

踩坑

  • derection前后拼写错误看了几十分钟。。。
  • 不知道●是两个字符,写成了‘●‘,改成“●”就好了
  • queue不能用auto访问:忘记了queue封装好了不能访问内部参数的,改成deque

总结

  • kbhit():判断键盘是否有输入的函数,有输入返回1,否则返回0

  • getch():与getchar()功能一样,但是不在控制台上显示输入的字符

  • 键盘↑↓←→的输入,是两个字符,所以需要先判断第一个字符是不是-32。

    		c = getch();
            switch(c){
            case -32:{
                //键盘上的上下左右输入进来有两个字节,第一个字节的十进制表示为-32
                c = getch();
                switch(c){
                case 72:
                    if(direction != Direction::DOWN)
                        direction = Direction::UP;
                    break;
                case 80:
                    if(direction != Direction::UP)
                        direction = Direction::DOWN;
                    break;
                case 75:
                    if(direction != Direction::RIGHT)
                        direction = Direction::LEFT;
                    break;
                case 77:
                    if(direction != Direction::LEFT)
                        direction = Direction::RIGHT;
                    break;
                default:
                    break;
                }
                return true;
            }
            case 27:    //ESC的键值
                return false;
            default:
                return true;
  • 成果:

    在这里插入图片描述

2020.6.19第三次更新及踩坑

更新:

  • 实现了food.h类:随机生成一个food
  • 实现了snakefood的操作,吃掉之后snake长度+1,并重新生成一个food
  • 实现了map.h类:一个简易的地图边框,正在用set实现蛇撞墙结束游戏的功能

踩坑:

  • 构造/析构函数声明了必须要实现,大括号内为空也行。

  • 不对类成员元素造成改变的函数,最好在声明的小括号后面/实现的大括号前面加上const,例如下面,

    // Point.h中
    void Clear() const;
    int getx() const;
    int gety() const;
    void ChangePosition(const int x, const int y);
    void Print(std::string s);
    
    void Print(std::string s) const;
    bool operator==(const Point& p) const;
    bool operator<(const Point& p) const;
    virtual ~Point();
    
    // Point.cpp中
    bool Point::operator==(const Point& p) const
    {
        return (p.getx() == x)&&(p.gety() == y);
    }
    int Point::gety() const
    {
        return y;
    }
    int Point::getx() const
    {
        return x;
    }

    不然可能会出现类似的错误

    passing ‘const *’ as ‘this’ argument of ‘*’ discards qualifiers

    这种错误是指该变量是const的,在下面的变量的使用中,所有涉及到的该变量的函数,也要是const的,避免函数改变那个变量。例如:

    std::set<Point> p(1,1);
    for(auto& i: p)		//这里用auto取出来的i,就是const的
    {
    	i.gety();		//所以这里使用到的gety(),也就必须是const的,不然报错
    }					//这个for循环中使用到的所有i的函数,都要是const的,像上面的gety()一样
  • 在使用自己定义的类的set的时候,需要重载运算符<

  • 在本文件使用其他文件的全局变量时,需要在本文件中用extern声明一次

  • 如果在snake.h类中,有函数要传递class food的参数,则要在class snake:前对food声明一次,不需要#include "food.h"

总结

  • 其实很多东西在书上都学过,但是用起来还是漏洞百出

  • 成果

    在这里插入图片描述

greedysnake's People

Contributors

okjiang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.