Snake Game In C Language

Snake Game In C Language Average ratng: 3,8/5 8473 votes
  • Please need help with this homework. The following code performs a snake game using graphics.h library in turbo c compiler.please add two more features. Using an '+' character to increase velocity or reduce delay and also an option to.
  • This SNAKE GAMING C project deals with the snake game. In this snake going to eat objects randomly emerging on screen and if successful in eating then it becomes larger in size and gains score. In this snake going to eat objects randomly emerging on screen and if successful in eating then it becomes larger in size and gains score.
  • Computer Programming - C Programming Language - Snake Game sample code - Build a C Program with C Code Examples - Learn C Programming.
  1. Snake In C
  2. Snake Game Program In C Language
  3. Snake And Ladder Game Code In C Language
  4. Snake Game In C
of 05

Introduction to the Games Programming Tutorials

You may have played snake games in nokia mobiles.in this instructables, you will learn how to make a snake game in c programing language. Don't worry, if you have no programing skills and you dont know about c.it is very simple and easy to make a snake game.you just have to follow my steps as i have done. C Language, Java, Advance java with all framework,Android, iPhone Java Strut,hibernate tutorial,best java example with source code,android tutorial,android interview question,iPhone all example,all c languages example,c languages project with source code,iPhone and android project free download. Mini project snake game in c. The purpose of this tutorial is to teach 2D game programming and C-language through examples. The author used to program games in the mid-1980s and was a game designer at MicroProse for a year in the 90s. Although much of that is not relevant to the programming of today's big 3D games, for small casual games it will serve as a useful introduction. I use Code:Blocks and it's better than C Free5 because Code:Blocks has more options like i don't know etc etc and it's a free compiler.I'm in the 7th class (sorry for my english) and my Computer teacher is impressed.My skills are so high,so i teach my collagues programming language.For me it's simple because i study a lot of programming language and i remember all the tricks of the programming.

This is the first of several games programming Tutorials in C for complete beginners. Instead of concentrating on teaching C then showing example programs they teach C by providing you with complete programs (ie games) in C

Keeping It Simple

The first game in the series is a console (i.e. text based game called Star Empires). Star Empires is a simple game where you have to capture all 10 systems in the Galaxy while stopping your AI opponent doing the same.

You start owning System 0, while your enemy own system 9. The remaining eight systems (1-8) all start neutral. All systems start within a 5 parsec x 5 parsec square so no system is more than 6 parsecs apart. The furthest two points are (0,0) and (4,4). By Pythagoras theorem, the furthest distance apart of any two systems is the square root ((4)2 + (4)2) which is the square root of 32 which is about 5.657.

Did you find this answer useful? A message of 'Modem Comm' or 'CC' should display. Wait a few seconds, you'll hear a click from the panel as the line seizure relay engages, then click the 'OK' button on the message box within Compass.Through the modem, the software will connect to the control panel, as if the connection were taking place over a phone line. Once connected, you can then upload and download panel programming. Honeywell vista programming software 2017.

Please note, this is not the final version and will be amended. Last change: August 21, 2011.

Turn Based & Real-Time

The game is turn based and each turn you give orders to move any number of fleets from any system you own to any other system. If you own more than one system you can order fleets to move from all your systems to the target system. This is done pro rata rounded up so if you own three systems (1,2,3) with 20, 10 and 5 fleets present and you order 10 Fleets to go to system 4 then 6 will go from system 1, 3 from system 2 and 1 from system 3. Each fleet moves 1 parsec per turn.

Each turn lasts 5 seconds though you can alter the speed to speed it up or slow it down by changing the 5 in this line of code to 3 or 7 or whatever you choose. Look for this line of code:

Snake

C Programming Tutorial

This game has been programmed and assumes that you don't know any C programming. I'll introduce C programming features in this and the next two or three tutorials as they progress. First though you'll need a compiler for Windows. Here are two free ones:

The CC386 article walks you through creating a project. If you install that compiler then all you have to do is load the Hello World program as described, copy and paste the source code over the example, save it and then hit F7 to compile it and run it. Likewise the Visual C++ 2010 article creates a hello world program. Overwrite it and press F7 to build Star Empires., F5 to run it.

of 05

Making Star Empires Work

Making Star Empires Work

We need to store infomation on fleets and systems in the game. A fleet is one or more ships with an order to move from one sytem to another. A star system is a number of planets but is more of an abstract entity in this game. We need to hold the following information for a fleet.

  • Origin System (1-10).
  • Destination System (1-10)
  • How Many Ships (1-Many)
  • Turns to Arrive
  • Whose Fleet is it? 0= Player, 9 = Enemy
struct fleet {
int fromsystem;
int tosystem;
int turns;
int fleetsize;
int owner;
};

A struct is a collection of data, in this case 5 numbers that we manipulate as one. Each number has a name, eg fromsystem, tosystem. These names are variable names in C and can have underscores like_this but not spaces. In C, numbers are either integer; whole numbers like 2 or 7 these are called ints, or numbers with decimal parts like 2.5 or 7.3333 and these are called floats. In the whole of Star Empires, we only use floats once. In a chunk of code calculating the distance between two places. Every other number is an int.

So fleet is the name for a data structure holding five int variables. Now that's for one Fleet. We don't know how many fleets we'll need to hold so we'll allocate generous room for 100 using an array. Think of a struct as like a dinner table with room for five people (ints). An array is like a long row of dinner tables. 100 tables means it can hold 100 x 5 people.

If we were actually serving those 100 dinner tables, we'd need to know which table was which and we do this by numbering. In C, we always numbers elements of arrays starting at 0. The first dinner table (fleet) is number 0, the next one is 1 and the last one is 99. I always remember it as being how many dinner tables is this table from the start? The first one is at the start so is 0 along.

This is how we declare the fleets (ie our dinner tables).

Read this from left to right. Struct fleet refers to our structure to hold one fleet. The name fleets is the name we give to all the fleets and [100] tells us there are 100 x struct fleet in the fleets variable. Each int occupies 4 locations in memory (called bytes) so one fleet occupies 20 bytes and 100 fleets is 2000 bytes. It's always a good idea to know how much memory our program needs to hold its data.

In the struct fleet, each of the ints holds an integer number. This number is stored in 4 bytes and the range of this is from -2,147,483,647 to 2,147,483,648. Most of the time we'll use smaller values. There are ten systems so both fromsystem and tosystem will hold values 0 to 9.

of 05

About Systems and Random Numbers

Each of the neutral systems (1-8) starts with 15 ships (a number I picked out of the air!) to start with and the other two (yours: system 0 and your computer opponent at system 9) have 50 ships each. Each turn the number of ships at a system is increased by 10% rounded down. So after one turn if you don't move them, your 50 will become 55 and each of the neutral systems will have 16 (15 + 1.5 rounded down). Note that fleets moving to another system don't increase in numbers.

Increasing the number of ships this way may seem a little odd, but I've done it to keep the game moving along. Rather than clutter this tutorial with too much on design decisions, I wrote a separate article about the design decisions of Star Empires.

Implementing Systems

Game

At the start we need to generate all the systems and put them on the map, with a maximum of one system in each location, As there are 25 locations on our 5 x 5 grid, we will have ten systems and 15 empty locations. We generate them using the function GenMapSystems() which we'll look at on the next page.

A system is stored in a struct, with the following 4 fields that are all int.

struct system {
int x,y;
int numfleets;
int owner;
};

The galaxy (all 10 systems) is stored in another array just like with fleets except we have 10 systems.

Random Numbers

All games need random numbers. C has a built in function rand() that returns a random int. We can force this into a range by passing the maximum number in and using the % operator. (Modulus). This is like clock arithemetic except instead of 12 or 24 we pass in an int number called max.

/* returns a number between 1 and max */
int Random(int max) {
return (rand() % max)+1;
}

Snake In C

This is an example of a function which is a piece of code wrapped up inside a container. The first line here that starts /* and end */ is a comment. It says what the code does but is ignored by the compiler which reads the C instructions and converts them into instructions that the computer understands and can execute very fast.

  • Wonder what a compiler is? Read What is a Compiler? (Article)

A function is like a mathematical function such as Sin(x). There are three parts to this function:

The int says what type of number it returns (usually int or float). Random is the name of the function and (int max) says that we're passing in an int number. We might use it like this:

int dice;
dice = Random(6); /* returns a random number between 1 and 6 */
return (rand() % max)+1;
of 05

Generating a Random Start Map

This code below generates the start map. That's it shown above.

void GenMapSystems() {
int i,x,y;
for (x=0;x for (y=0;y layout[x][y]=' ';
}
InitSystem(0,0,0,50,0) ;
InitSystem(9,4,4,50,1) ;
/* Find an empty space for remaining 8 systems*/
for (i=1;i do {
x= Random(5)-1;
y= Random(5)-1;
}
while (layout[x][y] !=' ') ;
InitSystem(i,x,y,15,-1) ;
}
}

Generating Systems is a matter of adding the player and the opponents systems (at 0,0) and (4,4) and then randomly adding 8 systems in the remaining 23 empty locations.

The code uses three int variables defined by the line

A variable is a location in memory that holds an int value. The variables x and y holds the coordinates of the systems and will hold a value in the range 0-4. The variable i is used for counting in loops.

To place the 8 random systems in the 5x5 grid we need to know if a location has a system already and prevent another one being put in the same location. For this we use a simple two dimensional array of characters. The type char is another type of variable in C and holds a single character like 'B' or 'x'.

Primer on Datatypes in C

The fundamental type of variables in C are int (integers like 46), char (a single character like 'A'), and float (for holding numbers with floating point like 3.567). Arrays [] are for holding lists of the same element. So char[5][5] defines a list of lists; a two dimensional array of chars. Think of it like 25 Scrabble pieces arranged in a 5 x 5 grid.

Now We Loop!

Each char is initially set to a space in a double loop using two for statements. A for statement has three parts. An initialization, a comparison part and a change part.

  • x=0; This is the initialization part.
  • x
  • x++. This is the change part. It adds 1 to x.

Inside the for (x loop is a for y loop that does the same for y. This y loop happens for each value of X. When X is 0, Y will loop from 0 to 4, when X is 1, Y will loop and so on. This means that every one of the 25 locations in the layout array is initialized to a space.

After the for loop the function InitSystem is called with five int parameters. A function has to be defined before it is called or the compiler won't know how many parameters it should have. InitSystem has these five parameters.

On the next page: Generating a Random Start Map Continues..

of 05

Generating a Random Start Map Continues

  • systemindex - a value from 0 -9.
  • x and y - coordinates of the system (0-4).
  • numships - how many ships there are at this system.
  • owner. Who owns a system. 0 means the player, 9 means the enemy.

So the line InitSystem(0,0,0,50,0) initializes system 0 at locations x=-0,y=0 with 50 ships to owner 0.

C has three types of loop, while loops, for loops and do loops and we use for and do in the function GenMapSystems. Here we have to place the remaining 8 systems somewhere in the galaxy.

for (i=1;i do {
x= Random(5)-1;
y= Random(5)-1;
}
while (layout[x][y] !=' ') ;
InitSystem(i,x,y,15,0) ;
}

There are two nested loops in this code. The outside loop is a for statement that counts up the i variable from an initial value of 1 to a final value of 8. We'll use i to refer to the system. Remember we've already initialzed system 0 and 9, so now we're initialising systems 1-8.

Everything from the do { to the while (layout[x][y] is the second loop. It's syntax is do {something} while (condition is true); So we assign random values to x and y, each value in the range 0-4. Random(5) returns a value in the range 1 to 5, subtracting 1 gets the range 0-4.

We don't want to put two systems at the same coordinates so this loop is looking for a random location that has a space in it. If there is a system there, the layout[x][y] will not be a space. When we call InitSystem it puts a different value there. BTW != means not equal to and means equal to.

When the code reaches the InitSystem after while (layout[x][y] != ' '), x and y definitely refer to a place in layout that has a space in it. So we can call InitSystem and then go round the for loop to find a random location for the next system until all 8 systems have been placed.

The first call to InitSystem sets up system 0 at location 0,0 (the top left of the grid) with 50 fleets and woned by me. The second call initialises system 9 at location 4,4 (bottom right) with 50 fleets and it's owned by player 1. We'll look closely at what InitSystem actually does in the next tutorial.

#define

These lines declare literal values. It's customary to put them in upper case. Everywhere the compiler sees MAXFLEETS, it uses the value 100. Change them here and it applies everywhere:

  • #define WIDTH 80
  • #define HEIGHT 50
  • #define MAXLEN 4
  • #define MAXFLEETS 100
  • #define MAXSYSTEMS 10
  • #define FIGHTMARKER 999

Conclusion

In this tutorial, We've covered variables and the use of int, char and struct to group them plus array to create a list. Then simple looping using for and do. If you examine the source code, the same structures are seen time after time.

Tutorial Twowill look at aspects of C mentioned in this tutorial.

Project: Classic Snake Game

– To download it for free (scroll down)

It's easy NOW! Corel draw old version download. With this program and Corel Draw you can print everything!

This classic arcade game is developed using C# Language. Itis a common game where the player has to eat Food which will be available in different Coordinates. The more you eat food, more you’ll get score points. Player has to move the direction of the snake by using Left, Right, Up, Down Arrow Keys and Backspace key to Pause the game/return to the main menu. From the settings: Graphics resolution can be maintained and difficulty level, as well as sound level, can also be maintained. This gaming project is easy to operate and understand by the users.

DOWNLOAD SNAKE GAME IN C# WITH SOURCE CODE : CLICK THE BUTTON BELOW

You May Also Like

Simple Banking System In C++ With Source Code

Project: Simple Banking System In C++ -to download simple banking system in c++ project for free(Scroll Down) The…

Staff Management System In C++ With Source Code

Project: Staff Management System in C++ with source code - To download the Staff Management System project for…

Vehicle Seat Reservation System in C++ With Source Code

Project: Vehicle Seat Reservation System in C++ with source code - To download the Vehicle Seat Reservation System…

Snake Game Program In C Language

Simple Text Editor In C# (updated) With Source Code

Project: - Simple Text Editor In C# (updated) with source code -To download simple text editor in c#…

Snake And Ladder Game Code In C Language

Pong Game In C# With Updated Version With Source Code

Snake Game In C

Project: Pong Game In C# With Updated Version With Source Code About Project The pong game in C#…