Again, this function takes self since Python requires it to. We'll be using the accelerometer for this which means that the user will be tilting the device to make the snake move. Handle_input(self): This function gets input from the user and uses it to change the direction of the snake. This means we can use it to access the state that's part of the class. This argument is a Python requirement for functions that are part of a class, and represents the instance of the class. You may notice that it takes an argument, self. For example, we need an initial position for the snake and a direction it will first be heading. The function will initialise our variables ready for the game to run. We'll see an example of creating an instance later on. _init_(self): This is a special Python function that is called when an "instance" of a class is created. Our class has a few different functions that we'll have to write. game = Snake ()Ī class is a handy way of keeping some state, like the position of our snake, alongside the functions that operate on it. Let's take a look at each part of the code to figure out what it does.Ĭlass Snake : """ This class contains the functions that operate on our game as well as the state of the game. Find it at this link and copy the contents into the create.withcode editor. To make it easier to get started, we'll be working from a skeleton file. So in theory, if we can build all these components, then we should have a snake game! Skeleton code In the video this happens when the snake crashes into itself, losing the game.Some randomly generated food on a cell.The snake has a direction (up, down, left, right).The snake occupies some cells of the grid (you can think of it as a list of co-ordinates).The grid has cells, which can be identified by their X and Y co-ordinates.Just in case it doesn't appear for you, you can play it here. Snakeįun fact - if you go to Google and search for "snake", a snake game should appear. You can drag this file to the "MICROBIT" device in your file manager to program it.īefore we start looking at some code, let's take a quick detour to look at the game of Snake. If you are working with a physical micro:bit, then the "Download HEX" button in the top-left of the pop-up will allow you to download the file you need to program your micro:bit. Your code will be running on the virtual micro:bit you can see, and there are tabs along the top for your to control various inputs to the device, which will become important later. Running your code will produce a pop-up that looks like the below. You can access these backups by clicking the "+" button in the lower-right corner then clicking the button that looks like a clock. Your editor will auto-backup your code to your computer. Alternatively, you can run your code by pressing Ctrl+ Enter. In the lower-right corner (2) is a button to run your code which looks like a green triangle. Most of the screen (1) is taken up with the editor. When you do, you should see a screen like the one below. Whether you have a micro:bit or not, the first step is to open the create.withcode editor. If you don't already have one, don't worry! There are simulators available online and you'll be able to follow along with this whole tutorial. They're also pretty cheap (£20/$20 at time of writing on Amazon), and programming them is as simple as dragging a file to a USB stick. It comes with an LED grid (for a screen), an accelerometer (to detect motion), electrical outputs (for controlling motors, lights etc) and a bunch of other cool features. Simulator only: How to test with the accelerometerĪ micro:bit is a tiny computer that you can program in Python.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |