DEV C++地图中的游戏机RPG

问题描述 投票:-1回答:1

我想做一个开发C++ RPG游戏。我卡在了地图上,它是一个双元数组,尺寸是以 "打破 "一条线之前有多少个字符可以容纳来衡量的。地图打印出来就好了,但我想让玩家用方向键移动,Heres what ive got so far (doesnt work):

#include <iostream>
#include <string>
#include <conio.h>
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77

void MapPrint(int StartX, int StartY){
    int Map[12][80]={0};
    Map[StartX][StartY]=1;
    for (int x=0; x<12; x++) 
    { 
        for (int y=0; y<80; y++) 
        { 
            if(Map[x][y]==0){
                std::cout<<"=";
            }
            if (y==80){
                std::cout<<"\n";
                continue;
            }
            if (Map[x][y]==1){
                std::cout<<"#";
                continue;
            }
        }   
    }
}
int main(){
    int Map[12][80]={0};
    int StartX,StartY;
    Map[StartX][StartY]=1;
    int c = 0;
    StartX=6;
    StartY=40;
    MapPrint(StartX,StartY);
    while(1)
    {

        c=0;

        switch((c=getch())) {
        case KEY_UP:
            system("CLS");
            Map[StartX][StartY]=0;
            Map[StartX][StartY+1]=1;
            std::cout<<StartY; //remains of a fix attempt
            MapPrint(StartX,StartY);
        case KEY_DOWN:
            system("CLS");
            Map[StartX][StartY]=0;
            Map[StartX][StartY-1]=1;
            MapPrint(StartX,StartY);
        case KEY_LEFT:
            system("CLS");
            Map[StartX][StartY]=0;
            Map[StartX-1][StartY]=1;
            MapPrint(StartX,StartY);
        case KEY_RIGHT:
            system("CLS");
            Map[StartX][StartY]=0;
            Map[StartX+1][StartY]=1;
            MapPrint(StartX,StartY);
        }

    }
    return 0;
}`
c++ c++11
1个回答
0
投票

我修改了你的代码,做得更好了。如果你有什么要问的,请在下面留言。

#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>

#define XSIZE 80
#define YSIZE 20

using namespace std;

int c_x=XSIZE/2,c_y=YSIZE/2; //the player will be in the middle of the map
int direction=1;//direction of the player
bool gameOver=false;
int tim=2000,tt;//that's how we set "speed"

void MapPrint(int newX, int newY)
{
    //if the new position of the player if out of the map
    //the game if over
    if(c_x<0 || c_x>XSIZE || c_y<0 ||c_y>YSIZE)
    {
        system("CLS");
        cout<<"GAME OVER!";
        gameOver=true;
        return;
    }

    //printing the map, without using an twodimesnional array

    for (int y=0; y<YSIZE; y++)
    {
        for (int x=0; x<XSIZE; x++)
        {
            if(newX==x && newY==y)cout<<'#';
            else cout<<' ';
        }
        cout<<'\n';
    }
}

int main()
{

    char c;
    MapPrint(c_x,c_y);

    while(!gameOver)
    {
        //_kbhit() tell us if any key is pressed
        if(_kbhit())
        {

            c=_getch();
            //setting the direction according to key pressed
            if(c=='w')direction=1;
            else if(c=='d')direction=2;
            else if(c=='s')direction=3;
            else if(c=='a')direction=4;

        }

        tt++;
        if(tt>=tim)
        {
            if(direction==1)
            {
                system("CLS");
                c_y--;  // we go up, so y position of the player decrements
                MapPrint(c_x,c_y);
            }
            else if(direction==3)
            {
                system("CLS");
                c_y++;  // we go down, so y position of the player increments
                MapPrint(c_x,c_y);
            }
            else if(direction==4)
            {
                system("CLS");
                c_x--;  // we go to the left, so x position of the player decrements
                MapPrint(c_x,c_y);
            }
            else if(direction==2)
            {
                system("CLS");
                c_x++;  // we go to the right, so x position of the player increments
                MapPrint(c_x,c_y);
            }
            tt=0;
        }


    }
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.