none
Can't get this IF statement to stop my paddle! RRS feed

  • Question

  • Below is my attempt at a pong game I am very new to programming but very keen. I cant get the paddle to stop when reaching the side of the graphics window.

    I am trying to use an If staement saying that if getleft = 1 then shapes.move(paddle, 1, 550). 550 is the horizontal position of the paddle.

    Do I need to put it into a sperate sub routine ? Not clear on Sub routines

    Any help much appreciated.

     

    GraphicsWindow.Height = 600

    GraphicsWindow.Width = 800

    GraphicsWindow.CanResize = "false"

    GraphicsWindow.Title = "Paddle Dash"

    Paddle = Shapes.AddRectangle(100, 20)

    Shapes.Move(paddle, 290, 550)

    Ball = Shapes.AddEllipse(20, 20)

    GraphicsWindow.KeyDown = paddlemove

     

    begin:

     

    Paddlex = Shapes.GetLeft(paddle)

    newx = -5 + Paddlex

    newy = 5 + Paddlex

    leftstop = Shapes.Move(paddle, 1, 550)

    Sub paddlemove

      If GraphicsWindow.LastKey = "Left" then

        Shapes.Move(paddle, newx, 550) 

        EndIf 

      If GraphicsWindow.LastKey = "Right" then

        Shapes.Move(paddle, newy, 550)

      EndIf

       

      EndSub

     

    Sub stop

      If Paddlex = "1" Then

      EndIf

    EndSub

    Goto begin

     

    Wednesday, February 16, 2011 9:52 AM

Answers

  • You dont need that goto to move paddle. Just put all paddle moving code inside one sub and filter Paddlex with IF.

    When user presses key IF checks is new Paddlex too small or too large. If it is too large or too small code fixes that before it moves the paddle to its new position.

    GraphicsWindow.Height = 600
    GraphicsWindow.Width = 800
    GraphicsWindow.CanResize = "false"
    GraphicsWindow.Title = "Paddle Dash"
    Paddlew = 100 ' Paddle Width = 100
    Paddle = Shapes.AddRectangle(Paddlew, 20)
    Shapes.Move(paddle, 290, 550)
    Ball = Shapes.AddEllipse(20, 20)
    GraphicsWindow.KeyDown = paddlemove

    Sub paddlemove 
      Paddlex = Shapes.GetLeft(paddle)
      If GraphicsWindow.LastKey = "Left" Then
        Paddlex = Paddlex - 10 'with 10 moving is smoother
        If Paddlex < 1 Then 'if getleft = 1 then shapes.move(paddle, 1, 550).
          Paddlex = 1
        EndIf
        Shapes.Move(paddle, Paddlex, 550)
      EndIF 
      If GraphicsWindow.LastKey = "Right" Then
        Paddlex = Paddlex + 10 'with 10 moving is smoother
        If Paddlex > GraphicsWindow.Width - Paddlew Then 'if getleft = GraphicsWindow.Width - Paddle Width then shapes.move(paddle,  GraphicsWindow.Width - Paddle Width, 550).
          Paddlex = GraphicsWindow.Width - Paddlew
        EndIf
        Shapes.Move(paddle, Paddlex, 550)
      EndIf
    EndSub


    Sorry My Bad English
    Wednesday, February 16, 2011 1:08 PM