![]() 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. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |