In the previous post , we setup our system and prepared our working directory , in this post we will begin with coding a few key components of the game , and see how they work.
- Open the "Cgame_part1.py" by right clicking and selecting "edit with IDLE" , or you may use notepad if you prefer , i just like using IDLE because u can run the programs directly.
- You can download the code from my Git Repo directly , so focus on understanding what each bit does in this post.
Coding the basic components
- Begin by importing the modules needed for this part of the game.
import mlcd,pygame
This allows us to use the functions contained in these modules, so we can use them in our game - Next we create a few constants and variables to store the state of our game, we will discuss them as they come up.
PLAYER_CHAR=">"
This constant basically defines how our player is going to look on the screen, changing this will change the shape of our ship , you are free to play with this value, but remember to keep it a single character.
screenbuff=[[" "," "," "," "," "," "," "," "," "," "," "," "], [" "," "," "," "," "," "," "," "," "," "," "," "]]
This variable stores the state of that part of the screen where our obstacles and ship will be,think of it like a virtual map of the playable area on the screen.
player={"position":0,"line":0,"score":000} keys={"space":False,"quit":False}
These are two "dictionary" type variables to store the states of the pressed keys and the player's position and score. - Now that we are done with creating the variables we need to use , we will create a function that will allow us to get the pressed keys using pygame.
def keypress(): (refer to the code from git repo) - The function first resets the keystates so that we don't get duplicate keypresses,
then it checks the pygame key events and sets our keystate to true if the key is pressed and crosschecks it with a key up or key down event to ensure we don't send the keys if the key has been pressed and never released(which would cause the game to move things too fast). - Now we can move on to the main part , the game loop,
#initialize mlcd as 16x2 character lcd
mlcd.init(16,2)
while not done:
#add player to the buffer
screenbuff[player["line"]][player["position"]]=PLAYER_CHAR
#ready the lines for drawing on lcd
lines=[''.join(screenbuff[0]) + "|scr",
''.join(screenbuff[1]) + "|"+str(player["score"])]
mlcd.draw(lines)
#remove player from buffer
screenbuff[player["line"]][player["position"]]=" "
#get keypresses
keypress()
#modify player line (move the player) if space is pressed
if keys["space"]:
if player["line"]==0:
player["line"]=1
else:
player["line"]=0
#quit
if keys["quit"]:
print("game quit")
done=True
Break down of the game loop
Before the loop begins,we set the control variable "done" to False , and initialize the mlcd display,
- first , we add the player to the screenbuff, so that the entire gameplay data is ready to be put on the screen ,we do this with the line
screenbuff[player["line"]][player["position"]]=PLAYER_CHAR - with the next two lines , we create a list, containing the gameplay data , and the score , and display the entire data on to the screen.lines=[''.join(screenbuff[0]) + "|scr",''.join(screenbuff[1]) + "|"+str(player["score"])]mlcd.draw(lines)
- on the next line , we remove the player character from the gameplay data in the screenbuff, so that we don't end up putting the player character in it's last position.
#remove player from buffer
screenbuff[player["line"]][player["position"]]=" " - Next , we get the keypresses and if space is pressed ,the player character switches lanes and if esc is pressed , we quit the loop by setting the loop condition "done" to true.this works because our looping condition is "while not done".
#get keypresses
keypress()#modify player line (move the player) if space is pressedif keys["space"]:if player["line"]==0:player["line"]=1else:player["line"]=0#quitif keys["quit"]:print("game quit")done=True
That ends the game loop , outside the loop block , we simply add one line to exit pygame after we are done executing everything.
pygame.quit()
Running this program displays a tiny 16x2 character lcd box like so,
so there you go, you have the basic Input and output of the game ready. now all we need to add are the obstacles and their movement across the screen,then we add a difficulty system and a few instructions on how to play, and our first game ,will be ready !
EDIT: added in part 3
if you are enjoying this series or have any questions , hit me up in the comments below , and i'll be glad to help
pygame.quit()
Running this program displays a tiny 16x2 character lcd box like so,
and pressing the space key will switch the line of the player " > " and pressing the esc will quit the game. be sure to click the box , if your input doesn't respond , the window needs to be in focus for pygame to catch the key input.
so there you go, you have the basic Input and output of the game ready. now all we need to add are the obstacles and their movement across the screen,then we add a difficulty system and a few instructions on how to play, and our first game ,will be ready !
EDIT: added in part 3
if you are enjoying this series or have any questions , hit me up in the comments below , and i'll be glad to help
Comments
Post a Comment