C++编程 ,定义交通类,实现简单的汽车移动

来源:互联网 发布:交换机telnet端口 编辑:程序博客网 时间:2024/04/27 14:16
#include <iostream>#include <Windows.h>#include <conio.h>using namespace std ;class Vehicle        //交通工具类Vehicle{public :    Vehicle(){x = 0 ; y = 0 ; }        //无参构造函数    Vehicle(int x,int y):x(x),y(y) {}        //含参构造函数    void Move(int newX,int newY) ;    void show() ;    int GetX(){return x ;}    int GetY(){return y ;}private :    int x ;    int y ;} ;Vehicle car(0,0) ;        //定义全局对象car,初始坐标在原点void Vehicle::Move(int newX,int newY)        //全局函数 Move  ,移动光标到指定坐标 ,同时更新成员变量x ,y{    //判断x轴坐标是否合理    if(newX>=71)        newX = 71 ;    else if(newX<=0)        newX = 0 ;    //判断y轴坐标是否合理    if(newY>=299)        newY = 299 ;    else if(newY<=0)        newY = 0 ;    //移动光标到指定坐标    CONSOLE_SCREEN_BUFFER_INFO    csbiInfo;                                HANDLE    hConsoleOut;    hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);    GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);    csbiInfo.dwCursorPosition.X = newX;                                        csbiInfo.dwCursorPosition.Y = newY;     x = newX ;        //同时更新成员变量x    y = newY ;        //同时更新成员变量y    SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition);   }void Vehicle::show()        //显示输出:表示汽车的五角星{    //显示符号,直观地看到在屏幕中的位置    //切换颜色,使得输出的界面比较和谐    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |FOREGROUND_INTENSITY | BACKGROUND_RED|FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);    cout<<"" ;        //输出符号    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |FOREGROUND_INTENSITY | FOREGROUND_RED|BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);    cout<<"("<<car.GetX()<<","<<car.GetY()<<")" ;    //输出当前位置的坐标信息    //切换颜色,换回去    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);}//主函数部分代码int main(){    //输出提示信息    MessageBox( NULL,(LPCWSTR)L"通过方向键可以移动五角星(代表交通工具)。当五角星在坐标轴中移动时,其后的括弧中会显示当前五角星的坐标信息 !", (LPCWSTR)L"温馨提示 !", MB_OK) ;    unsigned char Key ;            //储存捕获的键    car.show() ;    while(1)    {        Key = getch() ;        //捕获键位        if(Key==224)        //如果是否是方向键中的一个        {            Key = getch() ;    //继续判断具体是哪个方向键            switch(Key)            {            case 72 :        //up                system("cls") ;                 car.Move(car.GetX(),car.GetY()-1) ;                     car.show() ;                break ;            case 80 :        //down                system("cls") ;                 car.Move(car.GetX(),car.GetY()+1) ;                car.show() ;                break ;            case 75 :        //left                system("cls") ;                 car.Move(car.GetX()-1,car.GetY()) ;                    car.show() ;                break ;            case 77 :        //right                system("cls") ;                 car.Move(car.GetX()+1,car.GetY()) ;                car.show() ;                break ;            default :                system("cls") ;                 car.Move(car.GetX(),car.GetY()) ;                car.show() ;                break ;            }         }    }    return 0 ;}

 

0 0
原创粉丝点击