当前位置:编程学习 > C/C++ >>

推箱子源代码 要C++的 最好体现出继承

补充:不要太复杂的。地图只要一两个就够啦!~~

答案:#include <graphics.h>
#ifndef __PUSHING_H //to avoid include pushing.h file twice
#define __PUSHING_H
void drawbackgroundblock(int x,int y);
void drawboxpos(int x,int y);
void drawwallblock(int x,int y);
void drawbox_2(int x,int y);
void drawbox_1(int x,int y);
void drawboy(int x,int y,int ready);
void drawpathunit(int x,int y,int h,int l);
void drawpath(int x,int y);

#endif


void drawbackgroundblock(int x,int y)
{
char checkerboard[8] = {
0xAA, /* 10101010 = ???? */
0x55, /* 01010101 = ???? */
0xAA, /* 10101010 = ???? */
0x55, /* 01010101 = ???? */
0xAA, /* 10101010 = ???? */
0x55, /* 01010101 = ???? */
0xAA, /* 10101010 = ???? */
0x55 /* 01010101 = ???? */
};
setfillpattern(checkerboard,LIGHTGRAY);
bar(x,y,x+23,y+23);
}
/* add your codes to define the function drawboxpos
and function drawwallblock based the prototype contained in pushing.h */
void drawboxpos(int x,int y)
{
char checkerboard[8] = {
0xAA, /* 10101010 = ???? */
0x55, /* 01010101 = ???? */
0xAA, /* 10101010 = ???? */
0x55, /* 01010101 = ???? */
0xAA, /* 10101010 = ???? */
0x55, /* 01010101 = ???? */
0xAA, /* 10101010 = ???? */
0x55 /* 01010101 = ???? */
};
setfillpattern(checkerboard,LIGHTGRAY);
bar(x,y,x+23,y+23);
setcolor(GREEN);
circle(x+11,y+11,8);
}

void drawwallblock(int x,int y)
{
setfillstyle(SOLID_FILL,RED);
bar(x,y,x+23,y+23);
setcolor(WHITE);
line(x,y,x+23,y);
line(x,y+7,x+23,y+7);
line(x,y+15,x+23,y+15);
line(x,y,x,y+7);
line(x+7,y+7,x+7,y+15);
line(x+15,y+15,x+15,y+23);
}

//add your codes up here

void drawbox_2(int x,int y)
{
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x,y,x+23,y+23);
setcolor(WHITE);
line(x,y+23,x,y);
line(x,y,x+23,y);
setcolor(DARKGRAY);
line(x+23,y,x+23,y+23);
line(x,y+23,x+23,y+23);
setfillstyle(SOLID_FILL,DARKGRAY);
bar(x+8,y,x+16,y+23);
bar(x,y+8,x+23,y+16);
}
void drawbox_1(int x,int y)
{
setfillstyle(SOLID_FILL,LIGHTGRAY);
bar(x,y,x+23,y+23);
setcolor(WHITE);
line(x,y+23,x,y);
line(x,y,x+23,y);
setcolor(DARKGRAY);
line(x+23,y,x+23,y+23);
line(x,y+23,x+23,y+23);
setfillstyle(SOLID_FILL,BLUE);
bar(x+8,y,x+16,y+23);
bar(x,y+8,x+23,y+16);

}
void drawboy(int x,int y,int ready)
{
drawbackgroundblock(x,y);
setfillstyle(SOLID_FILL,YELLOW);
setcolor(RED);
fillellipse(x+12,y+12,11,11);
if(ready)
{
line(x+4,y+4,x+10,y+8);
line(x+24-4,y+4,x+24-10,y+8);
line(x+6,y+24-4,x+12,y+24-10);
line(x+12,y+24-10,x+24-6,y+24-4);
}
else
{
line(x+4,y+5,x+10,y+5);
line(x+24-4,y+5,x+24-10,y+5);
arc(x+12,y+14,200,340,6);
}
}


#include <stdlib.h>
#include <graphics.h>
#include <bios.h>
#include <dos.h>
#include <conio.h>
#include <string.h>
#define UP 0X4800
#define DOWN 0X5000
#define LEFT 0X4B00
#define RIGHT 0X4D00
#ifndef __PUSHING_H //to avoid include pushing.h file twice
#define __PUSHING_H
void drawbackgroundblock(int x,int y);
void drawboxpos(int x,int y);
void drawwallblock(int x,int y);
void drawbox_2(int x,int y);
void drawbox_1(int x,int y);
void drawboy(int x,int y,int ready);
void drawpathunit(int x,int y,int h,int l);
void drawpath(int x,int y);

#endif

char path[20][20]=
{
" ### ",
" #O# ",
" # # ",
" # # ",
"###### # ",
"#O XX#####",
"#####8X O#",
" #X######",
" # # ",
" # # ",
" # # ",
" #O# ",
" ### "
};
int boy_h,boy_l,boxnum;
int path_h,path_l;
void drawpathunit(int x,int y,int h,int l)
{
switch(path[h][l])
{
case ' ':
drawbackgroundblock(x+l*24,y+h*24);
break;
case 'X':
drawbox_1(x+l*24,y+h*24);
break;
case 'x':
drawbox_2(x+l*24,y+h*24);
break;
case 'O':
drawboxpos(x+l*24,y+h*24);
break;
case '#':
drawwallblock(x+l*24,y+h*24);
break;
case 'o':
case '8':
if(path[h][l-1]=='X'||path[h][l+1]=='X'||
path[h-1][l]=='X'||path[h+1][l]=='X'||
path[h][l-1]=='x'||path[h][l+1]=='x'||
path[h-1][l]=='x'||path[h+1][l]=='x'
)
drawboy(x+l*24,y+h*24,1);
else
drawboy(x+l*24,y+h*24,0);
break;
}

}
void drawpath(int x,int y)
{
int h,l;
int i,j;
boxnum=0;
for(i=0;i<20;i++)
{
if(strlen(path[i])==0)
{
h=i;
break;
}
}
l=strlen(path[0]);
path_h=h;
path_l=l;
setcolor(WHITE);
rectangle(x-1,y-1,x+l*24+1,y+h*24+1);
for(i=0;i<h;i++)
for(j=0;j<l;j++)
switch(path[i][j])
{
case ' ':
drawbackgroundblock(x+j*24,y+i*24);
break;
case 'X':
drawbox_1(x+j*24,y+i*24);
boxnum++;
break;
case 'O':
drawboxpos(x+j*24,y+i*24);
break;
case '#':
drawwallblock(x+j*24,y+i*24);
break;
case '8':
if(path[i][j-1]=='X'||path[i][j+1]=='X'||
path[i-1][j]=='X'||path[i+1][j]=='X')
drawboy(x+j*24,y+i*24,1);
else
drawboy(x+j*24,y+i*24,0);
boy_h=i;
boy_l=j;
break;
}
}
void main()
{
int gdriver = DETECT, gmode, errorcode;
int ch,i,j;
int count;

initgraph(&gdriver, &gmode, "d:\\borlandc\\bgi");

drawpath(50,50);
while(1)
{
ch=bioskey(0);
if((ch&0x00ff)==27)break;
switch(ch)
{
case UP:
// add your codes down here
if((path[boy_h-1][boy_l]=='X'||path[boy_h-1][boy_l]=='x')
&&(path[boy_h-2][boy_l]==' '||path[boy_h-2][boy_l]=='O'))
//x--box+pos, o--boy+pos
{
if(path[boy_h-2][boy_l]=='O')
path[boy_h-2][boy_l]='x';
else
path[boy_h-2][boy_l]='X';
if(path[boy_h-1][boy_l]=='X')
path[boy_h-1][boy_l]='8';
else
path[boy_h-1][boy_l]='o';
if(path[boy_h][boy_l]=='8')
path[boy_h][boy_l]=' ';
else
path[boy_h][boy_l]='O';
for(i=0;i<3;i++)
drawpathunit(50,50,boy_h-i,boy_l);
boy_h--;
}
else if(path[boy_h-1][boy_l]==' '||path[boy_h-1][boy_l]=='O')
{
if(path[boy_h-1][boy_l]=='O')
path[boy_h-1][boy_l]='o';
else
path[boy_h-1][boy_l]='8';
if(path[boy_h][boy_l]=='o')
path[boy_h][boy_l]='O';
else
path[boy_h][boy_l]=' ';
for(i=0;i<2;i++)
drawpathunit(50,50,boy_h-i,boy_l);
boy_h--;
}

//add your codes up here
break;
case DOWN:
// add your codes down here
if((path[boy_h+1][boy_l]=='X'||path[boy_h+1][boy_l]=='x')
&&(path[boy_h+2][boy_l]==' '||path[boy_h+2][boy_l]=='O'))
//x--box+pos, o--boy+pos
{
if(path[boy_h+2][boy_l]=='O')
path[boy_h+2][boy_l]='x';
else
path[boy_h+2][boy_l]='X';
if(path[boy_h+1][boy_l]=='X')
path[boy_h+1][boy_l]='8';
else
path[boy_h+1][boy_l]='o';
if(path[boy_h][boy_l]=='8')
path[boy_h][boy_l]=' ';
else
path[boy_h][boy_l]='O';
for(i=0;i<3;i++)
drawpathunit(50,50,boy_h+i,boy_l);
boy_h++;
}
else if(path[boy_h+1][boy_l]==' '||path[boy_h+1][boy_l]=='O')
{

上一个:细解一下C++的数据类型(高分)!
下一个:Visual C++ 6.0下编程问题——猜数字

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,