Background image moving vb.net - 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

Related

How to make the Form 2 and three animate while the buttons are on form 1

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

Setting a toggle key

I am currently making an autoclicker with human randomization.
The issue i am having is that i have no clue how to make a thing that you can bind a key and then make it run. This is my code so far, and also if you have any idea on how to improve the code then PLEASE share it, I mostly need to improve my randomization.
How can i make a thing so users can set their toggle key?
Public Class Form1
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10
Declare Function mouse_autoClicker Lib "user32.dll" Alias "mouse_event" (ByVal dwFlags As Int32, ByVal dX As Int32, ByVal dY As Int32, ByVal cButtons As Int32, ByVal dwExtraInfo As Int32) As Boolean
Private Sub StartClick()
System.Windows.Forms.Cursor.Position = New System.Drawing.Point(System.Windows.Forms.Cursor.Position)
Call mouse_autoClicker(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
Call mouse_autoClicker(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End Sub
Private Sub TrackStart_Scroll(sender As Object, e As EventArgs) Handles TrackStart.Scroll
If TrackStart.Value > TrackEnd.Value Then
TrackStart.Value = TrackEnd.Value
End If
valstart.Text = trackstart.Value
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = "ClickDevastator v.0.7"
End Sub
Private Sub TrackEnd_Scroll(sender As Object, e As EventArgs) Handles trackend.Scroll
If trackend.Value < trackstart.Value Then
trackend.Value = trackstart.Value
End If
ValEnd.Text = trackend.Value
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim rnd As New Random
Dim ClickString As Integer = rnd.Next(Val(valstart.Text), Val(ValEnd.Text))
txtRnd.Text = ClickString.ToString()
Timer2.Enabled = True
For i As Integer = 0 To Val(txtRnd.Text) - 1
StartClick()
Next
End Sub
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnstart.Click
If btnstart.Text = "Start" Then
Timer1.Enabled = True
btnstart.Text = "Stop"
btnstart.ForeColor = Color.Red
ElseIf btnstart.Text = "Stop" Then
Timer1.Enabled = False
btnstart.Text = "Start"
btnstart.ForeColor = Color.LawnGreen
End If
End Sub
Private Sub btnClick_Click(sender As Object, e As EventArgs) Handles btnclick.Click
txtClick.Text = Val(txtClick.Text) + 1
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
valstart.Text = Date.Now.ToString("hh : mm : ss")
End Sub
Private Sub Timer1_Disposed(sender As Object, e As EventArgs) Handles Timer1.Disposed
Timer2.Enabled = False
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
If btnstart.Text = "Stop" Then
btnstart.PerformClick()
End If
End If
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs)
End Sub
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btnstart.Click
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles txtRnd.TextChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnclick.Click
End Sub
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles ValEnd.Click
End Sub
Private Sub label1_Click_1(sender As Object, e As EventArgs) Handles valstart.Click
End Sub
Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Label1_Click_2(sender As Object, e As EventArgs)
End Sub
Private Sub trackstart_Scroll_1(sender As Object, e As EventArgs) Handles trackstart.Scroll
End Sub
Private Sub Label1_Click_3(sender As Object, e As EventArgs)
End Sub
Private Sub Label1_Click_4(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
End Sub
End Class
A Toggle in code works on boolean values like this...
Button2.Enabled = Not (Button2.Enabled)

How to paint on Picturebox VB.net

I have start a new project and i search for some solution to paint on a picturebox
with this code i am able to draw on a form but the i need to draw on a picture box, i have try multiple ways but i can not find the way to do it on the screenshoot in the picturebox
What i need to change to get it work?
This is my code
Public Class Form3
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Cursor = Cursors.Hand
End Sub
Dim mustPaint As Boolean = False
Private Sub MouseEvent_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
mustPaint = True
End Sub
Private Sub MouseEvent_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If mustPaint Then
Dim graphic As Graphics = CreateGraphics()
graphic.FillEllipse(New SolidBrush(Color.Red), e.X, e.Y, 10, 5)
End If
End Sub
Private Sub MouseEvent_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
mustPaint = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim bounds As Rectangle
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
bounds = Screen.PrimaryScreen.Bounds
screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(0, 0, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
screenshot.Save("c:\\dcap.jpg", Imaging.ImageFormat.Bmp)
PictureBox1.BackgroundImage = screenshot
End Sub
End Class
Do you have rights to write directly to c:\? What error message do you get?
Maybe try without saving the image file
'screenshot.Save("c:\\dcap.jpg", Imaging.ImageFormat.Bmp)
This absolutely puts a screenshot on the PictureBox. I don't know what else to tell you
PictureBox1.Image = screenshot
Here, I clicked the button 6 times and it kept working!
Well after to try so many times i got it work
This is the working code
Imports System.Drawing.Drawing2D
Public Class Form3
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Dim mustPaint As Boolean = False
Private lastPT As Point
Private signature As New GraphicsPath
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If Not IsNothing(signature) Then
If e.Button = Windows.Forms.MouseButtons.Left Then
lastPT = New Point(e.X, e.Y)
End If
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If Not IsNothing(signature) Then
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim curPt As New Point(e.X, e.Y)
signature.AddLine(lastPT, curPt)
lastPT = curPt
PictureBox1.Refresh()
End If
End If
End Sub
Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
If Not IsNothing(signature) Then
If e.Button = Windows.Forms.MouseButtons.Left Then
signature.StartFigure()
End If
End If
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If Not IsNothing(signature) Then
e.Graphics.DrawPath(Pens.Black, signature)
End If
End Sub
Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
signature.Reset()
PictureBox1.Refresh()
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim bmp As New Drawing.Bitmap(PictureBox1.Width, PictureBox1.Height)
PictureBox1.DrawToBitmap(bmp, PictureBox1.ClientRectangle)
bmp.Save(System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "test.bmp"), System.Drawing.Imaging.ImageFormat.Bmp)
End Sub
Private Sub MouseEvent_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
mustPaint = True
End Sub
Private Sub MouseEvent_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
If mustPaint Then
Dim graphic As Graphics = CreateGraphics()
graphic.FillEllipse(New SolidBrush(Color.Red), e.X, e.Y, 10, 5)
End If
End Sub
Private Sub MouseEvent_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
mustPaint = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim bounds As Rectangle
Dim screenshot As System.Drawing.Bitmap
Dim graph As Graphics
bounds = Screen.PrimaryScreen.Bounds
screenshot = New System.Drawing.Bitmap(bounds.Width, bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
graph = Graphics.FromImage(screenshot)
graph.CopyFromScreen(0, 0, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy)
screenshot.Save("c:\\dcap.jpg", Imaging.ImageFormat.Bmp)
'PictureBox1.BackgroundImage = screenshot
PictureBox1.Image = screenshot
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)