首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >KeyCode不工作

KeyCode不工作
EN

Stack Overflow用户
提问于 2015-02-23 01:12:10
回答 1查看 665关注 0票数 0

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

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

代码语言:javascript
复制
 //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.
    }

编辑:我放了第一个类的全部代码,也许它可以帮助你…我不知道我应该做什么,我是Visual Studio的新手:/

EN

回答 1

Stack Overflow用户

发布于 2015-02-27 03:06:36

好吧,由于某些原因,我的Visual Studio C# 2010版本似乎有一点buggy。在进行调试之前,我必须转到\bin\Debug文件夹并清空它,它可以使用普通代码+ keyPreview = true...希望这只是一个bug :/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28660694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档