Computer Science Canada Pong Program : Input.Keydown, and Computer Controlled Player |
Author: | Athlon64- [ 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. |
Author: | Neo [ Fri Jan 14, 2005 5:25 pm ] | ||
Post subject: | |||
Several problems with your code. 1. You need to declare chars
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. |
Author: | AsianSensation [ Fri Jan 14, 2005 5:26 pm ] | ||
Post subject: | |||
you should put the draw procedures inside the loop, like this:
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). |
Author: | Cervantes [ Fri Jan 14, 2005 5:31 pm ] |
Post 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. |
Author: | Athlon64- [ Fri Jan 14, 2005 5:36 pm ] |
Post subject: | |
Thanks for the quick responses ![]() ![]() |
Author: | Athlon64- [ Fri Jan 14, 2005 5:54 pm ] |
Post subject: | |
How would i go about putting boundaries in ? I have looked up maxx and maxy, but it doesnt seem to work. |
Author: | Neo [ Fri Jan 14, 2005 6:41 pm ] | ||
Post 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
Then do a similar set of ifs for Y boundries. |
Author: | Cervantes [ Fri Jan 14, 2005 7:07 pm ] | ||
Post subject: | |||
Correct, and if you want to shorten it, multiply the x velocity of the ball by -1.
|
Author: | Athlon64- [ Sat Jan 15, 2005 10:23 am ] |
Post 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 |
Author: | Athlon64- [ Sat Jan 15, 2005 10:27 am ] |
Post 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. |
Author: | Cervantes [ Sat Jan 15, 2005 10:56 am ] | ||
Post 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.
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. |
Author: | Athlon64- [ Sat Jan 15, 2005 12:11 pm ] | ||
Post subject: | |||
Sorry for the lack of information, hopefully this will be easier for you
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. |
Author: | Andy [ Sat Jan 15, 2005 12:22 pm ] | ||
Post subject: | |||
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. |
Author: | Neo [ Sat Jan 15, 2005 12:26 pm ] | ||||
Post subject: | |||||
You need to tell the ball to move.
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,
|
Author: | Andy [ Sat Jan 15, 2005 12:30 pm ] |
Post subject: | |
a bit too late wouldnt you say ![]() |
Author: | Athlon64- [ Sat Jan 15, 2005 12:33 pm ] |
Post subject: | |
Thanks guys ![]() |
Author: | Athlon64- [ Sat Jan 15, 2005 12:40 pm ] | ||
Post subject: | |||
What exactly does the
|
Author: | TheXploder [ Sat Jan 15, 2005 12:51 pm ] |
Post subject: | |
increases cy += ychange the circles y co-ordinate by a value of y. cx += xchange does the same, just for the x co-ordinate. |
Author: | Andy [ Sat Jan 15, 2005 12:53 pm ] |
Post subject: | |
xchange and ychange dictates the movement of the ball, you have to add it on to the current location to make the ball move |
Author: | Neo [ Sat Jan 15, 2005 1:40 pm ] |
Post subject: | |
cx+=xChange is the same as cx:=cx+xChange Perhaps that is were u are confused? Since I see you use the second way in ur program for changing paddle position. |
Author: | Athlon64- [ Sat Jan 15, 2005 6:58 pm ] | ||
Post subject: | |||
Alright, i got the CPU controlled part working ![]() ![]() Here is the code
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. |
Author: | Cervantes [ Sat Jan 15, 2005 7:49 pm ] | ||
Post subject: | |||
|
Author: | mike200015 [ Fri Feb 18, 2005 6:10 pm ] |
Post subject: | |
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 ... |
Author: | mike200015 [ Fri Feb 18, 2005 6:14 pm ] |
Post subject: | |
srry nvm, i didnt know you could write chars ('w') and it would work. nvm what i said b4 it works your original way |