以下是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("※"); }