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;//過關(guān)條件
t1 = 1;
char q[20] = "";
int w[5] = { 0 };//存放最高紀(jì)錄
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為當(dāng)前步數(shù),h為最高步數(shù)
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鍵可以重新開始本關(guān)卡。");
sprintf(m, "當(dāng)前已經(jīng)走了%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為玩家當(dāng)前位置的上一個位置的坐標(biāo)
I++;
}
if (g == 1){
g = 0;
goto xin;
}
}
outtextxy(130, 320, "恭喜過關(guān)!");
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āng)前關(guān)卡最高紀(jì)錄為%d步", w[dat]);
outtextxy(130, 350, q);
outtextxy(130, 400, "是否重新挑戰(zhàn)?(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, "請選擇關(guān)卡(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鍵繼續(xù)退出!");
break;
……………………
…………限于本文篇幅 余下代碼請從51黑下載附件…………
總結(jié)
以上是生活随笔為你收集整理的c语言 游戏程序,C语言做的推箱子游戏源程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux时钟告警,Linux记录-告警
- 下一篇: matlab调用c语言驱动函数,[转载]