![]() They can never work 100% the same as on Windows, but still usable. Keyboard input handling: - Windows version uses builtin function kbhit(), getch() from - Linux/MacOS version isn’t supported so I have to use an alternative helper and name them the same namesas Windows’ version: kbhit(), getch().Linux/MacOS version calls system(“clear”) to clear the terminal. Clear screen for Console / Terminal: - Windows version calls system(“cls”) to clear MSDOS console.Delay / Pause: - Windows version uses function Sleep(mili_sec) from - Linux/MacOS version uses usleep(micro_sec) from.The 3 main differences compared to linux/macos version are: You can control the speed of the snake through this. The snake will move faster if this value is lesser. The SnakeLohic will wait for input before continue to make the snake moves, there is a value for this maximum delay time. To break the game, ‘X’ key may be a good selection. To control the snake from player, I choose to use “A S D W” to control the snake face/direction. Void loop() Get user input (keyboard) to control the snake Here a quick example how to declare a snake lying on 5 cells on the field:Ī game logic should contains a loop that loop until the game is over, in this case it’s over when the snake is not alive or the player breaks it (close/exit): When the head reach the food, the snake appends to its body a new cell.īut wait, following this way we have to use 2 information (x,y) to store the cell information. There’s another special cell that not belongs to snake called “ food”.The snake cannot move with backward direction. The snake advance the head to the next cell in front of its face, and truncate the tail from the last cell to move.The face turns to only one direction at a time. I call the direction for the snake is “ face”. The snake is only able to run when it has a direction, there 4 directions that it can move: east, north, west, south (or right, up, left, down).There’s 2 special body cells called “ head” & “ tail”. The snake can run only on that cells, with its body contains some cells. The field has limited number of cells, every cell has a position of x and y.Figure 1: 2-dimension array of 10x10 (rows x columns)Ī traditional thinking will imagine a snake will run on a 2D field which a can represent by a matrix:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |