C#中的移动图片框

发布于 2025-02-05 08:51:09 字数 1450 浏览 3 评论 0原文

有人可以帮我吗? 我真的不知道该怎么办。即使里面很少的代码也不问我为什么,我也不想重新启动我的项目。也许是因为我懒惰。我认为我猜这可能是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慢慢从新开始 2025-02-12 08:51:10
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;
        Thread gameLoop;
        public Form1()
        {
            InitializeComponent();
            
            player.Controls.Add(ak47gun);
            ak47gun.Location = new Point(0, 0);
            ak47gun.BackColor = Color.Transparent;
            gameLoop = new Thread(new ThreadStart(Startup));
            gameLoop.Start();
        }
    
    public void Startup()
    {
        while(true)
        {
             player.Location = new Point(positionx, positiony);
             Thread.Sleep(128);
        }   
    }
                 
    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;
            }

        }

    }
}

我认为这将使您的图像在屏幕上移动,但是,我会认真建议如果您有兴趣制作游戏,则应该考虑在YouTube上观看很多教程。这种运动对时间敏感不敏感,因此您的图片将像CPU可以添加到X或Y位置一样快地移动。这应该很快。线程中的睡眠是为了确保您的图片并不总是更新。我首先建议一个解释Delta时间和速度的教程。

还有大量的在线免费示例游戏,可以为您提供一个很好的例子,说明其中很多事情是如何完成的。

需要注意的是,如果您不希望游戏循环/线程在更新整数之后在Keypress方法内更新player.location = new Point(positionx,positiony)。

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;
        Thread gameLoop;
        public Form1()
        {
            InitializeComponent();
            
            player.Controls.Add(ak47gun);
            ak47gun.Location = new Point(0, 0);
            ak47gun.BackColor = Color.Transparent;
            gameLoop = new Thread(new ThreadStart(Startup));
            gameLoop.Start();
        }
    
    public void Startup()
    {
        while(true)
        {
             player.Location = new Point(positionx, positiony);
             Thread.Sleep(128);
        }   
    }
                 
    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;
            }

        }

    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文