A C language implementation of the racing game

Source: Internet
Author: User
Tags sleep function

Bloggers have been learning C language for some time, for the purpose of their own study test, made a C language racing game.

Because the essence is testing and trying, so there is no focus on the game interface. Below is the development document, in the blogger's GitHub page can download the source code, note this project uses the way of engineering development, open also please use to open a project engineering way

Https://github.com/whatattitude/whatattitude.github.io

Absrtact: With the popularization of computer, the game has become an indispensable part of the users, there are many idle time in life, the game is widely circulated as a kind of entertainment way to pass time and relax body and mind. One of the car racing games should be for its stimulation, and the motion of the picture is popular like.

This paper mainly expounds the design and realization of a road racing game, and introduces the menu design, the movement and speed change of the car, the random generation and dynamic movement of the car, the storage and reading and sorting display of the file data. After opening the game, players can choose to start the game, view the leaderboard or check the Help, start the game according to the key action to play, and finally when the amount of blood is 0 o'clock, or when the game time is exhausted, end the game and return to the main menu.

This game is developed in C language, combined with EASYX graphics library to vc++6.0 as a system development platform.

Keywords: easyx graphics library; road racing game; C Language game design;

Directory

Introduction ... 1

1 Requirements Analysis and overview design ... 2

1.1 Overall requirements ... 2

1.2 System Design Architecture ... 2

1.3 Module Overview ... 3

1.4 Performance Requirements ... 4

1.5 Data Analysis ... 4

1.6 Development Platform ... 4

1.6.1 Visual c++6.0. 4

1.6.2EasyX.: 5

1.7 Operating environment ... 5

2 Detailed design ... 6

2.1 Interface Design ... 6

2.1.1 Main Menu interface ... 6

2.1.2 Game Interface Design ... 6

2.1.3 Game Help Interface design ... 6

2.1.4 Leaderboard Interface Design ... 6

2.2 Implementation of the function module ... 7

3 System Focus ... 9

3.1 Randomly generated cars every two seconds ... 9

3.2 Racing plus deceleration with dynamic movement ... 9

3.3 Reading and storing of file information ... 9

3.4 Game record Data bubble sort (doubly linked list game data) ... 10

3.5 Linked list destroys ... 10

3.6 Game Leaderboard Page display (two-way linked list game data) ... 10

Summarize ... 11

Thanks ... 12

Introduction

In this paper, the design and implementation of a road racing game is introduced in detail.

In this racing game with vc++6.0 as a system development platform, based on C language development, for the C language final project.

The development of the project has experienced several stages of document writing, game design, code writing and system testing.

The function module of the game has 4 parts, namely starts the game, the history leaderboard, the game help, exits the game. This game uses the Easyx graphics library to realize the game animation, the C language library function realizes the menu design, the menu design, the race car movement and the speed change, the game time computation and this council integral computation storage, the car's random generation and the dynamic movement, the file data storage reading and the sorting display and so on the key algorithm in the function module. In this paper, the game function module is briefly introduced before the implementation of the key functions of the detailed interpretation, convenient for the reader to understand.

1 Requirements Analysis and overview design

1.1 Overall requirements

This project developed a project development process for a preliminary understanding of the C language and achieved a racing game.

The Game menu interface for the beginning of the game, history leaderboard, game help, quit the game four options; After choosing to start the game, the player through the keyboard input the corresponding characters to start the game and control the car movement, acceleration, deceleration, by touching the blue car plus points, touch the red car will buckle blood, the right will operate tips game related actions, Left to indicate the remaining time, the number of items (hit the number of blue cars), the current points and other game data, meet the End condition (blood volume is 0 or time exhausted) game over. You can view the history of all points by clicking on the history leaderboard in the menu interface. Users who do not understand the rules can open the game by typing the corresponding numbers directly from the main interface to see the relevant tips. The game basically satisfies the player's demand for racing games, such as moving and adding, slowing down and picking up items in the four directions of a car.

1.2 System Design Architecture

The game consists of four parts, which are starting games, history leaderboards, game help, exiting the game.

1.3 Module Overview

The game consists of four parts, which are starting games, history leaderboards, game help, exiting the game

Start the game including game preparation, game play and end of Game 3 stages.

(1) Game preparation

When the player clicks from the main menu to start the game, the game data is initially drawn, the interface is initialized, waiting for the player to type in the appropriate action to start the game formally.

(2) The game

After the game began, the screen began to refresh the random vehicle down, the player through the keys to operate the car dodge, crashed into the car will change the data, the end of the condition to determine if the end condition to continue the game, meet the conditions to enter the game end stage.

(3) Game Over

At this point the data is written to the appropriate file and stored, and then the screen tip ends by pressing any key and returning to the main menu interface.

History charts. In this module, players can view game records sorted by game points, type the corresponding English lowercase characters to page back and forth, and press E to return to the main menu.

Game Help. See an introduction to the game's operations, and then press any key to return.

Quit the game. Click to exit the game normally.

1.4 Performance requirements

The game needs to be able to achieve the speed of car movement and acceleration, generate random cars, and for collision detection and processing the corresponding data to meet the basic needs of the players in the racing game.

The game interface needs to have information prompts, so that players can quickly get started, a short time to understand the game rules play.

At the same time, data storage and reading, data sorting and storage, data sorting and pagination display allow players to see the history of the past, and will record the ranking, the player can compare the relative ratio of three scoring factors to adjust the game strategy.

1.5 Data analysis

This game has three kinds of data, the first is to generate random car structure array, the second is to store the game records of the computer convenient to write to the structure of the file; The third is a doubly linked list used to read and sort records in a file.

The first is an array of structures used to generate random cars, including: random vertical coordinate change increments, real-time coordinate information x, Y, marking the type of car shaping variables.

The second is to store the game records of this bureau easy to write to the structure of the file include: The remaining number of life, pick up the number of items (that is, hit the number of blue cars), this council points, the time of the game.

The third is a list of two-way lists that are used to read and sort records in a file, including: An empty tail node, a pointer to a front and back, a history information structure that is read from a file (and the same as the second one).

1.6 Development Platform 1.6.1 Visual c++6.0

Visual c++6.0 is an object-oriented visual integration tool developed by Microsoft Corporation for the development of WIN32 applications (Windows 95/98/2000/xp/nt). Its greatest advantage is to provide a powerful MFC class Library, MFC is a large C + + class hierarchy, which encapsulates a large number of classes and their functions, many Windows programs common standard content can be provided by MFC classes, MFC class for these content provides a standard implementation of user interface, What programmers have to do is to fill in this profile with specific application specifics through a predefined interface, which simplifies programming and greatly reduces the number of code written by programmers, making programming easier and easier to work with.

1.6.2EasyX

Easyx is a graphics library for C + + that helps beginners in the C + + language to quickly get started with graphics and game programming.

For example, you can use VC + Easyx quickly with the geometry of a house, or a mobile car, you can write Tetris, Snake, Othello and other small games, you can practice the various algorithms of graphics, and so on.

1.7 Operating Environment

Microsoft Visual C + + 6.0

Microsoft Visual C + + 6.0, abbreviated VC6.0, is a C + + compiler that Microsoft introduced in 1998 with a minimum configuration of cpu:p 233 or more. 32MB memory.

Microsoft Visual C + + 6.0 has poor compatibility with Windows7 and WINDOWS8. The Windows7 use VC6.0 only need to ignore the compatibility prompt to use, [4] but in WINDOWS8 (including Windows8.1) use VC6.0 you need to change the original MSDEV.EXE file name and change the compatibility to normal use. [5] The first version of the WINDOWS10 can also be used normally VC6.0, but the WINDOWS10 system Upgrade update after the Chinese version of VC6.0 is not working properly, prompting "0xc0000142" error, Need to replace the original MSDEV.EXE file with the English version or Chinese version to normal use [6] (this version in WINDOWS8 (including Windows8.1) need to change the original MSDEV.EXE file name and change the compatibility to normal use. )。

Easyx

System support

Operating system version: Windows XP (SP3) and above operating systems.
Compiled environment version: Visual C + + 6.0/2008 ~ (x86 & x64).

2 Detailed design 2.1 interface design

The game interface for vc++6.0 call Easyx graphics Library drawing interface, directly using the original black background, font default printing white, racing by the way of illustration, simple style to achieve the purpose of rapid development and convenience.

2.1.1 Main Menu Interface

The main menu interface is used to guide the user into the corresponding module, and prompts the user how to operate. The main menu interface calls the function Outtextxy to print at the appropriate x, Y coordinates with the default axis: 1: Start the game, 2: History leaderboard, 3: Game Help, 4: Quit the game, type the corresponding number action game. It is convenient for the player to select the module for corresponding operation.

2.1.2 Game Interface Design

The game interface also uses the function Outtextxy to print the default axis at the appropriate x, Y coordinates on the right to prompt the user for the appropriate action: W Move up S Move down a left shift D right shift J accelerate K Decelerate Press L to start press E to pause, print at appropriate x, Y coordinates on left to show player earned points, remaining time , props score, four variables of life.

2.1.3 Game Help Interface design

The game help screen mainly for the player to indicate the game related operations and more detailed instructions, the same use function Outtextxy with the default axis at the right appropriate x, Y coordinates to print including (J acceleration k deceleration a left shift D right shift j acceleration K deceleration press L to start pressing E to pause the game, the other racing by Time generation, 2 seconds to hit the red car buckle blood, blue car added props when the amount of blood is 0, or the time is exhausted for the game, calculate the points and the current remaining amount of blood, time and the number of items picked up related to the game rules, the last print prompt press any key to return to the main menu.

2.1.4 Leaderboard Interface Design

The leaderboard interface is used to display the game history of the player, from the file to read the history of the good-side sequence of the intuitive top-down, from high to low display in front of the player, each page 5 rows show a row of 5 attacks, and at the bottom of the current page with the total number of leaderboard pages, The legal operation of the current interface--------Press N to the next page, press p to the previous page, and press E to return to the main menu. When the player makes a corresponding move up the page of the legitimate operation, the 5-group refresh interface to re-display the new 5 information and update the current page number of the leaderboard.

2.2 Implementation of the function module

The game is divided into four modules, which are starting games, history leaderboard, game help, exit the game

Start the game including game preparation, game play and end of Game 3 stages.

(1) Game preparation

When the player opens the app or the game ends, the player's vehicle coordinate position, the number of items picked, and the number of life items are reset to the original. Type the number that corresponds to the beginning of the Game 1 o'clock the game interface is static, such as the right operation instructions are drawn on the interface, and then the left need to change the real-time points, props, health initialization to print out, waiting for the player to type the appropriate action to formally start the game.

(2) The game

After receiving the correct starting game character typed by the player, set the game start time by the clock function start and end time end, and then calculate the scoring by the scoring relationship formula, and insert the player's car in the initial position, call a custom function to first define the initialization including the random car structure array, A series of necessary variables, such as interval variables, time-determining auxiliary variables, and a while loop with a life of 0 or a time-consuming variable not less than the one defined by the macro that reaches the header file is the end condition. At the top of this cycle starts to refresh the random vehicle down, the player through the key to operate the car dodge, crashed into the car will change the data, the end of the condition to determine if the end condition to continue the game, meet the conditions to enter the game end stage.

(3) Game Over

At this point the data is written to the appropriate file and stored, and then the screen tip ends by pressing any key and returning to the main menu interface.

History charts.

When the player enters this module, it will open the saved binary file in read-only form, and read the contents of the game information inside to generate a doubly linked list with null and tail nodes, then return the head node pointer, then use the bubble sort method to sort the doubly linked list by the highest to the lowest. Then open the file in a write-only manner to empty all the contents of the file, and then write the sorted records to the file and destroy the linked list. The file is then opened in a read-only manner, and the read content generates a doubly linked list with null and tail nodes, and calculates the total number of pages displayed based on the number of nodes that are reading the list. Next, use the while loop to print on the screen for every 5 printed or to the end of the tail node, according to the player type the corresponding English lowercase characters to page back and forth or press the return main menu.

Game Help. After you clear the screen, print a description of the game's operation, and then press any key to return.

Quit the game. Click to exit the game normally.

3 System Focus 3.1 randomly generated cars every two seconds

After opening the app, type 1 to enter the start Game module, set the time variable with the clock function start and end, the time of the game Costtime is (End-start)/1000, as the initial processing, set two auxiliary integer number a called key, A call judgment is initialized to 0;key to represent the time value for the next two seconds, and the Panduan variable is used to determine whether to generate a random vehicle. When at the same time to meet the judgment of 0 and the game to make the timing costtime and time point key phase, enter if statement key set to two seconds after the time, the judgment is set to 1, wait after 1 seconds, in the Panduan set to 0, avoid the same time repeatedly generated car, Random Racing uses a for loop to build a random car's structure array, generating each race car structure axis corresponding to an interval, the ordinate is 0, the initialization of the lower moving speed components of 40 to 70, randomly generated from 0 to 6 of the number used to determine whether the item is a blue car or a red car buckle.

3.2 Racing plus deceleration and dynamic movement

All random racing moves are made by the y-coordinate of each cycle with its own initial down-shift variable, and the dynamic movement is achieved by erasing and redrawing. The acceleration and deceleration is realized by the time variable in the sleep function of programmed sleep. The player moves the car by typing the corresponding characters and making the corresponding changes. Type W to decrease the y-coordinate of a speedy size, type S to increase the y-coordinate of a speedy size, type A to decrease the y-coordinate of one speedx size, type D to increase the y-coordinate of a speedx size, type J for time to decrease by 5 milliseconds, and type K to increase by 10 milliseconds. Determine if a threshold is reached before the change to avoid too fast or too slow.

3.3 Reading and storing of file information

The file information is stored in the game points of the relevant data, after each round of game opened in a write-only way to append the file at the end of a defined structure of the size of the data, and then close the file.

The read of the file information is opened in a read-only manner and then a message is read from the file, followed by a while loop with!feof (FP) as the end condition. In the loop, if the node pointer is a head node that points to null, use the malloc function to dynamically request memory space for head and tail, point the front pointer of the head node to null, the next pointer to the head node is tail,tail, the front pointer is the head node, and the tail's tail pointer points to null. Otherwise, the node pointer p uses the MALLOC function to dynamically request the memory space, the read information structure is copied to the data field of the linked list node, and then the back node of P points to the former node of the original tail, tail the former node of the original tail,p now points to P, Tail now the front node points to p, and each time a node is generated increments Len is used to count the number of nodes to remove the Kinsoku node.

3.4 Game record Data bubble sort (doubly linked list game data)

The doubly linked list generated after the file is read is sorted by the bubble sort method, which determines whether the linked list is empty or not. When the list is not empty, starting from the beginning of the node set m, p, q, n four pointer variables, with a double while loop to implement bubble sort, the outermost end condition is p know the last node of the tail node, the inner end condition is Q is the node. When the points in the P node are less than Q, Exchange two nodes, first the pointer to the left P node bypasses P point p to the front and back nodes, to move the left node p out, and then point to the right Q node's pointer to Q point to the front and back node, to the right node Q to move out, There is a connection between M and N, ignoring the pointer in P, q, and then inserting Q into the position of the original p by the forward and backward pointers in the M and N points (i.e., after M, the same reason p is inserted into the original Q position (i.e. before N), then the order of the logical nodes corresponding to p and Q pointers is restored. Regardless of whether you want to swap p or Q, the position of the Q and N pointers are shifted at each inner layer, and the outer layers move down m, p pointers.

3.5 Linked list destroyed

It is not necessary to decide whether the linked list is empty or not, and when it is not empty, the free function is used to release the memory space of the head pointer and move the head pointer down until the head pointer is null, and the list destroy operation is complete.

3.6 Game Leaderboard Page display (doubly linked list game data)

When the file information is read, it is sorted to generate a doubly linked list, and the number of valid information in the linked list is obtained. According to the number of valid information can be calculated the maximum number of pages, 5 in 1, greater than 5 of the 5 modulo is not 0 the page number is 5 rounding 1, in addition to the 5 rounding. Then use the sprintf function to convert the data into a string type and print it out every 5 pages. When you type N for the next page, if you can move down to the end of the current one, go to the next loop to redraw the output on the basis that the node pointer has reached the next header. When you type Y to page up, not on the first page, the node pointer is moved forward 10 node output according to the current number of pages in the middle, and the last two pages of data in the last page are shifted forward by the corresponding number of nodes to display.

A C language implementation of the racing game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.