none
Collision for a bullet and balloon RRS feed

  • Question

  • I'm trying to get a line printed showing the number of shots taken when the balloon and bullet touch.

    I just can't seem to wrap my head around it

    'Initialisation Window
    gw = 800
    gh = 400
    GraphicsWindow.Width = gw
    GraphicsWindow.Height = gh
    fpsTarget= 30

    'Initialise Key events
    GraphicsWindow.KeyDown = OnKeyDown
    GraphicsWindow.KeyUp = OnKeyUp


    keyUp = 0
    keyDown = 0

    'Initialise Cannon
    Cannon = Shapes.AddRectangle(50,50)
    Boom = Shapes.AddRectangle(50,20)
    balloon = Shapes.AddEllipse(50,50)
    balloonDelay=5

    StartY=10
    dir=-1


    CannonX = gw
    CannonY = gh
    speed = 3
    Shapes.Move(Cannon,CannonX-25,CannonY-25)
    Shapes.Move(Boom,CannonX-73,CannonY-8)


    'Main loop - just move the ball
    While ("True")
      processKey()

      Collison()

      Shapes.Move(Cannon,CannonX-25,CannonY-25)
      Shapes.Move(Boom,CannonX-73,CannonY-8)

      Program.Delay(10)
    EndWhile

    'Key press event subroutines
    'A separate Down and Up is checked for each key, this tells us the state for any key
    'And isn't affected by auto-repeat delays for keys
    Sub OnKeyDown
      lastKey = GraphicsWindow.LastKey

      If(lastKey = "Up") Then
        keyUp = 1
      ElseIf(lastKey = "Down") Then
        keyDown = 1
      ElseIf(lastKey="Space")Then
        KeySpace =1

      EndIf
    EndSub

    Sub OnKeyUp
      lastKey = GraphicsWindow.LastKey

      If(lastKey = "Up") Then
        keyUp = 0
      ElseIf(lastKey = "Down") Then
        keyDown = 0
      Elseif (lastKey="Space")Then
        KeySpace=0
      EndIf
    EndSub

    Sub processKey
      randNum = Math.GetRandomNumber(400)

      If(keyUp = 1) Then
        CannonY = CannonY-speed
        BoomY=BoomY - speed
      EndIf
      If(keyDown = 1) Then
        CannonY = CannonY+speed
        BoomY= BoomY+speed
      EndIf

      If KeySpace=1 Then
        bullet=Shapes.AddEllipse(20,20)


        For i = -800 To (GraphicsWindow.Width+200)
          Shapes.Move(bullet,-i,CannonY-6)

         EndFor

    EndIf
      'Check for hitting edges
      If (CannonY < 0) Then
        CannonY = -CannonY
        BoomY=-BoomY
      EndIf
      If (Cannon > gh) Then
        CannonY = -CannonY
        BoomY=-BoomY
      EndIf



      Shapes.Animate(balloon,10,randNum,10)

      If Shapes.GetTop(balloon)> gh Then
        balloon=-balloon
      EndIf
      If Shapes.GetTop(balloon) < 0 Then
        balloon=-balloon
        Endif
    EndSub

    Sub Collison
      Ball_Amount=2
      Bullet_X=Shapes.GetLeft(bullet)
      Balloon_X=Shapes.GetLeft(balloon)

          If Balloon=bullet Then

            Sound.PlayClick()

           TextWindow.Write("Hello")

          EndIf

    endsub











                                                                          
    Tuesday, January 15, 2019 1:13 PM

Answers