Saturday, 4 November 2017

Setting up the game!

The game I have decided to demonstrate in this blog is a simple one but it covers all areas of creating a pygame. It is a game with very little on the screen, only a paddle and a ball and a score, but it is a good way to start of learning how to code. The game is where a ball is moving around the screen and when it reaches near the bottom, you move the paddle over to hit it back up. I have this game already made but in each blog post, I am going to show you how to do it step by step.

So, to get this started we need to set the game up with all the variables that we will be using. I will go through why we need each one and then insert a screenshot of my code to help you along the way.
The first thing we need to do is import all the necessary libraries such as pygame, sys, random and time from the pygame.locals import*. If you do not have all of these then you will receive and error when trying to run.

Next, we set up the colours. BLACK is (0,0,0), WHITE is (255,255,255) and RED is (255,0,0). You can choose to change these colours by having a number between 0 and 255 in the order of how red, green and blue the shade is(RGB). You can find all the colours online. However, I decided to leave it simple.

We now need to set up the movement variables. You can name these anything you want but it works better if you name it something relatable to what you are using it for. With that in mind I named my boolean variables moveLeft, moveRight, moveUp and moveDown, setting moveLeft and moveUp to True and moveRight and moveDown to False. Also the variables for the paddle need to be set to false. I named them pLeft and pRight.

Other variables I have added in are MOVESPEED, WINDOWWIDTH, WINDOWHEIGHTMOVESPEED_V and MOVESPEED_H. These are for the speed of the ball to go, the size I want the window to be and when the block hits either the window width or height to bounce back. You can set these to any integer you want but remember if you decide to use different ones than I am, make sure to do the same with any other numbers as well!😊




No comments:

Post a Comment