我正在做一个游戏,它有一个数组,它代表一个岛,这个数组应该在你每次开始游戏时随机生成(我将在游戏结束时生成程序生成的部分)。而且,如果你点击箭头或wasd,它会让你看到你看不到的岛的某些部分。但我有一个问题:当我点击这个键时,什么也没有发生。
这是移动的代码(它在三个不同的类上):
//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的新手:/
发布于 2015-02-27 03:06:36
好吧,由于某些原因,我的Visual Studio C# 2010版本似乎有一点buggy。在进行调试之前,我必须转到\bin\Debug文件夹并清空它,它可以使用普通代码+ keyPreview = true...希望这只是一个bug :/
https://stackoverflow.com/questions/28660694
复制相似问题