下面是一些游戏代码!
#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);
}
}
2
#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;
}
3
#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int main()
{
int n,m;
char a[9]={32,32,32,32,32,32,32,32,32};
for(int i=1;i<=9;i++)
{
cout<<a[0]<<"|"<<a[1]<<"|"<<a[2]<<endl;
cout<<"-+-+-"<<endl;
cout<<a[3]<<"|"<<a[4]<<"|"<<a[5]<<endl;
cout<<"-+-+-"<<endl;
cout<<a[6]<<"|"<<a[7]<<"|"<<a[8]<<endl;
n=0;
while((n<1||n>9)&&a[n-1]!='x'&&a[n-1]!='o')
{
cin>>n;
}
for(int j=1;j<=10000;j++)
{
cout<<endl;
}
a[n-1]='x';
if((a[0]=='x'&&a[1]=='x'&&a[2]=='x')||(a[3]=='x'&&a[4]=='x'&&a[5]=='x')||(a[6]=='x'&&a[7]=='x'&&a[8]=='x')||(a[0]=='x'&&a[3]=='x'&&a[6]=='x')||(a[1]=='x'&&a[4]=='x'&&a[7]=='x')||(a[2]=='x'&&a[5]=='x'&&a[8]=='x'))
{
cout<<"p1 win";
break;
}
cout<<a[0]<<"|"<<a[1]<<"|"<<a[2]<<endl;
cout<<"-+-+-"<<endl;
cout<<a[3]<<"|"<<a[4]<<"|"<<a[5]<<endl;
cout<<"-+-+-"<<endl;
cout<<a[6]<<"|"<<a[7]<<"|"<<a[8]<<endl;
m=0;
Sleep(2000);
while((m<1||m>9)&&a[m-1]!='o'&&a[m-1]!='x')
{
m=rand()%9+1;
}
cout<<m;
Sleep(1000);
for(int j=1;j<=10000;j++)
{
cout<<endl;
}
a[m-1]='o';
if((a[0]=='o'&&a[1]=='o'&&a[2]=='o')||(a[3]=='o'&&a[4]=='o'&&a[5]=='o')||(a[6]=='o'&&a[7]=='o'&&a[8]=='o')||(a[0]=='o'&&a[3]=='o'&&a[6]=='o')||(a[1]=='o'&&a[4]=='o'&&a[7]=='o')||(a[2]=='o'&&a[5]=='o'&&a[8]=='o'))
{
cout<<"p2 win";
break;
}
}
return 0;
}
4
#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <cstring>
#include <cstdio>
#include <iostream>
#define N 22
using namespace std;
int gameover;
int x1, y1; // 随机出米
int x,y;
long start;
//=======================================
//类的实现与应用initialize
//=======================================
//下面定义贪吃蛇的坐标类
class snake_position
{
public:
int x,y; //x表示行,y表示列
snake_position(){};
void initialize(int &);//坐标初始化
};
snake_position position[(N-2)*(N-2)+1]; //定义贪吃蛇坐标类数组,有(N-2)*(N-2)个坐标
void snake_position::initialize(int &j)
{
x = 1;
y = j;
}
//下面定义贪吃蛇的棋盘图
class snake_map
{
private:
char s[N][N];//定义贪吃蛇棋盘,包括墙壁。
int grade, length;
int gamespeed; //前进时间间隔
char direction; // 初始情况下,向右运动
int head,tail;
int score;
bool gameauto;
public:
snake_map(int h=4,int t=1,int l=4,char d=77,int s=0):length(l),direction(d),head(h),tail(t),score(s){}
void initialize(); //初始化函数
void show_game();
int updata_game();
void setpoint();
void getgrade();
void display();
};
//定义初始化函数,将贪吃蛇的棋盘图进行初始化
void snake_map::initialize()
{
int i,j;
for(i=1;i<=3;i++)
s[1][i] = '*';
s[1][4] = '#';
for(i=1;i<=N-2;i++)
for(j=1;j<=N-2;j++)
s[i][j]=' '; // 初始化贪吃蛇棋盘中间空白部分
for(i=0;i<=N-1;i++)
s[0][i] = s[N-1][i] = '-'; //初始化贪吃蛇棋盘上下墙壁
for(i=1;i<=N-2;i++)
s[i][0] = s[i][N-1] = '|'; //初始化贪吃蛇棋盘左右墙壁
}
//============================================
//输出贪吃蛇棋盘信息
void snake_map::show_game()
{
system("cls"); // 清屏
int i,j;
cout << endl;
for(i=0;i<N;i++)
{
cout << '\t';
for(j=0;j<N;j++)
cout<<s[i][j]<<' '; // 输出贪吃蛇棋盘
if(i==2) cout << "\t等级:" << grade;
if(i==6) cout << "\t速度:" << gamespeed;
if(i==10) cout << "\t得分:" << score << "分" ;
if(i==14) cout << "\t暂停:按一下空格键" ;
if(i==18) cout << "\t继续:按两下空格键" ;
cout<<endl;
}
}
//输入选择等级
void snake_map::getgrade()
{
cin>>grade;
while( grade>7 || grade<1 )
{
cout << "请输入数字1-7选择等级,输入其他数字无效" << endl;
cin >> grade;
}
switch(grade)
{
case 1: gamespeed = 1000;gameauto = 0;break;
case 2: gamespeed = 800;gameauto = 0;break;
case 3: gamespeed = 600;gameauto = 0;break;
case 4: gamespeed = 400;gameauto = 0;break;
case 5: gamespeed = 200;gameauto = 0;break;
case 6: gamespeed = 100;gameauto = 0;break;
case 7: grade = 1;gamespeed = 1000;gameauto = 1;break;
}
}
//输出等级,得分情况以及称号
void snake_map::display()
{
cout << "\n\t\t\t\t等级:" << grade;
cout << "\n\n\n\t\t\t\t速度:" << gamespeed;
cout << "\n\n\n\t\t\t\t得分:" << score << "分" ;
}
//随机产生米
void snake_map::setpoint()
{
srand(time(0));
do
{
x1 = rand() % (N-2) + 1;
y1 = rand() % (N-2) + 1;
}while(s[x1][y1]!=' ');
s[x1][y1]='*';
}
char key;
int snake_map::updata_game()
{
gameover = 1;
key = direction;
start = clock();
while((gameover=(clock()-start<=gamespeed))&&!kbhit());
//如果有键按下或时间超过自动前进时间间隔则终止循环
if(gameover)
{
getch();
key = getch();
}
if(key == ' ')
{
while(getch()!=' '){};//这里实现的是按空格键暂停,按空格键继续的功能,但不知为何原因,需要按两下空格才能继续。这是个bug。
}
else
direction = key;
switch(direction)
{
case 72: x= position[head].x-1; y= position[head].y;break; // 向上
case 80: x= position[head].x+1; y= position[head].y;break; // 向下
case 75: x= position[head].x; y= position[head].y-1;break; // 向左
case 77: x= position[head].x; y= position[head].y+1; // 向右
}
if(!(direction==72||direction==80||direction==75 ||direction==77)) // 按键非方向键
gameover = 0;
else if(x==0 || x==N-1 ||y==0 || y==N-1) // 碰到墙壁
gameover = 0;
else if(s[x][y]!=' '&&!(x==x1&&y==y1)) // 蛇头碰到蛇身
gameover = 0;
else if(x==x1 && y==y1)
{ // 吃米,长度加1
length ++;
if(length>=8 && gameauto)
{
length -= 8;
grade ++;
if(gamespeed>=200)
gamespeed -= 200; // 改变贪吃蛇前进速度
else
gamespeed = 100;
}
s[x][y]= '#'; //更新蛇头
s[position[head].x][position[head].y] = '*'; //吃米后将原先蛇头变为蛇身
head = (head+1) % ( (N-2)*(N-2) ); //取蛇头坐标
position[head].x = x;
position[head].y = y;
show_game();
gameover = 1;
score += grade*20; //加分
setpoint(); //产生米
}
else
{ // 不吃米
s[position[tail].x][position[tail].y]=' ';//将蛇尾置空
tail= (tail+1) % ( (N-2) * (N-2) );//更新蛇尾坐标
s[position[head].x][position[head].y]='*'; //将蛇头更为蛇身
head= (head+1) % ( (N-2) * (N-2) );
position[head].x = x;
position[head].y = y;
s[position[head].x][position[head].y]='#'; //更新蛇头
gameover = 1;
}
return gameover;
}
//====================================
//主函数部分
//====================================
int main()
{
char ctn = 'y';
int nodead;
cout<<"\n\n\n\n\n\t\t\t 欢迎进入贪吃蛇游戏!"<<endl;//欢迎界面;
cout<<"\n\n\n\t\t\t 按任意键马上开始。。。"<<endl;//准备开始;;
getch();
while( ctn=='y' )
{
system("cls"); // 清屏
snake_map snake;
snake.initialize();
cout << "\n\n请输入数字选择游戏等级:" << endl;
cout << "\n\n\n\t\t\t1.等级一:速度 1000 \n\n\t\t\t2.等级二:速度 800 \n\n\t\t\t3.等级三:速度 600 ";
cout << "\n\n\t\t\t4.等级四:速度 400 \n\n\t\t\t5.等级五:速度 200 \n\n\t\t\t6.等级六:速度 100 \n\n\t\t\t7.自动升级模式" << endl;
snake.getgrade();//获取等级
for(int i=1;i<=4;i++)
{
position[i].initialize(i);//初始化坐标
}
snake.setpoint(); // 产生第一个米
do
{
snake.show_game();
nodead = snake.updata_game();
}while(nodead);
system("cls"); //清屏
cout << "\n\n\n\t\t\t\tGameover!\n\n"<<endl;
snake.display();//输出等级/得分情况
cout << "\n\n\n\t\t 是否选择继续游戏?输入 y 继续,n 退出" << endl;
cin >> ctn;
}
return 0;
}
求求了!点个赞吧加个关注吧!!!!!!!!!!!