locked
Adding mouse as controlling method in game RRS feed

  • Question

  • Hello,

    I'am new in programming and i will need a little help in the following game.

    So, the code:

    GraphicsWindow.Show()
    GraphicsWindow.Title = "Balloons"
    GraphicsWindow.Width = 400
    GraphicsWindow.Height = 400
    
    GraphicsWindow.BrushColor = "Black"
    GraphicsWindow.DrawText(100, 200 "Press, to start the game")
    GraphicsWindow.MouseDown = MouseDownEvent
    GraphicsWindow.KeyDown = KeyDownEvent
    Timer.Tick = TimerTickEvent
    Timer.Interval = 15
    Timer.Pause()
    
    
    Sub MouseDownEvent
      GraphicsWindow.Clear()
      Score = 0
      UpdateScore()
      BalloonSize = 40
      GraphicsWindow.PenColor = "Black"
      GraphicsWindow.BrushColor = "Red"
      Balloon[1] = Shapes.AddEllipse(BalloonSize, BalloonSize)
      GraphicsWindow.BrushColor = "Blue"
      Balloon[2] = Shapes.AddEllipse(BalloonSize, BalloonSize)
      GraphicsWindow.BrushColor = "Green"
      Balloon[3] = Shapes.AddEllipse(BalloonSize, BalloonSize)
      GraphicsWindow.BrushColor = "Magenta"
      Balloon[4] = Shapes.AddEllipse(BalloonSize, BalloonSize)
      GraphicsWindow.BrushColor = "Cyan"
      Balloon[5] = Shapes.AddEllipse(BalloonSize, BalloonSize)
      
      For i = 1 To 5
        BalloonX[i] = 10 + (i - 1) * (BalloonSize + 10)
        BalloonY[i] = 0
        BalloonSpeed[i] = Math.GetRandomNumber(4) + 2
        Shapes.Move(Balloon[i], BalloonX[i], BalloonY[i])
      EndFor
      ArrowSize = BalloonSize / 2
      ArrowX = 150
      ArrowY = 370
      GraphicsWindow.PenColor = "Black"
      GraphicsWindow.BrushColor = "Silver"
      Arrow = Shapes.AddTriangle(0, 20, ArrowSize / 2, 0, ArrowSize, 20)
      Shapes.Move(Arrow, ArrowX, ArrowY)
      Timer.Resume()
      TickCount = 0
    EndSub
    
    Sub UpdateScore
    
      GraphicsWindow.BrushColor = GraphicsWindow.BackgroundColor
      GraphicsWindow.FillRectangle(340, 80, 400, 120)
      GraphicsWindow.BrushColor = "Black"
      GraphicsWindow.DrawText(320, 60, "Точки:")
      GraphicsWindow.DrawText(350, 90, Score)
    EndSub
    
    Sub KeyDownEvent
    
      If (GraphicsWindow.LastKey = "Left") Then
        ArrowX = ArrowX - 5
      ElseIf (GraphicsWindow.LastKey = "Right") Then
        ArrowX = ArrowX + 5
      EndIf
      Shapes.Move(Arrow, ArrowX, ArrowY)
    EndSub
    
    Sub TimerTickEvent
      For i = 1 To 5
    
        BalloonY[i] = BalloonY[i] + BalloonSpeed[i]
        
        If ((BalloonY[i] + BalloonSize) > ArrowY) Then
          If (BalloonX[i] < ArrowX) Then
            If ((BalloonX[i] + BalloonSize) > (ArrowX + ArrowSize)) Then
              
              Sound.PlayChime()
              Score = Score + 1
              UpdateScore()
              BalloonY[i] = - BalloonSize
              BalloonSpeed[i] = Math.GetRandomNumber(4) + 2
            EndIf
          EndIf
        EndIf
       
        If ((BalloonY[i] + BalloonSize) > GraphicsWindow.Height) Then
         
          BalloonY[i] = -BalloonSize
          BalloonSpeed[i] = Math.GetRandomNumber(4) + 2
        EndIf
        Shapes.Move(Balloon[i], BalloonX[i], Balloony[i])
      EndFor
    
      TickCount = TickCount + 1
      If (TickCount > 2000) Then
      
        Timer.Pause()
        GraphicsWindow.Clear()
        GraphicsWindow.BrushColor = "Black"
        GraphicsWindow.DrawText(50, 200, "Game oveer. Final score:" + Score)
        GraphicsWindow.DrawText(50, 250, "Press, to play again.")
      EndIf
    EndSub

    My target is to add new feature which is to play also with mouse, not only with keyboard and i need advice to do this.


    Tuesday, January 24, 2017 11:15 PM

Answers

  • Almostthere,

    In your program insert the following:

    GraphicsWindow.MouseMove = OnMouseMove 'I would put this right after GraphicsWindowMouseDown =MouseDownEvent

    Sub OnMouseMove  'I would insert this right after your sub for keydownevent

      Shapes.Move(Arrow,GraphicsWindow.MouseX,ArrowY)

    EndSub

    With this all you need to do is move the mouse from left to right or visa versa. This is much faster than using the left and right keys.


    JR


    • Edited by jricestkEditor Wednesday, January 25, 2017 4:33 PM spelling error
    • Proposed as answer by litdev Wednesday, January 25, 2017 6:44 PM
    • Marked as answer by Almosttthere Thursday, January 26, 2017 12:01 AM
    Wednesday, January 25, 2017 4:30 PM
    Answerer

All replies

  • Almostthere,

    In your program insert the following:

    GraphicsWindow.MouseMove = OnMouseMove 'I would put this right after GraphicsWindowMouseDown =MouseDownEvent

    Sub OnMouseMove  'I would insert this right after your sub for keydownevent

      Shapes.Move(Arrow,GraphicsWindow.MouseX,ArrowY)

    EndSub

    With this all you need to do is move the mouse from left to right or visa versa. This is much faster than using the left and right keys.


    JR


    • Edited by jricestkEditor Wednesday, January 25, 2017 4:33 PM spelling error
    • Proposed as answer by litdev Wednesday, January 25, 2017 6:44 PM
    • Marked as answer by Almosttthere Thursday, January 26, 2017 12:01 AM
    Wednesday, January 25, 2017 4:30 PM
    Answerer
  • Thank you!!!
    Thursday, January 26, 2017 12:01 AM