簡體   English   中英

如何使用 WinForms c# 制作加速機制

[英]How can I make an acceleration mechanic using WinForms c#

我正在嘗試編寫這樣的運動機制,但我怎么做? 如果我們按住Space鍵,游戲中的角色會像這里一樣移動。 如果我們把手從鑰匙上拿開,角色就會在重力的作用下倒下。 我做了大約兩個小時的研究,但這個網站是我最后的解決方案。

有我的代碼

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
            }
        }
    }
}

從現在開始感謝!

我不能添加評論的聲譽原因,所以

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

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);
        }

代碼項目鏈接

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

在表單中,您需要雙擊 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 } } } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM