DING DONG, ITS PONG! (KINDA)
Ok, so here's my first little program that I've tried to do. I decided to choose something easy, with a lot of references to.
Obviously this isn't finished I still have to remove the sides, make the scoring if the ball goes past the paddles, and probably some kinda change in acceleration and/or angle depending on what speed the contact is... if that is how it works, I might have to research it.
Hopefully this works in blogger, I've been trying to find something where I can embed it in so that it would work right here. W and S controls the left paddle, O and L controls the right one. Its super simple I know, but gimmie a break I've just been coding for a month and a half!
Clicking on the </> will show you the code if your interested, how much is needed for such a simple game?
On [electronic] paper this seemed simple enough. Rectangle, Rectangle, Ball, Win. EZ.
However, this was not the case. Motion is quite annoying to deal with, but it was motion + interaction between the objects that killed me omg. Having to make the ball bounce of the paddles was a lot harder then I had realised.
Basically I broke down my plan like this;
- Draw the stuff, paddles and ball.
- Make the stuff move, paddles up and down, ball bouncing around.
- Have a bunch of IF statements to what would happen when ball hit paddle.
- Scoring, spawn new balls, maybe some super old skool retro sounds, and other misc if I want.
No.3 took me absolutely ages in my noob mind. First of all, trying to define a bounce area was a huge pain the ass. This is because in order to make the program know that there is intersection happening, it needed a formula;
Middle of ball (radius) co ordinates + Middle of Paddle = Distance between both objects.
If Current distance, which is a constantly changing number because the ball is moving, is less then the above formula, then must be overlapping and therefore intersecting.
But remember, the paddles are rectangles, so the middle of each of the sides are different, so you needed seperate if statements for x and y co ordinates. Lost? Yeah I was too for like 4 days lol. Basically if the ball came from below or top, the middle of the paddle is way shorter width-wise. So the distance might be way shorter then it should be.
The biggest reason for taking so long was that I used an OR statements instead of AND to check whether it was intersecting the rectangle or not.
Another funny thing was that I decided to have a go at Pong to take a break from the videos. However, the next set of videos was talking about vectors, forces, motion, oscillation etc etc. Basically a whole bunch of shit that would have been super useful, and watching it now I'm seeing how I could have done some things differently, ie better lol.
OBJECT ORIENTATED PROGRAMMING
One of the biggest things I've learned through these videos, is that Object Orientated Programming (OOP) is really important and so I decided to make Pong that way which made it much harder.
Basically OOP is a way of writing the code which makes it easier to read, but has a lot of groups and referrals back to them. So this allows the main page to have all the actions so to speak, and they refer to the objects which is on another page.
This also allows for easier customisation. For example if I wanted to change the size of the ball or paddles, I just change one number and everything else would still work without needing to go through the whole thing and change up whatever they effected.
If circle is written as "ellipse([draw at location x],[draw at location y],[radius of circle]);"
We can just go "x = 100, y = 100 r= 25", then "ellipse(x, y, r);"
This is a much longer way of writing it, but we can see that down the line its way better.
Hopefully this post isn't too dense. I kinda watch and learn this stuff with a lot of if going over my head, but enjoying the process and seeing the potential of how some ideas of mine can be implemented...