11.12.2015 Views

GAMES

Essentials_Games_v1

Essentials_Games_v1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

[ MAKE <strong>GAMES</strong> WITH PYTHON ]<br />

yOffset and the while loop continues to search for a black pixel.<br />

It will do this until it finds a black pixel above the x coordinate of our<br />

avatar, adding 1 to the yOffset variable each time. Once a black pixel<br />

is found, we’ve discovered where our platform ends and can subtract<br />

the yOffset from player[“y”] to put our avatar just on top of the<br />

platform; this is done on line 73. If we don’t find a black pixel before<br />

we reach the top of the surface, it’s game over: our avatar is trapped<br />

off screen.<br />

Moving our character left and right is done on lines 82-92. If the<br />

code looks familiar, it’s because we used it in our last tutorial to move<br />

our squares around. Now that we’ve worked out where our avatar can<br />

go, we can draw it by calling drawPlayer() on line 203.<br />

Game over<br />

We’re almost done; the last thing we want to handle is what happens<br />

when our player loses the game. The game is over once our avatar<br />

disappears off the top of our screen, and we want to tell the user that.<br />

When our avatar disappears, we call the gameOver function on line 79.<br />

All the gameOver function does is set some variables that our main<br />

loop will check to see if the game is underway. Once gameEnded is True<br />

and gameStarted is False, our main loop will draw our game over<br />

screen. Just like our welcome screen, we draw our game over image<br />

onto the surface on line 193 and give the player the option to restart<br />

the game with another space bar press.<br />

And that’s it! Using all the skills we’ve already acquired (and a few<br />

new ones), we’ve built our first fully fledged game. Like all good games,<br />

we’ve got a start, a middle, and an end.<br />

Left Just like our<br />

start screen, our<br />

game over screen<br />

is simply an image<br />

drawn straight onto<br />

our surface when<br />

we need it<br />

[ Your First Game ]<br />

50

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!