
-----------------------------------
Athlon64-
Fri Jan 14, 2005 5:17 pm

Pong Program : Input.Keydown, and Computer Controlled Player
-----------------------------------
Hi everyone, i am pretty new to turing but im making a pong program, my first question is regarding the Input.Keydown  here is the code i have right now


colorback (brightblue)
    cls

    var x : int := 5    %Bottom left x co-ordinate for the first paddle
    var y : int := 2    %Bottom left y co-ordinate for the first paddle
    var x1 : int := 15  %Top right x co-ordinate for the first paddle
    var y1 : int := 60  %Top right y co-ordinate for the first paddle
    var x2 : int := 625 %Bottom left x co-ordinate for the second paddle
    var y2 : int := 340 %Bottom left y co-ordinate for the second paddle
    var x3 : int := 635 %Top right x co-ordinate for the second paddle
    var y3 : int := 398 %Top right y co-ordinate for the second paddle

    Draw.FillBox (x, y, x1, y1, red)
    Draw.FillBox (x2, y2, x3, y3, green)

    

    loop
        Input.KeyDown (chars)
        if chars ('w') then
            y := y + 5
            y1 := y1 + 5
        elsif chars ('s') then
            y := y - 5
            y1 := y1 - 5
        end if
    end loop

So, when i press 'w' the paddle should move up, but it isnt.....i know i am probably just missing something here. 

Secondly, I am going to add in a CPU controlled opponent, now im assuming if i wanted to get it to hit the ball every time i would just make the y coordinates of the other paddle = the y coordinate of the ball right ? However, how would i allow the possibility of the CPU missing the ball ?


EDIT : I have declared chars, but in the global variable section (that part is in a procedure), i have tried moving it but it still does not work.

-----------------------------------
Neo
Fri Jan 14, 2005 5:25 pm


-----------------------------------
Several problems with your code.
1. You need to declare chars 

var chars : array char of boolean

2. Draw ur paddles inside the loop
3. Look up setscreen("offscreenonly"), View.Update and cls

As for ai, you dont give the paddle the same coordinates as the ball. Instead you tell the paddle what to do based on the balls position and the paddles position.

-----------------------------------
AsianSensation
Fri Jan 14, 2005 5:26 pm


-----------------------------------
you should put the draw procedures inside the loop, like this:

loop
    Input.KeyDown (chars)
    if chars ('w') then
        y := y + 5
        y1 := y1 + 5
    elsif chars ('s') then
        y := y - 5
        y1 := y1 - 5
    end if
    Draw.FillBox (x, y, x1, y1, red)
    Draw.FillBox (x2, y2, x3, y3, green)
    delay (50)
    cls
end loop

As for AI, don't just have the AI match the ball height, instead, have it so the AI would move 1 unit up at a time, depending on the ball position. A harder AI would be able to move faster (so moves more units than the crappier AI).

-----------------------------------
Cervantes
Fri Jan 14, 2005 5:31 pm


-----------------------------------
Don't declare variables inside a procedure.  It won't work.  At least, it doesn't for me.
As for AI, you might want to consider giving the AI an element of randomness and poor judgement.  That is, make a random number (say, from 1 to 1000).  If the random number = 1, make the computer's paddle move one pixel (or whatever it's usual speed is) in the wrong direction.

-----------------------------------
Athlon64-
Fri Jan 14, 2005 5:36 pm


-----------------------------------
Thanks for the quick responses :) That was such a dumb question :(.

-----------------------------------
Athlon64-
Fri Jan 14, 2005 5:54 pm


-----------------------------------
How would i go about putting boundaries in ? I have looked up maxx and maxy, but it doesnt seem to work.

-----------------------------------
Neo
Fri Jan 14, 2005 6:41 pm


-----------------------------------
Maxx and maxy simply give you the number of pixels the run window is. You'd have to use them with an if statment for boundries.
If your looking to do ball boundries you would do something like


if ballX>maxx then
     change_ball_direction_to_left
elsif ballX maxx then
   ball.x *= -1
   %The above line is the same as ball.x := ball.x * -1
end if


-----------------------------------
Athlon64-
Sat Jan 15, 2005 10:23 am


-----------------------------------
Alright, im sort of stuck again, the boundaries i have set don't seem to be working properly. 

Here is the code...

%Decleration of Global Variables

var playerorcpu : int  %Stores whether or not the player wants to play against someone, or the computer.
var ballcolor : int %Stores the color of the ball the user wishes to play with.
var ball : string
var xchange : int := 10
var ychange : int := 10

%PROCEDURE - This is the main menu of the program where you will set up the type of game that you wish to play.

procedure TopMenu

    locate (10, 10)

    put "Would you like to play against someone, or against the computer ?"
    put "(1) Another Player, (2) The Computer"

    get playerorcpu

    put "What color of ball would you like to play with ? (1) Red, (2) Yellow, or (3) Blue"

    get ballcolor

    if ballcolor = 1 then
        ball := 'red'
    elsif ballcolor = 2 then
        ball := 'yellow'
    elsif ballcolor = 3 then
        ball := 'blue'
    end if

end TopMenu


procedure ComputerControlled

    var x : int := 5        %Bottom left x co-ordinate for the first paddle
    var y : int := 2        %Bottom left y co-ordinate for the first paddle
    var x1 : int := 15      %Top right x co-ordinate for the first paddle
    var y1 : int := 60      %Top right y co-ordinate for the first paddle
    var x2 : int := 625     %Bottom left x co-ordinate for the second paddle
    var y2 : int := 340     %Bottom left y co-ordinate for the second paddle
    var x3 : int := 635     %Top right x co-ordinate for the second paddle
    var y3 : int := 398     %Top right y co-ordinate for the second paddle
    var cx : int := 200
    var cy : int := 200

    loop
        cls
        delay (50)
        View.Update
        setscreen ("offscreenonly")

        var chars : array char of boolean

        Input.KeyDown (chars)
        if chars ('w') then
            y := y + 10
            y1 := y1 + 10
        elsif chars ('s') then
            y := y - 5
            y1 := y1 - 5
        end if

        Draw.FillBox (x, y, x1, y1, red)
        Draw.FillBox (x2, y2, x3, y3, green)
        
        Draw.FillOval (cx, cy, 10, 10, red)
        
        cx := cx - xchange
        cy := cy - ychange
        
       if cx < 5 then
       cx := cx + xchange
       elsif cx > 625 then
       cx := cx - xchange
       elsif cy < 10 then 
       cy := cy + ychange 
       end if
       
       
        
        if cx = x - 10 and cy > y and cy < y1 then
        cx := cx + xchange
        end if 
       
        View.Update

    end loop

end ComputerControlled




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   MAIN PROGRAM   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%




ComputerControlled

-----------------------------------
Athlon64-
Sat Jan 15, 2005 10:27 am


-----------------------------------
Ok, I figured out it is because it always comes back to : cx := cx - xchange
so the ball never moves, so where should i put that part in my program (I need to have it somewhere to get the ball to move in the first place.

-----------------------------------
Cervantes
Sat Jan 15, 2005 10:56 am


-----------------------------------
Notice how we've all been using xchange should be dynamic. It should change if the ball hits the paddle.

if ballX = paddle1Y and ballY = y2 then                                %Changes the balls direction when it hits paddle
            xchange := xchange * -1
            cx := x2 - 5
        end if

        if cy > 380 then                                                    %Sets the ball boundaries.
            ychange := ychange * -1
        elsif cy < 15 then
            ychange := ychange * -1
        elsif cx < 10 then
            xchange := xchange * -1
        elsif cx > 620 then
            xchange := xchange * -1
        end if
        
        cx += xchange
        cy += ychange
        View.Update


I have one problem, im not sure what the best way to put boundaries on the paddles would be, anyone have soem insight ? 

Thanks alot for your help, i know most of these are basic questions.

-----------------------------------
Cervantes
Sat Jan 15, 2005 7:49 pm


-----------------------------------

if paddle1Y < 0 then
  paddle1Y := 0
elsif paddle1Y > maxy then
  paddle1Y := maxy
end if


-----------------------------------
mike200015
Fri Feb 18, 2005 6:10 pm


-----------------------------------
your paddle doesn't move when you press w because when you use Input.KeyDown there are specific codes for each key, so you need to write : if chars (KEY_UP_ARROW) then  ...

-----------------------------------
mike200015
Fri Feb 18, 2005 6:14 pm


-----------------------------------
srry nvm, i didnt know you could write chars ('w') and it would work. nvm what i said b4 it works your original way
