Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Pong Program : Input.Keydown, and Computer Controlled Player
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Athlon64-




PostPosted: Fri Jan 14, 2005 5:17 pm   Post subject: 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.
Sponsor
Sponsor
Sponsor
sponsor
Neo




PostPosted: Fri Jan 14, 2005 5:25 pm   Post subject: (No subject)

Several problems with your code.
1. You need to declare chars
code:

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




PostPosted: Fri Jan 14, 2005 5:26 pm   Post subject: (No subject)

you should put the draw procedures inside the loop, like this:

code:
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




PostPosted: Fri Jan 14, 2005 5:31 pm   Post subject: (No subject)

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-




PostPosted: Fri Jan 14, 2005 5:36 pm   Post subject: (No subject)

Thanks for the quick responses Smile That was such a dumb question Sad.
Athlon64-




PostPosted: Fri Jan 14, 2005 5:54 pm   Post subject: (No subject)

How would i go about putting boundaries in ? I have looked up maxx and maxy, but it doesnt seem to work.
Neo




PostPosted: Fri Jan 14, 2005 6:41 pm   Post subject: (No subject)

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

code:

if ballX>maxx then
     change_ball_direction_to_left
elsif ballX<0 then
     change_ball_direction_to_right
end if


Then do a similar set of ifs for Y boundries.
Cervantes




PostPosted: Fri Jan 14, 2005 7:07 pm   Post subject: (No subject)

Correct, and if you want to shorten it, multiply the x velocity of the ball by -1.
code:

if ball.x < 0 or ball.x > maxx then
   ball.x *= -1
   %The above line is the same as ball.x := ball.x * -1
end if
Sponsor
Sponsor
Sponsor
sponsor
Athlon64-




PostPosted: Sat Jan 15, 2005 10:23 am   Post subject: (No subject)

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-




PostPosted: Sat Jan 15, 2005 10:27 am   Post subject: (No subject)

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




PostPosted: Sat Jan 15, 2005 10:56 am   Post subject: (No subject)

Notice how we've all been using [ code][ /code] tags? Please, do the same.
As for your program: What is your question? And proper commenting of your code would make it easier for us to help you. So would naming variables with some description. Why do you have so many x's and y's, then you've got a cx, and cy, which I'm assuming is for the ball, based on the code. Just have an x and y for paddle 1, and an x and y for paddle 2. And, of course, an x and y for the ball.
xchange should be dynamic. It should change if the ball hits the paddle.
code:

if ballX <= paddle1X and ballY >= paddle1Y and ballY <= paddle1Y + paddle1width then
  ballVX := ballVX * -1
  % ballVX is the same as your xchange, though more descriptive.  ball = ball, V = velocity, X = value in the x plane
end if


I had lots of trouble not only understanding your code, but understanding your question. Please, describe your situation such that we don't have to guess and struggle just to help you.
Athlon64-




PostPosted: Sat Jan 15, 2005 12:11 pm   Post subject: (No subject)

Sorry for the lack of information, hopefully this will be easier for you

code:
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 := 5     %X coordinate for the ball
    var cy : int := 5     %Y Coordinate for the ball

   
   
    loop
        cls
        delay (50)
        View.Update
        setscreen ("offscreenonly")     %Prevents flickering

        var chars : array char of boolean

        Input.KeyDown (chars)           %Moves the paddle based on users commands
        if chars ('w') then
            y := y + 10
            y1 := y1 + 10
        elsif chars ('s') then
            y := y - 10
            y1 := y1 - 10
        end if

        Draw.FillBox (x, y, x1, y1, red)        %The first paddle   
        Draw.FillBox (x2, y2, x3, y3, green)    %The second paddle
       
        Draw.FillOval (cx, cy, 10, 10, red)     %The ball

       if cx < x1 + 5 and cy >= y and cy <= y1 then                      %Changes the balls direction when it hits paddle
       cx := cx + xchange
       elsif cx > x2 - 5 and cy >= y2 and cy <= y3 then               %Changes the balls direction when it hits paddle
       cx := cx - xchange
       end if

       
     
        View.Update

    end loop


Alright, the problem i am having is getting the ball to move properly, once it hits the paddle it seems to get stuck and just stays in one spot. I know I am probably just missing something small. If you need anymore details just ask.
Andy




PostPosted: Sat Jan 15, 2005 12:22 pm   Post subject: (No subject)

code:

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 := 100         %X coordinate for the ball
var cy : int := 100       %Y Coordinate for the ball
var xchange, ychange := 5


loop
    cls
    delay (50)
    setscreen ("offscreenonly")         %Prevents flickering

    var chars : array char of boolean

    Input.KeyDown (chars)               %Moves the paddle based on users commands
    if chars ('w') then
        y := y + 10
        y1 := y1 + 10
    elsif chars ('s') then
        y := y - 10
        y1 := y1 - 10
    end if

    Draw.FillBox (x, y, x1, y1, red)            %The first paddle
    Draw.FillBox (x2, y2, x3, y3, green)        %The second paddle

    Draw.FillOval (cx, cy, 10, 10, red)         %The ball

    if cx <= x1 + 5 and cy >= y and cy <= y1 then                         %Changes the balls direction when it hits paddle
        xchange := xchange * -1
        cx:=x1+5

    elsif cx >= x2 - 5 and cy >= y2 and cy <= y3 then                  %Changes the balls direction when it hits paddle
        xchange := xchange * -1
        cx:=x2-5
    end if

    cx += xchange
    cy += ychange
    View.Update

end loop


two things, you need to take off the View.Update at the top of the loop, or it wont help the flickering, also, your ball gets stuck because your ball could get stuck within the paddle, simply set the location of the ball to be out of the paddle after you hit it. also, everytime you hit the paddle, you need to flip the xchange so it'll go in the opposite directions. add xchange and ychange at the bottom of the loop.
Neo




PostPosted: Sat Jan 15, 2005 12:26 pm   Post subject: (No subject)

You need to tell the ball to move.
code:

cx+=xDirection
cy+=yDirection


If the ball hits a boundry or a paddle you multiply the direction by -1. Also make it easier on urself by using less variables. You dont need seperate variables for each corner of the paddle, do something like,

code:

Draw.FillBox (x, y, x+10, y+58, red)
Andy




PostPosted: Sat Jan 15, 2005 12:30 pm   Post subject: (No subject)

a bit too late wouldnt you say Wink
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 24 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: