- Snake Game In C
- C++ Snake Console Game
- C++ Snake Game For Mac Download
- C++ Snake Game With Graphics
- Computer Snake Game
- C++ Snake Game For Macbook
- C++ Snake Game Tutorial
Download the latest version of Screen Snake for Mac - Re-imagining of the classic computer game. Read 2 user reviews of Screen Snake on MacUpdate.
I had a class on Programming methodology during my 1st semester in NUS which uses the C language. 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. In order to do this, I decided to recreate the old Snake game from the Nokia phone using C++. It might seem complicated at first but it is actually quite simple. Below is a video on the Program, and here is the EXECUTABLE FILE.
If you are learning C++ programming, I would say that this exercise would be very beneficial to you, and it is fun as well!
* The attached file only works in the windows operating system. 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. So, Enjoy the game! Oh, please press [Ctrl + C] in case something acts up. The command will close the program.
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. Thereafter, we will create the display by changing those numbers into something that we can visualize and understand.
Main Function
Alright, lets begin with decoding the main function.
In line 5, srand(time(null)) initializes the random number generator that will be used to randomly place the “Food” for the Snake.
Line 6 then creates a snake object which we named nagini. This allows us to use the functions contained within the snake class.
In line 7, we call upon the initGround() function to initialise the playing area so lets take a look at the function itself.
Initialising the Ground
The #define statements can be found at the top of the program and it defines the variables FOOD to be 1, WALL to be -2, SNAKE to be -1 and NOTHING to be 0. As written in the comments, negative values represent areas that cannot be touched or else the game will end. In the later part of the program, the snake will be defined as -1 in the ground so whenever the head reaches a negative value in the ground, it means that it either bangs into itself, or the wall, and both results in a game over. On the other hand, when the head encounters a 1, it means that it has picked up food so it will grow in length. 0 represents safe spaces that the snake can travel freely.
The function initGround() starts off by creating an empty space of size [MAX][MAX] in the first for loop. MAX is defined in the beginning of the program to be 100 so the space created is a square with dimension 100 by 100 characters.
In the next two for loops, we draw the walls for the snake game. The value for WIDTH and HEIGHT were defined at the start of the program to be 77 and 22 respectively. This value is arbitrary so you can create the walls however you like it to be (you can actually create your own maze by defining your own walls within the enclosure!).
With this, we are done with initialising the ground area for our snake! Lets now move on to the next function in line 8 of main().
World Craft HD, is an epic online construction and engineering-based free-roam game similar in concept to Minecraft. In World Craft HD, you become a master landscape creator, and get to explore around a. Free construction games for mac.
*I made the ground area larger than the wall enclosure so that I don’t have to keep changing the ground values when I change the WIDTH and HEIGHT values. There are better ways to go about this but I was just lazy so please pardon me.
Initialising the Snake
The variable body is of an array of size WIDTH*HEIGHT of a custom type Coordinate with x and y. This just means that the maximum length of the snake is that of the whole playable area, and each part of the body has a x and y coordinate tagged to it.
The initSnake() function initialises a snake of length INIT_SNAKE_LENGTH with its head at the centre of the playable ground.
The initial direction of the snake is set to the RIGHT from the input variable. input is a global variable which is used to take in the user’s input to control the movement of the snake.
Lastly, the foodcounter is used to count the number of food picked up by the snake to show the final score of the player when the game ends.
In the for loop that comes after, we give the rest of the snake body a specific location so that it will “materialise”. As written in the comments, if the snake is moving right initially, the body must be on the left of the head. The array dx and dy are pre-filled with values to denote the change in the direction within the ground from the input variable. For example, if the input is RIGHT, it has a value of 0 so dx[0] is 1 and dy[0] is 0. This translates to a movement in (1,0), which is to the right. The same concept applies to the other directions as well. Since we want to define the rest of the body which is in the opposite direction of the snake’s movement, we minus dx and dy from the coordinates of the head of the snake to get the position of the next part of the snake progressively. Thereafter, we need to update the ground so that the snake is reflected in it and we are done with the snake!
Next up, lets take a look at the updateFood() function in line 9 of main().
How to play secret games on mac. Dear User.Here i can see your problem that you want to know that how to play hidden games in Mac OS X.So i will tell you that how to do that.So you just see my steps and then follow it.Let's have a look.First of all go to your Mac OS turn on and then go to the home screen and then Now to see the list of games which are available in your Mac just open the terminal with administrator privilege.And then type the command “ls /usr/share/emacs/21.2/lisp/play” and press Enter.After that you can see the list of games which are there in your Mac.
Creating Food for the Snake
Snake Game In C
This function is actually called every time the snake gets the food to create a new food on the ground but it can also be used to initialize the first food as what we are doing here. In the do while loop, we randomize a number for x and y to get the coordinate to place the food. The loop will keep looping until it finds a coordinate on the ground where there is nothing. This is to prevent spawning food on the snake or the walls. (A better way is to create a an array to store all the ground[][] coordinates with NOTHING and randomly pick a coordinate from there. This helps to make the process more efficient when the body of the snake is long and most of the ground is covered by the snake.)
Once a suitable location is found, we place the food there and increase the foodCounter variable. This function is called every time the snake eats the food so it can be used to record the number of food eaten as well. The gotoxy(x, y) function basically places the cursor at (x, y) so that the cout in the next line can print the character which defines the food. In this case, I choose to print u2022 which is the code for a middle dot. You can put any character in here to define your food actually.
Now that we are done with the creation of the food, lets move on to drawing the snake and the walls in the firstDraw() function in line 10 of main().
Drawing the Snake
The first clearScreen() function clears the command prompt console in case there is something written there. We then loop through all the playable area within HEIGHT * WIDTH and display accordingly based on the number that is set on the ground[][] array beforehand. The switch case within the for loops handles all the different scenarios that we have set.
When ground[i][j] is NOTHING, we print a blank space indicating that it is an accessible area.
When ground[i][j] is WALL, we print a ‘+’ sign for the corners, ‘-‘ for the top and bottom walls, and ‘|’ for the left and right walls.
When ground[i][j] is SNAKE, we print ‘+’ for the head and ‘0’ for the rest of the body.
The only condition left now is the food, so anything else on the ground must be the food and we print a middle dot for it. The reason why we need to print the food again here is because we cleared the screen initially.
At this point, we have the initial display of the snake and its environment ready! Lets get on to moving the snake next!
Capturing the User’s Input
The _beginthread(userInput, 0, (void*)0) function starts the function userInput(void* id) in a separate thread. 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).
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). The control for the snake is the traditional w,s,a,d button for up, down, left, right respectively. 27 is actually the ACSII code for the ‘esc’ button on your keyboard. When the user presses any of the button listed, the global variable, input, is reassigned to reflect the user input.
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. Otherwise, the thread will end and stop taking user inputs.
Next up, lets get the snake moving!
Moving the Snake
Up next in main() is the do while loop which actually moves the snake. The updateSnake(delay) function moves the snake to the next space then stops for a period of 50 milliseconds (denoted by the variable delay). The delay variable is used to adjust the speed of the snake so you can actually customize the game to include a difficulty level where you adjust the delay based on the difficulty selected. After moving to the next space, the updateSnake(delay) function will update the global variable item that the head of the snake landed on. If this item is FOOD, we call the updateFood() function to create a new food, and also update the score. The do while loop will keep running until the user presses the ‘esc’ button or when the head of the snake hits the wall or itself.
When we call the updateSnake(int delay) function, it first creates an array called prev[] of type Coordinate. The size of the array is initialized to be WIDTH * HEIGHT, which is the maximum length of the snake. This array creates a temporary copy of the current snake position so that we can use it as a reference for the new position. The first for loop copies all the positions of the snake from the body[] array.
Next, we check the user’s input to see if it is the ‘esc’ key, or opposite from the direction of the snake. If the snake is moving right, the user cannot make it move left immediately so we have to consider this case. Only when the player presses a valid key, we update the direction of the snake. The movement of the snake is done by updating the body[] array to its new location. prev[0].x and prev[0].y represents the coordinates of the snake’s head so we add the direction dx[direction] and dy[direction] to them respectively to get the new coordinate of the head.
Here, we check what the head of the snake encountered. NOTHING represents the value 0 so less than NOTHING means that the value is negative. As mentioned previously, meeting a negative number means that its GAMEOVER. We assign -1 to the item variable to notify the other functions and exit the updateSnake(int delay) function with the return keyword immediately.
If the head of the snake encounters a FOOD, we extend the length of its body with length++, then set the item to be FOOD. On the other hand, if the snake encountered nothing, we have to erase the last point of the snake because it is moving forward. This will be clearer when you see the diagram below.
In the NOTHING case, we have to erase the last point of the body, and this is done by updating the ground[y][x] to let it know that there is no longer anything there. The gotoxy(body[length – 1].x, body[length – 1].y) function brings the cursor to the location of the last point of the snake, and we print an empty space to “erase” the point.
Previously, we have updated the head of the snake so now we are left with updating the middle portion of the body. As seen from the diagram above, we can see that for both cases, body[0] becomes body[1], body[1] becomes body[2] and so on. Since we saved a copy of body[] as prev[], we write a for loop to update the body of the snake. Once this is done, we are left with updating the display on the console. The old head becomes a body, and we draw the new head. Lastly, we update the position of the whole snake once again for the ground and we are done!
GAMEOVER
C++ Snake Console Game
The last part of the program simply prints “GAMEOVER” together with the scoreat the center of the play area. With this, We are finally done with the game! Congrats on making it all the way to the end! I hope this has been a useful tutorial for those who are revising your C++ programming! (:
Possible Further Addons
C++ Snake Game For Mac Download
If you have gone through the whole tutorial, you would have realised that there is a lot of versatility when you program by yourself. For instance, you could make the delay variable a function of the food collected so that the snake moves faster and faster with each food it has collected. You could also draw your own maze in the play area by placing walls at wherever you like, and you could also add buttons that the user can press to alter the game while playing (such as pressing ‘r’ to speed up the snake during the game, or change the maze). The possibilities are limitless and I highly encourage you to try them out by yourself! Happy coding! (:
P.S. You can use the source code freely but please keep the reference to ThePoorEngineer at the top of the code!
BlogHPL is a cross-platform, open-source, in-House game engine written in C++. It also uses script language Angel Script that is similar to C/C++ language. It develops games for the platforms of Linux, Microsoft Windows and Mac OS and released by Frictional Games.
As the engine was developed for the horror games, its features are specified for the genre such as Bump Mapping, Normal Mapping and dynamic stencil shadows.
HPL was named after an American horror-fiction author, Howard Phillips Lovecraft. HPL was created in December 1994. This was developed for a university thesis and was developed as 2D engine. The first game developed using this engine was Energetic in 2005. Afterwards, it was upgraded to 3D engine. But the engine can still be used for 2D game development. Three versions of the engine have been released so far.
HPL Engine 1
Its first version HPL Engine 1 was mainly employed in Penumbra Series (link given at the end of article). Penumbra was a First Person, story based immersive Horror game. On May 2010, it was released as open source and was made licensed under GPL version-3 license. In 2010, Penumbra: Overture was included in Humble Indie Bundle (a platform that sells the games and donates a portion of profits to charity and rest goes to the developers). When it reached the milestone of 1 million dollars, the source code and modding tools of the game were made free under GNU license. On May 2010, the source code of the engine was made public.
HPL1 provides step by step guide to develop game via its tutorial page. These tutorials help creating maps, models and many more.
Gift 2 Gab lyrics performed by Mac Dre: Nick nack patty whack give a ho sum donkey Listen to the bass line. U didn't no about me The A the N the D the R the E Well peep game it goes like this I hold my mic tight, like my dick when I piss Cause when it comes to cock Girl I won't quiet it Always got the good rights to the muthafuckin gym. Peep game it goes like this mac dre lyrics eminem. Peep game, I started like this I'm nothin'. nice with the mic in my fist Never slackin when I'm mackin, get straight to the point And always got my damn lighter to the mothafuckin' joint. The A the N the D the R the E Well peep game it goes like this I hold my mic tight, like my dick when I piss Cause when it comes to cock.
Frictional Games WikiFeatures
- Newton Game Dynamics.
- Unique Physics Engine.
- Cutting Edge Technology.
- True Environment Interactions.
- Immersive Environment.
- Motion Blur and Bloom.
- Inventory Management System.
- Bump Mapping
- Normal Mapping
- Dynamic Stencil Shadows.
Games Developed in HPL Engine1
HPL Engine 2
HPL2 is the second version of HPL Engine series that specializes for horror games. It is a cross-platform, open-source, 3D game engine written in C++. This engine is the advanced version of HPL engine with several technical upgrades. Besides, Windows, Linux and Mac OS, it developed games for Mac OS X. this engine was mainly used to create Amnesia Series, developed by TheChinese Room Company.
The first game of the series, Amnesia: A Machine for Pig was to be released in September, 2013 but was postponed due to the massive expectations from the public. Also the delay was the result of the fact that the actual game exceeded in size from the experimental version.
C++ Snake Game With Graphics
HPL1 lack Modding Tools and in-depth documentation due to time constraint and personnel unavailability. But HPL2 came with a variety of modding tools and documentation. Each tool was well-described on how to be used in the Wiki page. HPL1 incorporated portal system for occlusion culling (the process of disabling objects rendering if the object is occluded behind other objects and not visible to camera) but in HPL2 the task is done through more efficient Coherent Hierarchical Occlusion and support for SSAO (Screen Space Ambient Occlusion). Other enhancements include Shadow maps that allowed more realistic environmental light and shadows.
HPL1 provides step by step guide to develop game via its tutorial page. These tutorials help creating maps, models and many more.
Computer Snake Game
Frictional Games WikiFeatures
- Material Editor
- Level Editor
- Particle Editor
- 3D Model Editor
- Model and Map viewer
- Shadow Maps
- Environmental Light
- Coherent Hierarchical Culling
- SSAO (Screen Space Ambient Occlusion)
Games Developed in HPL Engine 2
HPL Engine 3
HPL3 is the most recent version of HPL Engine series that specializes for horror games. It is a cross-platform, open-source, 3D game engine written in C++. It came with a number of technical upgrades. It is the first engine to Support both PC and Console. The Engine targets the platform of Windows, Mac OS X, Xbox One, Linux and PS4. Last version on HPL3 came in 2015. To provide same visuals to all platforms and for fast development, a dedicated shadow language HPSL was developed for HPL3.
It was develop along the development of the SOMA game. The game had been in development since 2010 and released on September 2015. Soma is a Greek word meaning body as distinct from the soul or mind. The second game of the engine is amnesia: Rebirth. Its announcement trailer was released in March 2020.
C++ Snake Game For Macbook
This engine is also made open source as described by the founder of HPL:
“We talked about the release of engine code and games for Open Source for quite some time. With the success of the Humble Indie Bundle, a great opportunity came up, and we decided to join other games in the open source. Although the code for the game and the engine has been developed for several years, even today it is not actively used. Basically, it just rots in the dark corners of our hard drives. The engine will feel much better in an open area, where it has the ability to grow and do something good. I hope that it will be useful, and I am very happy to see what people can do with it!”
– Thomas Grip, studio programmer and founderFeatures
- Global Sunlight and real-time Shadows.
- Fully lit Out Door Scenes.
- In- Engine Terrain Generation.
- Introduction of HPSL language.
- Open Space Support.
- Dynamic Terrain LODs (Level Of Details) for distant Scenes.
- Sophisticated Character AI.
- Anti-Aliasing.
- DOP (Depth Of Field).
- Streaming (No loading Screens between levels)
- Colored Specular.
- Particle Collision.
- Tessellation.
Games Developed in HPL Engine 3
Some Useful Resources
C++ Snake Game Tutorial
- Documentation of HPL : https://wiki.frictionalgames.com/start
- HPL 1 Source Code: https://github.com/FrictionalGames/HPL1Engine
- Penumbra Official Webpage: http://www.penumbragame.com/index.php
- Frictional Games Official webpage: https://frictionalgames.com/
- Frictional Games Forum: https://www.frictionalgames.com/forum/forum-35.html
- Wikipedia Link: https://ru.wikipedia.org/wiki/HPL_Engine