Current location - Education and Training Encyclopedia - Graduation thesis - There is an urgent need to design an experimental report on snakes.
There is an urgent need to design an experimental report on snakes.
Students' Curriculum Design (Thesis)

Title: Snake Game Programming Guide

Student name: Student ID: 200910801001.

School (Department): Computer College

Major: Computer Science and Technology

Class level: 2009 1 class

Instructor: What Title: Lecturer

June 20 10

catalogue

Abstract 6

1, design requirements.

2. Instruments and equipment used

3. Specific design process 8.

3. 1, program function.

3.2 Design concept

3.3. The concrete realization of the design 8.

3.4 Overall Flow Chart ....................................... 14

3.5. Programming and Annotation ......................................................................................................................................................................

3.6 Debugging Problems and Solutions 2 1

3.7 Debugging results 22.

4. Design experience ..........................................................................................................................................................................

5. Refer to ............................ 26

abstract

Write a C language program to realize the snake eating game. Snakebite is a popular game. A snake appeared randomly on a closed wall. By pressing the four cursor keys on the keyboard, the snake is controlled to move up and down, left and right. The snakehead touched the food, indicating that the food was eaten by the snake. At this time, the snake's body grew, which counted as 10. Then the food appeared, waiting to be eaten by the snake. As a complete program, especially a complete game, human-computer communication and user experience must be considered. The interface of the game can't be too ugly, let alone without a simple interface. The game should have a beautiful interface. With the necessary hardware support and software support, game developers must make the game as beautiful as possible. The beauty of the game is on the one hand, and the inherent quality of the game is on the other. The quality of a game is ultimately decided by the player. Before the game meets the player, the game developer should design a game mode that allows the player to participate, and conduct it under certain game rules.

Snake flow chart; C language source program turbo C

Programming of Snake Game

1, design requirements

Through game programming, you can improve your programming interest and thinking, consolidate what you have learned in C language, make rational use of materials and realize the combination of theory and practice.

(1). Collect data, analyze topics, decompose problems, and form an overall design idea;

(2) Thoroughly analyze various minor problems, list the outlines, and write the program modules of each part;

(3). For the key functions used in the design, we should learn to understand their usage by looking up information and introduce them in detail in combination with the problems;

(4). Debug on the computer, check the errors, and gradually analyze the reasons that can't run normally to ensure that the designed program is correct and can run normally;

(5) Complete the course design report and reply.

C language is a general programming language that is easy to learn and understand. Because of its powerful functionality, simple application, flexibility and the advantages of both high-level and low-level languages, as well as the characteristics of "high efficiency of object programs", portability and universal implementation on various systems, it has become one of the mainstream programming languages in the world today and has been selected as a compulsory subject widely used by college students. As a contemporary college student, we should make good use of it, learn a design language well, and apply what we have learned.

Making a C program report can consolidate and deepen your understanding and mastery of the basic knowledge of C language courses, and master the basic skills of C language programming and program debugging.

Through the training of game programming, you can improve your basic skills, better grasp the expression method of string and the function of string function, the basic knowledge of Tc graphic operation, the acquisition of special keys on the keyboard and the display of cursor in graphic mode, improve your programming interest and level, learn how to correctly write programming instructions, improve your ability to solve practical problems by using C language, consolidate your understanding and mastery of C language grammar rules, learn to write flowcharts through source programs, and improve your ability of self-study and reference.

2. Instruments and equipment used

1, a computer that can work normally; 2、WindowsXP

3.TC plan; 4. Microsoft Word2003

3. Specific design process

3. 1, program function

Snake game is a classic little game. A snake is in a closed wall, and a food appears randomly in the wall. By pressing the four cursor keys on the keyboard, the snake is controlled to move up and down, left and right. If the snakehead knocks down the food, the food will be eaten, the snake's body will increase by 10 points, and then the food will appear, waiting for the snake to eat. If the snake hits the wall or passes through the body during the movement, the game is over.

3.2 Design concept

The key of the program lies in the figure representing the snake and its movement. A small rectangle is used to quickly represent a part of a snake. For each part of the body, add a rectangular block, and the snake head is represented by two parts. When moving, you must start from the head of the snake, so that the snake cannot move in the opposite direction. If you don't press any key, the snake will move in the current direction by itself, but after pressing the effective direction key, the snake's head will move in this direction, one body at a time. So after pressing the effective direction key, the position of the snakehead will be determined first, and then the snake body will move with the snakehead. The realization of the figure is to draw the snake from the new position of the snake head. At this time, due to the unclear screen, the original. The appearance and disappearance of food is also to draw a rectangular block and cover it. In order to facilitate understanding, two structures are defined: food and snake.

3.3. The concrete realization of the design

(1) function definition

Function definition is the definition of each basic function, which sets the information needed for convenient calling.

# define N 200

# include < graphics.h >/* graphics header file */

# include & ltstdlib.h & gt/* contains functions such as rand */

# include & ltdos.h & gt/* contains bios function */

# define Left 0x4b 00/* Left cursor value */

# definereight 0x4d00/* Right cursor value */

# Define the downward 0x5000/* key value */

# Define UP 0x4800/* key value */

# Define ESC 0x011b/* ASCII code of ESC */

Int i, key/* Graphic coordinate variables and key variables */

Int score = 0; /* Score */

int gamespeed = 10000; /* Adjust the game speed by yourself */

Structured food

{

int x; /* food abscissa */

int y; /* The ordinate of food */

Int yes/* Judge whether the variable of food will appear */

} food; /* The structure of food */

Structural snake

{

int x[]; /* The abscissa of the snake */

int y[]; /* The ordinate of the snake */

Int node; /* The number of knots of the snake */

Int direction; /* The moving direction of the snake */

Int life/* Snake's life, 0 is alive, 1 is dead */

} snake;

void Init(void); /* Graphics driver */

Void Close (invalid); /* End of chart */

Void DrawK (invalid); /* Draw interface function */

void game over(void); /* End the game */

Void play (Void); /* The specific process of playing the game */

void PrScore(void); /* Output results */

Void main(void)/* main function */

(2) main function main ()

The main function is the main flow of the program. First, define the constants, global variables and function prototype descriptions used, then initialize the graphics system, call the function DrawK () to draw the start screen, and call the function GamePlay (), which is the specific process of playing games. After the game, call Close () to close the graphics system and end the program.

Void main(void)/* main function */

{

init(); /* Graphics driver */

DrawK(); /* Start screen */

Playfulness (); /* The specific process of playing the game */

close(); /* End of chart */

}

Void Init(void)/* graphics driver */

{

int gd=DETECT,GM;

init graph(& amp; gd,& ampgm," c:\ \ TC "); /* The first parameter indicates the type of graphics adapter, the second is the display mode of graphics under this type, and the third parameter specifies the directory where the driver is located. */

clear device();

}

(3) Draw the interface function DrawK ()

The main interface is a closed fence, and two loop statements are used to output a continuous rectangular box with the same width and height in the horizontal and vertical directions, which represents the fence and is set to white to stand out.

Void draw (void)/* starts the screen, with the coordinates of (50,40) in the upper left corner and (6/kloc-0,460 */

{

setbkcolor(0); /* Set the current background color */

Setcolor (yellow); /* Set the current line drawing color */

setlinestyle(SOLID_LINE,0,THICK _ WIDTH); /* Set the line type */

for(I = 50; I & lt=600; I+= 10)/* Draw a fence */

{

Rectangular (I, 40, i+ 10, 49); /* higher than */

Rectangular (I, 45 1, I i+ 10/0,460); /* less than */

}

for(I = 40; I & lt=450; i+= 10)

{

Rectangular (50, I, 59, I+10); /* Left */

Rectangular (60 1, I, 6 10, I+10); /* Right */

}

}

(4) The specific process function of the game ()

This is the main part of the game. He assigned the coordinates of the former part to the latter part and deleted the background color of the last part. When the coordinates of the snake head are equal to those of the food, it means that the food has been eaten.

Void gameplay (void)/* The specific process of playing the game */

{

randomize(); /* Random number generator */

food . yes = 1; /* 1 means that new food needs to appear, and 0 means that food already exists */

snake . life = 0; /* alive */

snake . direction = 1; /* Right direction */

snake . x[0]= 100; snake . y[0]= 100; /* snakehead */

snake . x[ 1]= 1 10; snake . y[ 1]= 100; /* Snake in the second quarter */

snake . node = 2; /* Number of sections */

PrScore(); /* Output score */

And (1)/* You can play the game repeatedly and press ESC to end */

{

And (! Khit ())/* The snake moves by itself without pressing the key */

{

If(food.yes== 1)/* Need new food */

{

food . x = rand()% 400+60;

food . y = rand()% 350+60;

And (food.x% 10! =0)/* After the food appears randomly, the food must be in the whole grid before the snake can eat */

food . x++;

And (food.y% 10! =0)

food . y++;

food . yes = 0; /* There is food on the screen */

}

If(food.yes==0)/* If there is food on the screen, it will be displayed */

{

Setcolor (green); /* The color of the food */

Rectangular (food.x, food.y, food.x+ 10, food.y-10);

}

for(I = snake . node- 1; I>0; I-)/* Every link of the snake moves forward, and the key algorithm of the snake */

{

snake . x[I]= snake . x[I- 1];

snake . y[I]= snake . y[I- 1];

}

Switch(snake.direction) /* defines 1, 2, 3, 4 as the left, right, up and down direction, and moves the snakehead */

{

case 1:snake . x[0]+= 10; Break;

Case 2: snake.x [0]-=10; Break;

Case 3: snake.y [0]-=10; Break;

Case 4: snake.y [0]+=10; Break;

}

for(I = 3; I < snake. node; I++)/* Judge whether you hit yourself from the fourth quarter of the snake, because the snake head has two sections, and it is impossible to turn around in the third quarter */

{

if(snake . x[I]= = snake . x[0]& amp; & ampsnake.y[i]==snake.y[0])

{

game over(); /* Display failed */

snake . life = 1;

Break;

}

}

if(snake . x[0]& lt; 55 | | snake . x[0]& gt; 595 | | snake . y[0]& lt; 55 | | snake . y[0]& gt; 455)/* Did the snake hit the wall */

{

game over(); /* This game is over */

snake . life = 1; /* Snake dies */

}

If(snake.life== 1)/* After the above two judgments, if the snake dies, jump out of the inner loop and start again */

Break;

if(snake . x[0]= = food . x & amp; & snake.y [0] = = food.y)/* Eat your food */

{

set color(0); /* Remove food from the picture */

Rectangular (food.x, food.y, food.x+ 10, food.y-10);

snake . x[snake . node]=-20; snake . y[snake . node]=-20; /*-20 means tail length */

/* Put the new paragraph out of sight first, and take the position of the previous paragraph in the next cycle */

snake . node++; /* The snake's body has a long section */

food . yes = 1; /* New food needs to appear on the screen */

Score+=10;

PrScore(); /* Output new score */

}

set color(4);

for(I = 0; I < snake. node; I++)/* Draw a snake */

Rectangular (snake.x[i], snake.y[i], snake.x[i]+ 10,

snake . y[I]- 10);

Game speed;

set color(0); /* Remove the last paragraph of the snake in black */

Rectangular (snake.x[snake.node- 1], snake.y[snake.node- 1],

snake . x[snake . node- 1]+ 10,snake . y[snake . node- 1]- 10);

} /*endwhile(! kbhit)*/

If(snake.life== 1)/* If the snake dies, jump out of the loop */

Break;

key = BIOS key(0); /* Receive button */

If(key==ESC)/* Press ESC to exit */

Break;

other

if(key = = UP & amp; & Snake, direction! =4)

/* Judge whether to move in the opposite direction */

snake . direction = 3;

other

if(key = = RIGHT & amp; & Snake, direction! =2)

snake . direction = 1;

other

if(key = = LEFT & amp; & Snake, direction! = 1)

snake . direction = 2;

other

if(key = = DOWN & amp; & Snake, direction! =3)

snake . direction = 4;

}/*endwhile( 1)*/

}

(5) GameOver function ()

After the game, clear the screen, output the score and display the game end information.

Void GameOver(void)/* Game over */

{

clear device();

PrScore();

Setcolor (red);

settextstyle(0,0,4);

Outtextxy (200,200, "Guo Jian");

getch();

}

Void PrScore(void)/* output score */

{

char str[ 10];

Setfillstyle(SOLID_FILL, white);

Ba (50,15,200,35);

set color(6);

settextstyle(0,0,2);

sprintf(str," score:%d ",score);

outtextxy(55,20,str);

}

Void Close(void)/* End of graph */

{

getch();

closegraph();

}

3.4 Overall Flow Chart

}

3.5, program code writing and annotation

# define N 200

# include & ltgraphics.h & gt

# include & ltstdlib.h & gt

# include & ltdos.h & gt

# Define left 0x4b00

# Define right 0x4d00

# Define 0x5000 downward

# Define up to 0x4800

# Define ESC 0x0 1 1b

int i,key

Int score = 0; /* Score */

Int gamespeed = 50000/* Adjust the game speed by yourself */

Structured food

{

int x; /* food abscissa */

int y; /* The ordinate of food */

Int yes/* Judge whether the variable of food will appear */

} food; /* The structure of food */

Structural snake

{

int x[N];

int y[N];

Int node; /* The number of knots of a snake */

Int direction; /* The moving direction of the snake */

Int life/* Snake's life, 0 is alive, 1 is dead */

} snake;

void Init(void); /* Graphics driver */

Void Close (invalid); /* End of chart */

Void DrawK (invalid); /* Start screen */

void game over(void); /* End the game */

Void play (Void); /* The specific process of playing the game */

void PrScore(void); /* Output results */

/* Main function */

Invalid master (invalid)

{

init(); /* Graphics driver */

DrawK(); /* Start screen */

Playfulness (); /* The specific process of playing the game */

close(); /* End of chart */

}

/* Graphics driver */

Void initialization (void)

{

int gd=DETECT,GM;

init graph(& amp; gd,& ampgm," c:\ \ TC ");

clear device();

}

/* Start the screen with (50,40) in the upper left corner and (6/kloc-0,460 */

Void DrawK (invalid)

{

/*setbkcolor (light green); */

set color( 1 1);

setlinestyle(SOLID_LINE,0,THICK _ WIDTH); /* Set the line type */

for(I = 50; I & lt=600; I+= 10)/* Draw a fence */

{

Rectangular (I, 40, i+ 10, 49); /* higher than */

Rectangular (I, 45 1, I i+ 10/0,460); /* less than */

}

for(I = 40; I & lt=450; i+= 10)

{

Rectangular (50, I, 59, I+10); /* Left */

Rectangular (60 1, I, 6 10, I+10); /* Right */

}

}

/* The specific process of playing the game */

Invalid game (void)

{

randomize(); /* Random number generator */

food . yes = 1; /* 1 means that new food needs to appear, and 0 means that food already exists */

snake . life = 0; /* alive */

snake . direction = 1; /* Right direction */

snake . x[0]= 100; snake . y[0]= 100; /* snakehead */

snake . x[ 1]= 1 10; snake . y[ 1]= 100;

snake . node = 2; /* Number of sections */

PrScore(); /* Output score */

And (1)/* You can play the game repeatedly and press ESC to end */

{

And (! Khit ())/* The snake moves by itself without pressing the key */

{

If(food.yes== 1)/* Need new food */

{

food . x = rand()% 400+60;

food . y = rand()% 350+60;

And (food.x% 10! =0)/* After the food appears randomly, the food must be in the whole grid before the snake can eat */

food . x++;

And (food.y% 10! =0)

food . y++;

food . yes = 0; /* There is food on the screen */

}

If(food.yes==0)/* If there is food on the screen, it will be displayed */

{

Setcolor (green);

Rectangular (food.x, food.y, food.x+ 10, food.y-10);

}

for(I = snake . node- 1; I>0; I-)/* Every link of the snake moves forward, which is the key algorithm of the snake */

{

snake . x[I]= snake . x[I- 1];

snake . y[I]= snake . y[I- 1];

}

/* 1, 2, 3, 4 means right, left, up and down, and this judgment can move the snake head */

Switch (serpentine direction)

{

case 1:snake . x[0]+= 10; Break;

Case 2: snake.x [0]-=10; Break;

Case 3: snake.y [0]-=10; Break;

Case 4: snake.y [0]+=10; Break;

}

for(I = 3; I < snake. node; I++)/* Judge whether you hit yourself from the fourth quarter of the snake, because the snake head has two sections, and it is impossible to turn around in the third quarter */

{

if(snake . x[I]= = snake . x[0]& amp; & ampsnake.y[i]==snake.y[0])

{

game over(); /* Display failed */

snake . life = 1;

Break;

}

}

if(snake . x[0]& lt; 55 | | snake . x[0]& gt; 595 | | snake . y[0]& lt; 55||

snake . y[0]& gt; 455)/* Did the snake hit the wall */

{

game over(); /* This game is over */

snake . life = 1; /* Snake dies */

}

If(snake.life== 1)/* After the above two judgments, if the snake dies, jump out of the inner loop and start again */

Break;

if(snake . x[0]= = food . x & amp; & snake.y [0] = = food.y)/* Eat your food */

{

set color(0); /* Remove food from the picture */

Rectangular (food.x, food.y, food.x+ 10, food.y-10);

snake . x[snake . node]=-20; snake . y[snake . node]=-20;

/* Put the new paragraph out of sight first, and take the position of the previous paragraph in the next cycle */

snake . node++; /* The snake's body has a long section */

food . yes = 1; /* New food needs to appear on the screen */

Score+=10;

PrScore(); /* Output new score */

}

set color(4); /* Draw a snake */

for(I = 0; I < snake. node; i++)

Rectangular (snake.x[i], snake.y[i], snake.x[i]+ 10,

snake . y[I]- 10);

Game speed;

set color(0); /* Remove the last paragraph of the snake in black */

Rectangular (snake.x[snake.node- 1], snake.y[snake.node- 1],

snake . x[snake . node- 1]+ 10,snake . y[snake . node- 1]- 10);

} /*endwhile(! kbhit)*/

If(snake.life== 1)/* If the snake dies, jump out of the loop */

Break;

key = BIOS key(0); /* Receive button */

If(key==ESC)/* Press ESC to exit */

Break;

other

if(key = = UP & amp; & Snake, direction! =4)

/* Judge whether to move in the opposite direction */

snake . direction = 3;

other

if(key = = RIGHT & amp; & Snake, direction! =2)

snake . direction = 1;

other

if(key = = LEFT & amp; & Snake, direction! = 1)

snake . direction = 2;

other

if(key = = DOWN & amp; & Snake, direction! =3)

snake . direction = 4;

}/*endwhile( 1)*/

}

/* Game over */

3.6 Debugging Problems and Solutions

When the modified program is copied to Turbo C, the software will move to the right because of too much content, so that most of the content cannot be copied to the interface of Turbo C, and finally the software cannot run. Solution: when changing the program, you should set the format well, pay attention to the left alignment, and at the same time, one line of statements should be as short as possible, and it is best for one statement to occupy one line.

Input the program into TC, compile and run the source program, and find many errors in the program, as shown in the following figure:

You can modify the original program according to the error prompt of the program running, and sometimes you will encounter problems you don't understand during debugging. I went to the library or looked up some information on the Internet or found a teacher to solve the problem of modifying the source program one by one until the operation was successful.

3.7 Debugging results

1. The following figure shows the results of program debugging and compilation:

The following figure is the debugging result of the program (that is, the snake game interface).

4 design experience

After just two weeks of computer software technology practice, I have a deeper understanding of C program. I used to think that C language was boring and the program we designed was useless, but now I know how to apply what I have learned to my life by designing a snake game. Although I didn't know how to design this program at first, I deeply felt the magic of C program after reading the Case Collection of C Language Curriculum Design and making this snake program in C language.

When designing this program, I mainly learned how to use the following knowledge about C language.

1) As the name implies, it is very important to define a function. It helps readers to understand the program correctly.

It is very important that in the process of modifying this program, we can quickly find the functions of each module of the program and greatly increase it.

And the readability of the program is increased.

2) The analysis function starts with the main () function. The Main () function is the beginning of compiling the C source program.

Starting with the main () function, we can understand the functions of other functions more thoroughly.

3) When making a program, first list the framework and analyze the purpose (function) to be achieved by this program.

In the future, it will be simpler and more reasonable to choose the correct data structure, then modularize the program and write the function according to the module.

4) I also learned a lot about the role of library functions, such as many string functions performed on strings.

I know all the functions and functions.

At the same time, I also gained a lot of valuable experience:

1) Before designing a program, you must have a systematic understanding of the topic and content you designed.

Know what resources are included in the title and content of the design.

2) It doesn't matter what programming language is used to design the program. The key is to have a clear idea.

And a complete software flow chart, therefore, it is much easier to design after defining the design principles and ideas first and then drawing the flow chart.

3) When designing a program, you can't expect to design the whole program at once, "repeatedly modify and constantly change."

Progress is the only way to program design, and finding mistakes is also an achievement.

4) Develop a good habit of annotating programs. The perfection of programs is not only to realize functions, but more importantly.

People should be able to understand your thoughts at a glance, which also facilitates the preservation and exchange of information.

Step 5 refer to

1. Guo, Selected cases of C language curriculum design, China Water Resources and Hydropower Press, March 2004.

2 Xu Jinwu, Yang Debin, etc. TURBO C practical encyclopedia, mechanical engineering press, 1996.5.

3 Li Lijuan "C Language Programming Course" People's Posts and Telecommunications Publishing House

4 Lin Huacong "The Thought and Practice of C Language Programming" Metallurgical Industry Press

5 Zhang Jiwen "C Language Programming Course" Higher Education Press

6 Pan Yunhe, Dong Jinxiang, et al. Computer Graphics-Principles, Methods and Applications. Beijing: Higher Education Press, 2003+02.

7 Sun et al., Computer Graphics (3rd Edition), Tsinghua University Press, 2004.

Chen. Practical technology of computer graphics. Beijing Science Press 2000.

9 what to do. Principles and algorithms of computer graphics. Beijing: Tsinghua University Publishing House.

10 Lu runmin C language drawing course. Beijing: Tsinghua University Publishing House, 1996.