這是我自己做的一個推箱子游戲,靈感來源于無意間在網上看到過類似的程序。從開始做到完善幾乎半個月左右
第一次做的時候只有簡單的推箱子,也沒用圖形化界面,箱子,人物等這些都是用一些符號代替
進過后面不斷改進,如今也加上了圖片,新增加了幾個關卡,同時每一個關卡都有完成本關的最高記錄
下面會附上一些圖片的代相關碼
2.png (53.37 KB, 下載次數: 57)
下載附件
2019-5-23 17:16 上傳
1.png (64.07 KB, 下載次數: 55)
下載附件
2019-5-23 17:16 上傳
單片機源程序如下:
- #include<stdio.h>
- #include<conio.h>
- #include<graphics.h> //圖形界面
- #include<iostream>
- #include<fstream>
- 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,
-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 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黑下載附件…………
復制代碼
所有資料51hei提供下載:
推箱子.zip
(12.61 KB, 下載次數: 26)
2019-5-23 17:19 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|