Making a Python game - Obstacles - Part 3

In the previous posts in this series , we have seen the following parts to making our game.
  1. Setting up our system
  2. Basic input and output
so far our game displays as such :





Now let's add the obstacles , so we need to keep in mind the following :
  • We need to move the screen so that the generated objects move towards the player.
  • We need our obstacles to generate randomly , but not block the player's route completely .
  • We need to give player points for dodging every obstacle, for the score.
  • We need to progressively increase the rate of the obstacle generation , and movement speed ,making the game harder to play as time goes on.
lets look at the additions to the code , now that we know our objectives ,

first of all , we need to import new modules, "time and random".

import mlcd,pygame,time,random

the "time" modules provides us timing functions so that the logic runs at a set "speed" and "random" module allows us to make random decisions.

next , we will add some more constants and variables.

OBSTACLE_CHAR="|"
game={"speed":4.05,"level":2.5,"obstacle":0}
lasttime=time.time()
curtime=0.0

"OBSTACLE_CHAR" stores our obstacles, again like the "PLAYER_CHAR" it can be changed ,just keep it a single character. "game" dictionary stores the movement speed, current level and obstacle count for the game."lasttime" and "curtime" , store the time to check if the game logic is running at a certain speed (not faster than human capability)






now we begin by adding the speed check for our game logic,inside the game loop,
we get the current time and see
if 1/speed time has passed,if yes then update 

variable for lasttime and then run the code for logic.






The above section of the code , checks if the player and the obstacle are at the same column, and if true , updates the values for score,level and speed being on the same line means from next cycle the obstacle cant cause any problems for the player , hence we also remove it from the count of current obstacles.





we now move every character (except for player) one position to the left,but you  will realize that once we do that, the leftmost characters are lost and the rightmost positions have no new values(the old ones are garbage for us). so now we need to add  new characters to the rightmost position , which will either be blank space(" ") or our "OBSTACLE_CHAR" in a random fashion, provided it doesn't block the path  completely.The code for doing this is here.
Now,all we are left to add is the obstacle collision,
#check for collision
if screenbuff[player["line"]][player["position"]]==OBSTACLE_CHAR:
done=True #player lost
after the "done=True" you may also add a "print" statement to display the final score in your console , however we will add a start screen , instructions and an endscreen which will display the code and instructions on how to play the game on the LCD itself, in a future post. Here are some screenshots of the game ,as it is now,
Code for this post is available on the git repo as the new file "Cgame_part2.py"

Comments