In this post I will be helping you the get to block to move automatically once the game is loaded. There will be no user input needed for this. It will bounce about on its own.
For this we will need four separate if statements with a nested if/else in each one. These if statements look very familiar. The only thing different is what side of the block you are changing in order for it to move and the speed you are setting it to.
For the moveDown if statement, you need to do the nested if/else statement to find out if the block.bottom >= WINDOWHEIGHT. If it is greater than the height of the window, then you need to subtract the MOVESPEED_V from block.bottom. After this the moveDown has to be changed to False and moveUp has to be changed to True. In the else, you just add the MOVESPEED_V to the block.bottom.
When writing the code for the block.right side, you need to check it against the WINDOWWIDTH and subtract the MOVESPEED_H to the block.right or else, add it. This code will detect if the block has hit the right side of the window and if so, bounce of it in the opposite direction.You then set the moveRight to False and the moveLeft to True.
However when checking the block.left and the block.top, they need to be checked if they are <= 0 because the top of the screen and the left of the screen both are 0. The MOVESPEED_H is added to the block.left, else, subtracted and the moveLeft is set to False and moveRight is set to True. When the block hits the left side of the window, it will also bounce in the opposite direction.
The MOVESPEED_V is added to the block.top, else also subtracted. block.top detects if the block has hit the top of the window and if so, bounce downwards. The block.top has to be set to False here and the block.down and to be set to True.
Tip: The integer you set the MOVESPEED_V and the MOVESPEED_H to at the top of the program is how fast the the block is going to move around the screen. MOVESPEED_V is the speed you want the ball to move when it is going in a vertical direction and MOVESPEED_H is the speed when it is going in a horizontal direction.
No comments:
Post a Comment