![]() ![]() ![]() # Check if the snake ran into the food if snake = food : # Since the snake ate the food, we need to set a new food positionįood = None while food is None : # Randomize the position of the new food The following code should go into the while True statement, below the code above. Now let's the handle the snake running into the food. If key right is pressed, we take the snake's X position and add 1.Īfter all that, we get something like this: If key left is pressed, we take the snake's X position and subtract 1. If key up is pressed, we take the snake's Y position and subtract 1. If key down is pressed, we take the snake's Y position and add 1 to it. We start by taking the old head of the snake and then checking for what key is being pressed. The code above is how we actually control the snake. New_head += 1 # Insert the new head of the snake New_head -= 1 # Player presses key right if key = curses. New_head -= 1 # Player presses key left if key = curses. New_head += 1 # Player presses key up if key = curses. New_head =, snake ] # Player presses key down if key = curses. while True : # Code that we wrote before. Now, let's determine the new head of the snake based on our movement. If any of the above happen, we close the curses window and quit the program. The X position of the snake is outside the boundaries of the screen.The Y position of the snake is outside the boundaries of the screen.In the above code, we also define an if statement to check if: We do this to make sure the game doesn't stop if, for example, we are going forward and we press to go backward. The first couple lines of this snippet tests every possible combination we can move and where we are currently moving. endwin ( ) print ( "Oops, you lost!" ) break # Handle snake losing if snake in or snake in or snake in snake : # Close the curses window and exit the programĬurses. Key = key if wrong_operation else next_key ![]() Wrong_operation = True if (next_key = - 1 or next_key =curses. How do we lose in snake? # Infinite loop repeating every time the snake moves while True : If the next key is equal to -1, we leave the key variable as is, otherwise we set the key variable to the next_key value. Key = key if next_key = - 1 else next_key We want the following code to run continuously, so we're going to put it inside an infinite loop: # Infinite loop repeating every time the snake moves while True : ![]() Now, we're going to handle the game logic. Let's add the initial direction of the snake. In the above code, we use the food's X and Y positions as well as the ASCII PI character to set the first food. addch ( int (food ), int (food ), curses. Each list item in the snake list is an initial body part position. The next body part is 2 less X from the head. This means that the the next body part has to be at 1 less X position than the head. The head is at the initial snk_x and snk_y positions. Snake =, , ] # Set the first food item at the center of the screen Snk_y = sh / 2 # Create the initial snake body parts Snk_x = sw / 4 # The snake's initial Y position Skip a line from the above code and let's define the snake, its position and food. We'll be using these variables and definitions when creating the rest of the program. Great! We have defined the screen dimensions and set the cursor to be invisible in the top left corner. keypad ( 1 ) # Determine how fast the snake moves newwin (sh, sw, 0, 0 ) # Enable all keys getmaxyx ( ) # Create a new window from the height and width at the top left corner curs_set ( 0 ) # Get the width and the height initscr ( ) # Set the cursor to 0 so it's invisibleĬurses. We'll use curses to set a cursor, screen width, screen height, snake speed and enable keys. First, we have to somehow declare that our terminal can be used as a user interface. Import these two libraries by adding the following lines at the top of the main.py file: import randomīelow our imports let's skip a line and start our game. curses is how we will be handling the user interface and game mechanics. random is a module that will allow us to give random positions to the fruits that will spawn on the map. We're going to use the random and curses libraries to help us out. Spin up a new Python project by going to. We're going to use, a free, online code editor, for this project. You can check out the source code and preview here. Let's try to recreate this game in the terminal. As complicated as it may seem at first, it's rather easy to code and takes less than 50 lines. It's also a classic coding challenge for learning a new programming language. Snake is a game that most of us have played on those old Nokia phones. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |