diff --git a/MetoderUppgift.sln b/MetoderUppgift.sln new file mode 100644 index 0000000..505df81 --- /dev/null +++ b/MetoderUppgift.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.14.36408.4 d17.14 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MetoderUppgift", "MetoderUppgift\MetoderUppgift.csproj", "{BABEC19A-A89A-4E67-871A-68DD536720B6}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BABEC19A-A89A-4E67-871A-68DD536720B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BABEC19A-A89A-4E67-871A-68DD536720B6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BABEC19A-A89A-4E67-871A-68DD536720B6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BABEC19A-A89A-4E67-871A-68DD536720B6}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B857B06C-46F3-47A5-BF97-027A9FB132B0} + EndGlobalSection +EndGlobal diff --git a/MetoderUppgift/Form1.Designer.cs b/MetoderUppgift/Form1.Designer.cs new file mode 100644 index 0000000..e1fb2a3 --- /dev/null +++ b/MetoderUppgift/Form1.Designer.cs @@ -0,0 +1,183 @@ +using System.Windows.Forms; + +namespace MetoderUppgift +{ + partial class Form1 + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + components = new System.ComponentModel.Container(); + Throttle = new TrackBar(); + lblThr = new Label(); + lblVel = new Label(); + lblAcc = new Label(); + btnStart = new Button(); + Timer = new System.Windows.Forms.Timer(components); + Car = new PictureBox(); + tbxAcceleration = new TextBox(); + tbxVelocity = new TextBox(); + lblMaxAcceleration = new Label(); + lblMaxHastighet = new Label(); + ((System.ComponentModel.ISupportInitialize)Throttle).BeginInit(); + ((System.ComponentModel.ISupportInitialize)Car).BeginInit(); + SuspendLayout(); + // + // Throttle + // + Throttle.Location = new Point(12, 438); + Throttle.Maximum = 4; + Throttle.Name = "Throttle"; + Throttle.Size = new Size(208, 90); + Throttle.TabIndex = 0; + Throttle.Value = 2; + // + // lblThr + // + lblThr.AutoSize = true; + lblThr.Location = new Point(50, 514); + lblThr.Name = "lblThr"; + lblThr.Size = new Size(126, 32); + lblThr.TabIndex = 1; + lblThr.Text = "GaspÃ¥drag"; + // + // lblVel + // + lblVel.AutoSize = true; + lblVel.Location = new Point(26, 327); + lblVel.Name = "lblVel"; + lblVel.Size = new Size(116, 32); + lblVel.TabIndex = 2; + lblVel.Text = "Hastighet"; + // + // lblAcc + // + lblAcc.AutoSize = true; + lblAcc.Location = new Point(26, 378); + lblAcc.Name = "lblAcc"; + lblAcc.Size = new Size(145, 32); + lblAcc.TabIndex = 3; + lblAcc.Text = "Acceleration"; + // + // btnStart + // + btnStart.Location = new Point(26, 260); + btnStart.Name = "btnStart"; + btnStart.Size = new Size(150, 46); + btnStart.TabIndex = 4; + btnStart.Text = "Starta"; + btnStart.UseVisualStyleBackColor = true; + btnStart.Click += btnStart_Click; + // + // Timer + // + Timer.Interval = 50; + Timer.Tick += Timer_Tick; + // + // Car + // + Car.BackColor = Color.Transparent; + Car.BackgroundImageLayout = ImageLayout.None; + Car.Image = Properties.Resources.Car; + Car.Location = new Point(552, 277); + Car.Name = "Car"; + Car.Size = new Size(148, 159); + Car.SizeMode = PictureBoxSizeMode.Zoom; + Car.TabIndex = 7; + Car.TabStop = false; + // + // tbxAcceleration + // + tbxAcceleration.Location = new Point(26, 153); + tbxAcceleration.Name = "tbxAcceleration"; + tbxAcceleration.Size = new Size(288, 39); + tbxAcceleration.TabIndex = 8; + // + // tbxVelocity + // + tbxVelocity.Location = new Point(26, 64); + tbxVelocity.Name = "tbxVelocity"; + tbxVelocity.Size = new Size(288, 39); + tbxVelocity.TabIndex = 9; + // + // lblMaxAcceleration + // + lblMaxAcceleration.AutoSize = true; + lblMaxAcceleration.Location = new Point(26, 118); + lblMaxAcceleration.Name = "lblMaxAcceleration"; + lblMaxAcceleration.Size = new Size(280, 32); + lblMaxAcceleration.TabIndex = 10; + lblMaxAcceleration.Text = "MaxAcceleration [m/s^2]"; + // + // lblMaxHastighet + // + lblMaxHastighet.AutoSize = true; + lblMaxHastighet.Location = new Point(26, 29); + lblMaxHastighet.Name = "lblMaxHastighet"; + lblMaxHastighet.Size = new Size(226, 32); + lblMaxHastighet.TabIndex = 11; + lblMaxHastighet.Text = "Max hastighet [m/s]"; + // + // Form1 + // + AutoScaleDimensions = new SizeF(13F, 32F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(1083, 622); + Controls.Add(lblMaxHastighet); + Controls.Add(lblMaxAcceleration); + Controls.Add(tbxVelocity); + Controls.Add(tbxAcceleration); + Controls.Add(Car); + Controls.Add(btnStart); + Controls.Add(lblAcc); + Controls.Add(lblVel); + Controls.Add(lblThr); + Controls.Add(Throttle); + MaximizeBox = false; + Name = "Form1"; + Text = "Form1"; + Load += Form1_Load; + ((System.ComponentModel.ISupportInitialize)Throttle).EndInit(); + ((System.ComponentModel.ISupportInitialize)Car).EndInit(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private TrackBar Throttle; + private Label lblThr; + private Label lblVel; + private Label lblAcc; + private Button btnStart; + private System.Windows.Forms.Timer Timer; + private PictureBox Car; + private TextBox tbxAcceleration; + private TextBox tbxVelocity; + private Label lblMaxAcceleration; + private Label lblMaxHastighet; + } +} diff --git a/MetoderUppgift/Form1.cs b/MetoderUppgift/Form1.cs new file mode 100644 index 0000000..1f920af --- /dev/null +++ b/MetoderUppgift/Form1.cs @@ -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ådrag: " + GetThrottlePercentage().ToString("0.00") + " [%]"; + } + + void ImplementSpecifications() + { + if (float.TryParse(tbxAcceleration.Text, out float result)) // tal från strä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ånd från initial plats för varje streck på vägen + } + float GetThrottlePercentage() //Omvanlda gaspådrag (0-4) till % + { + float percentage = (100 / Throttle.Maximum) * Throttle.Value; + return percentage; + } + float GetAcceleration(int throttle) // Uppdatera accelerationen från gaspå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ändringshastighet av hastighet. + stripeY += gap; + if ((stripeOffset) > 200) + { + stripeOffset = 0; // Flytta tillbaka strecken om de hamnar utanfö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) + { + } + + } +} diff --git a/MetoderUppgift/Form1.resx b/MetoderUppgift/Form1.resx new file mode 100644 index 0000000..8597150 --- /dev/null +++ b/MetoderUppgift/Form1.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 17, 17 + + \ No newline at end of file diff --git a/MetoderUppgift/MetoderUppgift.csproj b/MetoderUppgift/MetoderUppgift.csproj new file mode 100644 index 0000000..afba0ba --- /dev/null +++ b/MetoderUppgift/MetoderUppgift.csproj @@ -0,0 +1,30 @@ + + + + WinExe + net9.0-windows + enable + true + enable + + + + + + + + + True + True + Resources.resx + + + + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + \ No newline at end of file diff --git a/MetoderUppgift/Program.cs b/MetoderUppgift/Program.cs new file mode 100644 index 0000000..464b69f --- /dev/null +++ b/MetoderUppgift/Program.cs @@ -0,0 +1,17 @@ +namespace MetoderUppgift +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. + ApplicationConfiguration.Initialize(); + Application.Run(new Form1()); + } + } +} \ No newline at end of file diff --git a/MetoderUppgift/Properties/Resources.Designer.cs b/MetoderUppgift/Properties/Resources.Designer.cs new file mode 100644 index 0000000..51835e1 --- /dev/null +++ b/MetoderUppgift/Properties/Resources.Designer.cs @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace MetoderUppgift.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("MetoderUppgift.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized resource of type System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap Car { + get { + object obj = ResourceManager.GetObject("Car", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + + /// + /// Looks up a localized resource of type System.Drawing.Bitmap. + /// + internal static System.Drawing.Bitmap Road3 { + get { + object obj = ResourceManager.GetObject("Road3", resourceCulture); + return ((System.Drawing.Bitmap)(obj)); + } + } + } +} diff --git a/MetoderUppgift/Properties/Resources.resx b/MetoderUppgift/Properties/Resources.resx new file mode 100644 index 0000000..1ed6009 --- /dev/null +++ b/MetoderUppgift/Properties/Resources.resx @@ -0,0 +1,127 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + ..\Resources\Car.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + + ..\Resources\Screenshot 2026-01-18 192524.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a + + \ No newline at end of file diff --git a/MetoderUppgift/Resources/Car.png b/MetoderUppgift/Resources/Car.png new file mode 100644 index 0000000..e8182a0 Binary files /dev/null and b/MetoderUppgift/Resources/Car.png differ diff --git a/MetoderUppgift/Resources/Screenshot 2026-01-18 192012.png b/MetoderUppgift/Resources/Screenshot 2026-01-18 192012.png new file mode 100644 index 0000000..d55f5dd Binary files /dev/null and b/MetoderUppgift/Resources/Screenshot 2026-01-18 192012.png differ diff --git a/MetoderUppgift/Resources/Screenshot 2026-01-18 192524.png b/MetoderUppgift/Resources/Screenshot 2026-01-18 192524.png new file mode 100644 index 0000000..e6d16b9 Binary files /dev/null and b/MetoderUppgift/Resources/Screenshot 2026-01-18 192524.png differ