Computer Science Canada We got a big one here :) Procedure's vs processes , collision detection, tell time between "a" to "b" |
Author: | Shadow456732 [ Wed Oct 17, 2012 9:33 pm ] | ||
Post subject: | We got a big one here :) Procedure's vs processes , collision detection, tell time between "a" to "b" | ||
What is it you are trying to achieve? Well, a few things actually. In general, i'm trying to write a game of pong.. The classic bouncy paddle game What is the problem you are having? So , here are my problems: 1.) I'm essentially trying to make it so a ball is constantly bouncing around a screen, while at the same time, i can use arrow keys to control the location of a paddle.... The problem being that when i use process's to accomplish this (I know i shouldn't... Couldn't think of anything else), the code seems to bug out.. I.e collision detection is random, the ball bounces out of the screen, etc.... Though! When i use procedures in a loop, i run into a problem where the getch statement paused the program until input.. Which causes the ball to not move constantly. Suggestions? 2.) Collision detection. Now this could be a problem with the process's, because i haven't tested collision detection in a setup where i used procedure's. I am essentially trying to tell when a rectangle touches a ball. By checking if the x and y coordinates of the ball are within a range of the x1,x2,y1,y2 coordinates of the rectangle. If so, causing the ball to bounce back. Now, it seems that the code i use bugs out, again. Could be because of processes. Will provide both versions of the code. 3.) Now, i think as a solution to the procedure version of the code. I could add something to check the time before and during a getch statement. And if it is greater than x, then continue along with the code, ignoring input from the getch. Not sure how to accomplish this as in turing, looking at some of the documentation, the time functions don't seem to do the job. Thanks a bunch guys/girls if you could help me!!! I'm really liking programming so far, just need to get the basic concepts down. I really appreciate it ![]() Describe what you have tried to solve this problem Well, ive tried both processes and procedures, haven't gotten very far. It seems that there is a method that works through processes, though i dont really want to use processe's considering that if my code gets to complicated, ill have many problems. Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) VERSION WITH PROCESSES ------------------------------------- % Project : Pong setscreen ("graphics:700;500") View.Set ("nocursor") var choice : int var x : int := Rand.Int (300, 400) var y : int := Rand.Int (1, 500) var c : int := Rand.Int (10, maxcolor) var cc : int := Rand.Int (10, maxcolor) var radius : int := 10 var xchange, ychange : int := 1 var lastx, lasty : int var playerx : int := 50 var playery : int := 300 var playerx2 : int := playerx + 30 var playery2 : int := playery + 30 var arrow : string (1) proc hitbox if (x >= playerx and x <= playerx2)and (y >= playery and y <= playery2) then xchange := -1 end if end hitbox process ball loop delay (4) lastx := x - (xchange * 2) lasty := y - (ychange * 2) Draw.FillOval (lastx, lasty, 14, 14, 0) delay (1) Draw.FillOval (x, y, radius, radius, c) x := x + xchange y := y + ychange if x >= 680 and xchange = 1 then xchange := -1 elsif x <= 5 and xchange = -1 then xchange := 1 elsif y >= 480 and ychange = 1 then ychange := -1 elsif y <= 5 and ychange = -1 then ychange := 1 end if hitbox end loop end ball process playercube loop Draw.FillBox (playerx, playery, playerx2, playery2, cc) getch (arrow) if ord (arrow) = 200 then if playery2 > 480 then playery := 480 - 30 end if playery := playery + 15 Draw.FillBox (playerx, playery - 15, playerx2, playery2 - 15, 0) elsif ord (arrow) = 208 then if playery < 20 then playery := 20 end if playery := playery - 15 Draw.FillBox (playerx, playery + 15, playerx2, playery2 + 15, 0) end if hitbox playerx2 := playerx + 30 playery2 := playery + 30 end loop end playercube fork ball fork playercube VERSION WITH PROCEDURE'S, NO COLLISION CODE ---------------------------------------------------------------- % Project : Pong setscreen ("graphics:700;500") View.Set ("nocursor") var choice : int var x : int := Rand.Int (300, 400) var y : int := Rand.Int (1, 500) var c : int := Rand.Int (10, maxcolor) var cc : int := Rand.Int (10, maxcolor) var radius : int := 10 var xchange, ychange : int := 1 var lastx, lasty : int var playerx : int := 50 var playery : int := 300 var playerx2 : int := playerx + 30 var playery2 : int := playery + 30 var arrow : string (1) proc hitbox end hitbox proc ball delay (4) lastx := x - (xchange * 2) lasty := y - (ychange * 2) Draw.FillOval (lastx, lasty, 14, 14, 0) delay (1) Draw.FillOval (x, y, radius, radius, c) x := x + xchange y := y + ychange if x >= 680 and xchange = 1 then xchange := -1 elsif x <= 5 and xchange = -1 then xchange := 1 elsif y >= 480 and ychange = 1 then ychange := -1 elsif y <= 5 and ychange = -1 then ychange := 1 end if end ball proc playercube Draw.FillBox (playerx, playery, playerx2, playery2, cc) getch (arrow) if ord (arrow) = 200 then if playery2 > 480 then playery := 480 - 30 end if playery := playery + 15 Draw.FillBox (playerx, playery - 15, playerx2, playery2 - 15, 0) elsif ord (arrow) = 208 then if playery < 20 then playery := 20 end if playery := playery - 15 Draw.FillBox (playerx, playery + 15, playerx2, playery2 + 15, 0) end if playerx2 := playerx + 30 playery2 := playery + 30 end playercube loop ball playercube View.Update() end loop
Please specify what version of Turing you are using Newest Version (4.1.1 i believe) |
Author: | Tony [ Wed Oct 17, 2012 10:25 pm ] |
Post subject: | RE:We got a big one here :) Procedure\'s vs processes , collision detection, tell time between "a" to "b& |
you should not block (wait on user input), to allow other pieces of code to execute. You have two options here -- hasch or Input.KeyDown |
Author: | mirhagk [ Thu Oct 18, 2012 6:48 am ] |
Post subject: | RE:We got a big one here :) Procedure\'s vs processes , collision detection, tell time between "a" to "b& |
For games it's suggested you do a loop similar to the following: loop process input (use Input.KeyDown) move objects cls draw objects View.Update() end loop it's just better if you organize your code so you do all the updating and all the drawing at once. |
Author: | Insectoid [ Thu Oct 18, 2012 7:06 am ] |
Post subject: | RE:We got a big one here :) Procedure\'s vs processes , collision detection, tell time between "a" to "b& |
Mirhagk, this entire program can and should be written without any processes. |
Author: | mirhagk [ Thu Oct 18, 2012 10:53 am ] | ||
Post subject: | RE:We got a big one here :) Procedure\'s vs processes , collision detection, tell time between "a" to "b& | ||
Yes that's what I'm saying, maybe I should've been more clear.
Sorry if it was unclear and it seemed like I was suggesting to use processes. Processes in a good language are evil and should never be used unless you absolutely have to (networking mostly is the only scenario where multiple sections of code should run at once. Intense processing programs may, but it should be a last resort). Processes in turing are downright terrible, and should be killed. With fire. Now. |
Author: | Shadow456732 [ Thu Oct 18, 2012 4:46 pm ] |
Post subject: | RE:We got a big one here :) Procedure\'s vs processes , collision detection, tell time between "a" to "b& |
Alright! So using a nested if method, where the if statement for the getch was within an if with the hasch, it worked perfectly! Also, if you guys didn't know, this program dosen't use a single cls command. ![]() |
Author: | mirhagk [ Thu Oct 18, 2012 5:54 pm ] |
Post subject: | RE:We got a big one here :) Procedure\'s vs processes , collision detection, tell time between "a" to "b& |
with view.update done correctly there should be absolutely no flickering with cls. if you follow the way I suggested, using Time.DelaySinceLast() to delay it (after View.Update) then you should get rid of all your problems with flickering or inconsistencies. |