简体   繁体   English

GetAsyncKeyState() 不工作,制作控制台游戏

[英]GetAsyncKeyState() not working, making a console game

I'm trying to make a c++ console game - there is a map with '#' as a border and a '@' itside which can be moved via keyboard.我正在尝试制作一个 C++ 控制台游戏 - 有一张地图,边界为“#”,其边为“@”,可以通过键盘移动。 But when a press the button, nothing happens... Can't figure out what might have caused this behaviour.但是当按下按钮时,什么也没有发生......无法弄清楚是什么导致了这种行为。

      #include <iostream>
      #include <windows.h>

      using namespace std;

        char Map[10][10]={  "#########", 
                               "#       #",  
                                 "#       #",
                                "#       #",
                                 "#       #",
                                 "#       #",
                                "#       #",
                                 "#       #",
                                "#@      #",
                                 "#########"};

     int Gamespeed=100;
     int lvl=0;
     bool stopgame=false;


  int main()
  {
    while (stopgame==false)
    {
            system("cls");
            for (int y=0; y<10; y++)
            {
                cout<<Map[y]<<endl; //rows
    }


    for (int y=0; y<10; y++)   //rows
        for (int x=0; x<10; x++)   //columns
                {
                        switch (Map[y][x])
                        {
                        case '@':
                            {  cout<<"@ here";
                                if (GetAsyncKeyState(VK_UP)!=0)
                                {   cout<<"Key up";
                                    int y2=y-1;
                                    switch (Map[y2][x])
                                    {
                                    case ' ':
                                        {
                                            Map[y][x]=' ';
                                            y--;
                                            Map[y][x]='@';
                                        }
                                        break;
                                    }
                                }

                                if (GetAsyncKeyState(VK_DOWN)!=0)
                                {
                                            switch (Map[y+1][x])
                                                {
                                                case ' ':
                                                    {
                                                            Map[y][x]=' ';
                                                            y++;
                                                            Map[y][x]='@';
                                                    }
                                                    break;
                                                }

                                }

                                if (GetAsyncKeyState(VK_RIGHT)!=0)
                                {
                                    switch (Map[y][x+1])
                                    {
                                    case ' ':
                                        {
                                            Map[y][x]=' ';
                                            x++;
                                            Map[y][x]='@';
                                        }
                                        break;
                                    }
                                }

                                if (GetAsyncKeyState(VK_LEFT)!=0)
                                {
                                    switch (Map[y][x-1])
                                    {
                                    case ' ':
                                        {
                                            Map[y][x]=' ';
                                            x--;
                                            Map[y][x]='@';
                                        }
                                        break;
                                    }
                                }
                            }
                        }


                }






return 0;
 }

Oh, bad formatting and code style, but for me it is working if you add close brace for while cycle:哦,糟糕的格式和代码风格,但对我来说,如果你为while循环添加大括号,它就可以工作:

int main()
{
    while (stopgame == false)
    {
        // your code here
    } // <- you don't have this brace
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM