none
I need help with Looping! ASAP

    Question

  • Hi,

    I am really close to getting this bloody ball working correctly,

    So when I press F the ball shoots but when I click F again nothing happened

    So in the code below you'll see

    Loop: 

    GraphicsWindow.KeyDown = shootCannon 

    and

    Sub ball_hide

    ETC

    Goto Loop

    The ball doesn't spawn again

    Have a look for yourself

    (When sending the code back fixed or something to help me make sure you don't have the Cannon = Shapes.AddRectangle crap thanks :))

    CODE:

    cannon1_x = 375
    cannon1_y = 350
    
    balloon1_x = 0
    balloon1_y = 0
    
    ball1_x = cannon1_x+21.5
    ball1_y = cannon1_y
    
    ball_move1 = ball1_x+ball1_y
    
    Loop:
    GraphicsWindow.KeyDown = shootCannon
    GraphicsWindow.Width = "800"
    GraphicsWindow.Height = "500"
    GraphicsWindow.CanResize = "false"
    GraphicsWindow.Show()
    
    RESOURCE_PATH = Program.Directory + "\resources\"
    CANNON_IMAGE = RESOURCE_PATH + "cannon.png"
    BALLOON_IMG = RESOURCE_PATH + "balloon.png"
    balloon = Shapes.AddImage(BALLOON_IMG)
    cannon = Shapes.AddImage(CANNON_IMAGE)
    ball = Shapes.AddEllipse(20, 20)
    Shapes.HideShape(ball)
    Shapes.Move(ball, ball1_x, ball1_y)
    Shapes.Move(cannon, cannon1_x, cannon1_y)
    Shapes.Move(balloon, balloon1_x, balloon1_y)
    
    While "True"
      For moveBalloon = 1 To GraphicsWindow.Width-50
        Shapes.Move(balloon, moveBalloon, balloon1_y)
        Program.Delay(3)
      EndFor    
      
      For moveBalloon = GraphicsWindow.Width-50 To 1 Step -1
        Shapes.Move(balloon, moveBalloon, balloon1_y)
        Program.Delay(3)
      EndFor
    EndWhile
    
    While "True"
      Program.Delay(100)
    EndWhile
    Sub shootCannon
      If GraphicsWindow.LastKey = "F" Then
        Shapes.ShowShape(ball)
        ball_hide()  
        For moveBall = 1 To GraphicsWindow.Height-100 
          y=y+1
          Shapes.Move(ball, ball1_x, cannon1_y-y)
        EndFor
      EndIf 
    EndSub
    
    Sub ball_hide
      If ball = GraphicsWindow.Height-100 Then 
        Shapes.Remove(ball)
        Goto Loop
      EndIf
    EndSub


    I love coding. This is signature is 100% Original. :)

    Sunday, February 12, 2017 8:46 PM

Answers

  • Forbidden Duck,

    This should work.

    cannon1_x = 375
    cannon1_y = 350
    balloon1_x = 0
    balloon1_y = 0
    ball1_x = cannon1_x+21.5
    ball1_y = cannon1_y
    ball_move1 = ball1_x+ball1_y
    Loop:
    GraphicsWindow.KeyDown = OnKeyDown
    GraphicsWindow.Width = "800"
    GraphicsWindow.Height = "500"
    GraphicsWindow.CanResize = "false"
    GraphicsWindow.Show()
    RESOURCE_PATH = Program.Directory + "\resources\"
    CANNON_IMAGE = RESOURCE_PATH + "cannon.png"
    BALLOON_IMG = RESOURCE_PATH + "balloon.png"
    balloon = Shapes.AddImage(BALLOON_IMG)
    cannon = Shapes.AddImage(CANNON_IMAGE)
    balloon=Shapes.AddEllipse(50,50)
    cannon=shapes.addrectangle(50,100)
    ball = Shapes.AddEllipse(20, 20)
    Shapes.HideShape(ball)
    Shapes.Move(ball, ball1_x, ball1_y)
    Shapes.Move(cannon, cannon1_x, cannon1_y)
    Shapes.Move(balloon, balloon1_x, balloon1_y)
    While "True"
      For moveBalloon = 1 To GraphicsWindow.Width-50
        Shapes.Move(balloon, moveBalloon, balloon1_y)
        Program.Delay(3)
      EndFor   
     
      For moveBalloon = GraphicsWindow.Width-50 To 1 Step -1
        Shapes.Move(balloon, moveBalloon, balloon1_y)
        Program.Delay(3)
      EndFor
    EndWhile
    While "True"
      Program.Delay(100)
    EndWhile
    Sub shootCannon
      If key = "F" Then
        Shapes.ShowShape(ball)
        ball_hide() 
        For moveBall = 1 To GraphicsWindow.Height-100
          y=y+1
          Shapes.Move(ball, ball1_x, cannon1_y-y)
        EndFor
        key=""
       
      EndIf
    EndSub
    Sub ball_hide
      If ball = GraphicsWindow.Height-100 Then
        Shapes.Remove(ball)
      EndIf
    EndSub
    Sub OnKeyDown
      key=graphicswindow.lastkey
      shootcannon()
      y=0
      EndSub


    JR

    • Marked as answer by Forbidden Duck Monday, February 13, 2017 6:33 AM
    Monday, February 13, 2017 12:55 AM
    Answerer

All replies

  • see QPV250 (General invaders games technics)

    this doesn't work.

    While "True"
      Program.Delay(100)
    EndWhile

    Monday, February 13, 2017 12:27 AM
    Answerer
  • Forbidden Duck,

    This should work.

    cannon1_x = 375
    cannon1_y = 350
    balloon1_x = 0
    balloon1_y = 0
    ball1_x = cannon1_x+21.5
    ball1_y = cannon1_y
    ball_move1 = ball1_x+ball1_y
    Loop:
    GraphicsWindow.KeyDown = OnKeyDown
    GraphicsWindow.Width = "800"
    GraphicsWindow.Height = "500"
    GraphicsWindow.CanResize = "false"
    GraphicsWindow.Show()
    RESOURCE_PATH = Program.Directory + "\resources\"
    CANNON_IMAGE = RESOURCE_PATH + "cannon.png"
    BALLOON_IMG = RESOURCE_PATH + "balloon.png"
    balloon = Shapes.AddImage(BALLOON_IMG)
    cannon = Shapes.AddImage(CANNON_IMAGE)
    balloon=Shapes.AddEllipse(50,50)
    cannon=shapes.addrectangle(50,100)
    ball = Shapes.AddEllipse(20, 20)
    Shapes.HideShape(ball)
    Shapes.Move(ball, ball1_x, ball1_y)
    Shapes.Move(cannon, cannon1_x, cannon1_y)
    Shapes.Move(balloon, balloon1_x, balloon1_y)
    While "True"
      For moveBalloon = 1 To GraphicsWindow.Width-50
        Shapes.Move(balloon, moveBalloon, balloon1_y)
        Program.Delay(3)
      EndFor   
     
      For moveBalloon = GraphicsWindow.Width-50 To 1 Step -1
        Shapes.Move(balloon, moveBalloon, balloon1_y)
        Program.Delay(3)
      EndFor
    EndWhile
    While "True"
      Program.Delay(100)
    EndWhile
    Sub shootCannon
      If key = "F" Then
        Shapes.ShowShape(ball)
        ball_hide() 
        For moveBall = 1 To GraphicsWindow.Height-100
          y=y+1
          Shapes.Move(ball, ball1_x, cannon1_y-y)
        EndFor
        key=""
       
      EndIf
    EndSub
    Sub ball_hide
      If ball = GraphicsWindow.Height-100 Then
        Shapes.Remove(ball)
      EndIf
    EndSub
    Sub OnKeyDown
      key=graphicswindow.lastkey
      shootcannon()
      y=0
      EndSub


    JR

    • Marked as answer by Forbidden Duck Monday, February 13, 2017 6:33 AM
    Monday, February 13, 2017 12:55 AM
    Answerer
  • Hi,

    It works fine thanks! How do i implement random colors?

    Look under Loop:

    cannon1_x = 375
    cannon1_y = 350
    
    balloon1_x = 0
    balloon1_y = 0
    
    ball1_x = cannon1_x+21.5
    ball1_y = cannon1_y
    ball_move1 = ball1_x+ball1_y
    
    
    Loop:
    RCB = GraphicsWindow.GetRandomColor()
    GraphicsWindow.BrushColor = RCB
    GraphicsWindow.KeyDown = OnKeyDown
    GraphicsWindow.Width = "800"
    GraphicsWindow.Height = "500"
    GraphicsWindow.CanResize = "false"
    GraphicsWindow.Show()
    
    RESOURCE_PATH = Program.Directory + "\resources\"
    CANNON_IMAGE = RESOURCE_PATH + "cannon.png"
    BALLOON_IMG = RESOURCE_PATH + "balloon.png"
    balloon = Shapes.AddImage(BALLOON_IMG)
    cannon = Shapes.AddImage(CANNON_IMAGE)
    ball = Shapes.AddEllipse(20, 20)
    Shapes.HideShape(ball)
    Shapes.Move(ball, ball1_x, ball1_y)
    Shapes.Move(cannon, cannon1_x, cannon1_y)
    Shapes.Move(balloon, balloon1_x, balloon1_y)
    
    While "True"
      For moveBalloon = 1 To GraphicsWindow.Width-50
        Shapes.Move(balloon, moveBalloon, balloon1_y)
        Program.Delay(3)
      EndFor    
      
      For moveBalloon = GraphicsWindow.Width-50 To 1 Step -1
        Shapes.Move(balloon, moveBalloon, balloon1_y)
        Program.Delay(3)
      EndFor
    EndWhile
    
    Sub shootCannon
      If key = "F" Then
        Shapes.ShowShape(ball)
        ball_hide()  
        For moveBall = 1 To GraphicsWindow.Height-100 
          y=y+1
          Shapes.Move(ball, ball1_x, cannon1_y-y)
        EndFor
        key=""    
      EndIf 
    EndSub
    
    Sub ball_hide
      If ball = GraphicsWindow.Height-100 Then 
        Shapes.Remove(ball)
      EndIf
    EndSub
    
    Sub OnKeyDown
      key=graphicswindow.lastkey
      shootcannon()
      y=0
      EndSub


    I love coding. This is signature is 100% Original. :)

    Monday, February 13, 2017 6:33 AM
  • Forbidden Duck,

    Can't change the color of an image. What you can do is use shapes to create the balloon. Then when you want to change color set the color=graphicswindow.randomcolor. then make that the brushcolor. Then remove the balloon and create it again. It will use the new brushcolor.


    JR

    Monday, February 13, 2017 5:24 PM
    Answerer
  • For the Ball not the Balloon or Cannon

    I love coding. This is signature is 100% Original. :)

    Monday, February 13, 2017 5:26 PM
  • Forbidden Duck,

    Be best if you get 1 item working at a time. I would suggest changing the color of the balloon first. As I said you can't change the color of an image. An image is those items that you are bringing in from your resources file. In the above code the ball is already being created by using shape. You can do the same thing with balloon.

    Actually you can't really change the color of a shape either. What you are doing is creating the shape in a color, use it and then remove it. Then create another shape right away with the same name. It will have the color of what ever the brushcolor is set at.


    JR

    Monday, February 13, 2017 7:05 PM
    Answerer
  • Actually you can't really change the color of a shape either

    Hi ,

    yes !, there is a way to change the color of the shape balls.

    Here is a little program for that:

    press B to throw color balls.

    program: HFK959

    Monday, February 13, 2017 7:45 PM
    Answerer
  • Yled,

    Looking at your program I see that it is just creating many ellipses. If you do a textwindow.writeline it will show Ellipse1 and Ellipse2 and so on. It works, but I would think it could consume a lot of memory. Also, there is actually a way to change the color, but you have to to use the LitDev extension which makes it a lot easier.


    JR

    Tuesday, February 14, 2017 12:07 AM
    Answerer
  • Hi jricestk !

    The goal here was just to show how we can change the color of a shape without LD extension, and to share it so peoples could introduce as SUB in their own programs :)
    Also there is a way to change the color of an image, with LD extension.
    See how my baseballs has been colored , every time you press a key:
    program no: DXQ638

    Tuesday, February 14, 2017 5:02 PM
    Answerer