Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 键码不工作_C#_Visual Studio 2010_Random - Fatal编程技术网

C# 键码不工作

C# 键码不工作,c#,visual-studio-2010,random,C#,Visual Studio 2010,Random,我正在制作一个有一个数组的游戏,它代表一个岛屿,这个数组应该在每次开始游戏时随机生成(我将在游戏结束时生成程序生成的部分)。此外,如果你点击箭头或wasd,它会让你看到岛上你看不到的部分。但我有一个问题:当我点击这个键时,什么也没发生 这是运动的代码(在三个不同的类上): //类形式 私人游戏=新游戏(); 公共整数x=0; 公共整数y=0; 公共游戏窗口() { KeyPreview=true; KeyDown+=新的KeyEventHandler(GameWindow\u KeyDown);

我正在制作一个有一个数组的游戏,它代表一个岛屿,这个数组应该在每次开始游戏时随机生成(我将在游戏结束时生成程序生成的部分)。此外,如果你点击箭头或wasd,它会让你看到岛上你看不到的部分。但我有一个问题:当我点击这个键时,什么也没发生

这是运动的代码(在三个不同的类上):

//类形式
私人游戏=新游戏();
公共整数x=0;
公共整数y=0;
公共游戏窗口()
{
KeyPreview=true;
KeyDown+=新的KeyEventHandler(GameWindow\u KeyDown);
初始化组件();
}
void GameWindow_KeyDown(对象发送器,KeyEventArgs e)
{
if(e.KeyCode==Keys.W)
{
y=-1;
}
else if(e.KeyCode==Keys.A)
{
x=-1;
}
else if(e.KeyCode==Keys.S)
{
y=1;
}
else if(e.KeyCode==Keys.D)
{
x=1;
}
游戏。移动(x,y);
}
私有void panel 1_Paint(对象发送器,PaintEventArgs e)
{
Graphics g=panel1.CreateGraphics();
游戏开始图(g,100);
}
私有无效游戏窗口\u FormClosing(对象发送方,FormClosingEventArgs e)
{
游戏。停止();
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
x=1;
y=1;
game.Move(x,y);//我放了这个来看看它是否有效,但它仍然不起作用
}
//班级游戏
公共无效移动(整数x,整数y)
{
如果(x=-1&&gEngine.smallestXTileOnScreen==0){}
如果(x==1&&gEngine.smallestXTileOnScreen==size-25){}
如果(y==1&&gEngine.smallestYTileOnScreen==0){}
如果(y==1&&gEngine.smallestYTileOnScreen==size-15){}
其他的
{
gEngine.smallestXTileOnScreen+=x;
gEngine.smallestYTileOnScreen+=y;
gEngine.render();
}
}
//类石墨烯
公共无效呈现()
{
拉手。清晰(颜色:蓝色);
对于(int i=0;i<25;i++)
{
对于(int j=0;j<15;j++)
{
prop=世界[i+smallestXTileOnScreen,j+smallestYTileOnScreen];
开关(道具)
{
案例0:
drawHandle.DrawImage(sea,i*50,j*50,50,50);
打破
案例1:
drawHandle.DrawImage(普通,i*50,j*50,50,50);
打破
案例2:
drawHandle.DrawImage(山,i*50,j*50,50,50);
打破
案例3:
drawHandle.DrawImage(valley,i*50,j*50,50,50);
打破
违约:
drawHandle.DrawImage(sea,i*50,j*50,50,50);
打破
}
}
}
Form Debug=new Form();//注意,我在这里放了一个表单,如果它被显示,
Debug.Show();//然后调用了render()。它不显示。
}

编辑:我把第一堂课的全部代码都放进去了,也许它能帮你。。。我不知道该怎么办,我是Visual Studio的新手:/

嗯,出于某种原因,我的Visual Studio C#2010版本似乎有点问题。在进行调试之前,我必须转到\bin\Debug文件夹并清空它,它可以使用普通代码+keyPreview=true。。。希望这只是一个bug:///p>
world[i,j]=r.Next(0,3)你真的认为这会有多随机?你所有的
random
都有相同的种子。在begging中创建随机实例,在循环之外。@Scott,你只读了部分问题,不是吗?当然新窗口不会显示,原因与你的KeyDown事件从不触发相同。您无法确保已调度Windows通知。使用Application.Run()和调用移动/渲染的16毫秒计时器,或者在游戏循环中使用Application.DoEvents()。谷歌“windows窗体游戏循环”了解更多信息。
 //class Form
 private Game game = new Game();
    public int x = 0;
    public int y = 0;

    public GameWindow()
    {
        KeyPreview = true;
        KeyDown += new KeyEventHandler(GameWindow_KeyDown);
        InitializeComponent();
    }

    void GameWindow_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.W)
        {
            y = -1;
        }
        else if (e.KeyCode == Keys.A)
        {
            x = -1;
        }
        else if (e.KeyCode == Keys.S)
        {
            y = 1;
        }
        else if (e.KeyCode == Keys.D)
        {
            x = 1;
        }
        game.Move(x, y);
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = panel1.CreateGraphics();
        game.startGraphics(g, 100);
    }

    private void GameWindow_FormClosing(object sender, FormClosingEventArgs e)
    {
        game.Stop();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        x = 1;
        y = 1;
        game.Move(x, y); // I put this to see if it works, but it still does nothing
    }
// class Game
public void Move(int x, int y)
    {
        if (x == -1 && gEngine.smallestXTileOnScreen == 0) { }
        else if (x == 1 && gEngine.smallestXTileOnScreen == size - 25) { }
        else if (y == -1 && gEngine.smallestYTileOnScreen == 0) { }
        else if (y == 1 && gEngine.smallestYTileOnScreen == size - 15) { }
        else
        {
            gEngine.smallestXTileOnScreen += x;
            gEngine.smallestYTileOnScreen += y;
            gEngine.render();
        }
    }
//class GraphEngine
public void render()
    {
        drawHandle.Clear(Color.Blue);
        for (int i = 0; i < 25; i++)
        {
            for (int j = 0; j < 15; j++)
            {
                prop = world[i + smallestXTileOnScreen , j + smallestYTileOnScreen];
                switch (prop)
                {
                    case 0:
                        drawHandle.DrawImage(sea, i * 50, j * 50, 50, 50);
                        break;
                    case 1:
                        drawHandle.DrawImage(plain, i * 50, j * 50, 50, 50);
                        break;
                    case 2:
                        drawHandle.DrawImage(mountain, i * 50, j * 50, 50, 50);
                        break;
                    case 3:
                        drawHandle.DrawImage(valley, i * 50, j * 50, 50, 50);
                        break;
                    default:
                        drawHandle.DrawImage(sea, i * 50, j * 50, 50, 50);
                        break;
                }
            }
        }
        Form Debug = new Form(); //Notice that I put here a form, if it's shown,
        Debug.Show();//then render() has been called. It doesn't show.
    }