1、猜数字
#include <iostream> //猜数字
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int num, guess, tries = 0;
// 生成一个 1~100 的随机数
srand(time(NULL));
num = rand() % 100 + 1;
cout << "猜数字游戏\n";
do
{
cout << "请输入一个 1~100 的整数: ";
cin >> guess;
tries++;
if (guess > num)
{
cout << "太大了!\n";
}
else if (guess < num)
{
cout << "太小了!\n";
}
else
{
cout << "恭喜你,猜对了!你用了 " << tries << " 次猜对了!\n";
}
} while (guess != num);
return 0;
}
2、吃豆人
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int n = 809;
struct Point {
int x, y;
};
int dali;
int fx[4] = {-1, 27, 1, -27};
int fxfx[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
int dis[1000][1000]; //0:墙 1:有分的路 2:没分的路 3:怪物的家
int changdi[30][27] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0},
{0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0},
{0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0},
{0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1 ,0},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 3, 3, 3, 3, 3, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 0, 3, 3, 3, 3, 3, 0, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 3, 3, 3, 3, 3, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0},
{0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0},
{0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0},
{0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
int x, x1, x2, x3, x4, y, y1, y2, y3, y4;
int now, now1, now2, now3, now4;
int g1, g2, g3, g4;
int fangx, nextfx, last1, last2, last3, last4;
int fenshu, guozi, guaitimer;
int T1, T2, t1, t2, stopped; //T:计时 t1:玩家速度 t2:怪物速度
int f; //f:{0:继续 1:被吃 2:赢了 3:输了}
int beichi;
void color (int a) {//颜色函数
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE), a);
}
void gotoxy(int x, int y) {//位置函数(行为x 列为y)
COORD pos;
pos.X=2*y;
pos.Y=x;
SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE),pos);
}
void begin () {
system ("cls");
color (11);
printf (" ★");
color (10);
printf ("吃豆人");
color (11);
printf ("★\n\n");
color(7);
printf (" 正在初始化,请耐心等待");
for (int i = 0; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dis[i][j] = 900;
}
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= 3; j++) {
if (i + fx[j] >= 0 && i + fx[j] <= n) {
int k = i + fx[j], xx = k/27, yy = k % 27, kk;
if (changdi[i / 27][i % 27] && changdi[xx][yy]) {
dis[i][k] = kk = 1;
}
}
}
}
for (int k = 0; k <= n; k++) {
if (changdi[k]) {
for (int i = 0; i <= n; i++) {
if (changdi[i]) {
for (int j = 0; j <= n; j++) {
if (changdi[j]) {
if (dis[i][j] > dis[i][k] + dis[k][j]) {
dis[i][j] = dis[i][k] + dis[k][j];
}
}
}
}
}
if (k % 80 == 0) {
color (13);
gotoxy (3, 12);
printf("│");
} else if (k % 80 == 20) {
color (13);
gotoxy (3, 12);
printf ("╱");
} else if (k % 80 == 40) {
color (13);
gotoxy (3, 12);
printf ("─");
} else if (k % 80 == 60) {
color(13);
gotoxy (3, 12);
printf ("╲");
}
if (k % 60==0) {
color (11);
gotoxy (5, k / 60);
printf("●");
}
}
}
}
void shuru () {
char ch = getch ();
if (ch == 75) {
if (changdi[x + fxfx[0][0]][y + fxfx[0][1]] == 1 | changdi[x + fxfx[0][0]][y + fxfx[0][1]] == 2) {
fangx = nextfx = 0;
} else {
nextfx = 0;
}
} else if (ch == 80) {
if (changdi[x + fxfx[1][0]][y + fxfx[1][1]] == 1 | changdi[x + fxfx[1][0]][y + fxfx[1][1]] == 2) {
fangx = nextfx = 1;
} else {
nextfx = 1;
}
} else if (ch == 77) {
if (changdi[x + fxfx[2][0]][y + fxfx[2][1]] == 1 | changdi[x + fxfx[2][0]][y + fxfx[2][1]] == 2) {
fangx = nextfx = 2;
} else {
nextfx = 2;
}
} else if (ch == 72) {
if (changdi[x + fxfx[3][0]][y + fxfx[3][1]] == 1 | changdi[x + fxfx[3][0]][y + fxfx[3][1]] == 2) {
fangx = nextfx = 3;
} else {
nextfx = 3;
}
} else if (ch == ' ') {
stopped = (stopped + 1) % 2;
} else if (ch == '-') {
t1++;
} else if ((ch == '+') && t1 - 1 > 0) {
t1--;
}
else if (ch == '$') {
dali = (dali + 1) % 2;
}
}
void reset () {
system ("cls");
color (7);
x = 22;
y = 13;
x1 = x2 = x3 = x4 = 14;
y1 = 11;
y2 = 12;
y3 = 14;
y4 = 15;
now = 607;
now1 = 389;
now2 = 390;
now3 = 392;
now4 = 393;
for (int k = 0; k <= n; k++) {
int i = k / 27, j = k % 27;
gotoxy (i, j);
if (changdi[i][j] == 1) {
color (7);
printf("·");
} else if (!changdi[i][j]) {
color (1);
printf ("■");
}
if (j==26) {
gotoxy (i, 27);
color (7);
printf ("%d", i);
}
}
gotoxy (0, 0);
gotoxy (x, y);
color (14);
printf ("●");
gotoxy (x1, y1);
color (4);
printf ("◆");
gotoxy (x2, y2);
color (5);
printf ("◆");
gotoxy (x3, y3);
color (3);
printf ("◆");
gotoxy (x4, y4);
color (2);
printf ("◆");
fangx = 0;
T1 = T2 = guaitimer = 0;
t1 = 75;
t2 = 100;
stopped = 0;
fenshu = 0;
guozi = 237;
g1 = g2 = g3 = g4 = 0;
dali = 0;
gotoxy (14, 30);
printf (" ");
}
void move1 () {
int xx, yy;
xx = x + fxfx[nextfx][0];
yy = y + fxfx[nextfx][1];
if (changdi[xx][yy]) {
if (changdi[xx][yy] == 1) {
fenshu++;
changdi[xx][yy] = 2;
}
color (14);
gotoxy (x, y);
printf (" ");
gotoxy (xx, yy);
if (!dali) {
printf ("♀");
} else {
printf ("☆");
}
now = x * 27 + y;
x = xx;
y = yy;
fangx = nextfx;
} else {
if (x == 13 && y == 0 && fangx == 0) {
xx = x;
yy = 26;
} else if (x == 13 && y == 26 && fangx == 2) {
xx = x;
yy = 0;
} else {
xx = x + fxfx[fangx][0];
yy = y + fxfx[fangx][1];
}
if (changdi[xx][yy]) {
if (changdi[xx][yy] == 1) {
fenshu++;
changdi[xx][yy] = 2;
}
color (14);
gotoxy (x, y);
printf (" ");
gotoxy (xx, yy);
if (!dali) {
printf ("♀");
} else {
printf ("☆");
}
now = x * 27 + y;
x = xx;
y = yy;
}
}
color (7);
}
void move2 () {
int haha, minhaha, xx, yy, chi = 0;
if (g1) {
minhaha = 2147483647; //相当于INT_MAX
if (now1 % 27 == 0 | now1 % 27 == 26) {
haha = last1;
} else if (!dali) {
for (int i = 0; i <= 3; i++) {
if (changdi[(now1 + fx[i]) / 27][(now1 + fx[i]) % 27] && i != last1 &&
minhaha > dis[now1 + fx[i]][now]) {
minhaha = dis[now1 + fx[i]][now];
haha = i;
}
}
} else {
minhaha = -minhaha;
for (int i = 0; i <= 3; i++) {
if (changdi[(now1 + fx[i]) / 27][(now1 + fx[i]) % 27] && i != last1 &&
minhaha < dis[now1 + fx[i]][now]) {
minhaha = dis[now1 + fx[i]][now];
haha = i;
}
}
}
xx = now1 / 27;
yy = now1 % 27;
gotoxy (xx, yy);
if (changdi[xx][yy] == 1) {
printf ("·");
} else {
printf (" ");
}
now1 += fx[haha];
last1 = (haha + 2) % 4;
xx = now1 / 27;
yy = now1 % 27;
gotoxy (xx, yy);
color (4);
printf ("◆");
color (7);
if (xx == x && yy == y) {
if (!dali) {
chi++;
} else {
guozi += 50;
fenshu += 50;
last1 = 0;
gotoxy (now1 / 27, now1 % 27);
if (changdi[now1 / 27][now1 % 27] == 1) {
printf ("·");
} else {
printf (" ");
}
now1 = 389;
}
}
}
if (g2) {
int k;
minhaha = 2147483647;
if (fangx == 0 | fangx == 2) {
k = y + (fxfx[fangx][1]) * 3;
while (k > 25 | !changdi[x][k]) {
k--;
}
while (k < 1 | !changdi[x][k]) {
k++;
}
} else {
k = x + (fxfx[fangx][0]) * 3;
while (k > 28 | !changdi[k][y]) {
k--;
}
while (k < 1 | !changdi[k][y]) {
k++;
}
}
if (fangx == 0 | fangx == 2) {
k += x * 27;
} else {
k = k * 27 + y;
}
if (now2 % 27 == 0 | now2 % 27 == 26) {
haha = last2;
}
else if (!dali) {
for (int i = 0; i <= 3; i++) {
if (changdi[(now2 + fx[i]) / 27][(now2 + fx[i]) % 27] && i != last2 &&
minhaha > dis[now2 + fx[i]][k]) {
minhaha = dis[now2 + fx[i]][k];
haha = i;
}
}
} else {
minhaha = -minhaha;
for (int i = 0; i <= 3; i++) {
if (changdi[(now2 + fx[i]) / 27][(now2 + fx[i]) % 27] && i != last2 &&
minhaha < dis[now2 + fx[i]][k]) {
minhaha = dis[now2 + fx[i]][k];
haha = i;
}
}
}
xx = now2 / 27;
yy = now2 % 27;
gotoxy (xx, yy);
if (changdi[xx][yy] == 1) {
printf ("·");
} else {
printf (" ");
}
now2 += fx[haha];
last2 = (haha + 2) % 4;
gotoxy (18, 30);
xx = now2 / 27;
yy = now2 % 27;
gotoxy (xx, yy);
color (5);
printf ("◆");
color (7);
if (xx == x && yy == y) {
if (!dali) {
chi++;
} else {
guozi += 50;
fenshu += 50;
last2 = 0;
gotoxy (now2 / 27, now2 % 27);
if (changdi[now2 / 27][now2 % 27] == 1) {
printf ("·");
} else {
printf (" ");
}
now2 = 390;
}
}
}
if (g3) {
int k;
minhaha = 2147483647;
if (fangx == 0 | fangx == 2) {
k = y + (fxfx[(fangx + 1) % 4][1]) * 3;
while (k > 25 | !changdi[x][k]) {
k--;
}
while (k < 1 | !changdi[x][k]) {
k++;
}
} else {
k = x + (fxfx[(fangx + 1) % 4][0]) * 3;
while (k > 28 | !changdi[k][y]) {
k--;
}
while (k < 1 | !changdi[k][y]) {
k++;
}
}
if (fangx == 0 | fangx == 2) {
k += x * 27;
} else {
k = k * 27 + y;
}
if (now3 % 27 == 0 | now3 % 27 == 26) {
haha = last3;
} else if (!dali) {
for (int i = 0; i <= 3; i++) {
if (changdi[(now3 + fx[i]) / 27][(now3 + fx[i]) % 27] && i != last3 &&
minhaha > dis[now3 + fx[i]][k]) {
minhaha = dis[now3 + fx[i]][k];
haha = i;
}
}
} else {
minhaha = -minhaha;
for (int i = 0; i <= 3; i++) {
if (changdi[(now3 + fx[i]) / 27][(now3 + fx[i]) % 27] && i != last3 &&
minhaha < dis[now3 + fx[i]][k]) {
minhaha = dis[now3 + fx[i]][k];
haha = i;
}
}
}
xx = now3 / 27;
yy = now3 % 27;
gotoxy (xx, yy);
if (changdi[xx][yy] == 1) {
printf ("·");
} else {
printf (" ");
}
now3 += fx[haha];
last3 = (haha + 2) % 4;
gotoxy (18, 30);
xx = now3 / 27;
yy = now3 % 27;
gotoxy (xx, yy);
color (3);
printf ("◆");
color (7);
if (xx == x && yy == y) {
if (!dali) {
chi++;
} else {
guozi += 50;
fenshu += 50;
last3 = 0;
gotoxy (now3 / 27, now3 % 27);
if (changdi[now3 / 27][now3 % 27] == 1) {
printf ("·");
} else {
printf (" ");
}
now3 = 341;
}
}
}
if (chi) {
beichi++;
}
}
int main () {
MessageBox (NULL, "欢迎来到吃豆人游戏!", "温馨提示", MB_OK);
begin ();
int jixu = 1;
reset ();
string bb[4] = {"●", "①", "②" ,"③"};
color (7);
gotoxy (12, 12);
printf ("倒计时");
color (12);
for (int i = 3; i >= 0; i--) {
if (i==0) {
color (11);
}
gotoxy (13, 13);
cout << bb[i];
Sleep (1000);
}
gotoxy (12, 12);
printf (" ");
gotoxy (13, 13);
printf (" ");
while (!f) {
Sleep (1);
gotoxy (7, 30);
color (3);
printf ("得分:%d ", fenshu);
gotoxy (3, 30);
printf ("怪物速度:%d ", 300 - t2);
gotoxy (5, 30);
printf ("你的速度:%d ", 300 - t1);
gotoxy (9, 30);
printf ("被吃次数:%d ", beichi);
gotoxy (11, 30);
printf ("控制小人行走:方向键");
gotoxy (13, 30);
printf ("复活次数:无限次");
gotoxy (15, 30);
printf ("小人加速/减速:'+'/'-'");
gotoxy (17, 30);
printf ("大力丸(怪物们不会主动吃您):$键");
gotoxy (20, 10);
color (4);
if (kbhit ()) {
shuru ();
}
if (stopped) {
continue;
}
T1 = (T1 + 1) % t1;
T2 = (T2 + 1) % t2;
if (T1 % t1 == 0 && now + fx[fangx] > 0 && now + fx[fangx] < n) {
move1 ();
}
if (T2 % t2 == 0) {
if (guaitimer <= 8) {
if (guaitimer==0) {
g1 = 1;
}
if (guaitimer == 8) {
g2 = 1;
}
guaitimer++;
}
if (!g3 && fenshu >= 30) {
g3 = 1;
}
move2 ();
}
if (fenshu == guozi) {
f=2;
}
}
if (f == 2) {
Sleep (3000);
system ("cls");
gotoxy (10, 20);
color (3);
string str = "恭喜您吃完了所有豆子!";
for (int i = 0; i < str.size (); i++) {
Sleep (80);
cout << str[i];
}
Sleep (2000);
gotoxy (12, 20);
str = "您一共被怪物吃掉了";
for (int i = 0; i < str.size (); i++) {
Sleep (80);
cout << str[i];
}
Sleep (80);
cout << beichi;
Sleep (80);
cout << "次";
Sleep (80);
cout << "!";
gotoxy (14, 20);
Sleep (2000);
}
}
3、打方块
#include<bits/stdc++.h>//打方块 Windows10
#include<windows.h>
using namespace std;
int fen,mb[10][18],leaf;
void kg(int a) {
for(int i=0; i<a; i++)
cout<<' ';
}
void go(int x, int y) {
COORD p;
p.X=(x-1)*2;
p.Y=y-1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),p);
}
void printtu(int x,int y,bool a) {
go(x,y);
cout<<"■";
if(x>2&&x<11) {
go(x-1,y+1);
cout<<"■■■";
} else if(x==2) {
go(x,y+1);
cout<<"■■";
} else if(x==11) {
go(x-1,y+1);
cout<<"■■";
} else;
if(a)
for(int i=0; i<18; i++) {
go(2,i+2);
kg(20);
}
Sleep(100);
go(x,y);
kg(2);
if(x>2&&x<11) {
go(x-1,y+1);
kg(6);
} else if(x==2) {
go(x,y+1);
kg(4);
} else if(x==11) {
go(x-1,y+1);
kg(4);
} else;
go(14,5);
kg(4);
cout<<"\b\b\b\b"<<fen;
if(a)
for(int i=0; i<18; i++) {
go(2,i+2);
for(int o=0; o<10; o++) {
if(mb[o][i])
cout<<"■";
else kg(2);
}
}
}
void sj(int x) {
int i;
for(i=19;; i--) {
go(x,i);
cout<<"■";
Sleep(10);
cout<<"\b\b";
kg(2);
if(i<3)break;
if(mb[x-2][i-3]==1)break;
}
mb[x-2][i-2]=1;
go(x,i);
cout<<"■";
fen-=10;
for(int o=0; o<10; o++)
if(mb[o][i-2]==0)return;
for(int o=0; o<10; o++)
mb[o][i-2]=0;
for(int o=i-2; o<17; o++)
for(int j=0; j<10; j++)
mb[j][o]=mb[j][o+1];
for(int o=0; o<10; o++)
mb[o][17]=0;
printtu(x,20,1);
fen+=100;
}
void mouse(int &x,int &y) {
POINT p;
HWND h=GetForegroundWindow();
GetCursorPos(&p);
ScreenToClient(h,&p);
x=p.x;
y=p.y;
}
void m(int wt) {
lkr:
;
fen=-500;
leaf=8;
srand(time(0));
system("mode con cols=33 lines=24");
system("cls");
cout<<"┌ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┐"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆ 分数"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆ 生命"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆ ";
printf("%c %c %c %c\n",3,3,3,3);
cout<<"┆ ";
kg(20);
cout<<"┆ ";
printf("%c %c %c %c\n",3,3,3,3);
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"┆ ";
kg(20);
cout<<"┆"<<endl;
cout<<"└ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┄ ┘"<<endl;
lk:
;
for(int i=0; i<10; i++)
for(int o=0; o<18; o++)
mb[i][o]=0;
int x=6;
for(int i=wt*10;;) {
if(i<wt*10)goto asd;
for(int i=0; i<10; i++)
for(int o=0; o<18; o++)
if(mb[i][o]!=0)goto qwe;
fen+=500;
qwe:
;
for(int o=0; o<10; o++)
if(mb[o][17]==1)goto as;
for(int o=17; o>0; o--)
for(int j=0; j<10; j++)
mb[j][o]=mb[j][o-1];
for(int o=0; o<10; o++)
mb[o][0]=rand()%2;
asd:
;
if(GetAsyncKeyState(VK_RIGHT)!=0&&x<11)x++;
if(GetAsyncKeyState(VK_LEFT)!=0&&x>2)x--;
if(GetAsyncKeyState(VK_UP)!=0)sj(x);
printtu(x,20,i>=wt*10);
if(i<wt*10)i++;
else i=1;
}
as:
;
for(int i=2; i<22; i++) {
go(2,i);
kg(20);
}
fen-=600;
switch(leaf) {
case 1:
go(13,8);
cout<<' '<<' ';
break;
case 2:
leaf--;
go(14,8);
cout<<' '<<' ';
goto lk;
case 3:
leaf--;
go(15,8);
cout<<' '<<' ';
goto lk;
case 4:
leaf--;
go(16,8);
cout<<' '<<' ';
goto lk;
case 5:
leaf--;
go(13,9);
cout<<' '<<' ';
goto lk;
case 6:
leaf--;
go(14,9);
cout<<' '<<' ';
goto lk;
case 7:
leaf--;
go(15,9);
cout<<' '<<' ';
goto lk;
case 8:
leaf--;
go(16,9);
cout<<' '<<' ';
goto lk;
}
go(5,7);
cout<<"你输了!";
go(3,8);
cout<<" ┆ 再来[R]┆ ┆ 返回[E]┆";
for(;;) {
if(GetAsyncKeyState('R')!=0||GetAsyncKeyState('r')!=0)goto lkr;
if(GetAsyncKeyState('E')!=0||GetAsyncKeyState('e')!=0)return;
}
}
void dafangkuai() {}
int main() {
SetConsoleTitle("打方块Windows10");
int q=3;
a:
;
system("mode con cols=80 lines=25");
system("cls");
bool jh[8][27]= {0,0,1,0,0,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1,0,0,1,0,0,1,1,1,1,1,1,1,0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,1,1,1,1,0,0,0,1,1,1,0,0,1,0,0,0,0,1,1,1,1,0,1,1,1,0,0,0,1,0,1,0,1,1,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,1,0,1,1,1,1,1,1,1,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,1,1,0,0,0,1,0,0,0,0,1,1,0,0,0,1,1,0,0,0,1,0,0,1,1,0,0,1,0,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0};
for(int i=2; i<10; i++) {
go(7,i);
for(int o=0; o<27; o++) {
if(jh[i-2][o])
cout<<"■";
else cout<<' '<<' ';
}
}
go(17,11);
cout<<"|开始游戏|";
go(17,13);
cout<<"|设置游戏|";
go(17,15);
cout<<"|游戏规则|";
go(17,17);
cout<<"|退出游戏|";
go(1,23);
cout<<"[L]确定";
int y=1;
for(;;) {
if(GetAsyncKeyState(VK_DOWN)!=0)y+=((y==4)?-3:1);
if(GetAsyncKeyState(VK_UP)!=0)y-=((y==1)?-3:1);
if(GetAsyncKeyState('l')!=0||GetAsyncKeyState('L')!=0)
switch(y) {
case 1:
system("cls");
m(q);
goto a;
case 2:
system("cls");
go(16,11);
cout<<' '<<q<<"秒增加一行";
go(16,10);
printf("[%c]",30);
go(16,12);
printf("[%c]",31);
go(1,23);
cout<<"[K]确定";
for(;;) {
if(GetAsyncKeyState(VK_UP)!=0&&q<9) {
q++;
go(16,11);
cout<<' '<<q;
}
if(GetAsyncKeyState(VK_DOWN)!=0&&q>1) {
q--;
go(16,11);
cout<<' '<<q;
}
if(GetAsyncKeyState('k')!=0||GetAsyncKeyState('K')!=0)goto a;
Sleep(100);
}
case 3:
MessageBox(0,"点确定浏览规则" ,"规则",MB_OK);
MessageBox(0,"←→控制炮台","规则",MB_OK);
MessageBox(0,"满一行即消除","规则",MB_OK);
MessageBox(0,"每消除一行+100","规则",MB_OK);
MessageBox(0,"少一条命-100","规则",MB_OK);
MessageBox(0,"全部消除+500","规则",MB_OK);
MessageBox(0,"发射一炮-10","规则",MB_OK);
case 4:
return 0;
}
go(16,11);
cout<<' '<<' ';
go(22,11);
cout<<' ';
go(16,13);
cout<<' '<<' ';
go(22,13);
cout<<' ';
go(16,15);
cout<<' '<<' ';
go(22,15);
cout<<' ';
go(16,17);
cout<<' '<<' ';
go(22,17);
cout<<' ';
go(16,9+2*y);
cout<<' '<<'>';
go(22,9+2*y);
cout<<'<';
Sleep(100);
}
}
4、打飞机
#include<iostream>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<string>
using namespace std;
/*=============== all the structures ===============*/
typedef struct Frame
{
COORD position[2];
int flag;
}Frame;
/*=============== all the functions ===============*/
void SetPos(COORD a)// set cursor
{
HANDLE out=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(out, a);
}
void SetPos(int i, int j)// set cursor
{
COORD pos={i, j};
SetPos(pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
//把第y行,[x1, x2) 之间的坐标填充为 ch
void drawRow(int y, int x1, int x2, char ch)
{
SetPos(x1,y);
for(int i = 0; i <= (x2-x1); i++)
cout<<ch;
}
//在a, b 纵坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
void drawRow(COORD a, COORD b, char ch)
{
if(a.Y == b.Y)
drawRow(a.Y, a.X, b.X, ch);
else
{
SetPos(0, 25);
cout<<"error code 01:无法填充行,因为两个坐标的纵坐标(x)不相等";
system("pause");
}
}
//把第x列,[y1, y2] 之间的坐标填充为 ch
void drawCol(int x, int y1, int y2, char ch)
{
int y=y1;
while(y!=y2+1)
{
SetPos(x, y);
cout<<ch;
y++;
}
}
//在a, b 横坐标相同的前提下,把坐标 [a, b] 之间填充为 ch
void drawCol(COORD a, COORD b, char ch)
{
if(a.X == b.X)
drawCol(a.X, a.Y, b.Y, ch);
else
{
SetPos(0, 25);
cout<<"error code 02:无法填充列,因为两个坐标的横坐标(y)不相等";
system("pause");
}
}
//左上角坐标、右下角坐标、用row填充行、用col填充列
void drawFrame(COORD a, COORD b, char row, char col)
{
drawRow(a.Y, a.X+1, b.X-1, row);
drawRow(b.Y, a.X+1, b.X-1, row);
drawCol(a.X, a.Y+1, b.Y-1, col);
drawCol(b.X, a.Y+1, b.Y-1, col);
}
void drawFrame(int x1, int y1, int x2, int y2, char row, char col)
{
COORD a={x1, y1};
COORD b={x2, y2};
drawFrame(a, b, row, col);
}
void drawFrame(Frame frame, char row, char col)
{
COORD a = frame.position[0];
COORD b = frame.position[1];
drawFrame(a, b, row, col);
}
void drawPlaying()
{
drawFrame(0, 0, 48, 24, '=', '|');// draw map frame;
drawFrame(49, 0, 79, 4, '-', '|');// draw output frame
drawFrame(49, 4, 79, 9, '-', '|');// draw score frame
drawFrame(49, 9, 79, 20, '-', '|');// draw operate frame
drawFrame(49, 20, 79, 24, '-', '|');// draw other message frame
SetPos(52, 6);
cout<<"得分:";
SetPos(52, 7);
cout<<"称号:";
SetPos(52,10);
cout<<"操作方式:";
SetPos(52,12);
cout<<" a,s,d,w 控制战机移动。";
SetPos(52,14);
cout<<" p 暂停游戏。";
SetPos(52,16);
cout<<" e 退出游戏。";
}
//在[a, b)之间产生一个随机整数
int random(int a, int b)
{
int c=(rand() % (a-b))+ a;
return c;
}
//在两个坐标包括的矩形框内随机产生一个坐标
COORD random(COORD a, COORD b)
{
int x=random(a.X, b.X);
int y=random(a.Y, b.Y);
COORD c={x, y};
return c;
}
bool judgeCoordInFrame(Frame frame, COORD spot)
{
if(spot.X>=frame.position[0].X)
if(spot.X<=frame.position[1].X)
if(spot.Y>=frame.position[0].Y)
if(spot.Y<=frame.position[0].Y)
return true;
return false;
}
void printCoord(COORD a)
{
cout <<"( "<<a.X<<" , "<<a.Y<<" )";
}
void printFrameCoord(Frame a)
{
printCoord(a.position[0]);
cout <<" - ";
printCoord(a.position[1]);
}
int drawMenu()
{
SetPos(30, 1);
cout<<"P l a n e W a r";
drawRow(3, 0, 79, '-');
drawRow(5, 0, 79, '-');
SetPos(28, 4);
cout<<"w 和 s 选择, k 确定";
SetPos(15, 11);
cout<<"1. 简单的敌人";
SetPos(15, 13);
cout<<"2. 冷酷的敌人";
drawRow(20, 0, 79, '-');
drawRow(22, 0, 79, '-');
SetPos(47, 11);
cout<<"简单的敌人:";
SetPos(51, 13);
cout<<"简单敌人有着较慢的移动速度。";
SetPos(24, 21);
cout<<"制作:<bits/stdc++.h>";
int j=11;
SetPos(12, j);
cout<<">>";
//drawFrame(45, 9, 79, 17, '=', '|');
while(1)
{ if( _kbhit() )
{
char x=_getch();
switch (x)
{
case 'w' :
{
if( j == 13)
{
SetPos(12, j);
cout<<" ";
j = 11;
SetPos(12, j);
cout<<">>";
SetPos(51, 13);
cout<<" ";
SetPos(47, 11);
cout<<"简单的敌人:";
SetPos(51, 13);
cout<<"简单敌人有较慢的移动速度,比较容易对付";
}
break;
}
case 's' :
{
if( j == 11 )
{
SetPos(12, j);
cout<<" ";
j = 13;
SetPos(12, j);
cout<<">>";
SetPos(51, 13);
cout<<" ";
SetPos(47, 11);
cout<<"冷酷的敌人:";
SetPos(51, 13);
cout<<"冷酷的敌人移动速度较快,不是很好对付哟。";
}
break;
}
case 'k' :
{
if (j == 8) return 1;
else return 2;
}
}
}
}
}
/*
DWORD WINAPI MusicFun(LPVOID lpParamte)
{
//DWORD OBJ;
sndPlaySound(TEXT("bgm.wav"), SND_FILENAME|SND_ASYNC);
return 0;
}
*/
/*================== the Game Class ==================*/
class Game
{
public:
COORD position[10];
COORD bullet[10];
Frame enemy[8];
int score;
int rank;
int rankf;
string title;
int flag_rank;
Game ();
//初始化所有
void initPlane();
void initBullet();
void initEnemy();
//初始化其中一个
//void initThisBullet( COORD );
//void initThisEnemy( Frame );
void planeMove(char);
void bulletMove();
void enemyMove();
//填充所有
void drawPlane();
void drawPlaneToNull();
void drawBullet();
void drawBulletToNull();
void drawEnemy();
void drawEnemyToNull();
//填充其中一个
void drawThisBulletToNull( COORD );
void drawThisEnemyToNull( Frame );
void Pause();
void Playing();
void judgePlane();
void judgeEnemy();
void Shoot();
void GameOver();
void printScore();
};
Game::Game()
{
initPlane();
initBullet();
initEnemy();
score = 0;
rank = 25;
rankf = 0;
flag_rank = 0;
}
void Game::initPlane()
{
COORD centren={39, 22};
position[0].X=position[5].X=position[7].X=position[9].X=centren.X;
position[1].X=centren.X-2;
position[2].X=position[6].X=centren.X-1;
position[3].X=position[8].X=centren.X+1;
position[4].X=centren.X+2;
for(int i=0; i<=4; i++)
position[i].Y=centren.Y;
for(int i=6; i<=8; i++)
position[i].Y=centren.Y+1;
position[5].Y=centren.Y-1;
position[9].Y=centren.Y-2;
}
void Game::drawPlane()
{
for(int i=0; i<9; i++)
{
SetPos(position[i]);
if(i!=5)
cout<<"O";
else if(i==5)
cout<<"|";
}
}
void Game::drawPlaneToNull()
{
for(int i=0; i<9; i++)
{
SetPos(position[i]);
cout<<" ";
}
}
void Game::initBullet()
{
for(int i=0; i<10; i++)
bullet[i].Y = 30;
}
void Game::drawBullet()
{
for(int i=0; i<10; i++)
{
if( bullet[i].Y != 30)
{
SetPos(bullet[i]);
cout<<"^";
}
}
}
void Game::drawBulletToNull()
{
for(int i=0; i<10; i++)
if( bullet[i].Y != 30 )
{
COORD pos={bullet[i].X, bullet[i].Y+1};
SetPos(pos);
cout<<" ";
}
}
void Game::initEnemy()
{
COORD a={1, 1};
COORD b={45, 15};
for(int i=0; i<8; i++)
{
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
}
}
void Game::drawEnemy()
{
for(int i=0; i<8; i++)
drawFrame(enemy[i].position[0], enemy[i].position[1], '-', '|');
}
void Game::drawEnemyToNull()
{
for(int i=0; i<8; i++)
{
drawFrame(enemy[i].position[0], enemy[i].position[1], ' ', ' ');
}
}
void Game::Pause()
{
SetPos(61,2);
cout<<" ";
SetPos(61,2);
cout<<"暂停中...";
char c=_getch();
while(c!='p')
c=_getch();
SetPos(61,2);
cout<<" ";
}
void Game::planeMove(char x)
{
if(x == 'a')
if(position[1].X != 1)
for(int i=0; i<=9; i++)
position[i].X -= 2;
if(x == 's')
if(position[7].Y != 23)
for(int i=0; i<=9; i++)
position[i].Y += 1;
if(x == 'd')
if(position[4].X != 47)
for(int i=0; i<=9; i++)
position[i].X += 2;
if(x == 'w')
if(position[5].Y != 3)
for(int i=0; i<=9; i++)
position[i].Y -= 1;
}
void Game::bulletMove()
{
for(int i=0; i<10; i++)
{
if( bullet[i].Y != 30)
{
bullet[i].Y -= 1;
if( bullet[i].Y == 1 )
{
COORD pos={bullet[i].X, bullet[i].Y+1};
drawThisBulletToNull( pos );
bullet[i].Y=30;
}
}
}
}
void Game::enemyMove()
{
for(int i=0; i<8; i++)
{
for(int j=0; j<2; j++)
enemy[i].position[j].Y++;
if(24 == enemy[i].position[1].Y)
{
COORD a={1, 1};
COORD b={45, 3};
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
}
}
}
void Game::judgePlane()
{
for(int i = 0; i < 8; i++)
for(int j=0; j<9; j++)
if(judgeCoordInFrame(enemy[i], position[j]))
{
SetPos(62, 1);
cout<<"坠毁";
drawFrame(enemy[i], '+', '+');
Sleep(1000);
GameOver();
break;
}
}
void Game::drawThisBulletToNull( COORD c)
{
SetPos(c);
cout<<" ";
}
void Game::drawThisEnemyToNull( Frame f )
{
drawFrame(f, ' ', ' ');
}
void Game::judgeEnemy()
{
for(int i = 0; i < 8; i++)
for(int j = 0; j < 10; j++)
if( judgeCoordInFrame(enemy[i], bullet[j]) )
{
score += 5;
drawThisEnemyToNull( enemy[i] );
COORD a={1, 1};
COORD b={45, 3};
enemy[i].position[0] = random(a, b);
enemy[i].position[1].X = enemy[i].position[0].X + 3;
enemy[i].position[1].Y = enemy[i].position[0].Y + 2;
drawThisBulletToNull( bullet[j] );
bullet[j].Y = 30;
}
}
void Game::Shoot()
{
for(int i=0; i<10; i++)
if(bullet[i].Y == 30)
{
bullet[i].X = position[5].X;
bullet[i].Y = position[5].Y-1;
break;
}
}
void Game::printScore()
{
if(score == 120 && flag_rank == 0)
{
rank -= 3;
flag_rank = 1;
}
else if( score == 360 && flag_rank == 1)
{
rank -= 5;
flag_rank = 2;
}
else if( score == 480 && flag_rank == 2)
{
rank -= 5;
flag_rank = 3;
}
int x=rank/5;
SetPos(60, 6);
cout<<score;
if( rank!=rankf )
{
SetPos(60, 7);
if( x == 5)
title="初级飞行员";
else if( x == 4)
title="中级飞行员";
else if( x == 3)
title="高级飞行员";
else if( x == 2 )
title="王牌飞行员";
cout<<title;
}
rankf = rank;
}
void Game::Playing()
{
//HANDLE MFUN;
//MFUN= CreateThread(NULL, 0, MusicFun, NULL, 0, NULL);
drawEnemy();
drawPlane();
int flag_bullet = 0;
int flag_enemy = 0;
while(1)
{
Sleep(8);
if(_kbhit())
{
char x = _getch();
if ('a' == x || 's' == x || 'd' == x || 'w' == x)
{
drawPlaneToNull();
planeMove(x);
drawPlane();
judgePlane();
}
else if ('p' == x)
Pause();
else if( 'k' == x)
Shoot();
else if( 'e' == x)
{
//CloseHandle(MFUN);
GameOver();
break;
}
}
/* 处理子弹 */
if( 0 == flag_bullet )
{
bulletMove();
drawBulletToNull();
drawBullet();
judgeEnemy();
}
flag_bullet++;
if( 5 == flag_bullet )
flag_bullet = 0;
/* 处理敌人 */
if( 0 == flag_enemy )
{
drawEnemyToNull();
enemyMove();
drawEnemy();
judgePlane();
}
flag_enemy++;
if( flag_enemy >= rank )
flag_enemy = 0;
/* 输出得分 */
printScore();
}
}
void Game::GameOver()
{
system("cls");
COORD p1={28,9};
COORD p2={53,15};
drawFrame(p1, p2, '=', '|');
SetPos(36,12);
string str="Game Over!";
for(int i=0; i<str.size(); i++)
{
Sleep(80);
cout<<str[i];
}
Sleep(1000);
system("cls");
drawFrame(p1, p2, '=', '|');
SetPos(31, 11);
cout<<"击落敌机:"<<score/5<<" 架";
SetPos(31, 12);
cout<<"得 分:"<<score;
SetPos(31, 13);
cout<<"获得称号:"<<title;
SetPos(30, 16);
Sleep(1000);
cout<<"继续? 是(y)| 否(n)制作:<bits/stdc++.h>";
as:
char x=_getch();
if (x == 'n')
exit(0);
else if (x == 'y')
{
system("cls");
Game game;
int a = drawMenu();
if(a == 2)
game.rank = 20;
system("cls");
drawPlaying();
game.Playing();
}
else goto as;
}
/*================== the main function ==================*/
int main()
{
//游戏准备
srand((int)time(0)); //随机种子
HideCursor(); //隐藏光标
Game game;
int a = drawMenu();
if(a == 2)
game.rank = 20;
system("cls");
drawPlaying();
game.Playing();
}
5、打怪
#include <iostream>
using namespace std;
double shengmingli=2000;//定义主角初始生命力
int gongjili=150;//定义主角初始攻击力
int fangyuli=200;//定义主角初始防御力
int money=20;//定义主角初始金钱数量
bool guoguan;//定义是否通关判定
void wuqidian();//定义武器店函数
void yaodian();//定义药店函数
void guaiwu1();//定义小怪物函数
void guaiwu2();//定义大怪物函数
int main()
{
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
int xiaozhen;//定义选择项目
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
cin>>xiaozhen;
while(xiaozhen!=5)//输入5时退出游戏
{
if(shengmingli<=0)//主角生命力小于等于0时游戏结束
{
cout<<"你死啦!"<<endl;
break;
}
if(guoguan)
{
cout<<"恭喜通关!"<<endl;
break;
}
if(xiaozhen==6)//输入6可检测自己的状态
{
cout<<"你的生命力:"<<shengmingli<<endl;
cout<<"你的攻击力:"<<gongjili<<endl;
cout<<"你的防御力:"<<fangyuli<<endl;
cout<<"你拥有的钱:"<<money<<endl;
}
else
switch(xiaozhen)
{
case 1 : wuqidian();break;
case 2 : yaodian();break;
case 3 : guaiwu1();break;
case 4 : guaiwu2();break;
default : cout<<"请不要乱选!"<<endl;break;
}
cin>>xiaozhen;
}
if(xiaozhen==5)
{
cout<<"正在退出游戏……"<<endl;
}
cin.get();
cin.get();
return 0;
}
void wuqidian()
{
cout<<"欢迎来到武器店!"<<endl;
cout<<"1、买小刀(1M加2攻击力)"<<endl;
cout<<"2、买短剑(2M加20攻击力)"<<endl;
cout<<"3、买大砍刀(5M加40攻击力)"<<endl;
cout<<"4、买双节棍(7M加60攻击力)"<<endl;
cout<<"5、买盾牌(2M加30防御力)"<<endl;
cout<<"6、买铠甲(5M加60防御力)"<<endl;
cout<<"7、离开武器店"<<endl;
int wuqidian;
cin>>wuqidian;
while(wuqidian!=7)//输入7时结束函数
{
switch(wuqidian)
{
case 1 : if(money<10)
cout<<"你的钱不够"<<endl;//钱不够时返回Flase
else
cout<<"购买成功!"<<endl;//钱足够时返回True
gongjili+=2;
money-=1;
break;
case 2 : if(money<80)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
gongjili+=20;
money-=80;
break;
case 3 : if(money<140)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
gongjili+=40;
money-=140;
break;
case 4 : if(money<200)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
gongjili+=60;
money-=200;
break;
case 5 : if(money<60)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
fangyuli+=30;
money-=60;
break;
fangyuli+=60;
money-=100;
break;
default : cout<<"无"<<endl;
break;
}
cin>>wuqidian;
}
if(wuqidian==7)
{ //返回main()主函数
cout<<"欢迎下次再来!"<<endl;
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
}
/*
yaodian()的设置与wuqidian()相同,可参照阅读.
*/
void yaodian()
{
cout<<"欢迎来到药品店!"<<endl;
cout<<"1、买1号补血药(10M加200生命)"<<endl;
cout<<"2、买2号补血药(50M加1000生命力)"<<endl;
cout<<"3、买3号补血药(100M加2200生命力)"<<endl;
cout<<"4、离开武器店"<<endl;
int yaodian;
cin>>yaodian;
while(yaodian!=4)
{
switch(yaodian)
{
case 1 : if(money<10)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
shengmingli+=200;
money-=10;
break;
case 2 : if(money<50)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
shengmingli+=1000;
money-=50;
break;
case 3 : if(money<100)
cout<<"你的钱不够"<<endl;
else
cout<<"购买成功!"<<endl;
shengmingli+=2200;
money-=100;
break;
default : cout<<"无"<<endl;
break;
}
cin>>yaodian;
}
if(yaodian==4)
{
cout<<"欢迎下次再来!"<<endl;
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
}
/*这里是两个战斗函数,使用指针来处理.避免造成内存崩溃.*/
void guaiwu1()
{
cout<<"开始与小怪物战斗!!!"<<endl;
double* g_shengmingli=new double;//定义怪物生命
int* g_gongjili=new int;//定义怪物攻击力
int* g_fangyuli=new int;//定义怪物防御力
int* g_money=new int;//定义怪物金钱
*g_shengmingli=100;
*g_gongjili=5;
*g_fangyuli=3;
*g_money=5;
double* tongji1=new double;//用来计算主角对怪物的杀伤
double* tongji2=new double;//用来计算怪物对主角的杀伤
*tongji1=0;
*tongji2=0;
int* huihe=new int;//定义回合数
*huihe=1;
cout<<"你开始对小怪物进行攻击!"<<endl;
int* xuanze=new int;
/*
攻击计算公式
杀伤=攻击力*2-防御力
玩家每回合可以选择攻击与逃跑
*/
while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2)
{
cout<<"现在是"<<"第"<<*huihe<<"回合!"<<endl;
cout<<"请选择你的动作:\n";
cout<<"1、攻击\n2、逃跑\n";
cin>>*xuanze;
switch((*xuanze))
{
case 1 : cout<<"你对小怪物发动了攻击!"<<endl;
*g_shengmingli-=gongjili*2-(*g_fangyuli);
*tongji1=gongjili*2-(*g_fangyuli);
cout<<"你打掉了小怪物"<<*tongji1<<"的生命!"<<endl;
cout<<"小怪物还剩"<<(*g_shengmingli)-(*tongji1)<<"点生命"<<endl;
shengmingli-=(*g_gongjili)*2-fangyuli;
*tongji2=(*g_gongjili)*2-fangyuli;
cout<<"小怪物对你发动了攻击!"<<endl;
cout<<"小怪物打掉了你"<<*tongji2<<"的生命!"<<endl;
cout<<"你还剩"<<shengmingli-(*tongji2)<<"点生命"<<endl;break;
case 2 : cout<<"你决定逃跑!"<<endl;
cout<<"逃跑成功!"<<endl;continue;
default : cout<<"请不要乱选!"<<endl;
}
(*huihe)++;
}
if((*g_shengmingli)<=0)
{//杀死怪物后的返回
cout<<"小怪物被你杀死了!你真厉害!!!"<<endl;
money+=(*g_money);
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
else
if(shengmingli<=0)
{//被怪物杀死后的返回
cout<<"你被小怪物杀死了!游戏结束!!!"<<endl;
}
else
if((*xuanze)==2)
{//逃跑的返回
cout<<"你逃回了小镇!"<<endl;
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
delete g_shengmingli;
delete g_gongjili;
delete g_fangyuli;
delete g_money;
delete tongji1;
delete tongji2;
}
/*
设置均与void函数guaiwu1()相同,可参照上例阅读.
*/
void guaiwu2()
{
cout<<"开始与大怪物战斗!!!"<<endl;
double* g_shengmingli=new double;
int* g_gongjili=new int;
int* g_fangyuli=new int;
*g_shengmingli=3600;
*g_gongjili=500;
*g_fangyuli=500;
double* tongji1=new double;
double* tongji2=new double;
*tongji1=0;
*tongji2=0;
int* huihe=new int;
*huihe=1;
cout<<"你开始对大怪物进行攻击!"<<endl;
int* xuanze=new int;
while((*g_shengmingli)>0 && shengmingli>0 && (*xuanze)!=2)
{
cout<<"现在是"<<"第"<<*huihe<<"回合!"<<endl;
cout<<"请选择你的动作:\n";
cout<<"1、攻击\n2、逃跑\n";
cin>>*xuanze;
switch((*xuanze))
{
case 1 : cout<<"你对大怪物发动了攻击!"<<endl;
*g_shengmingli-=gongjili*2-(*g_fangyuli);
*tongji1=gongjili*2-(*g_fangyuli);
cout<<"你打掉了大怪物"<<*tongji1<<"的生命!"<<endl;
cout<<"大怪物还剩"<<(*g_shengmingli)-(*tongji1)<<"点生命"<<endl;
shengmingli-=(*g_gongjili)*2-fangyuli;
*tongji2=(*g_gongjili)*2-fangyuli;
cout<<"大怪物对你发动了攻击!"<<endl;
cout<<"大怪物打掉了你"<<*tongji2<<"的生命!"<<endl;
cout<<"你还剩"<<shengmingli-(*tongji2)<<"点生命"<<endl;break;
case 2 : cout<<"你决定逃跑!"<<endl;
cout<<"逃跑成功!"<<endl;continue;
default : cout<<"请不要乱选!"<<endl;
}
(*huihe)++;
}
if((*g_shengmingli)<=0)
{
cout<<"大怪物被你杀死了!你真厉害!!!"<<endl;
guoguan=true;
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
else
if(shengmingli<=0)
{
cout<<"你被大怪物杀死了!游戏结束!!!"<<endl;
}
else
if((*xuanze)==2)
{
cout<<"你逃回了小镇!"<<endl;
cout<<"欢迎你开始玩打怪物小游戏!\n";
cout<<"小镇\n";
cout<<"一个1000年的小镇。周围有一条河,有一片树林,很多房子和很多人。\n有一家药店"<<endl;
cout<<"和一家武器店。\n";
cout<<"1.去武器店"<<endl;
cout<<"2.去药品店"<<endl;
cout<<"3.去打小怪物"<<endl;
cout<<"4.去打大怪物"<<endl;
cout<<"5.退出游戏"<<endl;
cout<<"6.显示你的状态"<<endl;
}
delete g_shengmingli;
delete g_gongjili;
delete g_fangyuli;
delete tongji1;
delete tongji2;
}
6、俄罗斯方块
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#ifdef _MSC_VER
#if _MSC_VER <= 1200
#error 你是不是还在用VC6?!
#else
#if _MSC_VER >= 1600
#include <stdint.h>
#else
typedef signed char int8_t;
typedef unsigned short uint16_t;
#endif
#ifndef __cplusplus
typedef int bool;
#define true 1
#define false 0
#endif
#endif
#else
#include <stdint.h>
#ifndef __cplusplus
#include <stdbool.h>
#endif
#endif
static const uint16_t gs_uTetrisTable[7][4] =
{
{0x00F0U, 0x2222U, 0x00F0U, 0x2222U },
{0x0072U, 0x0262U, 0x0270U, 0x0232U },
{0x0223U, 0x0074U, 0x0622U, 0x0170U },
{0x0226U, 0x0470U, 0x0322U, 0x0071U },
{0x0063U, 0x0264U, 0x0063U, 0x0264U },
{0x006CU, 0x0462U, 0x006CU, 0x0462U },
{0x0660U, 0x0660U, 0x0660U, 0x0660U }
};
static const uint16_t gs_uInitialTetrisPool[28] ={
0xC003U,0xC003U,0xC003U,0xC003U,0xC003U,0xC003U,0xC003U,
0xC003U,0xC003U,0xC003U,0xC003U,0xC003U,0xC003U,0xC003U,
0xC003U,0xC003U,0xC003U,0xC003U,0xC003U,0xC003U,0xC003U,
0xC003U,0xC003U,0xC003U,0xC003U,0xC003U,0xFFFFU,0xFFFFU
};
#define COL_BEGIN 2
#define COL_END 14
#define ROW_BEGIN 4
#define ROW_END 26
typedef struct TetrisManager
{
uint16_t pool[28]; // 游戏池
int8_t x;
int8_t y;
int8_t type[3];
int8_t orientation[3];
unsigned score; // 得分
unsigned erasedCount[4]; // 消行数
unsigned erasedTotal; // 消行总数
unsigned tetrisCount[7]; // 各方块数
unsigned tetrisTotal; // 方块总数
bool dead;
} TetrisManager;
typedef struct TetrisControl{
bool pause;
bool clockwise;
int8_t direction;
int8_t color[28][16];
} TetrisControl;
HANDLE g_hConsoleOutput;
void initGame(TetrisManager *manager, TetrisControl *control); // 初始化游戏
void restartGame(TetrisManager *manager, TetrisControl *control); // 重新开始游戏
void giveTetris(TetrisManager *manager); // 给一个方块
bool checkCollision(const TetrisManager *manager); // 碰撞检测
void insertTetris(TetrisManager *manager); // 插入方块
void removeTetris(TetrisManager *manager); // 移除方块
void horzMoveTetris(TetrisManager *manager, TetrisControl *control); // 水平移动方块
void moveDownTetris(TetrisManager *manager, TetrisControl *control); // 向下移动方块
void rotateTetris(TetrisManager *manager, TetrisControl *control); // 旋转方块
void dropDownTetris(TetrisManager *manager, TetrisControl *control); // 方块直接落地
bool checkErasing(TetrisManager *manager, TetrisControl *control); // 消行检测
void keydownControl(TetrisManager *manager, TetrisControl *control, int key); // 键按下
void setPoolColor(const TetrisManager *manager, TetrisControl *control); // 设置颜色
void gotoxyWithFullwidth(short x, short y); // 以全角定位
void printPoolBorder(); // 显示游戏池边界
void printTetrisPool(const TetrisManager *manager, const TetrisControl *control); // 显示游戏池
void printCurrentTetris(const TetrisManager *manager, const TetrisControl *control); // 显示当前方块
void printNextTetris(const TetrisManager *manager); // 显示下一个和下下一个方块
void printScore(const TetrisManager *manager); // 显示得分信息
void runGame(TetrisManager *manager, TetrisControl *control); // 运行游戏
void printPrompting(); // 显示提示信息
bool ifPlayAgain(); // 再来一次
// =============================================================================
// 主函数
int main()
{
TetrisManager tetrisManager;
TetrisControl tetrisControl;
initGame(&tetrisManager, &tetrisControl); // 初始化游戏
do
{
printPrompting(); // 显示提示信息
printPoolBorder(); // 显示游戏池边界
runGame(&tetrisManager, &tetrisControl); // 运行游戏
if (ifPlayAgain()) // 再来一次
{
SetConsoleTextAttribute(g_hConsoleOutput, 0x7);
system("cls"); // 清屏
restartGame(&tetrisManager, &tetrisControl); // 重新开始游戏
}
else
{
break;
}
} while (1);
gotoxyWithFullwidth(0, 0);
CloseHandle(g_hConsoleOutput);
return 0;
}
void initGame(TetrisManager *manager, TetrisControl *control)
{
CONSOLE_CURSOR_INFO cursorInfo = { 1, FALSE }; // 光标信息
g_hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE); // 获取控制台输出句柄
SetConsoleCursorInfo(g_hConsoleOutput, &cursorInfo); // 设置光标隐藏
SetConsoleTitleA("俄罗斯方块控制台版——By: NEWPLAN");
restartGame(manager, control);
}
// =============================================================================
// 重新开始游戏
void restartGame(TetrisManager *manager, TetrisControl *control)
{
memset(manager, 0, sizeof(TetrisManager)); // 全部置0
// 初始化游戏池
memcpy(manager->pool, gs_uInitialTetrisPool, sizeof(uint16_t [28]));
srand((unsigned)time(NULL)); // 设置随机种子
manager->type[1] = rand() % 7; // 下一个
manager->orientation[1] = rand() & 3;
manager->type[2] = rand() % 7; // 下下一个
manager->orientation[2] = rand() & 3;
memset(control, 0, sizeof(TetrisControl)); // 全部置0
giveTetris(manager); // 给下一个方块
setPoolColor(manager, control); // 设置颜色
}
// =============================================================================
// 给一个方块
void giveTetris(TetrisManager *manager)
{
uint16_t tetris;
manager->type[0] = manager->type[1]; // 下一个方块置为当前
manager->orientation[0] = manager->orientation[1];
manager->type[1] = manager->type[2];// 下下一个置方块为下一个
manager->orientation[1] = manager->orientation[2];
manager->type[2] = rand() % 7;// 随机生成下下一个方块
manager->orientation[2] = rand() & 3;
tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]]; // 当前方块
// 设置当前方块y坐标,保证刚给出时只显示方块最下面一行
if (tetris & 0xF000)
{
manager->y = 0;
}
else
{
manager->y = (tetris & 0xFF00) ? 1 : 2;
}
manager->x = 6; // 设置当前方块x坐标
if (checkCollision(manager)) // 检测到碰撞
{
manager->dead = true; // 标记游戏结束
}
else // 未检测到碰撞
{
insertTetris(manager); // 将当前方块加入游戏池
}
++manager->tetrisTotal; // 方块总数
++manager->tetrisCount[manager->type[0]]; // 相应方块数
printNextTetris(manager); // 显示下一个方块
printScore(manager); // 显示得分信息
}
// =============================================================================
// 碰撞检测
bool checkCollision(const TetrisManager *manager)
{
// 当前方块
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
uint16_t dest = 0;
// 获取当前方块在游戏池中的区域:
// 游戏池坐标x y处小方格信息,按低到高存放在16位无符号数中
dest |= (((manager->pool[manager->y + 0] >> manager->x) << 0x0) & 0x000F);
dest |= (((manager->pool[manager->y + 1] >> manager->x) << 0x4) & 0x00F0);
dest |= (((manager->pool[manager->y + 2] >> manager->x) << 0x8) & 0x0F00);
dest |= (((manager->pool[manager->y + 3] >> manager->x) << 0xC) & 0xF000);
return ((dest & tetris) != 0);
}
void insertTetris(TetrisManager *manager){
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
manager->pool[manager->y + 0] |= (((tetris >> 0x0) & 0x000F) << manager->x);
manager->pool[manager->y + 1] |= (((tetris >> 0x4) & 0x000F) << manager->x);
manager->pool[manager->y + 2] |= (((tetris >> 0x8) & 0x000F) << manager->x);
manager->pool[manager->y + 3] |= (((tetris >> 0xC) & 0x000F) << manager->x);
}
void removeTetris(TetrisManager *manager){
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
manager->pool[manager->y + 0] &= ~(((tetris >> 0x0) & 0x000F) << manager->x);
manager->pool[manager->y + 1] &= ~(((tetris >> 0x4) & 0x000F) << manager->x);
manager->pool[manager->y + 2] &= ~(((tetris >> 0x8) & 0x000F) << manager->x);
manager->pool[manager->y + 3] &= ~(((tetris >> 0xC) & 0x000F) << manager->x);
}
void setPoolColor(const TetrisManager *manager, TetrisControl *control){
int8_t i, x, y;
uint16_t tetris = gs_uTetrisTable[manager->type[0]][manager->orientation[0]];
for (i = 0; i < 16; ++i)
{
y = (i >> 2) + manager->y; // 待设置的列
if (y > ROW_END) // 超过底部限制
{
break;
}
x = (i & 3) + manager->x;
if ((tetris >> i) & 1){
control->color[y][x] = (manager->type[0] | 8);
}
}
}
void rotateTetris(TetrisManager *manager, TetrisControl *control)
{
int8_t ori = manager->orientation[0]; // 记录原旋转状态
removeTetris(manager); // 移走当前方块
// 顺/逆时针旋转
manager->orientation[0] = (control->clockwise) ? ((ori + 1) & 3) : ((ori + 3) & 3);
if (checkCollision(manager)) // 检测到碰撞
{
manager->orientation[0] = ori; // 恢复为原旋转状态
insertTetris(manager); // 放入当前方块。由于状态没改变,不需要设置颜色
}
else
{
insertTetris(manager); // 放入当前方块
setPoolColor(manager, control); // 设置颜色
printCurrentTetris(manager, control); // 显示当前方块
}
}
// =============================================================================
// 水平移动方块
void horzMoveTetris(TetrisManager *manager, TetrisControl *control)
{
int x = manager->x; // 记录原列位置
removeTetris(manager); // 移走当前方块
control->direction == 0 ? (--manager->x) : (++manager->x); // 左/右移动
if (checkCollision(manager)) // 检测到碰撞
{
manager->x = x;
insertTetris(manager);
}
else
{
insertTetris(manager);
setPoolColor(manager, control); // 设置颜色
printCurrentTetris(manager, control); // 显示当前方块
}
}
// =============================================================================
// 向下移动方块
void moveDownTetris(TetrisManager *manager, TetrisControl *control)
{
int8_t y = manager->y; // 记录原行位置
removeTetris(manager); // 移走当前方块
++manager->y; // 向下移动
if (checkCollision(manager)) // 检测到碰撞
{
manager->y = y; // 恢复为原行位置
insertTetris(manager); // 放入当前方块。由于位置没改变,不需要设置颜色
if (checkErasing(manager, control)) // 检测到消行
{
printTetrisPool(manager, control); // 显示游戏池
}
}
else
{
insertTetris(manager); // 放入当前方块
setPoolColor(manager, control); // 设置颜色
printCurrentTetris(manager, control); // 显示当前方块
}
}
// =============================================================================
// 方块直接落地
void dropDownTetris(TetrisManager *manager, TetrisControl *control)
{
removeTetris(manager); // 移走当前方块
for (; manager->y < ROW_END; ++manager->y) // 从上往下
{
if (checkCollision(manager)) // 检测到碰撞
{
break;
}
}
--manager->y; // 上移一格当然没有碰撞
insertTetris(manager); // 放入当前方块
setPoolColor(manager, control); // 设置颜色
checkErasing(manager, control); // 检测消行
printTetrisPool(manager, control); // 显示游戏池
}
// =============================================================================
// 消行检测
bool checkErasing(TetrisManager *manager, TetrisControl *control)
{
static const unsigned scores[5] = { 0, 10, 30, 90, 150 }; // 消行得分
int8_t count = 0;
int8_t k = 0, y = manager->y + 3; // 从下往上检测
do
{
if (y < ROW_END && manager->pool[y] == 0xFFFFU) // 有效区域内且一行已填满
{
++count;
// 消除一行方块
memmove(manager->pool + 1, manager->pool, sizeof(uint16_t) * y);
// 颜色数组的元素随之移动
memmove(control->color[1], control->color[0], sizeof(int8_t [16]) * y);
}
else
{
--y;
++k;
}
} while (y >= manager->y && k < 4);
manager->erasedTotal += count; // 消行总数
manager->score += scores[count]; // 得分
if (count > 0)
{
++manager->erasedCount[count - 1]; // 消行
}
giveTetris(manager); // 给下一个方块
setPoolColor(manager, control); // 设置颜色
return (count > 0);
}
// =============================================================================
// 键按下
void keydownControl(TetrisManager *manager, TetrisControl *control, int key)
{
if (key == 13) // 暂停/解除暂停
{
control->pause = !control->pause;
}
if (control->pause) // 暂停状态,不作处理
{
return;
}
switch (key)
{
case 'w': case 'W': case '8': case 72: // 上
control->clockwise = true; // 顺时针旋转
rotateTetris(manager, control); // 旋转方块
break;
case 'a': case 'A': case '4': case 75: // 左
control->direction = 0; // 向左移动
horzMoveTetris(manager, control); // 水平移动方块
break;
case 'd': case 'D': case '6': case 77: // 右
control->direction = 1; // 向右移动
horzMoveTetris(manager, control); // 水平移动方块
break;
case 's': case 'S': case '2': case 80: // 下
moveDownTetris(manager, control); // 向下移动方块
break;
case ' ': // 直接落地
dropDownTetris(manager, control);
break;
case '0': // 反转
control->clockwise = false; // 逆时针旋转
rotateTetris(manager, control); // 旋转方块
break;
default:
break;
}
}
// =============================================================================
// 以全角定位
void gotoxyWithFullwidth(short x, short y)
{
static COORD cd;
cd.X = (short)(x << 1);
cd.Y = y;
SetConsoleCursorPosition(g_hConsoleOutput, cd);
}
// =============================================================================
// 显示游戏池边界
void printPoolBorder()
{
int8_t y;
SetConsoleTextAttribute(g_hConsoleOutput, 0xF0);
for (y = ROW_BEGIN; y < ROW_END; ++y) // 不显示顶部4行和底部2行
{
gotoxyWithFullwidth(10, y - 3);
printf("%2s","");
gotoxyWithFullwidth(23, y - 3);
printf("%2s", "");
}
gotoxyWithFullwidth(10, y - 3); // 底部边界
printf("%28s", "");
}
// 定位到游戏池中的方格
#define gotoxyInPool(x, y) gotoxyWithFullwidth(x + 9, y - 3)
// =============================================================================
// 显示游戏池
void printTetrisPool(const TetrisManager *manager, const TetrisControl *control)
{
int8_t x, y;
for (y = ROW_BEGIN; y < ROW_END; ++y) // 不显示顶部4行和底部2行
{
gotoxyInPool(2, y); // 定点到游戏池中的方格
for (x = COL_BEGIN; x < COL_END; ++x) // 不显示左右边界
{
if ((manager->pool[y] >> x) & 1) // 游戏池该方格有方块
{
// 用相应颜色,显示一个实心方块
SetConsoleTextAttribute(g_hConsoleOutput, control->color[y][x]);
printf("■");
}
else // 没有方块,显示空白
{
SetConsoleTextAttribute(g_hConsoleOutput, 0);
printf("%2s", "");
}
}
}
}
// =============================================================================
// 显示当前方块
void printCurrentTetris(const TetrisManager *manager, const TetrisControl *control)
{
int8_t x, y;
// 显示当前方块是在移动后调用的,为擦去移动前的方块,需要扩展显示区域
// 由于不可能向上移动,故不需要向下扩展
y = (manager->y > ROW_BEGIN) ? (manager->y - 1) : ROW_BEGIN; // 向上扩展一格
for (; y < ROW_END && y < manager->y + 4; ++y)
{
x = (manager->x > COL_BEGIN) ? (manager->x - 1) : COL_BEGIN; // 向左扩展一格
for (; x < COL_END && x < manager->x + 5; ++x) // 向右扩展一格
{
gotoxyInPool(x, y); // 定点到游戏池中的方格
if ((manager->pool[y] >> x) & 1) // 游戏池该方格有方块
{
// 用相应颜色,显示一个实心方块
SetConsoleTextAttribute(g_hConsoleOutput, control->color[y][x]);
printf("■");
}
else // 没有方块,显示空白
{
SetConsoleTextAttribute(g_hConsoleOutput, 0);
printf("%2s", "");
}
}
}
}
// =============================================================================
// 显示下一个和下下一个方块
void printNextTetris(const TetrisManager *manager)
{
int8_t i;
uint16_t tetris;
// 边框
SetConsoleTextAttribute(g_hConsoleOutput, 0xF);
gotoxyWithFullwidth(26, 1);
printf("┏━━━━┳━━━━┓");
gotoxyWithFullwidth(26, 2);
printf("┃%8s┃%8s┃", "", "");
gotoxyWithFullwidth(26, 3);
printf("┃%8s┃%8s┃", "", "");
gotoxyWithFullwidth(26, 4);
printf("┃%8s┃%8s┃", "", "");
gotoxyWithFullwidth(26, 5);
printf("┃%8s┃%8s┃", "", "");
gotoxyWithFullwidth(26, 6);
printf("┗━━━━┻━━━━┛");
// 下一个,用相应颜色显示
tetris = gs_uTetrisTable[manager->type[1]][manager->orientation[1]];
SetConsoleTextAttribute(g_hConsoleOutput, manager->type[1] | 8);
for (i = 0; i < 16; ++i)
{
gotoxyWithFullwidth((i & 3) + 27, (i >> 2) + 2);
((tetris >> i) & 1) ? printf("■") : printf("%2s", "");
}
// 下下一个,不显示彩色
tetris = gs_uTetrisTable[manager->type[2]][manager->orientation[2]];
SetConsoleTextAttribute(g_hConsoleOutput, 8);
for (i = 0; i < 16; ++i)
{
gotoxyWithFullwidth((i & 3) + 32, (i >> 2) + 2);
((tetris >> i) & 1) ? printf("■") : printf("%2s", "");
}
}
// =============================================================================
// 显示得分信息
void printScore(const TetrisManager *manager)
{
static const char *tetrisName = "ITLJZSO";
int8_t i;
SetConsoleTextAttribute(g_hConsoleOutput, 0xE);
gotoxyWithFullwidth(2, 2);
printf("■得分:%u", manager->score);
gotoxyWithFullwidth(1, 6);
printf("■消行总数:%u", manager->erasedTotal);
for (i = 0; i < 4; ++i)
{
gotoxyWithFullwidth(2, 8 + i);
printf("□消%d:%u", i + 1, manager->erasedCount[i]);
}
gotoxyWithFullwidth(1, 15);
printf("■方块总数:%u", manager->tetrisTotal);
for (i = 0; i < 7; ++i)
{
gotoxyWithFullwidth(2, 17 + i);
printf("□%c形:%u", tetrisName[i], manager->tetrisCount[i]);
}
}
// =============================================================================
// 显示提示信息
void printPrompting()
{
SetConsoleTextAttribute(g_hConsoleOutput, 0xB);
gotoxyWithFullwidth(26, 10);
printf("■控制:");
gotoxyWithFullwidth(27, 12);
printf("□向左移动:← A 4");
gotoxyWithFullwidth(27, 13);
printf("□向右移动:→ D 6");
gotoxyWithFullwidth(27, 14);
printf("□向下移动:↓ S 2");
gotoxyWithFullwidth(27, 15);
printf("□顺时针转:↑ W 8");
gotoxyWithFullwidth(27, 16);
printf("□逆时针转:0");
gotoxyWithFullwidth(27, 17);
printf("□直接落地:空格");
gotoxyWithFullwidth(27, 18);
printf("□暂停游戏:回车");
gotoxyWithFullwidth(25, 23);
printf("■By: NEWPLAN @ UESTC");
}
// =============================================================================
// 运行游戏
void runGame(TetrisManager *manager, TetrisControl *control)
{
clock_t clockLast, clockNow;
clockLast = clock(); // 计时
printTetrisPool(manager, control); // 显示游戏池
while (!manager->dead) // 没挂
{
while (_kbhit()) // 有键按下
{
keydownControl(manager, control, _getch()); // 处理按键
}
if (!control->pause) // 未暂停
{
clockNow = clock(); // 计时
// 两次记时的间隔超过0.45秒
if (clockNow - clockLast > 0.45F * CLOCKS_PER_SEC)
{
clockLast = clockNow;
keydownControl(manager, control, 80); // 方块往下移
}
}
}
}
// =============================================================================
// 再来一次
bool ifPlayAgain()
{
int ch;
SetConsoleTextAttribute(g_hConsoleOutput, 0xF0);
gotoxyWithFullwidth(15, 10);
printf("游戏结束");
gotoxyWithFullwidth(13, 11);
printf("按Y重玩,按N退出");
do
{
ch = _getch();
if (ch == 'Y' || ch == 'y')
{
return true;
}
else if (ch == 'N' || ch == 'n')
{
return false;
}
} while (1);
}