【问题标题】:How can I make an acceleration mechanic using WinForms c#如何使用 WinForms c# 制作加速机制
【发布时间】:2021-04-14 12:29:47
【问题描述】:

我正在尝试编写像this 这样的运动机制,但我该怎么做呢?如果我们按住Space键,游戏中的角色会像here一样移动。如果我们把手从钥匙上拿开,角色就会在重力的作用下倒下。我做了大约两个小时的研究,但这个网站是我最后的解决方法。

这是我的代码

using System;
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 c_sharp_form_test_2 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            timer.Interval = 5;
            timer.Tick += new EventHandler(FormUpdate);
            timer.Start();
        }

        Timer timer = new Timer();

        Bitmap bitmap;
        Point center = new Point(100, 100);
        int radius = 40;
        int FigureSize = 10;

        Point end;
        float angle = -90;
        float angVel = 0;
        float angAcc = 0;

        bool isPressing = false;

        private void Form1_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.Space) {
                isPressing = true;
                angle += 10;
            }
        }
        private void Form1_KeyUp(object sender, KeyEventArgs e) {
            angVel = 0;
            angAcc = 0;
            isPressing = false;
        }

        private void FormUpdate(object sender, EventArgs e) {
            end.X = (int)(radius * Math.Sin(angle / 57.2957795f)) +             center.X;// from degrees to radians
            end.Y = (int)(radius * Math.Cos(angle / 57.2957795f)) +     center.Y;// from degrees to radians

        Graphics g = Graphics.FromImage(bitmap);
        Pen bp = new Pen(Color.Black);
        SolidBrush b = new SolidBrush(Color.Black);

        g.Clear(Color.White);
        g.FillEllipse(b, end.X - FigureSize / 2, end.Y - FigureSize / 2, FigureSize, FigureSize);
        g.DrawLine(bp, center.X, center.Y, end.X, end.Y);
        pictureBox1.Image = bitmap;

        if (!isPressing) {
            angle += angVel;
            angVel += angAcc;
            angAcc = -0.15f * (float)Math.Sin(angle / 57.2957795f);
            angVel *= 0.985f;//dampening
            }
        }
    }
}

从现在开始感谢!

【问题讨论】:

    标签: c# winforms timer picturebox


    【解决方案1】:

    我无法添加评论的声誉原因, 所以

    您可以查看这些文章和代码示例以找到通往光明的道路;

    private void DrawIt()
            {
                System.Drawing.Graphics graphics = this.CreateGraphics();
                System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(
                    50, 100, 150, 150);
                graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
                graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);
            }
    

    codeProjectLink

    https://docs.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-draw-a-filled-ellipse-on-a-windows-form?view=netframeworkdesktop-4.8

    【讨论】:

      【解决方案2】:

      在表单中,您需要双击 KeyUp 和 KeyDown 事件,并从工具箱中创建一个图片框。计时器就像更新一样,每秒都在做某事。我没有实现图形旋转,但你可以通过在端点周围使用正弦和余弦来做到这一点,每个角度都除以 57...因为所有的三角计算都是以弧度完成的,度数感觉更自然。

      如果您打算构建一个相当大的游戏,那么您可能应该使用游戏引擎,因为在这些程序中使用输入、物理和绘图系统要简单得多。

      using System;
      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 TrynaHelppl
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }
      
              private void Form1_Load(object sender, EventArgs e)
              {
                  bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
                  timer.Interval = 5;
                  timer.Tick += new EventHandler(FormUpdate);
                  timer.Start();
              }
      
              Timer timer = new Timer();
      
              Bitmap bitmap;
              Point center = new Point(100, 100);
              int radius = 40;
              int FigureSize = 10;
      
              Point end;
              float angle = -90;
              float angVel = 0;
              float angAcc = 0;
      
              bool isPressing = false;
      
              private void Form1_KeyDown(object sender, KeyEventArgs e)
              {
                  if (e.KeyCode == Keys.Space)
                  {
                      isPressing = true;
                      angle += 10;
                  }
              }
              private void Form1_KeyUp(object sender, KeyEventArgs e)
              {
                  angVel = 0;
                  angAcc = 0;
                  isPressing = false;
              }
      
              private void FormUpdate(object sender, EventArgs e)
              {
                  end.X = (int)(radius * Math.Sin(angle / 57.2957795f)) + center.X;// from degrees to radians
                  end.Y = (int)(radius * Math.Cos(angle / 57.2957795f)) + center.Y;// from degrees to radians
      
                  Graphics g = Graphics.FromImage(bitmap);
                  Pen bp = new Pen(Color.Black);
                  SolidBrush b = new SolidBrush(Color.Black);
      
                  g.Clear(Color.White);
                  g.FillEllipse(b, end.X - FigureSize / 2, end.Y - FigureSize / 2, FigureSize, FigureSize);
                  //g.DrawLine(bp, center.X, center.Y, end.X, end.Y);
                  pictureBox1.Image = bitmap;
      
                  if (!isPressing)
                  {
                      angle += angVel;
                      angVel += angAcc;
                      angAcc = -0.15f * (float)Math.Sin(angle / 57.2957795f);
                      angVel *= 0.985f;//dampening
                  }
              }
          }
      }

      【讨论】:

      • 老兄对不起,我不知道如何使用三角计算:/你能再帮我一次吗?
      • Sin 函数以弧度表示角度,并给出从 0 到以该角度升高的长度为 1 的直线的垂直长度,而 cos 给出水平的,所以你需要旋转你的点围绕名为 end 的点的图形。原始示例是否有效?
      • 代码无效。什么也没发生。并且 KeyPreview 设置为 True。
      • 没有。我只看到了表格和一个未成像的图片框。想看代码吗?
      • 在解决方案资源管理器中点击表单上的查看代码
      猜你喜欢
      • 2021-07-17
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      相关资源
      最近更新 更多