推箱子游戏c语言课程设计,C语言做的推箱子游戏源程序

#include

#include

#include //图形界面

#include

#include

using namespace std;

#define VK_SHIFT  0x10

#pragma warning(disable:4996)//使用sprintf时需要的头文件

int a, b, X, Y;

int g = 0;

int t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0;

int map2D[9][10] = {

9, 9, 1, 1, 1, 1, 1, 1, 1, 1 ,

9, 9, 1, 0, 0, 0, 1, 2, 0, 1 ,

9, 1, 1, 0, 0, 3, 2, 2, 2, 1 ,

9, 1, 0, 0, 3, 0, 1, 5, 2, 1 ,

1, 1, 0, 1, 1, 3, 1, 0, 1, 1 ,

1, 0, 0, 0, 3, 0, 0, 3, 0, 1 ,

1, 0, 0, 0, 1, 0, 0, 0, 0, 1 ,

1, 1, 1, 1, 1, 1, 1, 4, 0, 1 ,

9, 9, 9, 9, 9, 9, 1, 1, 1, 1

}; //0空地,1墙,2目的地,4和6人物(玩家),3箱子,9背景

int mapSecond[9][10] = {

9, 9, 1, 1, 1, 1, 1, 1, 1, 1,

9, 9, 1, 0, 0, 0, 1, 2, 0, 1,

9, 1, 1, 0, 0, 3, 2, 2, 2, 1,

9, 1, 0, 0, 3, 0, 1, 5, 2, 1,

1, 1, 0, 1, 1, 3, 1, 0, 1, 1,

1, 0, 0, 0, 3, 0, 0, 3, 0, 1,

1, 0, 0, 0, 1, 0, 0, 0, 0, 1,

1, 1, 1, 1, 1, 1, 1, 4, 0, 1,

9, 9, 9, 9, 9, 9, 1, 1, 1, 1

};

int mapSecond1[9][10] = {

1, 1, 1, 1, 1, 1, 1, 1, 9, 9,

1, 0, 0, 1, 0, 0, 0, 1, 9, 9,

1, 0, 3, 2, 2, 3, 0, 1, 9, 9,

1, 4, 3, 2, 5, 0, 1, 1, 9, 9,

1, 0, 3, 2, 2, 3, 0, 1, 9, 9,

1, 0, 0, 1, 0, 0, 0, 1, 9, 9,

1, 1, 1, 1, 1, 1, 1, 1, 9, 9,

9, 9, 9, 9, 9, 9, 9, 9, 9, 9,

9, 9, 9, 9, 9, 9, 9, 9, 9, 9

};

int mapSecond2[9][10] = {

9, 9, 9, 9, 9, 9, 9, 9, 9, 9,

9, 9, 9, 1, 1, 1, 1, 9, 9, 9,

9, 9, 1, 1, 0, 0, 1, 9, 9, 9,

9, 9, 1, 4, 3, 0, 1, 9, 9, 9,

9, 9, 1, 1, 3, 0, 1, 1, 9, 9,

9, 9, 1, 1, 0, 3, 0, 1, 9, 9,

9, 9, 1, 2, 3, 0, 0, 1, 9, 9,

9, 9, 1, 2, 2, 5, 2, 1, 9, 9,

9, 9, 9, 9, 9, 9, 9, 9, 9, 9

};

int mapSecond3[9][10] = {

9, 9, 1, 1, 1, 1, 1, 1, 9, 9,

9, 9, 1, 2, 0, 2, 2, 1, 9, 9,

9, 9, 1, 2, 0, 3, 2, 1, 9, 9,

9, 1, 1, 1, 0, 0, 3, 1, 1, 9,

9, 1, 0, 3, 0, 0, 3, 0, 1, 9,

9, 1, 0, 1, 3, 1, 1, 0, 1, 9,

9, 1, 0, 0, 0, 4, 0, 0, 1, 9,

9, 1, 1, 1, 1, 1, 1, 1, 1, 9,

9, 9, 9, 9, 9, 9, 9, 9, 9, 9

};

int mapSecond4[9][10] = {

1, 1, 1, 1, 1, 9, 9, 9, 9, 9,

1, 0, 0, 0, 1, 1, 1, 1, 1, 9,

1, 0, 1, 0, 1, 0, 0, 0, 1, 9,

1, 0, 3, 0, 0, 0, 3, 0, 1, 9,

1, 2, 2, 1, 3, 1, 3, 1, 1, 9,

1, 2, 4, 3, 0, 0, 0, 1, 9, 9,

1, 2, 2, 0, 0, 1, 1, 1, 9, 9,

1, 1, 1, 1, 1, 1, 9, 9, 9, 9,

9, 9, 9, 9, 9, 9, 9, 9, 9, 9

};

void repeat(){

if (t1 == 1){

for (int i = 0; i < 9; i++){

for (int j = 0; j < 10; j++){

map2D[i][j] = mapSecond[i][j];

}

}

}

}

void repeat1(){

if (t2 == 1){

for (int i = 0; i < 9; i++){

for (int j = 0; j < 10; j++){

map2D[i][j] = mapSecond1[i][j];

}

}

}

}

void repeat2(){

if (t3 == 1){

for (int i = 0; i < 9; i++){

for (int j = 0; j < 10; j++){

map2D[i][j] = mapSecond2[i][j];

}

}

}

}

void repeat3(){

if (t4 == 1){

for (int i = 0; i < 9; i++){

for (int j = 0; j < 10; j++){

map2D[i][j] = mapSecond3[i][j];

}

}

}

}

void repeat4(){

if (t5 == 1){

for (int i = 0; i < 9; i++){

for (int j = 0; j < 10; j++){

map2D[i][j] = mapSecond4[i][j];

}

}

}

}

void initmap()

{

IMAGE map[7];

char filename[20] = "";

for (int i = 0; i < 7; i++)

{

sprintf_s(filename, "素材/%d.jpg", i);

loadimage(&map[i], filename, 20, 20);

}

int x, y;

for (int rowNum = 0; rowNum < 9; rowNum++)

{

for (int colNum = 0; colNum < 10; colNum++)

{

x = colNum * 20+100;

y = rowNum * 20+60;

putimage(x, y, &map[map2D[rowNum][colNum]]);

}

}

}

void playGame()

{

//a为行,b列

for (int i = 0; i < 9; i++){

for (int j = 0; j < 10; j++){

if (map2D[i][j] == 4 || map2D[i][j] == 6){

a = i;

b = j;

X = a;

Y = b;

}

}

}

char ch;

ch = getch();

switch (ch)

{

case 'T':

case 't':

g = 1;

break;

//0空地,1墙,2目的地,4人物(玩家),3箱子

case 'W':

case 'w':

case 72:

if (map2D[a - 1][b] == 0)

{

map2D[a - 1][b] += 4;

map2D[a][b] -= 4;

}

else if (map2D[a - 1][b] == 2)

{

map2D[a - 1][b] += 4;

map2D[a][b] -= 4;

}

else if (map2D[a - 1][b] == 3 || map2D[a - 1][b] == 5)//人前面是箱子或者箱子+目的地

{

if (map2D[a - 1][b] == 3)

{

if (map2D[a - 2][b] == 0)

{

map2D[a - 2][b] += 3;

map2D[a - 1][b] += 1;

map2D[a][b] -= 4;

}

else if (map2D[a - 2][b] == 2)

{

map2D[a - 2][b] += 3;

map2D[a - 1][b] += 1;

map2D[a][b] -= 4;

}

}

else//前面是5

{

if (map2D[a - 2][b] == 0)

{

map2D[a - 2][b] += 3;

map2D[a - 1][b] += 1;

map2D[a][b] -= 4;

}

else if (map2D[a - 2][b] == 2)

{

map2D[a - 2][b] += 3;

map2D[a - 1][b] += 1;

map2D[a][b] -= 4;

}

}

}

for (int i = 0; i < 9; i++){

for (int j = 0; j < 10; j++){

if (map2D[i][j] == 4 || map2D[i][j] == 6){

a = i;

b = j;

}

}

}

break;

case 'S':

case 's':

case 80:

if (map2D[a + 1][b] == 0)

{

map2D[a + 1][b] += 4;

map2D[a][b] -= 4;

}

else if (map2D[a + 1][b] == 2)

{

map2D[a + 1][b] += 4;

map2D[a][b] -= 4;

}

else if (map2D[a + 1][b] == 3 || map2D[a + 1][b] == 5)//人前面是箱子或者箱子+目的地

{

if (map2D[a + 1][b] == 3)

{

if (map2D[a + 2][b] == 0)

{

map2D[a + 2][b] += 3;

map2D[a + 1][b] += 1;

map2D[a][b] -= 4;

}

else if (map2D[a + 2][b] == 2)

{

map2D[a + 2][b] += 3;

map2D[a + 1][b] += 1;

map2D[a][b] -= 4;

}

}

else

{

if (map2D[a + 2][b] == 0)

{

map2D[a + 2][b] += 3;

map2D[a + 1][b] += 1;

map2D[a][b] -= 4;

}

else if (map2D[a + 2][b] == 2)

{

map2D[a + 2][b] += 3;

map2D[a + 1][b] += 1;

map2D[a][b] -= 4;

}

}

}

for (int i = 0; i < 9; i++){

for (int j = 0; j < 10; j++){

if (map2D[i][j] == 4 || map2D[i][j] == 6){

a = i;

b = j;

}

}

}

break;

case 'A':

case 'a':

case 75:

if (map2D[a][b - 1] == 0)

{

map2D[a][b - 1] += 4;

map2D[a][b] -= 4;

}

else if (map2D[a][b - 1] == 2)

{

map2D[a][b - 1] += 4;

map2D[a][b] -= 4;

}

else if (map2D[a][b - 1] == 3 || map2D[a][b - 1] == 5)//人前面是箱子或者箱子+目的地

{

if (map2D[a][b - 1] == 3)

{

if (map2D[a][b - 2] == 0)

{

map2D[a][b - 2] += 3;

map2D[a][b - 1] += 1;

map2D[a][b] -= 4;

}

else if (map2D[a][b - 2] == 2)

{

map2D[a][b - 2] += 3;

map2D[a][b - 1] += 1;

map2D[a][b] -= 4;

}

}

else

{

if (map2D[a][b - 2] == 0)

{

map2D[a][b - 2] += 3;

map2D[a][b - 1] += 1;

map2D[a][b] -= 4;

}

else if (map2D[a][b - 2] == 2)

{

map2D[a][b - 2] += 3;

map2D[a][b - 1] += 1;

map2D[a][b] -= 4;

}

}

}

for (int i = 0; i < 9; i++){

for (int j = 0; j < 10; j++){

if (map2D[i][j] == 4 || map2D[i][j] == 6){

a = i;

b = j;

}

}

}

break;

case 'D':

case 'd':

case 77:

if (map2D[a][b + 1] == 0)

{

map2D[a][b + 1] += 4;

map2D[a][b] -= 4;

}

else if (map2D[a][b + 1] == 2)

{

map2D[a][b + 1] += 4;

map2D[a][b] -= 4;

}

else if (map2D[a][b + 1] == 3 || map2D[a][b + 1] == 5)//人前面是箱子或者箱子+目的地

{

if (map2D[a][b + 1] == 3)

{

if (map2D[a][b + 2] == 0)

{

map2D[a][b + 2] += 3;

map2D[a][b + 1] += 1;

map2D[a][b] -= 4;

}

else if (map2D[a][b + 2] == 2)

{

map2D[a][b + 2] += 3;

map2D[a][b + 1] += 1;

map2D[a][b] -= 4;

}

}

else

{

if (map2D[a][b + 2] == 0)

{

map2D[a][b + 2] += 3;

map2D[a][b + 1] += 1;

map2D[a][b] -= 4;

}

else if (map2D[a][b + 2] == 2)

{

map2D[a][b + 2] += 3;

map2D[a][b + 1] += 1;

map2D[a][b] -= 4;

}

}

}

for (int i = 0; i < 9; i++){

for (int j = 0; j < 10; j++){

if (map2D[i][j] == 4 || map2D[i][j] == 6){

a = i;

b = j;

}

}

}

break;

}

}

int main()

{

int dat, I;

int P = 6;//过关条件

t1 = 1;

char q[20] = "";

int w[5] = { 0 };//存放最高纪录

char m[20] = "";

int d, h;

xin:

dat = 0, I = 0, a = 0, b = 0;

if (t1 == 1 || t2 == 1) {

P = 6;

}

else if (t3 == 1 || t4 == 1 || t5 == 1){

P = 5;

}

d = 0, h = 0;//d为当前步数,h为最高步数

initgraph(400, 550);

repeat();

repeat1();

repeat2();

repeat3();

repeat4();

for (int i = 0; i < 2000; i++){

cleardevice();//清屏

int R = 0;

outtextxy(28, 0, "注:1.开始游戏前需要点击shift键后才能使用;");

outtextxy(60, 20, "2.不可以悔棋,按T键可以重新开始本关卡。");

sprintf(m, "当前已经走了%d步", I);

outtextxy(130, 300, m);

for (int k = 0; k < 9; k++)

{

for (int j = 0; j < 10; j++)

{

if (map2D[k][j] == 5){

R = R + 1;

}

}

}

initmap();

d = i;

if (R == P)

break;

playGame();

if (a != X||b != Y){  //X,Y为玩家当前位置的上一个位置的坐标

I++;

}

if (g == 1){

g = 0;

goto xin;

}

}

outtextxy(130, 320, "恭喜过关!");

if (t1 == 1){

dat = 0;

ifstream infile("file/xin.dat", ios::in);

if (!infile){

}

else{

infile >> w[dat];

h = w[dat];

}

infile.close();

if (h == 0 || d < h) {

ofstream outfile("file/xin.dat", ios::out);

h = d;

w[dat] = h;

outfile << w[dat];

outfile.close();

}

}

else if (t2 == 1){

dat = 1;

ifstream infile("file/xin1.dat", ios::in);

if (!infile){

}

else{

infile >> w[dat];

h = w[dat];

}

infile.close();

if (h == 0 || d < h) {

ofstream outfile("file/xin1.dat", ios::out);

h = d;

w[dat] = h;

outfile << w[dat];

outfile.close();

}

}

else if (t3 == 1){

dat = 2;

ifstream infile("file/xin2.dat", ios::in);

if (!infile){

}

else{

infile >> w[dat];

h = w[dat];

}

infile.close();

if (h == 0 || d < h) {

ofstream outfile("file/xin2.dat", ios::out);

h = d;

w[dat] = h;

outfile << w[dat];

outfile.close();

}

}

else if (t4 == 1){

dat = 3;

ifstream infile("file/xin3.dat", ios::in);

if (!infile){

}

else{

infile >> w[dat];

h = w[dat];

}

infile.close();

if (h == 0 || d < h) {

ofstream outfile("file/xin3.dat", ios::out);

h = d;

w[dat] = h;

outfile << w[dat];

outfile.close();

}

}

else if (t5 == 1){

dat = 4;

ifstream infile("file/xin4.dat", ios::in);

if (!infile){

}

else{

infile >> w[dat];

h = w[dat];

}

infile.close();

if (h == 0 || d < h) {

ofstream outfile("file/xin4.dat", ios::out);

h = d;

w[dat] = h;

outfile << w[dat];

outfile.close();

}

}

//ifstream infile("file/xin.dat", ios::in);

//if (!infile){

//}

//else{

//        infile >> w[dat];

//        h = w[dat];

//}

//infile.close();

//if (h == 0 || d < h) {

//        ofstream outfile("file/xin.dat", ios::out);

//        h = d;

//        w[dat] = h;

//        outfile << w[dat];

//        outfile.close();

//}

sprintf(q, "当前关卡最高纪录为%d步", w[dat]);

outtextxy(130, 350, q);

outtextxy(130, 400, "是否重新挑战?(Y/N)");

char ch, cha;

ch = getch();

switch (ch){

case 'Y':

case 'y':

outtextxy(130, 420, "加载中......");

Sleep(1500);

goto xin;

break;

default:

break;

}

outtextxy(80, 420, "请选择关卡(1,2,3,4,5,0[退出])");

faulse:

cha = getch();

switch (cha){

case '1':

t1 = 0; t2 = 0, t3 = 0, t4 = 0, t5 = 0;

t1 = 1;

goto xin;

break;

case '2':

t1 = 0; t2 = 0, t3 = 0, t4 = 0, t5 = 0;

t2 = 1;

goto xin;

break;

case '3':

t1 = 0; t2 = 0, t3 = 0, t4 = 0, t5 = 0;

t3 = 1;

goto xin;

break;

case '4':

t1 = 0; t2 = 0, t3 = 0, t4 = 0, t5 = 0;

t4 = 1;

goto xin;

break;

case '5':

t1 = 0; t2 = 0, t3 = 0, t4 = 0, t5 = 0;

t5 = 1;

goto xin;

break;

case '0':

outtextxy(150, 450, "按enter键继续退出!");

break;

……………………

…………限于本文篇幅 余下代码请从51黑下载附件…………

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值