C#中的移动图片框
有人可以帮我吗? 我真的不知道该怎么办。即使里面很少的代码也不问我为什么,我也不想重新启动我的项目。也许是因为我懒惰。我认为我猜这可能是Varibels的问题。正如我说的我不知道的。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Project_Zomboid
{
public partial class Form1 : Form
{
int positiony = 0;
int positionx = 0;
public Form1()
{
InitializeComponent();
player.Controls.Add(ak47gun);
ak47gun.Location = new Point(0, 0);
ak47gun.BackColor = Color.Transparent;
Startup();
player.Location = new Point(positionx, positiony);
}
void Startup()
{
if (true == true)
{
}
}
private void keypress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.W)
{
positiony += 1;
}
else if(e.KeyCode == Keys.S)
{
positiony -= 1;
}
else if (e.KeyCode == Keys.D)
{
positionx += 1;
}
else if( e.KeyCode == Keys.A)
{
positionx -= 1;
}
}
}
}
Could someone help me?
I really don't know what to do. I don't want to restart my project even if there is little code inside it don't ask me why. Maybe because Im lazy. I think it is probably problems with the varibels I guess. As I said I don't know.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Project_Zomboid
{
public partial class Form1 : Form
{
int positiony = 0;
int positionx = 0;
public Form1()
{
InitializeComponent();
player.Controls.Add(ak47gun);
ak47gun.Location = new Point(0, 0);
ak47gun.BackColor = Color.Transparent;
Startup();
player.Location = new Point(positionx, positiony);
}
void Startup()
{
if (true == true)
{
}
}
private void keypress(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.W)
{
positiony += 1;
}
else if(e.KeyCode == Keys.S)
{
positiony -= 1;
}
else if (e.KeyCode == Keys.D)
{
positionx += 1;
}
else if( e.KeyCode == Keys.A)
{
positionx -= 1;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这将使您的图像在屏幕上移动,但是,我会认真建议如果您有兴趣制作游戏,则应该考虑在YouTube上观看很多教程。这种运动对时间敏感不敏感,因此您的图片将像CPU可以添加到X或Y位置一样快地移动。这应该很快。线程中的睡眠是为了确保您的图片并不总是更新。我首先建议一个解释Delta时间和速度的教程。
还有大量的在线免费示例游戏,可以为您提供一个很好的例子,说明其中很多事情是如何完成的。
需要注意的是,如果您不希望游戏循环/线程在更新整数之后在Keypress方法内更新player.location = new Point(positionx,positiony)。
I think that will get your image moving around the screen but, I would seriously suggest if you're interested in making games you should consider watching a lot of tutorials online on youtube. This movement isn't time sensitive so your picture will move as fast as the cpu can add to the x or y position. Which should be really fast. The sleep in the thread is to make sure your picture isn't always updating. I would first suggest a tutorial that explains delta time and velocity.
There are also a TON of free example games online that can give you a great example of how a lot of this is done.
Something else to note is if you don't want a game loop/thread update the player.Location = new Point(positionx, positiony) inside the keypress method after updating the integer.