Ropei-C language simple game programming teaching

Source: Internet
Author: User

Writing a lot of software requires a menu, so how do we write a menu if we use software like TC to compile programs? Let's try it together!
The first step: a simple example
Let's start by writing the simplest example:
#include <stdio.h>/* contains input/output header file */
int a;/* defines a variable A as the return value of the menu */
int menu_a; /* Define a variable menu_a as a variable for the number of inputs in the menu */
int menu ()/* Menu function */
{
printf ("1 menu1 2 menu2 3 exit \ n");/* Outputs the contents of double quotes on the screen */
scanf ("%d",& menu_a);/* Let the user enter a number and store it in variable A */
return (menu_a);/* takes the input number A as the return value of the function */
}
void Main ()/* Main function */
{
A=menu (); /* contains input/output header file */
printf ("You choose is%d", a); /* Output information, you can also be used to judge the menu here.
Getch (); /* Wait for key input */
}
After running and making a selection, you should be able to see the following screen:

Step Two: Beautify
However, we will say: "Now is what age, how still use this menu Ah!" "So we're going to beautify our menu. Let's modify the menu () with the following code:
int menu ()
{
printf ("--my solfwave--\ n");
printf ("%c menu1\n", 4);
printf ("menu2\n");
printf ("exit\n");
scanf ("%d",& menu_a);
return (MENU_A);
}
Here we simply adjust the menu to show a little more beautiful, where the two printf%c is the character that calls the ASCII code corresponding to the following parameter, here the parameter is 4, the corresponding character is a diamond. The effect after operation is as follows:

Step Three: Keyboard response
In the Bios.h header file There is a bioskey function that can judge our keys. For example, when we press the "up" of the keyboard, its return value is 18432, press the "bottom" of the keyboard to return a value of 20480, the return value of other keys we can search from the Internet.
Let's start with the following files
#include <bios.h>
We want the first option of the menu to be the default, so we assign it a value of 1 while defining the menu_a.
int menu_a=1;
The modified menu () is as follows:
int menu ()
{
printf ("--my solfwave--\ n");
printf ("%c menu1\n", 4);
printf ("menu2\n");
printf ("exit\n");
while ("")/* Add an infinite loop */
{
if (Bioskey (0) ==18432&&menu_a>1)
{menu_a-=1; printf ("%d", menu_a);}
/* If the "up" button is pressed on the keyboard, and the menu_a is reduced by 1 (that is, in the second and third menu) after pressing the "Up" menu (that is, the default menu is moving up), we finally show the menu singular so that we can clearly understand */
else if (bioskey (0) ==20480&&menu_a<3)
{menu_a+=1; printf ("%d", menu_a);}
/* If the keyboard, "down" is pressed, do the appropriate action */
}
/* Originally there is a return (menu_a), but this sentence is not actually executed, because the program will be stuck in the loop */
}
After the program runs, you should see the following effect: (2321 of the last line will vary depending on your key order)

Fourth Step: Prism "cursor"
Next we're going to move the ribs up and down in front of the menu.
Because we will use CLRSCR to clear the screen function, so we also need to include the header file Conio.h code as follows:
#include <conio.h>
Let's modify the contents of the while ("") {} in the menu as follows:
while ("")
{
if (Bioskey (0) ==18432&&menu_a>1)
Menu_a-=1; /* Before this sentence printf just to facilitate our debugging, now has no effect, delete it */
else if (bioskey (0) ==20480&&menu_a<3)
Menu_a+=1;
CLRSCR (); /* Clear the screen */
Switch (menu_a)/* To determine the value of menu_a */
{
Case 1:/* If the value of Menu_a is 1, that is, the prism cursor is in the first menu */
printf ("--my solfwave--\ n"); /* Re-display the contents of the screen and place the ribs in the first menu */
printf ("%c menu1\n", 4);
printf ("menu2\n", 4);
printf ("exit\n", 4);
Break
Case 2:
printf ("--my solfwave--\ n"); */* Re-display the contents of the screen, place the ribs in the second menu position */
printf ("menu1\n", 4); */* Re-display the contents of the screen, place the ribs in the third menu position */
printf ("%c menu2\n", 4);
printf ("exit\n", 4);
Break
Case 3:
printf ("--my solfwave--\ n");
printf ("menu1\n");
printf ("menu2\n");
printf ("%c exit\n", 4);
Break
}
After running, we can select the menu through the keyboard, as shown in:

Fifth step: Responding to events
We're going to let the program exit the menu function and give the corresponding return value when we press ENTER (the return value of Bioskey (0) is 7181 when we press the return of the keyboard) we modify a small piece of code inside the menu () while ("") {}:
if (Bioskey (0) ==18432&&menu_a>1)
Menu_a-=1;
else if (bioskey (0) ==20480&&menu_a<3)
Menu_a+=1;
else if (bioskey (0) ==7181)
return (MENU_A);
Here we add one more judgment, when the carriage return in the keyboard is pressed, exit the function menu () and menu_a as the return value.
Speech
Learn all kinds of software or game production, the menu is an essential part of it, I hope that everyone in the study to make unremitting efforts. Of course, this tutorial is just an example, you can make your own more beautiful, more powerful menu.

Ropei-C language simple game programming teaching

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.