Add points when PictureBox1 hits PictureBox2 - vb.net

I'm trying to make a simple game using VB.Net and its concept is like egg catching.
My expectation is that when the egg is falling (PictureBox1), the catcher (PictureBox2) catches the egg and it gets 1 point every catch. My idea is when the location of egg matches the location of the catcher it adds point. But it didnt work. Any suggestion here?
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PictureBox1.Top += 5
If PictureBox1.Location.Y = PictureBox2.Location.Y Then
score += 1
Label1.Text = score
End If
End Sub
And this is the code for game over:
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
If PictureBox1.Location.Y > 400 Then
Me.Dispose()
MsgBox("game over")
End If
End Sub

Welcome to the site! I believe the Bounds property should work for this.
Also, may want to just use a boolean (or raise an event) for the collision, so this way you're not doing the work twice.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
PictureBox1.Top += 5
Dim itHappened as Boolean
Dim other_boxes as New List(Of PictureBox) from {PictureBox2} ', PictureBox3, PictureBox4}
For each box in other_boxes
If PictureBox1.Bounds.IntersectsWith(box.Bounds) Then
score += 1
Label1.Text = score
itHappened = True
else
itHappened = False 'depending on your logic, may not be the best place
End If
Next
If PictureBox1.Location.Y > 400 Then
Me.Dispose()
MsgBox("game over")
End If
End Sub

Related

Multiple timer thread with single Datagridview

I am using VB.NET with Visual Studio 2017.
I am testing a form which has a datagridview. That grid is being populated with initial trading symbols. After loading symbols, I need to press Start button, which starts a timer1 to look for latest price of that symbol from exchange and start timer2. Timer2 will look for certain conditions of price, if met, will place an order to exchange and again look for another loop. While this is happening, timer1 is also running at 2 seconds interval to get latest trading price.
I am using two different BackgroundWorker for each timer to do these two tasks with grid.
This works almost fine, but sometimes, both or one of timers gets disabled.
I am looking for another good and best approach to this as far as code, design and architechure is concerned. If anyone can help.
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Me.Timer1.Stop()
Try
With Me.DataGridView4
For RowIndex = 0 To .RowCount - 1
With .Rows(RowIndex)
'CODE FOR CHECKING LTP OF SYMBOL
End With
Next
End With
Catch ex As Exception
End Try
End Sub
Private Sub BackgroundWorker2_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker2.DoWork
Me.Timer2.Stop()
Try
For Each row As DataGridViewRow In Me.DataGridView4.Rows
Dim TRDSYM As String = row.Cells(1).Value
Dim orderid As String = row.Cells(6).Value
Dim Ltp As Decimal = row.Cells(2).Value
Dim EntryPrice As Decimal = Ltp
'PLACING AN ORDER TO EXCHANGE BASED ON CONDITION
Next
Catch ex As Exception
End Try
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'SetLog("Market Watch For Ltp Background Worker going on...", LogType.Others)
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
'SetLog("Order Placement Algorithm Background Worker going on...", LogType.Others)
BackgroundWorker2.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If Me.Timer2.Enabled = False Then
Timer2.Enabled = True
Me.Timer2.Start()
Me.btnOrderplacementstatus.Text = "Order Placement Running..."
Me.btnOrderplacementstatus.BackColor = Color.Green
End If
'SetLog("BackgroundWorker 2 Order Placement Thread started...", LogType.Others)
Me.Timer1.Enabled = True
Me.Timer1.Start()
Me.btnMarketwatchstatus.Text = "Market Watch Running..."
Me.btnMarketwatchstatus.BackColor = Color.Green
End If
End Sub
Private Sub BackgroundWorker2_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker2.RunWorkerCompleted
If Me.Timer2.Enabled = False Then
Me.Timer2.Enabled = True
Me.Timer2.Start()
Me.btnOrderplacementstatus.Text = "Order Placement Running..."
Me.btnOrderplacementstatus.BackColor = Color.Green
End If
End Sub

How can I make a button move up in size (by going vertically up), but not in total size?

I'm having a slight conflict with a button which I've been working with in Visual Basic NET.
My first code sample is for my Button_Height_Tick, which controls changing the button's height:
Dim ChangeHeight As Boolean = False
Private Sub Button_Height_Tick(sender As Object, e As EventArgs) Handles Button_Height.Tick
If Not ChangeHeight Then
Do Until FlatButton1.Height = 63
FlatButton1.Height += 1
System.Threading.Thread.Sleep(1)
Loop
ChangeHeight = True
Else
End If
End Sub
And for my FlatButton1_MouseHover.
Private Sub FlatButton1_MouseHover(sender As Object, e As EventArgs) Handles FlatButton1.MouseHover
Button_Height.Enabled = True
Button_Height.Start()
End Sub
Now, as you can see in the Button_Height_Tick sub, the code changes the height of the button to 63, however, when this code is ran, the buttons total height is changed.
Here are some photos in-case I haven't explained it well.
What my original button looks like
What I want it to do
What it's doing (going up in size vertically going down, when I want it to go up)
Please comment below if you don't understand this question.
You need to change the 'Top' position and also I notice you have a timer then just go in a do a loop. In your example there's no need for a timer.
I'll give an example using a timer and hopefully you'll understand it and can use it for what you want. I've changed 'hover' to 'enter' and 'leave'.
If it's too slow just change the increment amount.
Dim ChangeHeight As Boolean = False
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If ChangeHeight Then
FlatButton1.Height += 2
FlatButton1.Top -= 2
If FlatButton1.Height < 63 Then Exit Sub
FlatButton1.Height = 63
Timer1.Enabled = False
Else
FlatButton1.Height -= 2
FlatButton1.Top += 2
If FlatButton1.Height > 31 Then Exit Sub
FlatButton1.Height = 31
Timer1.Enabled = False
End If
End Sub
Private Sub FlatButton1_MouseEnter(sender As Object, e As EventArgs) Handles FlatButton1.MouseEnter
ChangeHeight = True
If Timer1.Enabled Then Exit Sub
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Sub FlatButton1_MouseLeave(sender As Object, e As EventArgs) Handles FlatButton1.MouseLeave
ChangeHeight = False
If Timer1.Enabled Then Exit Sub
Timer1.Enabled = True
Timer1.Start()
End Sub
Hello and welcome to StackOverflow. I did a little example of how to achieve what you are looking for.
Code:
Public Class Form1
Dim buttonXCoordinate As Integer
Dim buttonYCoordinate As Integer
Dim buttonOriginalHeight As Integer
Dim buttonOriginalLocation As Point
Private Sub GetButtonCoordinate()
buttonXCoordinate = testBtn.Left
buttonYCoordinate = testBtn.Top
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
buttonOriginalHeight = testBtn.Height
buttonOriginalLocation = testBtn.Location
GetButtonCoordinate()
End Sub
Private Sub testBtn_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles testBtn.MouseEnter
Dim buttonLocation As Point = Nothing
GetButtonCoordinate()
buttonLocation.X += buttonXCoordinate
buttonLocation.Y += buttonYCoordinate - buttonOriginalHeight
testBtn.Height += buttonOriginalHeight
testBtn.Location = buttonLocation
End Sub
Private Sub testBtn_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles testBtn.MouseLeave
testBtn.Height = buttonOriginalHeight
testBtn.Location = buttonOriginalLocation
End Sub
End Class
I did it really fast but it's enough to give you an idea to how to achive your goal.
In my example there is a button called testBtn, when you go over it with the mouse it the button's height is increased and it returns back to normal when you move your mouse out of it

Panel Location Sliding Effect in VB.NET

what's wrong in my code? my syntax is right, the logic and concept is right.
but when i run the program and pressed the button(that make the panel slides UPWARD) the panel doesn't stop. even though I put a condition inside the timer.
here's the code;
Private Sub btn_existing_Click(sender As Object, e As EventArgs) Handles btn_existing.Click
timerPanelSlider3.Start()
pnl_info1.Enabled = False
pnl_options.Enabled = False
End Sub
Private Sub timerPanelSlider3_Tick(sender As Object, e As EventArgs) Handles timerPanelSlider3.Tick
yy -= 5
pnl_existing.Location = New Point(4, yy)
'pnl_existing.BringToFront()
If pnl_existing.Location.Y = 225 Then
timerPanelSlider3.Stop()
End If
End Sub
Your Location.Y is not hitting 225 you can check if it gets below 225. Or change it's starting position to 625.
Private Sub btn_existing_Click(sender As Object, e As EventArgs) Handles btn_existing.Click
timerPanelSlider3.Start()
pnl_info1.Enabled = False
pnl_options.Enabled = False
End Sub
Private Sub timerPanelSlider3_Tick(sender As Object, e As EventArgs) Handles timerPanelSlider3.Tick
yy -= 5
pnl_existing.Location = New Point(4, yy)
'pnl_existing.BringToFront()
If pnl_existing.Location.Y < 225 Then
timerPanelSlider3.Stop()
End If
End Sub

How to generate multiple picture boxes and control them using a timer?

My uni asked us to make a game using VB, and I really don't know much about the language.
I'm trying to make a game where balloons go up to them top of the screen and must be popped before getting there.
Public Class Form1
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If PictureBox1.Top = 0 Then
PictureBox1.Visible = False
Timer1.Enabled = False
End If
PictureBox1.Top = PictureBox1.Top - 1
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Interval = 1
Timer1.Enabled = True
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
PictureBox1.Visible = False
End Sub
End Class
This is my code so far, when I click the button, the balloon starts to go up, if I click the balloon, it disappears, it also disappears if it reaches the top and the timer stops.
How can I generate more balloons and control them using that timer?
Now all you have let to do is add the functionality of adding more PictureBoxes, maybe a second timer and when you create them use an Addhandler statement to point them the the pbs_Click event that I made and add them to the List I made as well.
Public Class Form1
Private PBs As New List(Of PictureBox)
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
For Each pb As PictureBox In PBs
If pb.Top = 0 Then
pb.Visible = False
Timer1.Enabled = False
Else
pb.Top = pb.Top - 1
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Interval = 1000'ms
Timer1.Enabled = True
End Sub
Private Sub pbs_Click(sender As Object, e As EventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
PBs.Remove(pb)
End Sub
Private Sub makeNewPB()
Dim pb As New PictureBox
Addhandler pb.Click, AddressOf pbs_Click
'don't forget to make them the size you need
PBs.Add(pb)
End Sub
End Class

ReportProgress in BackgroundWorker

The progress bar is repeated two or three times before completing the operation on my backgroundworker
this code:
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'Load Data
For i As Integer = 0 To ListData.Count
BackgroundWorker1.ReportProgress((i / ListData.Count) * 100)
Next
If BackgroundWorker1.CancellationPending Then
e.Cancel = True
End If
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
'set datasource of DatagridView
ProgressBar1.Style = ProgressBarStyle.Blocks
End Sub
In my load form
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.RunWorkerAsync()
ProgressBar1.Style = ProgressBarStyle.Marquee
please help me
You have several mistakes. First, if you intend to display an increasing progress bar, you should use:
ProgressBar1.Style = ProgressBarStyle.Continuous
in your load form. Next, you are checking BackgroundWorker1.CancellationPending only after going through all your ListData. That is too late, you have to check it every iteration of the loop. I also really doubt you want your loop to go from 0 to ListData.Count; you probably either want to start at 1 or go to ListData.Count - 1. I can't tell from your question. Your loop should look more like this:
For i as Integer = 0 To ListData.Count - 1
If BackgroundWorker1.CancellationPending Then
e.Cancel = True
Exit For
Else
' You should be doing some work here, not just calling ReportProgress
BackgroundWorker1.ReportProgress(100 * (i+1) / ListData.Count)
End If
Next
Another mistake is calculating (i / ListData.Count) * 100; i and ListData.Count are integers, so their division will always be zero until the end, when it will be 1. Instead, multiply the numerator by 100 to get percentages.