none
Pong

    Question

  • I have written a code for the game pong, it is the user against the computer. So far I have everything working fine but the game is unwinnable. I set the centre of the computers paddle to track the Y co-ordinates of the ball so it is impossible for the ball to pass the computers paddle. I want it to track the ball slightly slower than the ball is moving so that it is possible to score but I'm unsure how to do this. I think I may have to set another variable for how much the paddle moves and make this slightly less than deltaY but when I tried this it didn't work. Here is the code below, any help is appreciated. Thanks

    GraphicsWindow.Show()                     'Shows graphics window

    GraphicsWindow.Title = "Pong"             'Entitles the window 'pong'

    GraphicsWindow.Width = 700                'Sets the width of the window

    GraphicsWindow.Height = 500               'Sets the height of the window

    GraphicsWindow.CanResize = 8652345        'Sets the window so it cannot be resized

    gw = GraphicsWindow.Width                 'Declares the variable 'gw' as the height of the graphics window

    gh = GraphicsWindow.Height                'Declares the variable 'gh' as the width of the graphics window

    GraphicsWindow.MouseDown = moveball       'Runs the 'moveball' subroutine when the mouse is clicked

    GraphicsWindow.MouseMove = movepaddle     'Runs the 'movepaddle' subroutine when the mouse is moves

    Controls.ButtonClicked = Buttonclicked    'Runs the 'buttonclicked' subroutine when a button is clicked

    score1 = 0                                'declares the variable 'score1' as 0   (this will be the user's score)

    score2 = 0                                'declares the variable 'score2' as 0   (this will be the computers score)

     

     

    GraphicsWindow.FontSize="25"                                 '[[----------------------

    GraphicsWindow.DrawText(240,200,"WELCOME TO PONG")           '[[  DRAWS THE

    PLAY=Controls.AddButton("PLAY GAME",290,260)                 '[[  MAIN MENU

    INSTRUCTIONS=Controls.AddButton("Instructions",285,335)      '[[_____________

     

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Sub Buttonclicked

      If Controls.LastClickedButton = PLAY Then    'If user clicks the PLAY button then...

        GraphicsWindow.Clear()                     'Clears the graphics window

        Screen()                                   'Draws the game screen

        MovePaddle()                               'Moves the user's paddle (controlled by mouse)

        moveball()                                 'Sub for ball movement and computer paddle movement

      ElseIf Controls.LastClickedButton = INSTRUCTIONS Then   'If user clicks th INSTRUCTIONS button then

        GraphicsWindow.Clear()                                                                  

        GraphicsWindow.DrawText(20,80,"Use the W and S keys to control the left paddle")  '[[----------------------

        GraphicsWindow.DrawText(20,190,"The aim is to get more points than the computer") '[[   DRAW THE

        GraphicsWindow.DrawText(20,300,"If the ball passes the computers paddle ")        '[[   INSTRUCTIONS

        GraphicsWindow.DrawText(20,340,"you score a point.  GOOD LUCK!")                  '[[   SCREEN

        Main = Controls.AddButton("Main Menu", 270, 405)                                  '[[_____________

      ElseIf Controls.LastClickedButton = Main Then   'If user clicks the MAIN button then

        GraphicsWindow.Clear()                                            

        GraphicsWindow.FontSize="25"                                 '[[-------------------

        GraphicsWindow.DrawText(240,200,"WELCOME TO PONG")           '[[   REDRAWS THE 

        PLAY = Controls.AddButton("PLAY GAME",290,260)               '[[   MAIN MENU

        INSTRUCTIONS = Controls.AddButton("Instructions",285,335)    '[[___________

      EndIf

    EndSub

     

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Sub Screen

        ballsize = 15                                        'the diameter of the ball

        padlength = 100                                      'the length of the paddles

        padwidth = 10                                        'the width of the paddles

        pad1 = 0                                             'Starting Y co-ordinate of paddle 1 (for collisions)

        pad2 = 0                                             'Starting Y co-ordinate of paddle 2 (for collisions)

     

        Paddle = Shapes.AddRectangle(padwidth,padlength) '[[=============

        PaddleB = Shapes.AddRectangle(padwidth,padlength)'[[

        Ball = Shapes.AddEllipse(ballsize,ballsize)      '[[    DRAWS THE

        Shapes.Move(Ball,gw-gw/2,gh-gh/2)                '[[    GAME SCREEN

        Shapes.Move(PaddleB,670,0)                       '[[

        Shapes.Move(paddle,20,0)                         '[[

        GraphicsWindow.DrawLine(350,0,350,510)           '[[=============

    EndSub

     

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Sub MovePaddle

      paddleY = GraphicsWindow.MouseY                    'Y co-ordinates or paddle equal that of the mouse

      Shapes.Move(paddle,20, paddleY - padlength/2)      'Move paddle to mouseY, (-padlength/2 to centre of paddle)

      pad1 = paddleY - padlength/2                       'pad1 is variable used to work out collisions with this paddle

    EndSub

     

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Sub moveball

     

        x= gw - gw/2                 'Sets the X co-ordinate of the ball to the centre of the screen

        y= gh - gh/2                 'Sets the Y co-ordinate of the ball to the centre of the screen

    Poll=Math.GetRandomNumber(2)  '[[==============

      If poll=1 Then              '[[

        deltaX = 0.3              '[[   RANDOMISES

        deltaY = -0.05            '[[   DIRECTION OF

                                  '[[   THE BALL

      ElseIf poll = 2 Then        '[[   WHEN A NEW

        deltaX = -0.3             '[[   GAME STARTS

        deltaY = 0.05             '[[                          

      EndIf                       '[[==============

     

        GraphicsWindow.DrawText(300,50,score1)   'Prints the user's score (0)

        GraphicsWindow.DrawText(400,50,score2)   'Prints the computers score (0)

     

    While x<50000000                       'Will loop until X is equal to 50 million (never)

        x = x + deltaX                     'Sets a new X co-ordinate for the ball

        y = y + deltaY                     'Sets a new Y co-ordinate for the ball

     

        If (y <= 0 Or y >= gh - 30) Then   'top and bottom boundaries

          deltaY = -deltaY                 'reverses direction when the ball collides with boundary

        EndIf

     

        pad2=y                            'the Y co-ordinate for the computers paddle (for collisions)

     

        If (y >= pad1 and y <= pad1 + 100 and x <= 34 And x >= 24) then   'user paddle collision (when user hits ball)

          deltaX = -deltaX

          deltaX = deltaX + 0.01  'speeds up

          If y <= pad1 + 10 Then                    '[[============================

            deltaY = -0.2                           '[[

          ElseIf y <= pad1 + 20 Then                '[[

            deltaY = -0.16                          '[[

          ElseIf y <= pad1 + 30 Then                '[[

            deltaY= -0.12                           '[[

          ElseIf y <= pad1 + 40 Then                '[[

            deltaY= -0.08                           '[[

          ElseIf y <= pad1 + 50 Then                '[[            CONTROLS

            deltaY= -0.04                           '[[            THE ANGLE

          ElseIf y <= pad1 + 60 Then                '[[            THAT THE    

            deltaY= 0                               '[[            BALL BOUNCES

          ElseIf y <= pad1 + 70 Then                '[[            OFF THE 

            deltaY= 0.04                            '[[            USER'S PADDLE

          ElseIf y <= pad1 +80 Then                 '[[

            deltaY= 0.09                            '[[

          ElseIf y <= pad1 + 90 Then                '[[

            deltaY= 0.14                            '[[

          ElseIf y <= pad1 + 100 Then               '[[

            deltaY= 0.19                            '[[

        EndIf                                       '[[

        EndIf                                       '[[=========================

     

        If (y >= pad2 - 5 and y <= pad2 + 100 and x <= 661 And x >= 655) then  'computer paddle collision (when computer hits ball)

          deltaX = -deltaX

          deltaX = deltaX - 0.01  'speeds up                    '[[=========================

          If y <= pad2 + 10 Then                   '[[

            deltaY= -0.2                           '[[

          ElseIf y <= pad2 + 20 Then               '[[

            deltaY= -0.16                          '[[

          ElseIf y <= pad2 + 30 Then               '[[

            deltaY= -0.12                          '[[

          ElseIf y <= pad2 + 40 Then               '[[

            deltaY = -0.08                         '[[         CONTROLS

          ElseIf y <= pad2 + 50 Then               '[[         THE ANGLE

            deltaY = -0.04                         '[[         THAT THE               

          ElseIf y <= pad2 + 60 Then               '[[         BALL BOUNCES

            deltaY = 0                             '[[         OFF THE                

          ElseIf y <= pad2 + 70 Then               '[[         COMPUTER'S

            deltaY = 0.04                          '[[         PADDLE

          ElseIf y <= pad2 + 80 Then               '[[

            deltaY = 0.09                          '[[

          ElseIf y <= pad2 + 90 Then               '[[

            deltaY = 0.14                          '[[

          ElseIf y <= pad2 + 100 Then              '[[

            deltaY = 0.19                          '[[

          EndIf                                    '[[====================================

        EndIf

     

        If (x<0) Then                                    'If computer scores...

          GraphicsWindow.BrushColor="White"

          GraphicsWindow.DrawText(300,50,score1)         'NOTE: I was unsure how to clear text so I wrote the same text

          GraphicsWindow.DrawText(400,50,score2)         '              on top in white writing instead

          GraphicsWindow.BrushColor="Black"

          score2 = score2 + 1                            'sets computers new score (score 2)

          x = gw - gw/2                                  'sets X co-ordinate of ball to screen centre

          y = gh - gh/2                                  'sets Y  co-ordinate of ball to screen centre

          deltaX = 0.3                                   'sets initial ball velocity

          deltaY = -0.05                                 'sets initial ball gradient

          GraphicsWindow.DrawText(300,50,score1)         'prints user score

          GraphicsWindow.DrawText(400,50,score2)         'prints computer score

        EndIf

     

        If (x>700) Then                                  'If user scores...

          GraphicsWindow.BrushColor="White"      '[[----------

          GraphicsWindow.DrawText(300,50,score1) '[[  Covers up the old scores by writing them again in white

          GraphicsWindow.DrawText(400,50,score2) '[[______

          GraphicsWindow.BrushColor="Black"              'Sets the text colour to black for the new scores

          score1 = score1 + 1                            'sets user's new score (score 1)

          x = gw - gw/2                                  'sets X co-ordinate of ball to screen centre

          y = gh - gh/2                                  'sets Y  co-ordinate of ball to screen centre                                       

          deltaX = -0.3                                  'sets initial ball velocity

          deltaY = 0.05                                  'sets initial ball gradient

          GraphicsWindow.DrawText(300,50,score1)         'prints user score

          GraphicsWindow.DrawText(400,50,score2)         'prints computer score

        EndIf

     

        Shapes.Move(ball,x,y)                              'ball movement

        Movepaddle2()                                      'Moves the computers paddle

        Program.Delay(1)                                   'delays the loop 1ms so that the ball doesn't move too fast

     

     

      Endwhile

    EndSub

     

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Sub Movepaddle2                                  'Computers paddle

        padY = Y - padlength/2                       'Sets the centre of the paddle to mirror the Y co-ordinate of the ball 

        Shapes.Move(PaddleB,670,padY)                'Moves the computers paddle to its new position

    EndSub

    Wednesday, October 19, 2011 12:16 PM

Answers

  • Mike:

    Look into dithering of resolution, where your area of play can be increased according to level of play like 2pix, 4pix, 8pix, and 16pix being the easy level of play. What this means is your area of contact is greater based on easy level of play and allows for computer simulated error.

    Computer simulated errors is based on odds like one in two chances would be 2pix for the paddle postion and only one of the two positions would equate to contact. Think of it this way: you have a one pixel ball which is hit everytime by a one pixel paddle and it never misses.

    If you increase the paddle size you increase the odds of succesful hits, but when you distort the base area to a larger area of impact the definition controls the odds of success by changing a paddle position to one of two locations at impact thus introducing computer error and the chance of missing on impact. The computer random function is used to decide which position applies.

    This is the way casinos produce machines which can pay based on a selected payout ratio and control the odds of play where the game automatically adjusts for limited payouts when the game has randomly paid out to much already and adjusts the other way based on not having paid out to the required quota for that location.

    I will not supply any code for this, but based on your prior logic I believe you can accomplish this and setup of parameters for adjusting the odds during testing then apply your conditions based on user level of play for beginner, immediate, advanced.

    Cheers!

    Friday, October 21, 2011 3:58 AM

All replies

  • Pasting the code into small basic will make it easier to read. My notes got in the way a bit when I pasted it on here
    • Proposed as answer by James Kringel Friday, October 21, 2011 3:18 AM
    • Unproposed as answer by James Kringel Friday, October 21, 2011 3:19 AM
    Wednesday, October 19, 2011 12:19 PM
  • Mike:

    Look into dithering of resolution, where your area of play can be increased according to level of play like 2pix, 4pix, 8pix, and 16pix being the easy level of play. What this means is your area of contact is greater based on easy level of play and allows for computer simulated error.

    Computer simulated errors is based on odds like one in two chances would be 2pix for the paddle postion and only one of the two positions would equate to contact. Think of it this way: you have a one pixel ball which is hit everytime by a one pixel paddle and it never misses.

    If you increase the paddle size you increase the odds of succesful hits, but when you distort the base area to a larger area of impact the definition controls the odds of success by changing a paddle position to one of two locations at impact thus introducing computer error and the chance of missing on impact. The computer random function is used to decide which position applies.

    This is the way casinos produce machines which can pay based on a selected payout ratio and control the odds of play where the game automatically adjusts for limited payouts when the game has randomly paid out to much already and adjusts the other way based on not having paid out to the required quota for that location.

    I will not supply any code for this, but based on your prior logic I believe you can accomplish this and setup of parameters for adjusting the odds during testing then apply your conditions based on user level of play for beginner, immediate, advanced.

    Cheers!

    Friday, October 21, 2011 3:58 AM