How to make the Form 2 and three animate while the buttons are on form 1 - vb.net

I want my form 2 and 3 to animate while the buttons are on form 1 and when it goes to form 2 it doesn't stop it continues animating.
FORM 1 CODE:
Public Class Form1
Private runSequence() As Image
Private walkSequence() As Image
Private idleSequence() As Image
Private runIndex As Integer = -1
Private walkIndex As Integer = -1
Private idleIndex As Integer = -1
Private WithEvents trmRun As New System.Windows.Forms.Timer
Private WithEvents trmWalk As New System.Windows.Forms.Timer
Private WithEvents trmIdle As New System.Windows.Forms.Timer
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Form2.PictureBox1.Visible = False
Form3.PictureBox1.Visible = False
Form2.Show()
Form3.Show()
Me.Width = 300
Form2.Width = 300
Form3.Width = 300
Me.Height = 300
Form2.Height = 300
Form3.Height = 300
Me.Location = New Point(100, 100)
Form2.Location = New Point(385, 100)
Form3.Location = New Point(670, 100)
runSequence = {My.Resources.sonic1, My.Resources.sonic2,
My.Resources.sonic3, My.Resources.sonic4}
walkSequence = {My.Resources.SONICWALK1_removebg_preview, My.Resources.SONICWALK2_removebg_preview,
My.Resources.SONICWALK3_removebg_preview, My.Resources.SONICWALK4_removebg_preview,
My.Resources.SONICWALK5_removebg_preview, My.Resources.SONICWALK6_removebg_preview,
My.Resources.SONICWALK7_removebg_preview, My.Resources.SONICWALK8_removebg_preview}
idleSequence = {My.Resources.SONICIDLE1_removebg_preview, My.Resources.SONICIDLE2_removebg_preview,
My.Resources.SONICIDLE3_removebg_preview, My.Resources.SONICIDLE4_removebg_preview,
My.Resources.SONICIDLE5_removebg_preview, My.Resources.SONICIDLE6_removebg_preview,
My.Resources.SONICIDLE7_removebg_preview, My.Resources.SONICIDLE8_removebg_preview,
My.Resources.SONICIDLE9_removebg_preview}
trmRun.Interval = 100
trmRun.Enabled = False
trmWalk.Interval = 100
trmWalk.Enabled = False
trmIdle.Interval = 170
trmIdle.Enabled = False
End Sub
Private Sub btnRUN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRUN.Click
trmWalk.Stop()
trmRun.Start()
trmIdle.Stop()
End Sub
Private Sub btnWALK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWALK.Click
trmRun.Stop()
trmWalk.Start()
trmIdle.Stop()
End Sub
Private Sub trmRun_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles trmRun.Tick
runIndex = runIndex + 1
PictureBox1.Left = PictureBox1.Left + 6
If runIndex = runSequence.Length Then
runIndex = 0
Timer1.Stop()
Timer2.Start()
End If
PictureBox1.Image = runSequence(runIndex)
End Sub
Private Sub trmWalk_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles trmWalk.Tick
walkIndex = walkIndex + 1
PictureBox1.Left = PictureBox1.Left + 4
If walkIndex = walkSequence.Length Then
walkIndex = 0
Timer1.Stop()
Timer2.Start()
End If
PictureBox1.Image = walkSequence(walkIndex)
End Sub
Private Sub btnIDLE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIDLE.Click
trmRun.Stop()
trmWalk.Stop()
trmIdle.Start()
Timer2.Stop()
End Sub
Private Sub trmIdle_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles trmIdle.Tick
idleIndex = idleIndex + 1
If idleIndex = idleSequence.Length Then
idleIndex = 0
End If
PictureBox1.Image = idleSequence(idleIndex)
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
If PictureBox1.Left >= 275 Then
Timer2.Stop()
Form2.PictureBox1.Visible = True
Form2.Timer1.Start()
End If
End Sub
End Class
The thing is I want it to animate on form 2 and 3 without having to put buttons on form 2 and three I want the sprite to be controlled from buttons on the form 1. And I want it to be like if I pressed run on form 1 before it goes to form 2 the continuing animation will be run or walk if I pressed walk before.
Video Example of the current project:
https://youtu.be/DLb68CXy0dI

Related

If my counter ends at 10 at textbox1(TB1) the random number won't show in my textbox2(TB2)

If my counter ends at 10 at textbox1 (TB1) the random number won't show in my textbox2(TB2)
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TB1.Text = 10 Then
Dim num1 As Integer
Dim randomnumber As New Random
num1 = randomnumber.Next(100, 201)
TB2.Text = num1
End If
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TB1.Text = TB1.Text + 1
If TB1.Text = 10 Then
Timer1.Enabled = False
End If
End Sub
End Class
Use a numeric variable to keep the count, not the TextBox. The TextBox.Text property is of type String, not Integer!
Putting Option Strict On at the top of your code will enforce using proper types. VB by default allows you to loosely convert between integer and string, which is poor practice.
Option Strict On
Public Class Form1
Private count As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
count = 0
Timer1.Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If count = 10 Then
Dim randomnumber As New Random()
Dim num1 = randomnumber.Next(100, 201)
TB2.Text = num1.ToString()
End If
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
count += 1
Timer1.Enabled = count <> 10
TB1.Text = count.ToString()
End Sub
End Class

Background image moving vb.net

I'm trying to create a small game, but I'm getting trouble.
I would like the background image to move as a loop when the player is moving across the map. The following code is a sample of what I want but the reverse.
Public Class Form1
Private WithEvents Tmr As New Timer With {.Interval = 40}
Private water As New Bitmap("C:\TestFolder\Water.png")
Private waterposition As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
PictureBox1.Size = New Size(240, 74)
Tmr.Start()
End Sub
Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
waterposition += 1
If waterposition = water.Width Then waterposition = 0
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.DrawImage(water, waterposition, 0)
If waterposition > 0 Then e.Graphics.DrawImage(water, 0 - (water.Width - waterposition), 0)
End Sub End class
And now, when I'm trying to reverse it (from right to left) The image won't redraw.
Private Sub Tmr_Tick(sender As Object, e As EventArgs) Handles Tmr.Tick
waterposition -= 1
If waterposition = 0 Then waterposition = water.Width
PictureBox1.Invalidate()
End Sub
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
e.Graphics.DrawImage(water, waterposition, 0)
If waterposition < 0 Then e.Graphics.DrawImage(water, (water.Width + waterposition), 0)
End Sub
Any suggestion? Thanks

How to clear or initialize imagelist for began game again?

I want to make a game for 43 image from imagelist as random and 1 picturebox for display , and I have 43 button , I would to clear imagelist when I click the button "starr" for began the game without reset form.
My code just for first 4 button
Public Class Form1
'Random Class included in .NET
Private RND As New Random
Dim ImageIndexes As New List(Of Integer)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
okkk: 'Populate the array with the Index of each Image in the ImageList
For i As Integer = 0 To ImageList1.Images.Count - 1
ImageIndexes.Add(i)
Next i
End Sub
Private Sub Buttons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click,
Button2.Click, Button3.Click, Button4.Click
CType(sender, Button).Visible = False
Dim ArrayIndex As Integer = RND.Next(0, ImageIndexes.Count)
PictureBox1.Image = ImageList1.Images(ImageIndexes(ArrayIndex))
ImageIndexes.Remove(ArrayIndex)
End Sub
Private Sub starr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles starr.Click
ImageIndexes.Clear()
PictureBox1.Image = Nothing
Button1.Visible = True
Button2.Visible = True
Button3.Visible = True
Button3.Visible = True
Button4.Visible = True
End Sub
End Class

Close form after set time

The code below allows me to fade in and out when it opens and closes, which is what I want. However, I would like my form to remain open for 10 seconds before the fading starts. I am struggling to get that part done.
Here is what I have so far:
Public Class frmDefinitions
Private Sub Button1_Click(sender As Object, e As EventArgs) _
Handles Button1.Click
tmr_out.Enabled = True
End Sub
Private Sub frmDefinitions_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
Me.Opacity = 100
tmr_in.Enabled = True
End Sub
Private Sub tmr_in_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles tmr_in.Tick
Me.Opacity += 0.05
If Me.Opacity = 1 Then
tmr_in.Enabled = False
End If
End Sub
Private Sub tmr_out_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles tmr_out.Tick
Me.Opacity -= 0.05
If Me.Opacity = 0 Then
tmr_out.Enabled = False
Me.Close()
End If
End Sub
End Class
You will need to setup a third Timer to delay the start of your tmr_out Timer. I would trigger the delay as soon as your tmr_in is disabled. You should then get your 10 second delay before you start your fade out. You could also try to use the Form's Shown event to start the Delay but you would need to adjust the 10 seconds to accommodate the fade in delay.
Public Class Form1
Dim tmrDelay As New Timer()
Public Sub New()
InitializeComponent()
tmrDelay.Interval = 10000
AddHandler tmrDelay.Tick, AddressOf tmrDelay_Tick
End Sub
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Me.Opacity = 0
tmr_in.Enabled = True
End Sub
Private Sub tmrDelay_Tick(sender As System.Object, e As System.EventArgs)
tmrDelay.Stop()
tmr_out.Start()
End Sub
Private Sub tmr_in_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_in.Tick
Me.Opacity += 0.05
If Me.Opacity = 1 Then
tmr_in.Enabled = False
tmrDelay.Start() 'Start Your 10 second delay here.
End If
End Sub
Private Sub tmr_out_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_out.Tick
Me.Opacity -= 0.05
If Me.Opacity = 0 Then
tmr_out.Enabled = False
Me.Close()
End If
End Sub
End Class

Reset a loop in Visual Basic

Basically If somebody presses button2 then I want the program to reset the measuring. The actually code keeps measuring forever and I only want to reset that measuring. Sorry for my bad english, I hope you understand what I really want, If not I will explain in details.
Here is my code:
Public Class Form1
Private MonitorWidth As Double = 51.5 'cm
Private MonitorHeigth As Double = 31 'cm - This must be enter by the user
Private TotalDistance As Double
Private PixelHeigth As Double 'cm
Private PixelWidth As Double 'cm
Private WithEvents TTimer As New Timers.Timer
Private PointQueue As New Queue(Of Point)(100)
Private Lastpoint As Point
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TTimer.Stop()
While PointQueue.Count > 0
Dim P As Point = PointQueue.Dequeue
TotalDistance += Math.Sqrt(((Lastpoint.X - P.X) * PixelWidth) ^ 2 + ((Lastpoint.Y - P.Y) * PixelHeigth) ^ 2)
Lastpoint = P
TextBox1.Text = Format(TotalDistance, "Distance: #,##0.0 cm")
TTimer.Start()
End While
End Sub
Private Sub TTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles TTimer.Elapsed
PointQueue.Enqueue(MousePosition)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Enabled = True Then
Lastpoint = MousePosition
TTimer.AutoReset = True
TTimer.Interval = 5
TTimer.Start()
Dim ScreenBounds As Rectangle = Screen.GetBounds(New Point(0, 0))
Dim Screenwidth_Pixel As Integer = ScreenBounds.Width
Dim screenHeigth_Pixel As Integer = ScreenBounds.Height
PixelHeigth = MonitorHeigth / screenHeigth_Pixel
PixelWidth = MonitorWidth / Screenwidth_Pixel
Timer1.Interval = 200
Timer1.Start()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Button2.Enabled = True Then
Timer1.Stop()
TextBox1.Text = ""
TextBox1.Text = Format(TotalDistance = 0)
End If
End Sub
End Class
In your Button2 event handler (Button2_Click), reset the queue:
PointQueue = New Queue(Of Point)(100)