Computer Science Canada Moving when something touches it |
Author: | J-MoNeY [ Mon Sep 18, 2006 1:10 pm ] |
Post subject: | Moving when something touches it |
I'm doing an Air Hockey game. So far I have the paddle moving using input keydown and I need help making it so that the puck will move when the paddle collides with the puck. |
Author: | J-MoNeY [ Mon Sep 18, 2006 1:12 pm ] |
Post subject: | |
Sorry for double posting but forgot the code Quote: %********************************
%name block ICS ICS ICS ICS ICS %name - James Nguyen %date - September 18, 2006 %program description - game using input keydown %********************************* % % %********************************* %Algorithm %A set of instructions in order, used to pre-plan a program %********************************* % % %********************************* %Variables Dictionary %declare variable by name and type var chars : array char of boolean var x : int := 325 %..................x location of gamepiece var y : int := 150 %..................y location of gamepiece var a : int := 0 %....................comp score var b : int := 0 %....................playa score var ballx : int := 325 %..............x location of ball var bally : int := 250 %..............y location of ball %********************************* % % %********************************* %Procedures/Subprograms/Subroutines %do each seperatly in a block %<><><><><><><><><><><><><><><><><><>THE SCREEN BACKROUND proc settings drawfillbox (0, 0, 700, 700, brightblue) %......backround drawfillbox (270, 0, 400, 20, black) %..........net drawfillbox (270, 450, 400, 430, black) %.......net drawfillbox (0, 450, 639, 479, white) %.........top locate (1, 1) %.................................locates the put put "Computer score: ", a %.....................shows the score locate (1, 30) %................................locates the put put "Player score: ", b %.......................shows the score end settings %<><><><><><><><><><><><><><><><><><>THE GAMEPIECE proc gamepiece drawfilloval (x, y, 25, 25, red) %.............draws the main gamepiece drawfilloval (x, y, 10, 10, grey) %............part of the gamepiece end gamepiece %<><><><><><><><><><><><><><><><><><>THE BALL proc ball drawfilloval (ballx, bally, 15, 15, black) %...draws the ball end ball %********************************* % % %********************************* %Mainline %step by step enter the program settings %...................................draws backround loop %.......................................repeats forever setscreen ("offscreenonly") %............puts things into memory settings %...............................puts backround up again gamepiece %..............................puts gamepiece on screen ball %...................................puts ball on screen Input.KeyDown (chars) %..................checks for what characters are touched if chars (KEY_UP_ARROW) then %...........checks if the player touches up key delay (2) %..........................slows the game View.Update %........................puts things into memory y := y + 1 end if if chars (KEY_RIGHT_ARROW) then %........checks if player touches right arrow delay (2) %..........................slows the game View.Update %........................puts things into memory x := x + 1 end if if chars (KEY_LEFT_ARROW) then %.........checks if player touches left key delay (2) %..........................slows the game View.Update %........................puts things into memory x := x - 1 end if if chars (KEY_DOWN_ARROW) then %.........checks if player touches down key delay (2) %..........................slows the game View.Update %........................puts things into memory y := y - 1 end if %.......................................prevents game piece from goin left off screen if x < 30 then %........................checks if gamepiece goes too far left x := x + 1 end if %.......................................prevents game piece from going off screen if y > 420 then %.......................checks if game piece goes too far off screen y := y - 1 end if %.......................................prevents game piece from going off screen if x > 610 then %.......................checks if game piece goes too far off screen x := x - 1 end if %.......................................prevents game piece from going off the screen if y < 30 then %........................checks if game piece goes too far off screen y := y + 1 end if end loop %********************************* |
Author: | Clayton [ Mon Sep 18, 2006 3:04 pm ] | ||
Post subject: | |||
What you are looking for is Collision Detection also, your commenting could use a little work. you dont need to comment every single line of code you produce, variables should rarely need to be commented as their names should be all you need to tell you what they do. var a,b : int is not going to tell you in their names that what they do is hold the scores for the computer and player respectively. naming for variables also applies to procedures as well, the procedure "settings" sounds to me like it should control settings of the game, instead it draws the background ![]() also:
would be better as a single if construct using the elsif keyword instead of separate ifs. the reason for this is, if you hold down both the up and down arrow keys at the same time, you wont move! because "chars" is an [/b]array char of boolean[/b] (meaning it is an array with the number of elements being equal to the number of accessible characters with the type being boolean) you can have more than one char being pressed and Input.KeyDown will register all of them. Check out this tutorial if you need help on anything, as well, check out the entire Turing Walkthrough if you want to find out anything more. One last thing, remember to post code within [code ][/code ] or [syntax=][/syntax ] tags (without the spaces) as it allows your code to keep its indentation on the site ![]() |
Author: | J-MoNeY [ Mon Sep 18, 2006 4:42 pm ] |
Post subject: | |
Thanks Freakman. I have another question. When I run my program and I click off the screen everything but the backround and score disapears until I touch any of the moving keys. |
Author: | Clayton [ Mon Sep 18, 2006 4:48 pm ] |
Post subject: | |
its just because you catch it whenever the screen buffer is cleared (due to the effects of Draw.Cls) which you should consequentially read about here as your commenting about it suggests that you dont really understand whats going on. |
Author: | J-MoNeY [ Mon Sep 18, 2006 5:48 pm ] | ||
Post subject: | |||
Ok...heres what my code looks like now. Its really messed up.
|
Author: | Clayton [ Mon Sep 18, 2006 6:54 pm ] |
Post subject: | |
this is gonna require some good knowledge of trig to pull off and make it look half decent, not to say give up on it completely, but maybe start with something a bit less complex for now, unless you want to figure out all of the trig etc, i dont think you are quite up to this task right now for your collision detection though, as both of your objects that are colliding are circles, use Math.Distance (x1, y1, x2, y2 : int), which uses the distance formula to calculate the distance between two points (x1,y1) (x2,y2). |
Author: | J-MoNeY [ Mon Sep 18, 2006 8:28 pm ] |
Post subject: | |
We haven't learned Math.Distance yet in our class and our teacher isn't very smart, so could you explain how I would use Math.Distance for my program? |
Author: | NikG [ Mon Sep 18, 2006 11:17 pm ] |
Post subject: | |
All you need to do is search this site! But I did it for you: Math.Distance tutorial For future reference, check out the Turing Walkthrough page before asking for help on something. I'm sure it'll cover most of the questions you'll have. |
Author: | J-MoNeY [ Tue Sep 19, 2006 11:33 am ] |
Post subject: | |
I've read that tutorial but I'm still not sure how to use it in my program. |
Author: | TheOneTrueGod [ Tue Sep 19, 2006 12:29 pm ] | ||
Post subject: | |||
If the distance between two objects is less than the combined radius, then they have collided.
omg they have collided Then you just apply trig ratios. You can get the angle between the two by using Math.Angle (Though I prefer my function, as it proves that I actually know what i'm doing, as opposed to proving that the people who created Turing knew what they were doing ![]() Use this angle, and the paddle's speed (if you are using the mouse) to calculate how much force you should apply to the puck. I suggest you use the mouse, only because it allows you to hit the puck based on how hard you move the mouse... Though you would have to do some extra checks to see if you actually hit the puck or not. |
Author: | [Gandalf] [ Tue Sep 19, 2006 3:04 pm ] |
Post subject: | |
TheOneTrueGod wrote: You can get the angle between the two by using Math.Angle (Though I prefer my function, as it proves that I actually know what i'm doing, as opposed to proving that the people who created Turing knew what they were doing
![]() Incidentally, Math.Angle is your function. ![]() Math.Angle is not a function that comes with any official version of Turing, so either you created it and added it in, or someone else created it and you added it in. ![]() |
Author: | TheOneTrueGod [ Tue Sep 19, 2006 3:33 pm ] |
Post subject: | |
Quote: Incidentally, Math.Angle is your function.
Incidentally, yes it is, however, I wrote it before coming on this site, and before seeing any other implementations of it, so i'm proud of my work ![]() Anyways, since math.Angle apparantly doesn't come with Turing, i'll give you a l'il tutorial, but this is meant to be followed up by classwork... [syntax="ASCII Art Involved"] Note: FOR RIGHT TRIANGLES ONLY. Hypoteneuse || /| |V/ |Opposite |/o | ------------- |Adjacent | | o = "theta" (Its an angle. The others are the sides of the triangle) O = Opposite (Called thus because it is OPPOSITE theta) A = Adjacent (Called thus because it is ADJACENT to theta, and it isn't the hypoteneuse) H = Hypoteneuse (Called thus because some guy thought it would be cool to give the longest side the longest name ![]() SOH CAH TOA Sin(theta) = O/H Cos(theta) = A/H tan(theta) = O/A Now, you can apply these concepts something like this [calculator notation] tan^-1(O/A) = theta [turing notation] arctand(O/A) Where "arctan" is what tan^-1 is called, and "d" tells it you are working in degrees (And "arctan(O/A)" would be in radians) Remember, when A = 0, this is a special case. [/syntax] Hopefully that helps... If there isn't a tutorial on this stuff allready, I could try turning this into one if necessary... |
Author: | J-MoNeY [ Tue Sep 19, 2006 3:51 pm ] | ||
Post subject: | |||
I think I've got the collision detection working, but I can only get the puck moving in 1 direction (the left) and when it moves its moving too fast for me to see so its like disapering to the left everytime my paddle collides with it and when i hit the puck to the far left my ifs don't stop the puck from going off the screen and bouncing off somewhere else. I'm using input keydown because this is our first assignment and our teacher wants it to be a game using keydown only.
|