In this post I am going to help you to get the paddle to move either left or right depending on the users input.
To begin, we need to use a while loop to enter the game and allow it to continue running until the user exits the game. We do this by setting the while loop to True. This way we will always be able to enter it. Once in the while loop, a For loop needs to be created to queue up all the events in the order they are going to run. This line of code that is used for this is for event in pygame.event.get(). Before we get the paddle to move we first need to create an if statement so that the game terminates when the user exits the game. This is done by checking if the event.type == QUIT. If true, you need to call the quit method from the pygame class to quit the game the user is currently playing and then call the exit method from the sys class to exit the open window.
To get the paddle to move, we now need to use nested if statements and the event.type and the event.key methods. When the event.type == KEYDOWN the pLeft and pRight variables need to be changed depending on whether the left key is pressed or the right key.
For example: if the left key is pressed, pLeft has to be set to True and pRight has to be set to False. It would be the opposite if the right key is pressed.
When the event.type == KEYUP, this means when the key pressed is released, the pLeft and pRight variables need to be set back to False so that they can be reset. For example: if event.type == K_LEFT, then pLeft == False. The same should be done for the right key. Do not worry if this is confusing for you, I will insert a screenshot of the solution at the end of this post.
To finish this off we need two if statements to make the paddle move. For the first if statement, you need to find out if pLeft is true and if the left side of the paddle is greater than 0. 0 is the edge of the left side of the window. This is checking whether or not the paddle has reached the left side of the window. If it has it will enter this if statement.
This is where we add the line of code that makes the paddle actually move. What you need to do is use the left side of the paddle and the MOVESPEED variable. You need to set player.left = player.left - MOVESPEED. This will change where the left side of the paddle is placed and in turn, allow us to move it.
Same goes for moving right. Although instead of checking player.left is greater than 0, you check if player.right < WINDOWWIDTH and if so, add the MOVESPEED to player.right.
At the end of the while loop you need to add in a few lines of code that will allow the game to run and also update any movements made while the game is running. First we need to fill in the background, I picked white. Then we need to draw the paddle onto the screen again but this time, it has moved and call the update method in the pygame class.
We also need to add in the time.sleep method and add in the amount of seconds as the parameter. This method pauses execution of your Python program.
Okay so that is how we move the paddle either left or right depending on the user's input. Now that we have one of the two main objects on the screen, it will not be too hard to finish this game!🙂
Tip: Pyscripter does not use curly brackets when creating an if statement or any loops. It uses indentation. So when you want to write something after an if statement, you do 1 indent in and if it is nested, you continue to indent after each statement.
No comments:
Post a Comment