You can also download the code file here if you want to play the game yourself right away. We’ll also be drawing heavily on the methods and properties provided by Pygame – see documentation here. If anything seems unclear, your first stop should be the Python documentation for more information. On the following pages we’ll walk you through the code for Raspberry Pie, so you can see how the program works and type along if you wish. Remember that, while classes are written as part of the code, objects are created only when the program runs, and don’t exist beyond the duration of the game. For now, however, let’s stick to a simple implementation. You can even base one class on another, giving you a hierarchy of subclasses. Since all the code relating to the fruit sits within the fruit class, bugs are easy to find and, once fixed, apply to all instances across the entire program. So we might build a method into our class that says “take my current vertical position and move it down by 3 pixels”. In the case of fruit, the major thing each object does is fall. We can then create fruits as independent instances of the class (these instances are the “objects” of OOP), each of which contains information about what sort of fruit it is and its location.Īs well as properties, classes can have methods – built-in actions that we can invoke whenever we want. We can therefore represent them all very simply by creating a single fruit class with properties that indicate genus and position. Think about the visible objects in our game: the only aspects that vary from one tumbling fruit to another are their position and genus (that is, whether they’re raspberries, strawberries or cherries). Object-oriented programming (OOP) underpins most modern programming languages, and it isn’t too difficult to understand. Once the game ends, the score is displayed. Then there’s a main loop that handles the gameplay. We then initialise everything – for example, setting the score to zero. We begin by setting up the execution loop environment, and create the objects that will be used in the game. The basic structure of an arcade game is the same whether you’re planning to use Scratch, Python or any other tool. Now let’s think about how we’re going to structure our program.
0 Comments
Leave a Reply. |