The updateSnake(delay) function moves the snake to the next space then stops for a period of 50 milliseconds (denoted by the variable delay). Up next in main() is the do while loop which actually moves the snake. Next up, lets get the snake moving! Moving the Snake //Initialisation of delay in main() Otherwise, the thread will end and stop taking user inputs. While the input is not EXIT (user did not press the ‘esc’ button) and the item is not negative (previously we mentioned that negative means that the snake hits itself or the wall), the program will continue to run. When the user presses any of the button listed, the global variable, input, is reassigned to reflect the user input. 27 is actually the ACSII code for the ‘esc’ button on your keyboard. The control for the snake is the traditional w,s,a,d button for up, down, left, right respectively. This function then waits for a user input by calling _getch() (if this were to be in the main function, it will halt the snake to get the user input). This is done so that the user’s input can be processed at anytime while the snake thread is looping continuously (if we use only 1 thread, the snake will pause for the user input before moving). The _beginthread(userInput, 0, (void*)0) function starts the function userInput(void* id) in a separate thread. Int oppositeDirection(int input1, int input2) SetConsoleCursorPosition(hStdOut, homeCoords) * Fill the entire buffer with the current colors and attributes */Ĭsbi.wAttributes, //Character attributes to useĬellCount, //Number of cells to set attribute &count //receives the number of characters written (TCHAR) ' ', //character to write to the buffer HStdOut, //handle to console screen buffer If (!GetConsoleScreenBufferInfo(hStdOut, &csbi)) return ĬellCount = * * Get the number of cells and cell attributes in the current buffer */ SetConsoleCursorPosition(hStdOut, coord) If (hStdOut = INVALID_HANDLE_VALUE) return HStdOut = GetStdHandle(STD_OUTPUT_HANDLE) Int input = RIGHT //global variable to take in the user's input (0, -1) is UP (because the row number increase from top to bottom) direction array, for use with RIGHT, UP, LEFT, DOWN constants negative values represent areas that cannot be touched or else game over Thereafter, we will create the display by changing those numbers into something that we can visualize and understand. The code for the Snake program shown in the video above is shown below here! I will explain it in detail below too! The main idea is to create a mathematical layer where each number represent the wall, snake, food, etc. So, Enjoy the game! Oh, please press in case something acts up. It will probably prompt a warning for virus but don’t worry about that because ThePoorEngineer has no interest in infecting your computer or anything. * The attached file only works in the windows operating system. If you are learning C++ programming, I would say that this exercise would be very beneficial to you, and it is fun as well! Below is a video on the Program, and here is the EXECUTABLE FILE. It might seem complicated at first but it is actually quite simple. In order to do this, I decided to recreate the old Snake game from the Nokia phone using C++. Both the Arduino IDE and Qt programming (my work in the ARC Lab in NUS requires me to use Qt) uses C++ so I thought it might be good to revise the lecture notes, and also pick up C++ programming along the way. I had a class on Programming methodology during my 1st semester in NUS which uses the C language.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |