简体   繁体   中英

How can I make an acceleration mechanic using WinForms c#

I'm trying to write a movement mechanic like this but how can I? If we hold to Space key, character in the game will move like in here . If we take our hand from the key, the character will fall against to gravity. I did a research for about two hours but this site is the last solution way for me.

There is my code

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

Thanks from now!

i can't add a comment cause of reputation, so

you can look these articles and code sample to find your way to the light;

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

In the Form you need to double click the KeyUp and KeyDown events and also create a picturebox from the toolbox. The Timer is working like an update to do something every second. I didn't implement figure rotation but you can do that by using Sin and Cosine around the endpoint, and every angle is divided by 57... because all the trig calculations are done in radians, and degrees just feel more natural.

If you are planning to build a reasonably big game, then you're probably should use a game engine, because the input, physics and drawing systems are much more simple for you to work with in those programs.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM