Results 1 to 9 of 9

Thread: Help with Graphics And timer game! THANKS

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    Help with Graphics And timer game! THANKS

    Hi im trying to do this game project for Class.

    The assignment is to create a "Gun/Laser" Game. One button will be able to shoot a laser when the user clicks. The other button automatically shoots a laser beam ever couple of seconds. If the user laser hits the auto laser then the user wins. Also the laser isn't a full line. im trying to make it more like a rectangle.
    Name:  cea46a56c7be6c5f01e73070916363a5.gif
Views: 265
Size:  115.4 KB


    Here is the Form.

    My code. (NOT FINISHED) i am having problems with the user gun/laser.

    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Timer1.Start()
            Timer2.Start()
            Button1.Visible = False
            Button2.Visible = True
            Button3.Visible = True
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim bluex As Integer
            Dim box As System.Drawing.Graphics = Me.CreateGraphics()
            Dim blueBrush As New SolidBrush(Color.Blue)
    
            If bluex = 250 Then
                Me.Timer1.Enabled = False
            Else
                box.FillRectangle(blueBrush, 232, bluex, 25, 5)
            End If
    
            bluex = bluex + 20
        End Sub
    End Class


    Here is what the form looks like when you run. And when you click the "Laser" Button.

    Name:  dfdd.gif
Views: 243
Size:  42.2 KB



    Name:  adad.gif
Views: 239
Size:  124.0 KB




    Thanks for the help and time.

  2. #2
    Hyperactive Member
    Join Date
    Feb 2009
    Posts
    258

    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.

  3. #3
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    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.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    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

  6. #6
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    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.

  7. #7
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    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."

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    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.

  9. #9
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Help with Graphics And timer game! THANKS

    Quote Originally Posted by Pompy View Post
    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
  •  



Click Here to Expand Forum to Full Width