-
Apr 6th, 2015, 03:04 PM
#1
Thread Starter
Junior Member
Help with Graphics And timer game! THANKS
-
Apr 6th, 2015, 03:22 PM
#2
Hyperactive Member
Re: Help with Graphics And timer game! THANKS
What, exactly, is the problem you are having? From what you posted, I can't really tell.
-
Apr 6th, 2015, 03:25 PM
#3
Re: Help with Graphics And timer game! THANKS
Firstly you should be drawing graphics in the Paint method of the form, not the click method of the button. To raise the Paint event, you call the Invalidate method.
You're currently drawing a rectangle that I assume is intended to move across the screen. So... put any graphics into the Paint method, call Invalidate when something happens (e.g. when the timer ticks or a button is pressed).
Something like this;
Code:
Dim x As Integer = 0
Dim y As Integer = 0
Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) _
Handles MyBase.Paint
Dim g As Graphics = e.Graphics
Dim blueBrush As New SolidBrush(Color.Blue)
g.FillRectangle(blueBrush, x, y, 25, 5)
If x >= Me.Width Then
Button4.Enabled = True
Timer1.Enabled = False
End If
End Sub
Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) _
Handles Button4.Click
x = 0
Button4.Enabled = False
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) _
Handles Timer1.Tick
x += 20
Me.Invalidate()
End Sub
Last edited by Bulldog; Apr 6th, 2015 at 03:34 PM.
- If my post helped you, please Rate it
- If your problem is solved please also mark the thread resolved
I use VS2015 (unless otherwise stated).
_________________________________________________________________________________
B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.
-
Apr 6th, 2015, 03:42 PM
#4
Thread Starter
Junior Member
Re: Help with Graphics And timer game! THANKS
Sorry. The problem is that the User laser button isn't properly working. The rectangle is starting in the wrong place.
-
Apr 6th, 2015, 03:45 PM
#5
Thread Starter
Junior Member
Re: Help with Graphics And timer game! THANKS
Thanks for the help. We have not learned any of the paint method or Invalidate . Is there any other way to make this work without using those? Thanks again
-
Apr 6th, 2015, 03:46 PM
#6
Re: Help with Graphics And timer game! THANKS
Then set x and y to the position it's supposed to start from.
- If my post helped you, please Rate it
- If your problem is solved please also mark the thread resolved
I use VS2015 (unless otherwise stated).
_________________________________________________________________________________
B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.
-
Apr 6th, 2015, 04:20 PM
#7
Re: Help with Graphics And timer game! THANKS
It will also be a lot easier to use a single timer.
Do you HAVE to use the painting methods? If so, then you HAVE to use the Paint method, which handles the paint event. You can do this using picture boxes or other controls moving on the screen.
Here's a quick 10 minute example:
(NOTE: the control properties are set in the form load event, so it can be pasted directly into a blank form).
Code:
Public Class Form1
Private WithEvents FireButton As Button
Private Laser As Label
Private Target As Label
Private WithEvents LoopTimer As Timer
Private Initialized As Boolean
Private MoveAmount As Integer = 5
Private Score As Integer
Private ScoreLabel As Label
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' Button
FireButton = New Button With {
.Parent = Me, .Visible = True, .Size = New Size(64, 32), .Text = "FIRE!"}
FireButton.Location = New Point((Me.ClientSize.Width - Me.FireButton.Width) \ 2, Me.ClientSize.Height - Me.FireButton.Height)
' Target
Target = New Label With {
.Parent = Me, .Visible = True, .Location = New Point(0, 20), .Size = New Size(32, 8), .Text = String.Empty, .AutoSize = False,
.BackColor = Color.Blue}
' Laser
Laser = New Label With {
.Parent = Me, .Visible = False, .Size = New Size(2, 12), .Text = String.Empty, .AutoSize = False,
.BackColor = Color.Red}
' Score
ScoreLabel = New Label With {
.Parent = Me, .Visible = True, .Text = "0"
}
' Timer
Me.LoopTimer = New Timer
Me.LoopTimer.Interval = 40
Me.LoopTimer.Enabled = True
'
Initialized = True
End Sub
Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
If Not Initialized Then Return
Me.FireButton.Location = New Point(
(Me.ClientSize.Width - Me.FireButton.Width) \ 2, Me.ClientSize.Height - Me.FireButton.Height)
End Sub
Private Sub LoopTimer_Tick(sender As Object, e As System.EventArgs) Handles LoopTimer.Tick
If Not Initialized Then Return
Me.Target.Left += MoveAmount
If Me.Target.Right > Me.ClientSize.Width Then MoveAmount = -1 * Math.Abs(MoveAmount)
If Me.Target.Left < 0 Then MoveAmount = Math.Abs(MoveAmount)
'
If Laser.Visible Then
Laser.Top -= 10
If Laser.Bottom < 0 Then Laser.Visible = False
End If
' Hit Check
If Me.Laser.Bounds.IntersectsWith(Me.Target.Bounds) Then
Score += 1
Me.ScoreLabel.Text = Score.ToString
Me.Laser.Visible = False
Me.Target.Location = New Point(-20, 20)
Me.Target.Width -= 4
If Me.Target.Width < 4 Then Me.Target.Width = 4
End If
End Sub
Private Sub FireButton_Click(sender As Object, e As System.EventArgs) Handles FireButton.Click
If Laser.Visible = True Then Return
Laser.Location = New Point(Me.FireButton.Left + Me.FireButton.Width \ 2, Me.FireButton.Top)
Laser.Visible = True
End Sub
End Class
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Apr 6th, 2015, 08:01 PM
#8
Thread Starter
Junior Member
Re: Help with Graphics And timer game! THANKS
Yep we have to use painting methods. And thanks ill try a single timer instead. Thanks for everyone help too.
-
Apr 7th, 2015, 06:50 AM
#9
Re: Help with Graphics And timer game! THANKS
Originally Posted by Pompy
Yep we have to use painting methods. And thanks ill try a single timer instead. Thanks for everyone help too.
In which case, you need to use the Paint method which handles the Paint event.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|