Add project files.
This commit is contained in:
125
MetoderUppgift/Form1.cs
Normal file
125
MetoderUppgift/Form1.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using System.Drawing;
|
||||
namespace MetoderUppgift
|
||||
{
|
||||
public partial class Form1 : Form
|
||||
{
|
||||
float velocity = 0; // [m/s]
|
||||
float vMax; // [m/s]
|
||||
float aMax; // [m/s^2]
|
||||
float engineBraking = 2; // [m/s^2]
|
||||
float dt = 0.1f; // [s]
|
||||
float stripeOffset = 0; // horizontal offset for stripes
|
||||
|
||||
public Form1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
void UpdateUI(float acceleration)
|
||||
{
|
||||
lblAcc.Text = "Acceleration: " + acceleration.ToString("0.00") + " [m/s<>]";
|
||||
lblVel.Text = "Hastighet: " + velocity.ToString("0.00") + " [m/s]";
|
||||
lblThr.Text = "Gasp<73>drag: " + GetThrottlePercentage().ToString("0.00") + " [%]";
|
||||
}
|
||||
|
||||
void ImplementSpecifications()
|
||||
{
|
||||
if (float.TryParse(tbxAcceleration.Text, out float result)) // tal fr<66>n str<74>ng, v<>rden blir NaN om input inte fungerar
|
||||
{
|
||||
aMax = result;
|
||||
}
|
||||
if (float.TryParse(tbxVelocity.Text, out float result2))
|
||||
{
|
||||
vMax = result2;
|
||||
}
|
||||
}
|
||||
void Simulate()
|
||||
{
|
||||
ImplementSpecifications(); // Uppdatera specifikationer av bilen angivna av textboxes
|
||||
|
||||
float acceleration = GetAcceleration(Throttle.Value); // H<>mta acceleration
|
||||
velocity += acceleration * dt; // Uppdatera Acceleration
|
||||
|
||||
UpdateUI(acceleration); // Uppdatera kontroller
|
||||
UpdateStripes(); // Uppdatera strecken p<> v<>gen f<>r att simulera grafisk hastighet
|
||||
|
||||
}
|
||||
|
||||
void UpdateStripes()
|
||||
{
|
||||
stripeOffset += 10 * velocity * dt; // Uppdatera avst<73>nd fr<66>n initial plats f<>r varje streck p<> v<>gen
|
||||
}
|
||||
float GetThrottlePercentage() //Omvanlda gasp<73>drag (0-4) till %
|
||||
{
|
||||
float percentage = (100 / Throttle.Maximum) * Throttle.Value;
|
||||
return percentage;
|
||||
}
|
||||
float GetAcceleration(int throttle) // Uppdatera accelerationen fr<66>n gasp<73>drag
|
||||
{
|
||||
if (velocity < 0)
|
||||
{
|
||||
velocity = 0;
|
||||
return 0; // Undvik negativ hastighet
|
||||
}
|
||||
|
||||
float ratio = (float)throttle / (float)Throttle.Maximum;
|
||||
float acceleration = ratio * aMax * (1.1f - velocity / vMax); // Formel f<>r att g<>ra fysik mer realistisk
|
||||
float netAcceleration = acceleration - engineBraking;
|
||||
|
||||
return netAcceleration;
|
||||
}
|
||||
|
||||
private void btnStart_Click(object sender, EventArgs e) //Start och stopp knapp
|
||||
{
|
||||
if (!Timer.Enabled)
|
||||
{
|
||||
Timer.Enabled = true;
|
||||
btnStart.Text = "Stopp";
|
||||
}
|
||||
else
|
||||
{
|
||||
Application.Exit();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnPaint(PaintEventArgs e) // Paint funktion f<>r grafik
|
||||
{
|
||||
base.OnPaint(e);
|
||||
Graphics g = e.Graphics;
|
||||
|
||||
int roadX = 800;
|
||||
int roadWidth = 150;
|
||||
g.FillRectangle(Brushes.DimGray, roadX, 0, roadWidth, ClientSize.Height);
|
||||
Car.Location = new Point(800, ClientSize.Height / 2);
|
||||
|
||||
// Streck
|
||||
int stripeWidth = 10;
|
||||
int stripeHeight = 30;
|
||||
int gap = 200;
|
||||
int stripesCount = 5;
|
||||
int stripeX = roadX + roadWidth / 2 - stripeWidth / 2;
|
||||
int stripeY = 0;
|
||||
|
||||
for (int i = 0; i < stripesCount; i++)
|
||||
{
|
||||
g.FillRectangle(Brushes.White, stripeX, stripeY + stripeOffset, stripeWidth, stripeHeight); // Offset uppdateras varje tick. F<>r<EFBFBD>ndringshastighet av hastighet.
|
||||
stripeY += gap;
|
||||
if ((stripeOffset) > 200)
|
||||
{
|
||||
stripeOffset = 0; // Flytta tillbaka strecken om de hamnar utanf<6E>r rutan.
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
private void Timer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
Simulate(); // Simulera fysik och grafik
|
||||
Invalidate(); // uppdatera grafik
|
||||
}
|
||||
|
||||
private void Form1_Load(object sender, EventArgs e)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user