Ping Pong in Python





     For this weeks assignment I decided to learn how to make a game using Python. I have been looking up online how to get better at coding and other than doing coding challenges its useful to create real world projects to put everything together. I learned so much just from doing this project that I didn't know from just doing coding challenges. Now this is 147 lines of code so I wont explain every single detail but I did leave comments on all of my code so you can go through it and understand what each block of codes adds to the game.I will also upload a video at the end of this page so you can see the game working in real time.

    The first thing I had to do is import a built in library in Python called Turtle. It allows you to draw different objects and animations on to your screen. Once I installed Turtle I created a variable holding the Turtle.Screen function that allows me to create a screen that pops up. Once I did that I changed different attributes of my screen such as adding my name, color of the screen and the size of the screen.
    We then call 2 constructors of the Turtle class. This allows us to create real life objects on our screen. I called these paddle_a and paddle_b which represent both of the paddles on our screen. We then changes their attributes like the shape, color, size and their starting locations on the screen. What I also learned is that when you create these objects on the screen it is almost like creating a pencil and as these objects move they draw on the screen. That is also why I had to call penup() on both of my paddles that way as the paddles move up and down they dont write on the screen. I also did the same thing to create the ball on the screen by creating another object of Turtle. The last object that I created what an object to display the score of the game with player a and player b.
    On the second image I created functions that we will call on to make both of our paddles be able to move up and down. I call these in the third image I uploaded. First we call the function called listen() which tells the program to listen to user input from the keyboard. For paddle a its W for up and S for down. For paddle b we simply used the up and down directional to move up and down. That is actually it for everything that is on the screen.
    Another very important thing I learned is that every game is ran inside a while loop since it is constantly running. When the ball is moving on the screen we have to create logic that way it can bounce off the top and bottom of the screen as well as the paddles. In reality the ball doesn't touch the sides of the screens or the paddles. What actually happens is we implement logic that way when the ball gets to a certain y and x distances on the screen it has to bounce thus it does not actually bounce. We also create logic that way when the paddles and the ball are at a certain location at the exact same time we know they have made contact thus the ball has to bounce the opposite way depending on how the ball made contact. This logic is inside of my while loop and I left comments so it is fairly easy to understand what is happening. I also uploaded a sound file that way every time the ball bounces there is a bouncing noise that occurs.

Comments

  1. I have been looking into the coding files of some basic online games and seeing how mods work so this was nice seeing the work that goes into. Most videogames or even space shuttles involve thousands of lines of code written by multiple huge teams of people for over half a year, so 142 lines by yourself is extremely impressive!

    ReplyDelete
    Replies
    1. I for sure did not know how to code this entire thing right off the bat. I been learning that in coding you will always have to do research online. The concepts of how to do this is easy. How do I place 2 paddles and a ball on a black screen? To know how to communicate that to a computer makes you have to know so many different functions. But that is why you research online to learn something new every time.

      Delete
    2. Also Pong is the most basic game you can code. It's just a black screen with 3 objects in it. That does not compare to other games that change backgrounds and require more logic. Also a space shuttle im sure has millions of more variables than Pong. It does not require that much code.

      Delete

Post a Comment

Popular posts from this blog

Sunset Views Coding Challenge

Week 9 Coding Challenges