Detecting a hit of a target

  • Thread starter Thread starter Deek
  • Start date Start date
Now I got my target to detect a hit, but it does multiple times in one shot, I
am working on that part now :)
 
Hi Derrick,

If you take my suggestion of keeping the timer intervals the same and
short (eg, 40ms), then you can put the collision test in either tick event
handler.

If you have the target with a long timer interval (which makes the action
jerky) then you need to put the collision test in with the cannonball as this
is moving more frequently.

If controlOverlap (picC, picT) Then
Stop the action.
Let off the fireworks!!!
End If

Regards,
Fergus
 
Hi Derrick,

Well done!! :-)

Now you can either switch the Timers off. This is the simple solution.

More of a challenge is to start counting how many times you get a hit
detected. It will happen consecutively so when a run of detections stops,
<then> you stop the Timers and let off the fireworks. And the count of
multiple detections that you've just made can be used as a score, or to have
different messages. (Have a read of the other post again and see if it makes
more sense now)

Regards,
Fergus
 
Thanks for the help Fergus, I am going to try and tackle the rest of the
assigned project then I will do your suggestion.

My next goal is:

1)give each player 3 shots in a row
2) declare the winner
3) record to a txt file for a high score table
4) setup my error checks
 
Hi Derrick,

Ah yes, you'll come across this a lot in your future - the todo list
versus the wishlist!!

You're on your way at last, after being blocked. Well done. :-))

Come back if you need anything more - just to sound off ideas if
necessary. You can start a new thread or continue in this thread, whichever
you like, I have this one monitored.

All the best,
Fergus
 
Now I am torn between the 2 player's turns. Any suggestions on what is the best
way to handle this? I was thinking about having a player1counter and a
player2counter.

Add 1 for each hit. Not sure how i would tell the program whose turn it was at
that point. Easiest way i imagine would have player 1 fire 3 shots, then pl 2
fire 3


Here is my updated code:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
Friend WithEvents radLevel1 As System.Windows.Forms.RadioButton
Friend WithEvents radLevel2 As System.Windows.Forms.RadioButton
Friend WithEvents radLevel3 As System.Windows.Forms.RadioButton
Friend WithEvents lblTitle As System.Windows.Forms.Label
Friend WithEvents btnSelect As System.Windows.Forms.Button
Friend WithEvents lblNames As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents btnContinue As System.Windows.Forms.Button
Friend WithEvents btnClear As System.Windows.Forms.Button
Friend WithEvents tmrTarget As System.Windows.Forms.Timer
Friend WithEvents txtpl1 As System.Windows.Forms.TextBox
Friend WithEvents txtpl2 As System.Windows.Forms.TextBox
Friend WithEvents picIntro As System.Windows.Forms.PictureBox
Friend WithEvents picTarget As System.Windows.Forms.PictureBox
Friend WithEvents picCannonBall As System.Windows.Forms.PictureBox
Friend WithEvents picBall As System.Windows.Forms.PictureBox
Friend WithEvents tmrShoot As System.Windows.Forms.Timer
Friend WithEvents btnShoot As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(Form1))
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.radLevel3 = New System.Windows.Forms.RadioButton
Me.radLevel2 = New System.Windows.Forms.RadioButton
Me.radLevel1 = New System.Windows.Forms.RadioButton
Me.lblTitle = New System.Windows.Forms.Label
Me.btnSelect = New System.Windows.Forms.Button
Me.picIntro = New System.Windows.Forms.PictureBox
Me.lblNames = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.btnContinue = New System.Windows.Forms.Button
Me.btnClear = New System.Windows.Forms.Button
Me.tmrTarget = New System.Windows.Forms.Timer(Me.components)
Me.picTarget = New System.Windows.Forms.PictureBox
Me.txtpl1 = New System.Windows.Forms.TextBox
Me.txtpl2 = New System.Windows.Forms.TextBox
Me.picBall = New System.Windows.Forms.PictureBox
Me.tmrShoot = New System.Windows.Forms.Timer(Me.components)
Me.btnShoot = New System.Windows.Forms.Button
Me.GroupBox1.SuspendLayout()
Me.SuspendLayout()
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.radLevel3)
Me.GroupBox1.Controls.Add(Me.radLevel2)
Me.GroupBox1.Controls.Add(Me.radLevel1)
Me.GroupBox1.Location = New System.Drawing.Point(320, 264)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(264, 184)
Me.GroupBox1.TabIndex = 0
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "Select a skill level:"
Me.GroupBox1.Visible = False
'
'radLevel3
'
Me.radLevel3.Location = New System.Drawing.Point(56, 104)
Me.radLevel3.Name = "radLevel3"
Me.radLevel3.TabIndex = 2
Me.radLevel3.Text = "Level # 3"
'
'radLevel2
'
Me.radLevel2.Location = New System.Drawing.Point(56, 72)
Me.radLevel2.Name = "radLevel2"
Me.radLevel2.TabIndex = 1
Me.radLevel2.Text = "Level # 2"
'
'radLevel1
'
Me.radLevel1.Location = New System.Drawing.Point(56, 40)
Me.radLevel1.Name = "radLevel1"
Me.radLevel1.TabIndex = 0
Me.radLevel1.Text = "Level # 1"
'
'lblTitle
'
Me.lblTitle.Font = New System.Drawing.Font("Arial Black", 22.2!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0,
Byte))
Me.lblTitle.Location = New System.Drawing.Point(264, 104)
Me.lblTitle.Name = "lblTitle"
Me.lblTitle.Size = New System.Drawing.Size(392, 64)
Me.lblTitle.TabIndex = 1
Me.lblTitle.Text = "TARGET HUNTER"
'
'btnSelect
'
Me.btnSelect.Location = New System.Drawing.Point(496, 456)
Me.btnSelect.Name = "btnSelect"
Me.btnSelect.Size = New System.Drawing.Size(88, 23)
Me.btnSelect.TabIndex = 2
Me.btnSelect.Text = "Select Level"
Me.btnSelect.Visible = False
'
'picIntro
'
Me.picIntro.Image = CType(resources.GetObject("picIntro.Image"),
System.Drawing.Image)
Me.picIntro.Location = New System.Drawing.Point(400, 160)
Me.picIntro.Name = "picIntro"
Me.picIntro.Size = New System.Drawing.Size(112, 80)
Me.picIntro.TabIndex = 3
Me.picIntro.TabStop = False
Me.picIntro.Visible = False
'
'lblNames
'
Me.lblNames.Font = New System.Drawing.Font("Microsoft Sans Serif",
12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.lblNames.Location = New System.Drawing.Point(328, 168)
Me.lblNames.Name = "lblNames"
Me.lblNames.Size = New System.Drawing.Size(288, 40)
Me.lblNames.TabIndex = 4
Me.lblNames.Text = "Please enter player's names:"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(344, 208)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(64, 16)
Me.Label1.TabIndex = 5
Me.Label1.Text = "Player 1:"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(344, 240)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(64, 16)
Me.Label2.TabIndex = 7
Me.Label2.Text = "Player 2:"
'
'btnContinue
'
Me.btnContinue.Location = New System.Drawing.Point(424, 272)
Me.btnContinue.Name = "btnContinue"
Me.btnContinue.Size = New System.Drawing.Size(104, 24)
Me.btnContinue.TabIndex = 8
Me.btnContinue.Text = "Continue"
'
'btnClear
'
Me.btnClear.Location = New System.Drawing.Point(544, 272)
Me.btnClear.Name = "btnClear"
Me.btnClear.TabIndex = 9
Me.btnClear.Text = "Clear"
'
'tmrTarget
'
'
'picTarget
'
Me.picTarget.Image = CType(resources.GetObject("picTarget.Image"),
System.Drawing.Image)
Me.picTarget.Location = New System.Drawing.Point(768, 168)
Me.picTarget.Name = "picTarget"
Me.picTarget.Size = New System.Drawing.Size(104, 96)
Me.picTarget.TabIndex = 10
Me.picTarget.TabStop = False
Me.picTarget.Visible = False
'
'txtpl1
'
Me.txtpl1.Location = New System.Drawing.Point(416, 208)
Me.txtpl1.Name = "txtpl1"
Me.txtpl1.Size = New System.Drawing.Size(128, 22)
Me.txtpl1.TabIndex = 11
Me.txtpl1.Text = ""
'
'txtpl2
'
Me.txtpl2.Location = New System.Drawing.Point(416, 240)
Me.txtpl2.Name = "txtpl2"
Me.txtpl2.Size = New System.Drawing.Size(128, 22)
Me.txtpl2.TabIndex = 12
Me.txtpl2.Text = ""
'
'picBall
'
Me.picBall.Image = CType(resources.GetObject("picBall.Image"),
System.Drawing.Image)
Me.picBall.Location = New System.Drawing.Point(464, 464)
Me.picBall.Name = "picBall"
Me.picBall.Size = New System.Drawing.Size(16, 8)
Me.picBall.TabIndex = 14
Me.picBall.TabStop = False
Me.picBall.Visible = False
'
'tmrShoot
'
Me.tmrShoot.Interval = 25
'
'btnShoot
'
Me.btnShoot.Image = CType(resources.GetObject("btnShoot.Image"),
System.Drawing.Image)
Me.btnShoot.Location = New System.Drawing.Point(440, 480)
Me.btnShoot.Name = "btnShoot"
Me.btnShoot.Size = New System.Drawing.Size(56, 64)
Me.btnShoot.TabIndex = 15
Me.btnShoot.Visible = False
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
Me.ClientSize = New System.Drawing.Size(920, 560)
Me.Controls.Add(Me.btnShoot)
Me.Controls.Add(Me.txtpl2)
Me.Controls.Add(Me.txtpl1)
Me.Controls.Add(Me.picTarget)
Me.Controls.Add(Me.btnClear)
Me.Controls.Add(Me.btnContinue)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.lblNames)
Me.Controls.Add(Me.picIntro)
Me.Controls.Add(Me.btnSelect)
Me.Controls.Add(Me.lblTitle)
Me.Controls.Add(Me.GroupBox1)
Me.Controls.Add(Me.picBall)
Me.Name = "Form1"
Me.Text = "TARGET HUNTER"
Me.GroupBox1.ResumeLayout(False)
Me.ResumeLayout(False)

End Sub

#End Region
Private timeStep As Integer 'global var
Private player1 As String, player2 As String
Private player1cnt As Integer, player2cnt As Integer

Private Sub boolReverse2(ByRef a As Boolean, ByRef b As Boolean)
'Reverse the values of the parameters
a = Not a
b = Not b
End Sub
Private Sub visReverse2(ByRef c1 As Control, ByRef c2 As Control)
'Reverse the visibility of the parameters
boolReverse2(c1.Visible, c2.Visible)
End Sub

Private Sub visReverse4(ByRef c1 As Control, ByRef c2 As Control, _
ByRef c3 As Control, ByRef c4 As Control)
visReverse2(c1, c2)
visReverse2(c3, c4)
End Sub
Private Sub visReverse8(ByRef c1 As Control, ByRef c2 As Control, _
ByRef c3 As Control, ByRef c4 As Control, _
ByRef c5 As Control, ByRef c6 As Control, _
ByRef c7 As Control, ByRef c8 As Control)
visReverse4(c1, c2, c3, c4)
visReverse4(c5, c6, c7, c8)
End Sub
Private Sub btnSelect_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSelect.Click
If radLevel1.Checked Then
tmrTarget.Enabled = True
tmrTarget.Interval = 800 ' slow speed for target
visReverse2(picTarget, btnShoot)
btnShoot.Visible = True
Else
If radLevel2.Checked Then
tmrTarget.Enabled = True
tmrTarget.Interval = 400 ' Medium speed for target
visReverse2(picTarget, btnShoot)
btnShoot.Visible = True
Else
If radLevel3.Checked Then
tmrTarget.Enabled = True
tmrTarget.Interval = 200 ' Fast speed for target
visReverse2(picTarget, btnShoot)
btnShoot.Visible = True
Else ' Then none of the radio buttons are checked
MsgBox("Please select a difficulty level!")
visReverse4(GroupBox1, btnSelect, lblTitle, picIntro)

End If
End If
End If
visReverse4(lblTitle, picIntro, GroupBox1, btnSelect)
'picTarget.Visible = True
'tmrTarget.Enabled = True
End Sub

Private Sub btnContinue_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnContinue.Click
'Dim player1 As String, player2 As String
player1 = txtpl1.Text
player2 = txtpl2.Text
visReverse8(lblNames, txtpl1, txtpl2, btnContinue, btnClear, GroupBox1,
_
btnSelect, picIntro)
visReverse2(Label1, Label2)
End Sub

Private Sub tmrTarget_Tick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles tmrTarget.Tick
'Move the graphic(target) across the form
Static intX As Integer = picTarget.Left
Static intY As Integer = picTarget.Top
Static intWidth As Integer = picTarget.Width
Static intHeight As Integer = picTarget.Height

'Set new x coordinate
intX -= 10
If intX <= -picTarget.Width Then 'Graphic is off edge of form
intX = Me.Width
End If
'Move image
picTarget.SetBounds(intX, intY, intWidth, intHeight)

End Sub
Private Sub tmrShoot_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles tmrShoot.Tick
Dim jump As Integer = 10
With picBall
.Top = .Top - jump
hitIt()
If .Top < jump Then
.Visible = False
tmrShoot.Enabled = False

End If

End With
End Sub
Private Sub btnShoot_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs)
With picBall
.Left = 454
.Top = 440
.Visible = True
End With
tmrShoot.Enabled = True
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' btnContinue.PerformClick()
'btnSelect.PerformClick()

End Sub

Private Sub btnShoot_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnShoot.Click
With picBall
.Left = 454
.Top = 440
.Visible = True
End With
tmrShoot.Enabled = True
End Sub
Function IntervalMember(ByVal x As Single, ByVal a As Single, ByVal b As
Single) _
As Boolean
'true if and only if x is a member of the interval[a,b]
Return (a <= x) And (x <= b)
End Function
Function intervalOverlap(ByVal a As Single, ByVal b As Single, _
ByVal x As Single, ByVal y As Single) As Boolean
'true if and only if interval[a,b] and [x,y] intersect
Return IntervalMember(a, x, y) Or IntervalMember(b, x, y) Or _
IntervalMember(x, a, b) Or IntervalMember(y, a, b)
End Function
Function controlOverlap(ByVal picBall As Control, ByVal picTarget As
Control) As Boolean
'Parameters are assumed to be controls that occupy rectangles, as
determined by
'.Top, .Left, .Height, and .Width properties. Function is true if and
only if the rectangles
'occupied by the parameters overlap.
Return intervalOverlap(picBall.Top, picBall.Top + picBall.Height, _
picTarget.Top, picTarget.Top + picTarget.Height)
And _
intervalOverlap(picBall.Left, picBall.Left +
picBall.Width, _
picTarget.Left, picTarget.Left +
picTarget.Width)
End Function
Function hitIt()
If controlOverlap(picBall, picTarget) = True Then
tmrShoot.Enabled = False
MsgBox("You hit it!")
picBall.Visible = False
End If
End Function
Function playerTurn()

End Function
End Class
 
Hi Derrick,

First off, can I ask a favour? Rather than appending the whole of your
code to the message - complete with its long lines broken and reams of Form
Designer stuff, could you just include the bits that have changed or are new.
Or, and I'd prefer this, could you post a zip of the project. This way I can
just copy it into VS and try it out. With the zip I can have the resx file
(with the graphics) - I haven't got your target and cannonball images - I'm
using a cloud and a smiley!! ;-) [Don't bother zipping up the bin or obj
folders, though.].

And to the game again.

For the turns, I'd have a whose-turn index variable (0 or 1 - but not
Boolean) to determine whose turn it is. I'd have a variable to count the
number of turns, and I'd have an array** with two items, indexed by the
whose-turn variable for totting up each player's number of hits.

With separate player1 and player2 counters you'd have

If a hit was made
If it was player 1's turn
player1 counter += 1
else
player2 counter += 1

With the array it's

If a hit was made
playercounter (whoever's turn) += 1


The difference isn't much here, but it's a very useful principle of
code/effort reduction.

With game play it's usual for players to take turns. This keeps the
excitement more evenly spread out rather than having each player waiting for
half the game and it adds the fun of swapping over.

You would want to change the whose-turn variable from 0 to 1 and then to 0
again, etc. Every time it goes to 0 again you would increment the number of
turns counter. When this is 3...

Regards,
Fergus

** Have you done arrays yet?
 
No we have not yet done arrays. I cannont post files here, although i wish i
could and i dont know if i can email you yet? Is it exactly as above with the
-1? I am not really following your post. Do i need to use counters and an
array?

sorry :(

Derrick
 
Hi Derrick,

No worries, apologies accepted but not necessary. ;-). You could try this
email address - it doesn't go through my ISP
(e-mail address removed)
(Don't worry about the name. I just use the 'filter' names for public
email. When the spam gets too much, I abandon the account and create a new
filter address.)

If you haven't done arrays yet, then that's perhaps not the best solution
for you. However, I'll show you how it looks, and you can have a play with it.
If it works for you then fine, if not, just leave it and stay with Player1Cnt
and Player2Cnt. I would still keep the whose-turn and turn counter variable,
though.

Declarations in the Class
Private WhoseTurn As Integer = 0 '0 is Player1, 1 is Player2
Private PlayerHits(1) As Integer 'This has 2 elements: (0) and (1).

When a hit is scored, WhoseTurn will be the current Player. So
PlayerHits (WhoseTurn) += 1
will add 1 to the score for that player.

To look at the scores later you could have:
Dim PlayerIndex As Integer
For PlayerIndex = 0 To 1
Console.WriteLine (PlayerHits (PlayerIndex))
Next

Regards,
Fergus
 
:( i really dont understand what is going on in your last post. I emailed you
my current file so we can be on the same page :). I tried reading about arrays
in the text but didnt seem to help much.
 
Fergus,

Thanks for all your help. I really appreaciate it and have learned a lot in
the process. Not many people would take the time to explain stuff in such
detail to someone that is pretty new to the task.

Thanks so much,
Derrick
 
Hi Derrick,

You're most welcome. :-)

I'm sorry if I've neglected you in the last couple of days - I put rather
a lot on my plate, as it were. I got your project, but unfortunately it's in
VS2003 and try as I might I couldn't get the Form to display properly in my
VS2002. I was intending to play cut and paste with the code again but I put it
aside in frustration and then got carried away elsewhere.

I still have time for you so don't hesitate if you need any more pointers.
:-)

Regards,
Fergus
 
Back
Top