The first chapter of the game tutorial (C++ version) that Xiaobai can understand

Overview
Playing games is actually simulating according to their own ideas, what will happen to tourists doing Operation A and Operation B. In this way, some of the most basic mini games can be realized.
Here is an example~

#include <bits/stdc++.h>
using namespace std;
int main() {
    
    
A:
	cout << "请输入一个1~1000的整数。";
	srand(time(0)); int x, sum = 0, res = rand() % 1000 + 1;
	while (true) {
    
     
		cin >> x; ++sum; 
		if (x > 1000 || x < 1) {
    
    
			cout << "输入错误,请重新输入!"; goto A;
		}
		if (x == res) {
    
    
			cout << "答对了!一共用了" << sum << "次!\n"; return 0; 
		} else if (x < res) {
    
    
			cout << "输入的数小于答案!\n"; 
		} else {
    
    
			cout << "输入的数大于答案!\n"; 
		}
	}
	return 0;
}

Even the simplest guessing game requires a lot of grammar! Please keep learning!
1. How to generate random numbers in the range of n to m
First, we need to call the header file: #include <ctime> (time.h)
Then, we need to plant a random seed (otherwise it will be the same as the last time) ~srand( time(0));
Finally, we have to call the rand function and mod it cleverly~
int n = rand()% m + n;
2. The usage of
goto statement The semantics of goto statement is to change the flow of the program and turn to execute the statement label Identified statement. It is equivalent to calling a function.
Of course, this kind of the simplest small games will make people annoying to play, so we have two solutions:
1. Beautify the game page
2. Change a game
Obviously, the first solution is simpler. So, let's learn how to beautify the page with Mr. Konjai!
The method of beautifying the game page
1. Change the window size
The system function in #include <windows.h> will be used here.
Specific implementation: system("mode con cols = wide lines = high");
Note
1. There must be double quotation marks in system
2. There is a space between width and lines, not commas
2. Change color
1) Change background/foreground color
first Define the
background color: the color of the small black window. Foreground color: the color of the input/output stuff.
The system function is also used here (don't forget the header file duck).
Specific implementation: system("color background color and foreground color");
tips: here is a table for everyone.
Background color, foreground color
0 = black 8 = gray
1 = blue 9 = light blue
2 = green A = light green
3 = lake blue B = light light green
4 = red C = light red
5 = purple D = light purple
6 = yellow E = light yellow
7 = white F = bright white
2) Change the font color
Specific implementation (also call windows.h): SetConsoleTextAttribute (handle|color). GetStdHandle and FOREGROUND_ or BACKGROUND_. The value is INTENSITY or RED or GREEN or BLUE.
Here is a list for everyone~
GetStdHandle(STD_OUTPUT_HANDLE) to get the handle.
FOREGROUND_INTENSITY means to set the foreground color to be highlighted.
FOREGROUND_RED means to set the foreground color to red, that is, the font color is red.
FOREGROUND_GREEN means setting the foreground color to green, that is, the font color is green.
FOREGROUND_BLUE means to set the foreground color to blue, that is, the font color to blue.
BACKGROUND_INTENSITY means to set the background color to be highlighted.
BACKGROUND_RED means to set the background color to red.
BACKGROUND_GREEN means to set the background color to green.
BACKGROUND_BLUE means to set the background color to blue.
Note : Use | to separate the two parameters instead of commas.
3. Slow output
The "slow output" referred to here is a cycle of output pause, output pause...
The Sleep function in windows.h is used here, and the parameter is the number of milliseconds of the pause.
Implementation:

#include <windows.h>
#include <iostream>
using namespace std;
......
char ch[] = "大家好";
for (int i = 0; ch[i] != '\0'; ++i) {
    
    
	cout << ch[i]; Sleep(1000);
}

tips:
1. Capitalize the first letter S of Sleep.
2.1 seconds=1000 milliseconds
4. Output at the specified position
Here, the SetConsoleCursorPosition function in windows.h is called, and the two parameters are the handle and position respectively. It is recommended to use the COORD structure for the location (in fact, it doesn't matter that it is a magical stuff, just call it directly).
Implementation:

void gotoxy(int x, int y)  {
    
    
	COORD pos = {
    
    y - 1, x - 1};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}

There will be more optimizations in the future. Let's talk about so much for today's beautification page!
If you want to make better games, you need the basis of algorithms! If you encounter problems, please send me a private message, and continue to look forward to Chapter 2! ! ! If you think it’s good, it’s better to leave after one-click three consecutive times, thank you lovely fans~

Guess you like

Origin blog.csdn.net/yueyuedog/article/details/112976948