Monday, 13 November 2017

Adding the ball to the screen

Now that we have the paddle moving, we need to add the ball to the screen. An actual circle as a ball is more complicated to do because it can not be done like we made the paddle. It has to be done with many more steps and personally that is to complicated for this simple game so instead I have decided to use a square. If you wish to use a ball instead that is no problem and if so, feel free to leave a comment on how you done it so I could update my code. It is easy to find this code online.

Anyway, the block is done the same way as the paddle. The only thing different is that the size of the shape is smaller so that it can be turned into a square. This block will be moving randomly about the screen so first what we need to do is set up the x position and the y position of the block using the random.randint method. The x position needs to be set with a random number between 0 and the WINDOWWIDTH.
The y position needs to be set with a random number between 0 and 300. The reason why I have picked 300 instead of the WINDOWHEIGHT is because 300 is where the paddle is placed and we do not want it to start up with the block passed the paddle or the game would not work.

These are both saved into two variables. I called them xPos and yPos for convenience. Now that these are set, they can be used as the first two parameters when creating the block followed by the height and width you want the block to be. Make sure these are the same size so that you get a square.

Once this is done you then add it to the screen by using the pygame.draw.rect method and enter in the parameters windowSurface, BLACK and block. block is what I named this object as.

Don't forget to add in the extra line of code at the bottom of the loop, like we done when creating the paddle, so that it is added to the game!



No comments:

Post a Comment