Computer Science Canada Help me comprehend some (SIMPLE) Pong coding |
Author: | Aange10 [ Sat Sep 03, 2011 5:10 pm ] | ||
Post subject: | Help me comprehend some (SIMPLE) Pong coding | ||
What is it you are trying to achieve? One of my friends had made this super simple pong game, and gave me the source code to play around with. I'm trying to learn how to code in turing, and I know that this is one of the best ways to go about it. I've read a lot of the walkthroughs, but everything is easier, and frankly better hands on. So I was wondering if somebody could help me understand this SIMPLE pong coding. So I'm asking, to whoever finds time to help me out, if he/she would go through this short coding and put % comments, telling me what the computer is "doing now". If you don't have time, I understand. Just please don't flame. What is the problem you are having? I'm not comprehending all of the coding. Describe what you have tried to solve this problem I've tried going through and taking mental notes of what i do know, but I run into gaps that pretty much make any information after it "come from nowhere" (because I don't know what had just happened). Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Please specify what version of Turing you are using This coding works with 4.1.1 (newest version) |
Author: | Insectoid [ Sat Sep 03, 2011 5:26 pm ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
Quote: I'm trying to learn how to code in turing, and I know that this is one of the best ways to go about it.
I'm not a fan of this method of 'learning'. It's like saying taking apart your car is the best way to become a mechanic. There are several tutorials that explain how to create a game in Turing from scratch. There's also the Turing Walkthrough, which will teach you enough to start using the language. Nobody is gonna go through this code an explain it to you. It's not worth our time. If you really want to learn, put some effort in. Pasting code on a forum and asking us to explain it isn't effort. |
Author: | Aange10 [ Sat Sep 03, 2011 5:41 pm ] |
Post subject: | Re: Help me comprehend some (SIMPLE) Pong coding |
Quote: Nobody is gonna go through this code an explain it to you. It's not worth our time. If you really want to learn, put some effort in. Pasting code on a forum and asking us to explain it isn't effort.
If you don't want to do it, then don't. But don't flame me. For example, telling me that I am not trying to "put some effort in" is very unproductive, and I consider that flaming. Because you are insulting me when you have no idea what I've been doing. Also, I said very nicely if you didn't want to take the time to do it, then that is fine. But telling me that asking somebody to explain it to me isn't "effort" is ignorance on your part. I'm using this stuff to learn. I'm not being lazy OR lacking effort. Simply putting it, your teacher explains a worksheet to you before you do it. Or the paper has directions at the top before you start working. I am just asking for somebody kind enough to help me, and ask those who don't want to NOT to flame me. I'm currently working on coding a game based off the same code this is using. Sadly I don't think it's worth my time to copy and paste and just change some words... I'm trying to take this code and pretty much re-write the game, in my own taste. I've read the walkthrough, and I use it as a reference everytime I need help. I said that I've read the walkthroughs in my post. It's just hard to read text straight from the book, and use it in practice. Which is why I said that hands on is better. |
Author: | Raknarg [ Sat Sep 03, 2011 5:45 pm ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
If your friend is going to give you this as source code, he should comment it to make it easier for others to read. Also he should start using variable with a little more meaning. Eventually once you get into larger and more complx games, you cannot have a hoarde of random letter variables. Not to mention its a pain in the ass to read. |
Author: | Insectoid [ Sat Sep 03, 2011 6:29 pm ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
Ah, I missed the line where you said you read some walkthroughs. However, that doesn't change my opinion. How is looking at someone else's code any more hands-on than reading a tutorial or textbook? They have examples with comments in them! I'd not be surprised if there was a specific Pong tutorial. Yes, I'm flaming. I admit it. But now I'm gonna help you out. So, what is Pong? It's essentially two paddles, each of which moves up and down. Do you know how to take keyboard input and use it to make something move? If you don't, look up Input.KeyDown. There's also a ball, which moves in many directions. For simplicity let's limit it to 4 (up right, down right, up left, down left). You'll want x and y direction variables, which are either 1 or -1. The ball moves at ball_speed*x_direction and ball_speed*y_direction (where ball_speed is the number of pixels the ball moves in the X or Y direction per frame). Changing the direction of the ball is as simple as multiplying the x_direction or y_direction by -1. For example, if your ball hits the roof, multiply the direction by -1 to change direction. If you don't know how to do basic rectangular collision (we can treat the ball as a rectangle) look it up. There's tons of tutorials. As for score- maintain an integer variable for each player that represents the score. If you want sounds, you can just play them whenever say, a collision happens (by the time you even think about sounds the game should be functional). If you want the ball to move in more than 4 directions based on the speed of the paddle, look up 360 degree movement (there's a tutorial dedicated to it). If, say, the 'up' key is pressed when the ball collides with a paddle, the ball gains Y velocity and loses X velocity (the formula itself is up to you). Oh, and you may run into issues with your ball or paddle 'sticking' to the walls. This is because the ball may be 5 pixels from the roof in one frame, and then it moves 10 pixels up so it's 5 pixels above the roof. If this happens the game will just keep flipping the y_direction variable so the ball will never really move. To remedy this, whenever the ball collides with the roof, set the y position to just under the roof (adapt for other walls). tl;dr- things you need to know: -Input.KeyDown -Collision detection -movement (basic or advanced. Up to you) -Making things happen at the same time (I didn't mention this earlier) -View.Update (I didn't mention this earlier either) Thats it. That's all you need to know to build a functional game of Pong. |
Author: | Zren [ Sat Sep 03, 2011 6:36 pm ] | ||
Post subject: | Re: Help me comprehend some (SIMPLE) Pong coding | ||
These variables could be better. ip = isPaused, but it really should be gameState if your using it as an integer instead of a boolean (as you use it for the main menu as well (values 2, 3, ... as well). gx, gy = bottom left point of paddle. The size of the paddle are hardcoded constants (bad). Hardcoded constants make it hard to change sizes on the fly. Right now its a 59x11 box. If we were to store then in paddleWidth and paddleHeight, then we could later on change the size of it easier (powerups etc). x, y are the coordinates of the ball. You should specify it! Especially when you're dealing with more than one object with coordinates (which is nearly everytime). xdir, ydir is the displacement in their respective planes each frame. When it hits a wall, exteremely simplified (and incorrect) collision detection is done. Basically when you hit the left wall (x < 0) you reverse your direction in the x plane. He screwed up since he used the range (-2, 2), so when it hit that wall: if x < 0 then xdir := 1 end if Boom, the speed decreased. Use: xDir := -xDir as -(-n) = +n t represents the frameCount. t is usually short for time, so in this case he could probably get away with it. m = minutes delay(5) is 5 milliseconds. 1000 / 5 = 200 framesPerSecond Since t = frameCount ... frameCount / framesPerSecond = seconds t div 200 = 60 is one minute. s is a score -> put "Times Ape-Shit'd : ", s fs is used to count frames. Specifically for the little blinking "hold space" message. It's one of the more attrocious variables here as your already counting the frames and could just do: if frameCount % 3 = 0 then blink! % is the modulus operator btw, it's to calculate the remainder in division. Useful for seeing if a number is even or odd (n % 2 = 0 -> even). Ignore the fact he created 3 different arrays to capture keyboard states. var chars : array char of boolean var chars5 : array char of boolean var chars6 : array char of boolean Each section of block of code has it's own scope. At the end of that block of code, all local variables (those defined in the block of code) are forgotten). Eg, This is valid code:
... Okay, this turned out longer than I thought. |
Author: | Aange10 [ Sat Sep 03, 2011 6:51 pm ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
Thank you guys both for the help! I'm going to go begin a new coding, and start it from scratch. Shouldn't take me too long. I'll post the results here. and *ALSO* I'm very confused on how he made the "hold space" instead of "press space". |
Author: | Zren [ Sat Sep 03, 2011 7:09 pm ] | ||
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding | ||
I should probably mention the right ways to do things he did badly too... Mainly because there's the chance you'll try to copy this guy instead of thinking for yourself or searching around as you're probably ignorant to realizing there's better ways. To calculate your timePlayed:
I mentioned you could use the same variable name between different blocks of code (so long as they're locally declared variables), but you could just get array with one variable to begin with. I'm talking about the boolean array for keyboard input. The only reason you'd have two arrays is if you wanted to store what you pressed the last frame. Which is particularly useful if you wanted to find out if a key was released or pressed down from the last frame. The "hold" thing works because he drew the text, then delayed for half a second before checking the keyboard input and reacting to it. If you released before the delay was up, it wouldn't register. It works, but only when nothing else is being drawn/calculated. |
Author: | Aange10 [ Sat Sep 03, 2011 9:54 pm ] | ||
Post subject: | Re: Help me comprehend some (SIMPLE) Pong coding | ||
Done! ![]() .. And to prove i'm not just effortlessly sitting around, I've done my best at programming a paddle ball game (I call that pong, sorry) and keeping it nice and neat. PLEASE critique!
If you guys would.. I'd like to know 2 things. 1) How can i make my timer only tell me the integer the second is in, not the real number? (No decimals) 2) Can you help me fix my ball disappearing at the top (Because the put lines are coloring the yellow white)? Also if you wanted to help me with an annoyance I have found... My "Main Menu Debugging System" makes my menu tell you that you entered an incorrect key if you choose a key other than "b". When it does this all it does is put a statement, and then exits the loop and continues on through the main loop. (Which when you get back to go through it a second time, mainmenu will still be set to true since you never hit "b", making you go back through the main menu. Which is how the debugging works.) However, the "annoyance" is that it flickers the pong ball/paddle screen before it takes you back. Can you think of a way to prevent this? |
Author: | Insectoid [ Sat Sep 03, 2011 10:39 pm ] | ||
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding | ||
1. round() is your friend. 2. colorback() will change the color of those lines. Don't draw a box around the screen. Just use colorback(). Annoyance: Look at your code.
Remember that you're in offscreenonly mode. |
Author: | Aange10 [ Sat Sep 03, 2011 11:17 pm ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
Thankyou for the help! It all works! Anything that I should improve on/ add in? |
Author: | Aange10 [ Mon Sep 05, 2011 1:05 am ] | ||||
Post subject: | Re: Help me comprehend some (SIMPLE) Pong coding | ||||
At this section in my coding i'd like to make my time reset. How would I go about doing that? I've tried a couple different ways, but none of them worked. Some steering in the right direction would be greatly appreciated! ![]() This is the section i need to reset the time at.
This is the full coding.
![]() |
Author: | Insectoid [ Mon Sep 05, 2011 1:19 am ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
I'm sure you can figure it out. Remember Time.Elapsed()? Big hint right there. |
Author: | Aange10 [ Mon Sep 05, 2011 11:43 am ] | ||||
Post subject: | Re: Help me comprehend some (SIMPLE) Pong coding | ||||
I've read all the help section on the turing window, and i've searched the forums for "Time.Elapsed()" "Time Resetting" "Resetting time", and spent about half an hour trying to find out the syntax of Time.Elapsed ... The help menu for turing didn't say what the () was for, and Time.Elapsed() seems to be the same as Time.Elapsed. I've tried codes like
I've tried a lot of other things that the turing said was inapproriate.
Is there something i'm missing here? Is there not a way to say Time.Elapsed (or timestart since it's my variable) := 0 ...... And it work? |
Author: | Insectoid [ Mon Sep 05, 2011 12:24 pm ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
The () just specifies that it's a function. That's right, it's not a variable. It *always* returns the time in milliseconds since the program began execution. You can't change what Time.Elapsed() returns. You can, however, assign the return value to a variable. int i = Time.Elapsed() You don't need any extra commands. You need math. You have got a few things to work with. -You can get the time since the program began (Time.Elapsed()) -You can assign that to a variable -Because 'time' is just an integer, you can apply any math function to it (addition, subtraction, etc). Big hint: You need subtraction. |
Author: | Aange10 [ Mon Sep 05, 2011 2:59 pm ] | ||
Post subject: | Re: Help me comprehend some (SIMPLE) Pong coding | ||
I've spent half of last night and nearly all day today trying to figure out why my timer isn't working. I've taken what you've said, and took in to account what the guy a couple posts above me said, and I came up with this code.
I really enjoy this game and I plan on turning it into a brick breaker game, but it is absolutely bugging me that my timing doesn't work! I just can't figure out why. I've done just about everything I could think of, and iIve searched on here for somebody with the same problem for hours. The example I was giving earlier just doesn't seem to be working. No matter what variable i set to 0 or what variables i subtract from one another the time never restarts. The most I've done is destroy the time all together =( Please help <3 |
Author: | Insectoid [ Mon Sep 05, 2011 3:27 pm ] | ||
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding | ||
You never need set anything to 0. Since you seem to be putting in a decent effort I'll help you out in more than hints. timeSinceReset = Time.Elapsed() - startTime. startTime is taken when you 'reset' the timer. Whenever you want to display the time, use this formula. If you want to reset, do startTime := Time.Elapsed. For the sake of clean code, put startTime := Time.Elapsed at the top of the main loop. You *Do NOT* want to put this in your "If continue = 'y' or continue = 'n'. DO NOT DO THIS. Honestly, that if statement doesn't even have to be there- all that stuff can be put in the top of the loop. Play around with this if you still don't get it:
Note that I don't have a copy of Turing available atm, so I haven't been able to test that code, so I can't guarantee it's error-free. |
Author: | Aange10 [ Tue Sep 06, 2011 4:42 pm ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
Oh my goodness. Thank you so much Insectoid! It works! Ahh. You should have seen the expersion on my face when my timer went from "25" to "0"!!! Thanks a ton!! How do I get bits? I'd like to donate <3 |
Author: | Insectoid [ Tue Sep 06, 2011 5:45 pm ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
No need to donate (bits come from posting). Karma's cool though! |
Author: | Aange10 [ Tue Sep 06, 2011 6:47 pm ] | ||
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding | ||
Hehe when I get 8 more posts, I will be allowed to karma, which I shall do! (and look, this post goes 1 towards that.. 7 left!) I'm very much enjoying this game, and I absolutely love watching this expand. (If you knew me personally, you'd know that seeing progress is just absolutely fantastic to me.) I've come to a minor problem (doesn't bother me nearly as bad as the timer) that I'm not sure how to fix. My problem is with the collision of my ball and paddle. My ball will occasionally go into one of the sides of my paddle. Before I attempted to fix the problem, I decided I needed to actually learn (not follow a formula) what my collision equation was saying. So I drew a box and a square on a piece of paper and found out exactly what my program was saying. Now the reason I am confused and unable to fix this, is because what i found was that the = signs in my formula drew the border of my box, and the > and < signs "filled it in". So with that being said I have no idea how my ball is going inside my paddle. (Goes in one end comes out the other) So I decided instead of just coming and asking, I would go to your tutorial on how to make a game and re-read the collision section. After re-reading it, I felt that my hypothesis of how my formula worked was true, but I didn't find the answer to my problem. Though I got two links! After reading the collision section of your tutorial I decided to go back over the Turing Walkthrough and re-read their collision tutorial. I read it and it honestly didn't teach me anything. I've read the thing a bunch of times and I didn't really feel like it told me anything relevant to my problem. However, I noticed it told about a problem very similar to mine. Which happened to be with ball collision. So long story short, I went and read the advanced guide. To be honest I'm very happy to say I comprehended the majority of what he was saying. I'm not going to lie; I didn't grasp every bit of it, especially considering I only read it once and then skimmed it once more, but I got the gist. Where all this is leading to, and the reason I am here now is that I still haven't found a solution to my problem. To be honest I doubt my problem will need anything to do with advanced circular collision (though I will do it, if need be). But it still leaves me with one question: What's not right? Current coding for collision detection (full coding is a couple posts up...)
|
Author: | Aange10 [ Wed Sep 07, 2011 7:07 pm ] | ||
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding | ||
Also this is my current coding for a pause. Obviously, I ran into the problem of not being able to use 'p' for both chars statements, so i used v for one. However this is bugging me, and I was wondering if I could get some help =/. I went to the turing help section and searched "pausing" and read all 20 forums that came up but... well I'm still here ![]()
|
Author: | DemonWasp [ Wed Sep 07, 2011 7:18 pm ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
Your first problem is that you should be using or, not a vertical bar. |
Author: | Aange10 [ Wed Sep 07, 2011 8:26 pm ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
What is a verticle bar...? |
Author: | DemonWasp [ Wed Sep 07, 2011 8:30 pm ] | ||
Post subject: | Re: RE:Help me comprehend some (SIMPLE) Pong coding | ||
|
Author: | Aange10 [ Wed Sep 07, 2011 9:17 pm ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
Okay, thankyou. I changed them all.... But that didn't fix the problem. What do I need to correct? |
Author: | DemonWasp [ Wed Sep 07, 2011 10:19 pm ] | ||
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding | ||
Oh! If you were meaning the problem where you can't use 'p' for both pause and unpause, that's because you need to detect the key-down event, not the key-down state. The state of key-down ( 'p' ) is whether or not that key is down right now. The event of key-down ( 'p' ) is whether or not that key was just pressed down now. That is, whether 'p' is pressed now, but not on the last iteration. The most straightforward way to detect this event is to keep the old keys array around and record into a new one on each iteration. Then, if new_keys_down ( 'p' ) and not old_keys_down ( 'p' ), then you know that p was pressed just this instant. Looks something like this:
|
Author: | Aange10 [ Sat Sep 10, 2011 1:50 pm ] | ||
Post subject: | Re: Help me comprehend some (SIMPLE) Pong coding | ||
Alright, so I've been busy latley, but for the past 3 or 4 days i've been working on making the computer generate bricks. I'm kind of (as you can assume) lost. My coding so far has been
... Pretty much all i was trying to make it do what generate bricks starting from the top right corner (629, 399) and having the computer generate a width (the x2) & color (brick_color). After it did that, it would generate another brick starting from where the brick behind it had ended. Obviously I didn't do too well, and I was wondering how I should go about doing this. Is 4 arrays bad? Is multiple fors bad? Do I need a loop? |
Author: | Zren [ Sat Sep 10, 2011 3:23 pm ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
How would you draw a grid? How would you draw a bunch of points spaced n pixels apart? Start with a new program, and try drawing a grid from the bottom left (0,0). After you can, it's easy to translate it to the top. Tip: Think of it as iterating through each column in a row. Using a different array for x1, x2 isn't bad, you can't change that until you've learnt how to make your own data types (records/classes). maxx and maxy are useful variables to find the maximum coordinates. |
Author: | Aange10 [ Mon Sep 12, 2011 7:23 pm ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
Ok but why do i need a grid? I can find any cords i need with my Mouse.Where.. Not to mention i already found where i wanted my brick to start |
Author: | Zren [ Tue Sep 13, 2011 12:26 am ] | ||||
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding | ||||
Because the behaviour you were describing is similar to a grid. As your eventual output of bricks would look like it has columns and rows. It's also very easy to draw a grid.
I might have misinterpreted. I thought you were doing the game breakout where all the bricks are a fixed width. You seem to have some randomness in there. If you know how to make a for loop into a regular loop, then you can do a non fixed increment to the counter.
|
Author: | Aange10 [ Tue Sep 13, 2011 8:05 pm ] | ||||||||
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding | ||||||||
I'm trying to decipher this (so I can use it in my own programs) and I'm tracing the execution to see how it is working. But what it is doing is when it hits the right edge it ends the x for and adds 1 to the y for, and repeats. ... I don't know what is telling it to do that! I think it has to do with
but i don't know what
means. Could you please explain the "by cellW"? ... also what is making the y jump up by 10 and not 1? Thanks a ton for the code, it's a ton of help!(: EDIT: I just realized that the "by cellW" is just saying to move it by increments of CellW! xD I'm very happy to have figure that out. However I'm a bit confused with
in it's relation to
... What is the point of making it 1 less than cell_width? |
Author: | Aange10 [ Tue Sep 13, 2011 8:26 pm ] | ||
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding | ||
Also, I hate to double post.. But this is my deciphered version of the code. If there is any concept i missed/ incorrectly analyzed, please tell me!(: Thanks
|
Author: | Zren [ Tue Sep 13, 2011 10:04 pm ] | ||
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding | ||
We don't want the boxes to overlap. Drawing (0 .. 40) and then (40 .. 80) would have the second box draw over (a small) bit of the previous box. Also: The range of numbers 0 .. 39, or 40 .. 79 is exactly 40, or the target width we desire. 0 <= x <= 39 0 <= x <= 40 The latter has 41 numbers. if you still don't understand, try this:
|
Author: | Aange10 [ Wed Sep 14, 2011 6:45 pm ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
I see I see, thankyou!(: |
Author: | Aange10 [ Wed Sep 14, 2011 8:51 pm ] | ||||
Post subject: | Re: Help me comprehend some (SIMPLE) Pong coding | ||||
Alright, cool. So I've developed a program to randomly generate the blocks with a random width. Now I ask two questions. 1) Is there something I could use to make this very long line shorter?
My goal here is to say that if any integer within 10 numbers of color_1 is = color_2 then it will execute the if statement. 2) I have my blocks being generated. But now how will I set up collision on these blocks (My ball colliding on them)? Also, once i do set up collision, how do i make the block disappear? Thanks! EDIT: And I'm also still having the same problem as before; My ball is going into the sides of my paddle and my code isn't exactly showing me why.
|
Author: | Zren [ Thu Sep 15, 2011 1:16 am ] |
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding |
Please tell me you're not using whatdotcolor colision. As for Q1: isn't that just a-10 <= b <= a+10 ? Q2: Either remove it from the flexible array, or have a 'flag' variable for if it exists anymore. Q3: You're only checking if the center point is within the rectangle. |
Author: | Aange10 [ Thu Sep 15, 2011 5:14 pm ] | ||
Post subject: | RE:Help me comprehend some (SIMPLE) Pong coding | ||
Q1: Fixed, thankyou Zren! and for Q3: I'll go and re read some of the tutorials to get the entire ball to be detected, thanks! Q2: Well I'm not using an array (my codes at the bottom), so I guess i'll have to go with a flag variable. But how would I program collision on these random width blocks, and how would i make the flag detect if the blocks are still there?
|