注册 登录
编程论坛 C++教室

怎么用C++控制一个移动字符的方向?

汐杨 发布于 2011-08-06 22:43, 918 次点击
求解,求代码!
4 回复
#2
诸葛修勤2011-08-07 00:14

自己根据需求 想怎么着都可以做到
#3
诸葛修勤2011-08-07 00:58
程序代码:
#include <windows.h>
#include <conio.h>
#include <iostream>
using namespace std;

#define UP        'w'
#define RIGHT    'a'
#define LEFT    'd'
#define DOWN    's'

class Point
{
public:
    Point(COORD coord, char Char):ShowChar(Char)
    {
        Pos = coord;

        Move();
    }
    Point(char Char, SHORT x = 0, SHORT y = 0):ShowChar(Char)
    {
        Pos.X = x;
        Pos.Y = y;

        Move();
    }

    void Up(){Pos.Y = (25+Pos.Y-1)%25;}
    void Down(){Pos.Y = (Pos.Y+1)%25;}
    void Left(){Pos.X = (80+Pos.X-1)%80;}
    void Right(){Pos.X = (Pos.X+1)%80;}
    void Move();
    void Clear()
    {
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
        cout << ' ';
    }
    void Print()
    {   
        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
        cout << ShowChar;
    }
private:
    COORD Pos;//当前字符显示的位置
    const char ShowChar;//显示的字符
};

void Point::Move()
{
    char get = 0;
    Print();
    while (1)
    {
        get = getch();
        switch (get)
        {
        case UP:
            Clear();
            Up();
            Print();
            break;
        case DOWN:
            Clear();
            Down();
            Print();
            break;
        case RIGHT:
            Clear();
            Left();
            Print();
            break;
        case LEFT:
            Clear();
            Right();
            Print();
            break;
        default:break;
        }
        get = 0;
    }
}

Point p_('Y');

int main(void)
{
    return 0;
}
#4
汐杨2011-08-07 20:17
谢谢咯~慢慢看!
#5
fenyujinian2012-10-18 18:38
回复 3楼 诸葛修勤
为什么那里是25、下边是80?
1