前言:
百款c++小游戏,一定有你喜欢的(没有勿喷噢)
第一款游戏:贪吃蛇
无限版:
代码(体验一下):
#include <cstdio>
#include <iostream>
#include <ctime>
#include <conio.h>
#include <windows.h> //停顿:Sleep();
#include <cstdlib> //清屏:system("cls");
#include <cstring> //1:'◎' 2:'⊙' 3:'▲' 4:'◆' 5:'■'
using namespace std;
int a[100][100], dir[100][100];
//string guo[5]={'▲','◆','■','▼','★'};
int fx[4][2] = { {0,-1},{1,0},{0,1},{-1,0} }; //ap来记录奖励块的分
int n, m, ff, f = 1, t = 50, T = 50, ap = 10, egg[2], danshu, headx, heady, endx, endy, point = 0, apple = 0, fangx, stopp = 0, panduan = 0; //ff用来判断 panduan 0:中途退出;1:咬到自己;2:重置
int lastfx;
int zuobi = 0;
int fff[110][110];
string print[4][4] = { {"═","╔"," ","╚"},{"╝","║","╚"," "},{" ","╗","═","╝"},{"╗"," ","╔","║"} };
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 reset() {
system("cls");
color(7); printf(" ★"); color(10); printf("贪吃蛇"); color(11); printf("★\n\n"); color(7);
printf("⊙打印若有错位请调整窗口大小或按r重输小点的数⊙\n\n 请输入场地大小(格式:行数 列数) 推荐(20 20) \n\n>>");
scanf("%d%d", &n, &m);
system("cls");
color(14);
for (int i = 1; i <= m; i++) { gotoxy(0, i); printf("※"); gotoxy(n + 1, i); printf("※"); }
for (int i = 1; i <= n; i++) { gotoxy(i, 0); printf("※"); gotoxy(i, m + 1); printf("※"); }
color(10);
memset(fff, 0, sizeof(fff));
gotoxy(0, 0); printf("┍");
gotoxy(n + 1, 0); printf("┕");
gotoxy(0, m + 1); printf("┑");
gotoxy(n + 1, m + 1); printf("┙");
color(7);
gotoxy(0, m + 5); printf("方向:a/s/d/w | 1/2/3/5");
gotoxy(1, m + 5); printf("暂停:p | 0");
gotoxy(2, m + 5); printf("减速:q/j | 4/7");
gotoxy(3, m + 5); printf("加速:e/k | 6/9");
gotoxy(4, m + 5); printf("重置:r | .");
gotoxy(5, m + 5); printf("地图:h | +");
headx = endx = endy = 1; heady = 3;
f = 1; ff = 3;
memset(a, 1, sizeof(a));
memset(dir, 0, sizeof(dir));
a[1][1] = a[1][2] = a[1][3] = 0;
gotoxy(1, 1); printf("⊙");
gotoxy(1, 2); printf("⊙");
gotoxy(1, 3); printf("◎");
dir[1][1] = dir[1][2] = dir[1][3] = 2;
apple = 0; fangx = 2; point = 0; danshu = 0; lastfx = 2; stopp = 0;
if (zuobi) { color(12); gotoxy(12, m + 5); printf("作弊可耻!"); gotoxy(m + 4, 0); printf("做了弊还没路走那就救不了你了,乖乖重来吧......"); color(7); }
}
void haha() {
char ch = getch();
if ((ch == 'a' || ch == '1') && dir[headx][heady] != 2) { fangx = 0; return; }
if ((ch == 's' || ch == '2') && dir[headx][heady] != 3) { fangx = 1; return; }
if ((ch == 'd' || ch == '3') && dir[headx][heady] != 0) { fangx = 2; return; }
if ((ch == 'w' || ch == '5') && dir[headx][heady] != 1) { fangx = 3; return; }
if (ch == 'p' || ch == '0') { stopp = (stopp + 1) % 2; return; }
if (ch == 'q' || ch == '4') { T += 10; if (T >= 1005) T = 1005; return; }
if (ch == 'e' || ch == '6') { T -= 10; if (T <= 1) T = 2; return; }
if (ch == 'j' || ch == '7') { T += 1; if (T >= 1005) T = 1005; return; }
if (ch == 'k' || ch == '9') { T -= 1; if (T <= 1) T = 2; return; }
if (ch == 'r' || ch == '.') { ff = 2; f = 0; panduan = 0; return; }
if (ch == 'h' || ch == '8') {
zuobi += 1; zuobi %= 2;
if (zuobi) { color(12); gotoxy(12, m + 5); printf("作弊可耻!"); gotoxy(m + 4, 0); printf("做了弊还没路走那就救不了你了,乖乖重来吧......"); color(7); }
else { printf(" "); gotoxy(m + 4, 0); printf(" "); }
}
if (ch == 'g' || ch == '+') {
gotoxy(n + 50, 0);
printf("现在切换到地图编辑模式\n\nq/4 为开始放置模式(光标移动过的痕迹会留下障碍物)\n\ne/6 为取消放置障碍物模式\n\nj/0 为橡皮模式(再按一次退出橡皮模式)\n\n按 h/+ 继续游戏\n\n");
printf("\n按任意键开始编辑地图");
color(13);
int x = 1, y = 1, dfx, moshi = 0;
ch = getch(); gotoxy(0, 0); gotoxy(1, 1);
while (ch != 'h' && ch != '+') {
if (ch == 'a' || ch == '1') dfx = 0;
else if (ch == 's' || ch == '2') dfx = 1;
else if (ch == 'd' || ch == '3') dfx = 2;
else if (ch == 'w' || ch == '5') dfx = 3;
else dfx = 5;
if (ch == 'q' || ch == '4') moshi = 1;
if (ch == 'e' || ch == '6') moshi = 0;
if (ch == 'j' || ch == '0') if (moshi - 2) moshi = 2; else moshi = 0;
if (dfx <= 4) {
if (x + fx[dfx][0] > 0 && x + fx[dfx][0] <= n && y + fx[dfx][1] > 0 && y + fx[dfx][1] <= m) { x += fx[dfx][0]; y += fx[dfx][1]; }
if (x > 0 && x <= n && y > 0 && y <= m) {
gotoxy(x, y);
if (moshi == 1 && a[x][y] && !fff[x][y]) { a[x][y] = 0; fff[x][y] = 1; printf("□"); }
if (moshi == 2 && fff[x][y]) { a[x][y] = 1; fff[x][y] = 0; printf(" "); }
gotoxy(x, y);
}
}
ch = getch();
}
color(7);
}
}
void findegg() {
srand(time(NULL));
int aa = 1;
while (!a[egg[0]][egg[1]]) { egg[0] = (rand() % (n)) + 1; egg[1] = (rand() % m) + 1; }
a[egg[0]][egg[1]] = 0;
point += ap;
gotoxy(egg[0], egg[1]); if (!apple) { printf("★"); ap = 8000 / T; if (ap >= 200) ap = 200; }
else { printf("●"); ap = 10; }
apple++; apple = apple % 10;
gotoxy(n + 2, 0); printf("距离下一个奖励蛋还有 "); color(11); cout << 9 - (8 + apple) % 10; color(7); printf(" 个");
danshu++;
return;
}
int gg() {
if (ff == 0) return(0);
if (ff == 1) { gotoxy(n + 2, 1); Sleep(3000); gotoxy(n + 26, 1); gotoxy(n + 10, 0); printf("哦豁~ 你咬到自己了o( ̄▽ ̄)d\n\n\n你共吃蛋 %d 个\n\n\n你共得分 %d 分,恭喜!\n\n\n等一下就继续", danshu, point); Sleep(5000); if (kbhit) return(1); }
if (ff == 2) return(1);
return 0;
}
void dong() {
gotoxy(11, m + 3); printf("当前的蛋有 %d 分 ", ap);
if (ap - 3 > 10) ap -= 3;
if (ap != 10) {
gotoxy(egg[0], egg[1]);
color(ap % 10);
printf("★");
color(7);
}
int x, y;
x = (headx + fx[fangx][0] - 1 + n) % (n)+1; y = (heady + fx[fangx][1] - 1 + m) % (m)+1;
dir[headx][heady] = fangx;
if (x == egg[0] && y == egg[1]) {
gotoxy(headx, heady); cout << print[lastfx][fangx];
dir[headx][heady] = fangx;
headx = x; heady = y; a[x][y] = 0;
gotoxy(headx, heady); printf("◎");
findegg();
}
else if (a[x][y]) {
gotoxy(headx, heady); cout << print[lastfx][fangx];
headx = x; heady = y; a[x][y] = 0;
gotoxy(headx, heady); printf("◎");
gotoxy(endx, endy); printf(" "); a[endx][endy] = 1;
x = endx; y = endy;
endx += fx[dir[x][y]][0]; endx = (endx - 1 + n) % (n)+1;
endy += fx[dir[x][y]][1]; endy = (endy - 1 + m) % (m)+1;
dir[x][y] = -1;
}
else if (!zuobi) { f = 0; ff = 1; }
dir[headx][heady] = fangx;
lastfx = fangx;
return;
}
int main() {
while (1) {
srand(time(NULL));
reset();
egg[0] = (rand() % n - 1) + 2; egg[1] = (rand() % m) + 1;
a[egg[0]][egg[1]] = 0;
gotoxy(egg[0], egg[1]); if (apple) { printf("★"); ap = 100; }
else printf("●");
while (f) {
if (kbhit()) haha();
gotoxy(9, m + 5); printf("吃蛋:%d 个", danshu);
gotoxy(8, m + 5); printf("速度: %d ", 1005 - T);
//while (stopp) {Sleep(10); if (kbhit()) haha();}
Sleep(1); t = (t - 1 + T) % T;
if (t == 0 && !stopp) dong();
gotoxy(7, m + 5); printf("得分:%d 分", point);
gotoxy(12, m + 5);
}
if (!gg()) return 0;
}
return 0;
}
经典版:
代码如下:
//WASD控制/开始游戏
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#define W 32
#define H 22
const int SIZE_SK = 30*20;
HANDLE hout;
COORD pos= {0, 0};
CONSOLE_CURSOR_INFO cur_info = {1, 0};
struct snake {
short x, y; //节点坐标
short md; //节点运动方向
short num; //节点内容
int col; //节点颜色
} snake[30*20];
const short dx[4] = {-1, 1, 0, 0}; //方向?
const short dy[4] = {0, 0, -1, 1}; //方向?
short gamemap[W][H];
short head, tail, score=0;
void initmap(int tm);
void createFood(void);
int getkeys(void);
int move(int idx);
void pntSnake(int i);
void drawEdge(void);
void gameinfo(char *s, int n);
int main(void)
{
int tm = 150; //刷新间隔150毫秒?
hout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(hout, &cur_info); //隐藏光标?
system("COLOR 2f");
initmap(tm);
return 0;
}
void initmap(int tm)
{
short x, y, i, j;
int index, tmp, ret_m;
while (1){
head=0;
tail=-1;
score=0;
//初始化蛇节点随机颜色
srand(time(0));
for (i=0; i<SIZE_SK; i++)
snake[i].col = 0x20;
//初始化地图数组
for (i=0; i<W; i++)
for (j=0; j<H; j++)
gamemap[i][j] = 0;
system("cls");
//绘制边界?
drawEdge();
//打印蛇头
srand(time(0));
do {
x = rand()%(W-2) + 1;
y = rand()%(H-2) + 1;
} while (gamemap[x][y] != 0); //只在空白处生成?
snake[head].x = x;
snake[head].y = y;
gamemap[x][y] = snake[head].num = 1;
pos.X = x*2;
pos.Y = y;
SetConsoleCursorPosition(hout, pos);
printf("█");
//打印食物
createFood();
while ((index=getkeys()) == 4)
Sleep(500);
//循环开始
while (1){
ret_m = move(index);
if (ret_m == 1){
pos.X = 0; pos.Y = H;
SetConsoleCursorPosition(hout, pos);
printf("哎呀,挂了哦 ^_^");
break;
}
else if (ret_m == 2){
pos.X = 0; pos.Y = H;
SetConsoleCursorPosition(hout, pos);
printf("哎呀,超神了 ^_^");
break;
}
Sleep(tm);
if ((tmp = getkeys()) != 4)
index = tmp;
}
//菜单代码
puts(" 按w\\s\\a\\d 继续玩,退出请关闭我");
getch();
}
}
void createFood(void)
{
short x, y;
srand(time(0));
do {
x = rand()%(W-2) + 1;
y = rand()%(H-2) + 1;
} while (gamemap[x][y] != 0);
gamemap[x][y] = 2;
pos.X = x*2; pos.Y = y;
SetConsoleCursorPosition(hout, pos);
SetConsoleTextAttribute(hout, 0xccccc);
printf("█");
}
int getkeys(void) //获取方向键?
{
char ch;
while(kbhit()){
ch = getch();
if(ch == 'w')
return 2;
if(ch == 's')
return 3;
if(ch == 'a')
return 0;
if(ch == 'd')
return 1;
while (kbhit());
}
return 4;
}
int move(int idx)
{
int newx, newy, pt_tail, i;
newx = snake[head].x + dx[idx];
newy = snake[head].y + dy[idx];
switch (gamemap[newx][newy]){
case 0:
if ((head = ++head%SIZE_SK) != tail){ //不相等即蛇节点队列未满
snake[head].x = newx;
snake[head].y = newy;
snake[head].num = 1;
gamemap[newx][newy] = 1;
tail = ++tail%SIZE_SK;
snake[tail].num = 0; //清除尾部,打印空字符
gamemap[snake[tail].x][snake[tail].y] = 0;
snake[head].md = idx; //存储移动方向?
pt_tail = tail - 1;
}
else
return 2; //回合结束标记2代表蛇填满空间
break;
case 2:
if ((head = ++head%SIZE_SK) != tail){ //不相等即蛇节点队列未满
snake[head].x = newx; //head+1后存储新坐标为蛇头?
snake[head].y = newy;
snake[head].num = 1;
gamemap[newx][newy] = 1;
snake[head].md = idx; //存储移动方向
pt_tail = tail = tail%SIZE_SK;
score++;
createFood();
}
break;
case 3:
return 1; //遇到墙壁
case 1:
return 1; //遇到自身
default: ;
}
//打印蛇
if (pt_tail == -1)
pt_tail++;
if (pt_tail < head){ //尾巴在头部后面(这两者是数组索引)?
for (i=pt_tail; i<=head; i++){
pntSnake(i); //打印?
}
}
else{
for (i=pt_tail; i<SIZE_SK; i++)
pntSnake(i);
for (i=0; i<=head; i++)
pntSnake(i);
}
gameinfo(" 分数", score);
return 0;
}
void pntSnake(int i)
{
//转换x坐标:内部是连续整数,打印时一个字符占位2,所以要2x
pos.X = snake[i].x * 2;
pos.Y = snake[i].y;
SetConsoleCursorPosition(hout, pos);
if (snake[i].num == 0){
putchar('\0'); //清除蛇尾
putchar('\0');}
else if (snake[i].num == 1){
//下面函数第二个参数低4位控制前景色,高4位控制背景色,共8位?
SetConsoleTextAttribute(hout, snake[i].col);
printf("█");
}
}
void drawEdge(void)
{
int i;
for (i=0; i<W; i++){
pos.X = i*2; pos.Y = 0;
SetConsoleCursorPosition(hout, pos);
printf("█");
gamemap[i][pos.Y] = 3;
pos.Y = H-1;
SetConsoleCursorPosition(hout, pos);
printf("█");
gamemap[i][H-1] = 3;
}
for (i=1; i<H-1; i++){
pos.X = 0; pos.Y = i;
SetConsoleCursorPosition(hout, pos);
printf("█");
gamemap[pos.X][i] = 3;
pos.X = (W-1)*2;
SetConsoleCursorPosition(hout, pos);
printf("█");
gamemap[W-1][i] = 3;
}
}
void gameinfo(char *s, int n)
{
pos.X = 0; pos.Y = H;
SetConsoleCursorPosition(hout, pos);
SetConsoleTextAttribute(hout, 0x2f);
printf(" 贪吃蛇");
printf(" %s: %d", s, n);
}
第二款游戏:吃豆人
游戏介绍:
简介:吃豆人是电子游戏历史上的经典街机游戏,由Namco公司的岩谷彻设计并由Midway Games在1980年发行。《吃豆人》是易学难精的典型:控制吃豆人吃掉迷宫里面的所有豆子,同时尽可能躲避小鬼怪。一旦吃豆人吃掉能量药丸,它就可以在一定时间内反过来欺负小鬼怪了。特别值得一提的是,迷宫的左右出口是相通的,灵活应用往往带来柳暗花明的奇效。
上代码:
#include <cstdio>
#include <iostream>
#include <ctime>
#include <conio.h>
#include <windows.h> //停顿:Sleep();
#include <cstdlib> //清屏:system("cls");
#include <cstring>
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,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=0; //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(" 请将窗口开至"); color(11); printf("全屏\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("│");}
if (k%80==20){color(13); gotoxy(3,12); printf("╱");}
if (k%80==40){color(13); gotoxy(3,12); printf("─");}
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=='1' | ch=='j') 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=='2' | ch=='k') 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=='3' | ch=='l') 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=='5' | ch=='i') 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=='0' | ch=='s') stopped=(stopped+1)%2;
else if (ch=='4' | ch=='a') t1++;
else if (ch=='7' | ch=='q') t2++;
else if ((ch=='6' | ch=='d') && t1-1>0) t1--;
else if ((ch=='9' | ch=='e') && t2-1>0) t2--;
else if (ch=='g') dali=(dali+1)%2;
}
void reset(){
system("cls"); color(7);
gotoxy(2,30); printf("控制方向:1/2/3/5");
gotoxy(4,30); printf("你的速度:4/6");
gotoxy(6,30); printf("怪物速度:7/9");
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+=1; 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+=1; 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);
//gotoxy(15,28); printf("(%d,%d) ",x,y); gotoxy(16,28); printf("now:%d ",now); gotoxy(17,28); printf("%d (%d,%d) ",fangx,fxfx[fangx][0],fxfx[fangx][1]); gotoxy(18,28); printf("(%d,%d) changdi:%d ",xx,yy,changdi[xx][yy]);
}
void move2(){
int haha,minhaha,xx,yy,chi=0;
if (g1){
minhaha=2147483647;
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+=1;
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+k; 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+=1;
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+k; 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+=1;
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(){
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(" "); color(7);
while (!f){
Sleep(1);
gotoxy(10,30); printf("得分:%d ",fenshu);
gotoxy(8,30); printf("怪物速度:%d ",300-t2);
gotoxy(9,30); printf("你的速度:%d ",300-t1);
gotoxy(15,30); printf("被吃次数:%d ",beichi);
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");
printf("恭喜你吃完了!\n你一共被怪物吃掉了 %d 次",beichi);
Sleep(3000);
char ying;
scanf("%c",&ying);
}
}
第三款游戏:随机迷宫(简单)
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <time.h>
#define Height 25 //迷宫的高度,必须为奇数
#define Width 25 //迷宫的宽度,必须为奇数
#define Wall 1
#define Road 0
#define Start 2
#define End 3
#define Esc 5
#define Up 1
#define Down 2
#define Left 3
#define Right 4
int map[Height+2][Width+2];
void gotoxy(int x,int y) { //移动坐标
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition( GetStdHandle( STD_OUTPUT_HANDLE ), coord );
}
void hidden() { //隐藏光标
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(hOut,&cci);
cci.bVisible=0;//赋1为显示,赋0为隐藏
SetConsoleCursorInfo(hOut,&cci);
}
void create(int x,int y) { //随机生成迷
int c[4][2]= {0,1,1,0,0,-1,-1,0}; //四个方向
int i,j,t;
//将方向打乱
for(i=0; i<4; i++) {
j=rand()%4;
t=c[i][0];
c[i][0]=c[j][0];
c[j][0]=t;
t=c[i][1];
c[i][1]=c[j][1];
c[j][1]=t;
}
map[x][y]=Road;
for(i=0; i<4; i++)
if(map[x+2*c[i][0]][y+2*c[i][1]]==Wall) {
map[x+c[i][0]][y+c[i][1]]=Road;
create(x+2*c[i][0],y+2*c[i][1]);
}
}
int get_key() { //接收按键
char c;
while(c=getch()) {
if(c==27) return Esc; //Esc
if(c!=-32)continue;
c=getch();
if(c==72) return Up; //上
if(c==80) return Down; //下
if(c==75) return Left; //左
if(c==77) return Right; //右
}
return 0;
}
void paint(int x,int y) { //画迷宫
gotoxy(2*y-2,x-1);
switch(map[x][y]) {
case Start:
printf("入");
break; //画入口
case End:
printf("出");
break; //画出口
case Wall:
printf("▇");
break; //画墙
case Road:
printf(" ");
break; //画路
}
}
void game() {
int x=2,y=1; //玩家当前位置,刚开始在入口处
int c; //用来接收按键
while(1) {
gotoxy(2*y-2,x-1);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);
printf("▇▇"); //画出玩家当前位置
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_GREEN);
if(map[x][y]==End) { //判断是否到达出口
gotoxy(30,24);
printf("到达终点,按任意键结束");
getch();
break;
}
c=get_key();
if(c==Esc) {
gotoxy(0,24);
break;
}
switch(c) {
case Up: //向上走
if(map[x-1][y]!=Wall) {
paint(x,y);
x--;
}
break;
case Down: //向下走
if(map[x+1][y]!=Wall) {
paint(x,y);
x++;
}
break;
case Left: //向左走
if(map[x][y-1]!=Wall) {
paint(x,y);
y--;
}
break;
case Right: //向右走
if(map[x][y+1]!=Wall) {
paint(x,y);
y++;
}
break;
}
}
}
int main() {
int i,j;
srand((unsigned)time(NULL)); //初始化随即种子
hidden(); //隐藏光标
for(i=0; i<=Height+1; i++)
for(j=0; j<=Width+1; j++)
if(i==0||i==Height+1||j==0||j==Width+1) //初始化迷宫
map[i][j]=Road;
else map[i][j]=Wall;
create(2*(rand()%(Height/2)+1),2*(rand()%(Width/2)+1)); //从随机一个点开始生成迷宫,该点行列都为偶数
for(i=0; i<=Height+1; i++) { //边界处理
map[i][0]=Wall;
map[i][Width+1]=Wall;
}
for(j=0; j<=Width+1; j++) { //边界处理
map[0][j]=Wall;
map[Height+1][j]=Wall;
}
map[2][1]=Start; //给定入口
map[Height-1][Width]=End; //给定出口
for(i=1; i<=Height; i++)
for(j=1; j<=Width; j++) //画出迷宫
paint(i,j);
game(); //开始游戏
getch();
return 0;
}
第四款游戏:
#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);
}
}