當前位置:首頁 » 游戲問答 » 推箱子小游戲代碼多少

推箱子小游戲代碼多少

發布時間: 2022-05-02 12:37:44

① 誰有推箱子的C語言游戲代碼

#include <dos.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <bios.h>
#include <alloc.h>
/* 定義二維數組ghouse來記錄屏幕上各點的狀態,
其中:0表示什麼都沒有,'b'表示箱子,'w'表示牆壁,'m'表示目的地,'i'表示箱子在目的地。 */
char ghouse[20][20];/* 以下函數為直接寫屏函數,很酷的函數哦!是我朋友告訴我的。 */
char far *screen=(char far* )0xb8000000;
void putchxy(int y,int x,char ch,char fc,char bc)
{
screen[(x*160)+(y<<1)+0]=ch;
screen[(x*160)+(y<<1)+1]=(bc*16)+fc;
}/* 定義判斷是否勝利的數據結構 */
typedef struct winer {
int x,y;
struct winer *p;
}winer;/* 箱子位置的數據結構 */
typedef struct boxs {
int x,y;
struct boxs *next;
}boxs;/* 在特定的坐標上畫牆壁並用數組記錄狀態的函數 */
void printwall(int x,int y)
{
putchxy(y-1,x-1,219,MAGENTA,BLACK);
ghouse[x][y]='w';
}/* 在特定的坐標上畫箱子並用數組記錄狀態的函數 */
void printbox(int x,int y)
{
putchxy(y-1,x-1,10,WHITE,BLACK);
ghouse[x][y]='b';
}/* 在特定的坐標上畫目的地並用數組記錄狀態的函數 */
void printwhither1(int x,int y,winer **win,winer **pw)
{
winer *qw;
putchxy(y-1,x-1,'*',YELLOW,BLACK);
ghouse[x][y]='m';
if(*win==NULL)
{
*win=*pw=qw=(winer* )malloc(sizeof(winer));
(*pw)->x=x;(*pw)->y=y;(*pw)->p=NULL;
}
else
{
qw=(winer* )malloc(sizeof(winer));
qw->x=x;qw->y=y;(*pw)->p=qw;(*pw)=qw;qw->p=NULL;
}
}
/* 在特定的坐標上畫目的地並用數組記錄狀態的函數 */
void printwhither(int x,int y)
{
putchxy(y-1,x-1,'*',YELLOW,BLACK);
ghouse[x][y]='m';
}
/* 在特定的坐標上畫人的函數 */
void printman(int x,int y)
{
gotoxy(y,x);
_AL=02;_CX=01;_AH=0xa;
geninterrupt(0x10);
}/* 在特定的坐標上畫箱子在目的地上並用數組記錄狀態的函數 */
void printboxin(int x,int y)
{
putchxy(y-1,x-1,10,YELLOW,BLACK);
ghouse[x][y]='i';
}/* 初始化函數,初始化數組和屏幕 */
void init()
{
int i,j;

clrscr();
for(i=0;i<20;i++)
for(j=0;j<20;j++)
ghouse[i][j]=0;
_AL=3;
_AH=0;
geninterrupt(0x10);
gotoxy(40,4);
printf("Welcome to push box world!");
gotoxy(40,6);
printf("Press up,down,left,right to play.");
gotoxy(40,8);
printf("Press Esc to quit it.");
gotoxy(40,10);
printf("Press space to reset the game.");
gotoxy(40,12);
printf("April 30th 2004.");
}/* 第一關的圖象初始化 */
winer *inithouse1()
{

int x,y;
winer *win=NULL,*pw;
gotoxy(8,2);
printf("Level No.1");
for(x=1,y=5;y<=9;y++)
printwall(x+4,y+10);
for(y=5,x=2;x<=5;x++)
printwall(x+4,y+10);
for(y=9,x=2;x<=5;x++)
printwall(x+4,y+10);
for(y=1,x=3;x<=8;x++)
printwall(x+4,y+10);
for(x=3,y=3;x<=5;x++)
printwall(x+4,y+10);
for(x=5,y=8;x<=9;x++)
printwall(x+4,y+10);
for(x=7,y=4;x<=9;x++)
printwall(x+4,y+10);
for(x=9,y=5;y<=7;y++)
printwall(x+4,y+10);
for(x=8,y=2;y<=3;y++)
printwall(x+4,y+10);
printwall(5+4,4+10);
printwall(5+4,7+10);
printwall(3+4,2+10);
printbox(3+4,6+10);
printbox(3+4,7+10);
printbox(4+4,7+10);
printwhither1(4+4,2+10,&win,&pw);
printwhither1(5+4,2+10,&win,&pw);
printwhither1(6+4,2+10,&win,&pw);
printman(2+4,8+10);
return win;
}

② 求推箱子小游戲的VB代碼

網路「推箱子 dim」

③ Java推箱子怎麼寫啊

這是我之前寫的一篇java實現推箱子演算法的文章,簡單的給你看一下:

《推箱子游戲》是一款益智游戲,游戲目標是搬運工自己來找出到某個位置的最短路徑,然後自己走過去。

最後完成地圖顯示問題,每個節點存儲自己父親節點的地址,當節點發現自己已經完成之後根據地址向上查找直到樹頂,望採納,謝謝。

④ 本人需要用JAVA編寫一個推箱子的小游戲 源代碼有了 但是缺少做推箱子這小游戲的圖片

package com.txz1;
import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Txz {
public static void main(String[] args) {
JFrame frame = new JFrame("������1.0");//����
frame.setBounds(100, 20, 20 * 48 + 16, 14 * 48 + 38);//���ý���λ�úʹ�С

JPanel panel = new JPanel();//��壬�൱������
panel.setBackground(Color.red);//������ɫ
frame.setContentPane(panel);
panel.setLayout(null);//�Զ��壬���λ��
//�������
ImageIcon boxImg = new ImageIcon("box2.PNG");
JLabel box = new JLabel(boxImg);//��ǩ���൱������
panel.add(box);
box.setBounds(5 * 48, 3 * 48, 48, 48);
//��ӹ���
ImageIcon workerImg = new ImageIcon("workerDown2.png");
JLabel worker = new JLabel(workerImg);
panel.add(worker);
worker.setBounds(8 * 48, 8 * 48, 48, 48);
//����ϰ���
ImageIcon goalImg = new ImageIcon("goal2.png");
JLabel goal = new JLabel(goalImg);
panel.add(goal);
goal.setBounds(7 * 48, 6 * 48, 48, 48);
//���Χǽ
ImageIcon wallImg = new ImageIcon("wall2.PNG");
JLabel[] walls = new JLabel[100];

for (int i = 0; i < walls.length; i++) {
walls[i] = new JLabel(wallImg);
}
int index = 0;//������������ͳ��Χǽ����
for (int i = 0; i < 20; i++) {
panel.add(walls[index]);
walls[index].setBounds(i * 48, 0, 48, 48);
index++;
panel.add(walls[index]);
walls[index].setBounds(i * 48, 13 * 48, 48, 48);
index++;
}
for (int i = 0; i < 12; i++) {
panel.add(walls[index]);
walls[index].setBounds(0, (i + 1) * 48, 48, 48);
index++;
panel.add(walls[index]);
walls[index].setBounds(19 * 48, (i + 1) * 48, 48, 48);
index++;
}
for (int i = 0, j = 0; i < 5; i++, j += 2) {
panel.add(walls[index]);
walls[index].setBounds((i + 2) * 48, j * 48, 48, 48);
index++;
}
for (int i = 0, j = 10; i < 5; i++, j--) {
panel.add(walls[index]);
walls[index].setBounds((i + 12) * 48, j * 48, 48, 48);
index++;
}
//����Ϊ�ɼ��
frame.setVisible(true);
}
}

⑤ 用匯編語言編寫一個推箱子游戲

出來了,代碼如下:
data segment
car db 2,2,2,2,2,2,2,2,2,2
db 2,2,2,2,2,2,0,0,0,2
db 2,2,2,2,2,2,0,0,0,2
db 2,2,2,2,2,2,0,0,0,2
db 2,2,2,2,2,2,2,2,2,2
db 2,2,2,2,2,2,2,2,2,2
db 0,0,2,0,0,0,0,2,0,0
db 0,2,0,2,0,0,2,0,2,0
db 0,0,2,0,0,0,0,2,0,0
x dw 10
y dw 10
msg db 'car program finish!!$'
speed dw 5
data ends

code segment
assume cs:code,ds:data
start:
mov ax,data
mov ds,ax
mov ax,0a000h
mov es,ax
mov ax,13h
int 10h
call picture
ag: mov ah,7
int 21h
cmp al,'s'
jne ag
start1: in al,60h
cmp al,80h ;if release key
jae keyrelease
cmp al,2 ;1 key
jz upspeed
cmp al,3 ;2 key
jz downspeed
jmp keyrelease
upspeed:
mov si,speed
inc si
mov speed,si
jmp keyrelease
downspeed:
mov si,speed
dec si
mov speed,si
jmp keyrelease
keyrelease:
call clearpicture
mov si,speed
add si,x
mov x,si
call picture
call delay
mov si,x
cmp si,310
jc start1
mov ah,9
mov dx,offset msg
int 21h
mov ah,7
int 21h
mov ax,4c00h
int 21h

picture proc near
mov dx,y
mov bx,0
again: cmp dx,0
je over
add bx,320
dec dx
jmp again
over: mov dx,9
lea si,car
next1: mov cx,10
mov di,x
next: mov al,[si]
mov es:[bx+di],al
inc si
inc di
loop next
add bx,320
dec dx
jne next1
ret
picture endp

clearpicture proc near
mov dx,y
mov bx,0
again2: cmp dx,0
je over2
add bx,320
dec dx
jmp again2
over2: mov dx,9
next12: mov cx,10
mov di,x
next2: mov al,0
mov es:[bx+di],al
inc di
loop next2
add bx,320
dec dx
jne next12
ret
clearpicture endp

delay proc near
push bx
push cx
mov bx,1234
back:
mov cx,66
push ax
wait1:
in al,61h
and al,10h
cmp al,ah
je wait1
mov ah,al
loop wait1
pop ax
dec bx
jne back
pop cx
pop bx
ret
delay endp

code ends
end start

⑥ 求個推箱子的C++代碼 要有註解 800到1000行



//*******************************************************
Sokoban.h:類定義Sokoban.c:類成員函數實現
Use_Sokoban.c:主函數
請用VC6(別編譯器的也行)先運行Use_Sokoban.c文件,要編譯該文件一下,
再點Project->AddToProject->Files選擇Sokoban.c文件,
即將Sokoban.c載入到工程里,最後運行就OK拉。

//*******************************************************
Sokoban.h
//*******************************************************
#ifndefSOKOBAN_H_//防止文件重復包含
#defineSOKOBAN_H_
#include<queue>
usingstd::queue;
//每一步的數據類型
structnode
{
intbx,by;//箱子的坐標
intpx,py;//人的坐標
};
//推箱子類
classSokoban
{
private:
enum{L=15,H=7};
charGameMap[H][L];//地圖
intPex,Pey;//人的位置
intBoxx,Boxy;//箱子的位置
intSucceed,Prove;//是否成功到目的地,是否可玩性
intdx[4],dy[4];//方向數組
protected:
charEmpty;
charPeople;
charBox;
charBlock;
charTarget;
intdir;//記錄按鍵方向
nodes,e;
public:
Sokoban();//構建函數
~Sokoban(){}//析構函數,即為inline
//地圖初始化函數
voidInitial();
//箱子路勁驗證函數,參數為箱子坐標(bx,by),人坐標(px,py)
voidBox_Bfs(intbx,intby,intpx,intpy);
//人路勁驗證函數,人所到的目的地(ex,ey)
boolPeople_Bfs(intex,intey);
//地圖刷新函數
voidShow();
//按鍵判斷函數
voidButton();
//箱子人移動函數
voidMove();
//驗證越界函數
boolCheck(intx,inty);
};
#endif
//*******************************************************
Sokoban.cpp
//*******************************************************
#include"Sokoban.h"
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<iostream>
#include<conio.h>
usingstd::cout;
usingstd::endl;
Sokoban::Sokoban()//構建函數即對變數初始化
{
dir=-1;
Succeed=Prove=0;
memset(GameMap,'.',sizeof(GameMap));
Empty='.';
People='P';
Box='#';
Block='*';
Target='T';
//方向依次為上右下左
dx[0]=-1;dx[1]=0;dx[2]=1;dx[3]=0;
dy[0]=0;dy[1]=1;dy[2]=0;dy[3]=-1;
//隨機種子,使程序每次運行時所產生的隨機數不同
srand(time(0));
}
//地圖初始化函數
voidSokoban::Initial()
{
intcount=0,x,y;
//對地圖中隨機產生25個阻礙物
while(count!=25)
{
x=rand()%H;
y=rand()%L;
if(GameMap[x][y]==Empty)
{
GameMap[x][y]=Block;
count++;
}
}
while(true)//隨機產生人開始的位置
{
x=rand()%H;
y=rand()%L;
if(GameMap[x][y]==Empty)
{
GameMap[x][y]=People;
Pex=x;
Pey=y;
break;
}
}
while(true)//隨機產生箱子開始的位置
{
x=rand()%H;
y=rand()%L;
//不讓箱子在地圖的邊界處
if(GameMap[x][y]==Empty&&x!=0&&y!=0
&&x!=H-1&&y!=L-1)
{
GameMap[x][y]=Box;
Boxx=x;
Boxy=y;
break;
}
}
while(true)//隨機產生目標的位置
{
x=rand()%H;
y=rand()%L;
if(GameMap[x][y]==Empty)
{
GameMap[x][y]=Target;
break;
}
}
//對游戲地圖檢查是否可將箱子推到目的地,即判斷游戲可玩性
Sokoban::Box_Bfs(Boxx,Boxy,Pex,Pey);
//如游戲不可玩,即再隨機產生地圖
if(!Prove)
{
memset(GameMap,'.',sizeof(GameMap));
Sokoban::Initial();
}
else
Sokoban::Show();
}
//箱子路勁驗證函數
//用BFS演算法對箱子驗證是否可到目的地
voidSokoban::Box_Bfs(intbx,intby,intpx,intpy)
{
queue<node>_Box;//創建箱子隊列
//visit對上一步走到下一步的記錄,防止箱子走重復路勁
//visit[i][j][z][k]表示箱子從點(i,j)到點(z,k)
//visit[][][][]為0時表示為走過,1時表示已走過
intvisit[H][L][H][L];

memset(visit,0,sizeof(visit));//visit數組初始化
s.bx=bx;s.by=by;//將起始的箱子、人位置放入隊列
s.px=px;s.py=py;
_Box.push(s);
intpe_x,pe_y;
while(!_Box.empty())//隊列為空時跳出
{
s=_Box.front();
_Box.pop();

if(GameMap[s.bx][s.by]==Target)//到達目的地
{
Prove=1;
break;
}
for(inti=0;i<4;i++)
{
e.bx=s.bx+dx[i];e.by=s.by+dy[i];

switch(i)//人推箱子的位置
{
case0:pe_x=s.bx+dx[2];pe_y=s.by+dy[2];break;
case1:pe_x=s.bx+dx[3];pe_y=s.by+dy[3];break;
case2:pe_x=s.bx+dx[0];pe_y=s.by+dy[0];break;
case3:pe_x=s.bx+dx[1];pe_y=s.by+dy[1];break;
}

//驗證箱子和人的位置的合法性
if(!Check(e.bx,e.by)||!Check(pe_x,pe_y)
||GameMap[e.bx][e.by]==Block||GameMap[pe_x][pe_y]==Block
||visit[s.bx][s.by][e.bx][e.by])
continue;

//如人可推箱子即進入隊列
if(Sokoban::People_Bfs(pe_x,pe_y))
{
//保存人推箱子後的位置
e.px=pe_x;e.py=pe_y;
_Box.push(e);
visit[s.bx][s.by][e.bx][e.by]=1;//箱子路勁的標記
}
}
}
}

//人路勁驗證函數
//用BFS演算法對人驗證是否可推箱子
boolSokoban::People_Bfs(intex,intey)
{
queue<node>_People;
nodet,end;
//visit數組對人的路勁進行標記,0為未走過,1為走過
intvisit[H][L];
//visit數組初始化為0
memset(visit,0,sizeof(visit));
t.px=s.px;t.py=s.py;//人初始位置進入隊列
_People.push(t);
visit[t.px][t.py]=1;

while(!_People.empty())//對立為空時跳出
{
t=_People.front();
_People.pop();
if(t.px==ex&&t.py==ey)//人可到達(ex,ey)該點
return1;
for(inti=0;i<4;i++)
{
end.px=t.px+dx[i];end.py=t.py+dy[i];
//檢查人的位置合法性
if(!Check(end.px,end.py)||GameMap[end.px][end.py]==Block
||GameMap[end.px][end.py]==Box||visit[end.px][end.py])
continue;
//進入隊列
_People.push(end);
visit[end.px][end.py]=1;//記錄
}
}
return0;
}

//地圖刷新函數
voidSokoban::Show()
{
inti,j;
while(true)
{
//每半秒刷新一次地圖
clock_ts=clock();
while(clock()-s<CLOCKS_PER_SEC/2)
;
//先判斷按鍵在移動
Sokoban::Button();
Sokoban::Move();
system("cls");
for(i=0;i<H;i++)
{
for(j=0;j<L;j++)
cout<<GameMap[i][j];
cout<<endl;
}
cout<<endl;

cout<<" **********************************"<<endl;
cout<<"*小小C++語言推箱子游戲*"<<endl;
cout<<"*游戲規則:*"<<endl;
cout<<"*P:人#:箱子*"<<endl;
cout<<"**:障礙物T:目的地*"<<endl;
cout<<"**********************************"<<endl;
cout<<"*每次游戲地圖不一樣*"<<endl;
cout<<"*人將箱子推到目的地即過關*"<<endl;
cout<<"*所給地圖,一定可過關,請慎重移箱子*"<<endl;
cout<<"*箱子無路可走時,機器不會提示*"<<endl;
cout<<"**********************************"<<endl;
//箱子成功到達目的地
if(Succeed)
{
cout<<" ^_^>_<"<<endl;
cout<<"恭喜過關成功!再來一盤吧"<<endl;
getchar();
break;
}
}
}

//按鍵判斷函數
voidSokoban::Button()
{
intkey;
if(kbhit()!=0)//檢查當前是否有鍵盤輸入,若有則返回一個非0值,否則返回0
{
while(kbhit()!=0)//可能存在多個按鍵,要全部取完,以最後一個為主
key=getch();//將按鍵從控制台中取出並保存到key中
switch(key)
{
//上
case72:dir=0;
break;
//右
case77:dir=1;
break;
//下
case80:dir=2;
break;
//左
case75:dir=3;
break;
}
}
}

//人推箱子移動函數
voidSokoban::Move()
{
intx,y;
//有按鍵時
if(dir!=-1)
{
//人所推向的位置坐標
x=Pex+dx[dir];y=Pey+dy[dir];
//人所推位置為空,即走向該位置
if(Check(x,y)&&GameMap[x][y]=='.')
{
GameMap[Pex][Pey]='.';//人的位置改變
GameMap[x][y]='P';
Pex=x;Pey=y;
dir=-1;//按鍵記錄為無即-1
}
else//人所推位置為箱子,即將箱子推向該方向的前面這點
if(Check(x,y)&&GameMap[x][y]=='#'
&&Check(x+dx[dir],y+dy[dir])
&&GameMap[x+dx[dir]][y+dy[dir]]=='.')
{
GameMap[Boxx][Boxy]='.';//箱子的位置改變
GameMap[x+dx[dir]][y+dy[dir]]='#';
Boxx=x+dx[dir];Boxy=y+dy[dir];

GameMap[Pex][Pey]='.';//人的位置改變
GameMap[x][y]='P';
Pex=x;Pey=y;
dir=-1;
}
else//將箱子推向該方向的前面這點為目的地
if(Check(x,y)&&GameMap[x][y]=='#'
&&Check(x+dx[dir],y+dy[dir])
&&GameMap[x+dx[dir]][y+dy[dir]]=='T')
{
GameMap[Boxx][Boxy]='.';//箱子的位置改變
GameMap[x+dx[dir]][y+dy[dir]]='#';
Boxx=x+dx[dir];Boxy=y+dy[dir];

GameMap[Pex][Pey]='.';//人的位置改變
GameMap[x][y]='P';
Pex=x;Pey=y;
dir=-1;
Succeed=1;//記錄成功到達目的地
}
}
}

//判斷越界情況
boolSokoban::Check(intx,inty)
{
if(x<0||x>=H||y<0||y>=L)
return0;
else
return1;
}
//*************************************************
Use_Sokoban.cpp
//*************************************************
#include<iostream>
#include"Sokoban.h"
usingnamespacestd;

intmain()
{
Sokobans;
s.Initial();
return0;
}
//*************************************************

⑦ 求一些C語言小游戲的源代碼,謝謝

「推箱子」C代碼:

#include <stdio.h>

#include <conio.h>

#include<stdlib.h>

#include<windows.h>

int m =0; //m代表第幾關

struct maps{short a[9][11]; };

struct maps map[5]={ 0,0,0,0,0,0,0,0,0,0,0, //共5關,每關9行11列

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

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

1,1,4,1,1,1,0,0,0,1,0, //0空地,1牆

1,5,0,0,4,0,0,4,0,1,0, //4是箱子,5是人

1,0,3,3,1,0,4,0,1,1,0, //3是目的地

1,1,3,3,1,0,0,0,1,0,0, //7是箱子在目的地(4+3)

0,1,1,1,1,1,1,1,1,0,0, //8是人在目的地(5+3)

0,0,0,0,0,0,0,0,0,0,0,

0,0,0,0,0,0,0,0,0,0,0,

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

0,0,1,5,0,1,1,1,0,0,0,

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

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

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

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

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

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

0,0,0,0,0,0,0,0,0,0,0,

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

0,0,1,1,0,0,1,0,5,1,0,

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

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

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

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

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

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

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

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

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

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

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

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

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

1,0,0,0,0,0,1,0,5,0,1,

1,1,1,1,1,1,1,1,1,1,1,

0,0,0,0,0,0,0,0,0,0,0,

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

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

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

1,3,3,4,0,4,0,0,5,1,0,

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

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

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

0,0,0,0,0,0,0,0,0,0,0 };

void DrMap( ) //繪制地圖

{ CONSOLE_CURSOR_INFO cursor_info={1,0}; //隱藏游標的設置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);

printf(" 推箱子");

printf(" ");

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

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

{switch (map[m].a[i][j])

{case 0: printf(" "); break;

case 1: printf("■"); break;

case 3: printf("◎");break;

case 4: printf("□"); break;

case 5: printf("♀"); break; //5是人

case 7: printf("□"); break; //4 + 3箱子在目的地中

case 8: printf("♀");break; // 5 + 3人在目的地中

}

}

printf(" ");

}

}

void gtxy(int x, int y) //控制游標位置的函數

{ COORD coord;

coord.X = x;

coord.Y = y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

void start( ) //開始游戲

{ int r, c; //人的下標

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

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

{if (map[m].a[i][j] == 5||map[m].a[i][j]==8) { r = i; c = j; } } //i j 人的下標

}

char key;

key = getch( );

switch (key)

{case 'W':

case 'w':

case 72:

if (map[m]. a[r - 1][c] == 0|| map[m]. a [r - 1][c] == 3)

{ gtxy(2*c+8,r-1+3); printf("♀"); // gtxy(2*c+8,r-1+3)是到指定位置輸出字元

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r - 1][c] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r - 1][c] == 4 || map[m]. a [r - 1][c] == 7)

{ if (map[m]. a [r - 2][c] == 0 || map[m]. a [r - 2][c] == 3)

{ gtxy(2*c+8,r-2+3); printf("□"); gtxy(2*c+8,r-1+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r - 2][c] += 4; map[m]. a [r - 1][c] += 1;

map[m]. a [r][c] -= 5; }

} break;

case 'S':

case 's':

case 80:

if (map[m]. a [r + 1][c] == 0 || map[m]. a [r + 1][c] == 3)

{ gtxy(2*c+8,r+1+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r + 1][c] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r + 1][c] == 4 || map[m]. a [r+ 1][c] == 7)

{ if (map[m]. a [r + 2][c] == 0 || map[m]. a [r + 2][c] == 3)

{ gtxy(2*c+8,r+2+3); printf("□"); gtxy(2*c+8,r+1+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r + 2][c] += 4; map[m]. a [r + 1][c] += 1;

map[m]. a [r][c] -= 5; }

}break;

case 'A':

case 'a':

case 75:

if (map[m]. a [r ][c - 1] == 0 || map[m]. a [r ][c - 1] == 3)

{ gtxy(2*(c-1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r ][c - 1] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r][c - 1] == 4 || map[m]. a [r][c - 1] == 7)

{if (map[m]. a [r ][c - 2] == 0 || map[m]. a [r ][c - 2] == 3)

{ gtxy(2*(c-2)+8,r+3); printf("□"); gtxy(2*(c-1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r ][c - 2] += 4; map[m]. a [r ][c - 1] += 1;

map[m]. a [r][c] -= 5; }

}break;

case 'D':

case 'd':

case 77:

if (map[m]. a [r][c + 1] == 0 || map[m]. a [r][c + 1] == 3)

{ gtxy(2*(c+1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8) {gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r][c + 1] += 5; map[m]. a [r][c] -= 5; }

else if (map[m]. a [r][c + 1] == 4 || map[m]. a [r][c + 1] == 7)

{ if (map[m]. a [r][c + 2] == 0 || map[m]. a [r][c + 2] == 3)

{ gtxy(2*(c+2)+8,r+3); printf("□"); gtxy(2*(c+1)+8,r+3); printf("♀");

if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }

if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}

map[m]. a [r][c + 2] += 4; map[m]. a [r][c + 1] += 1;

map[m]. a [r][c] -= 5; }

}break;

}

}

int ifwan( ) //是否完成(1是0否)

{ if(m==0){if(map[m].a[5][2]==7&& map[m].a[5][3]==7&&

map[m].a[6][2]==7&& map[m].a[6][3]==7) return 1;}

if(m==1){if(map[m].a[5][2]==7&& map[m].a[6][2]==7&&

map[m].a[7][2]==7) return 1;}

if(m==2){if(map[m].a[7][1]==7&& map[m].a[7][2]==7&& map[m].a[7][3]==7&&

map[m].a[7][4]==7&& map[m].a[7][5]==7) return 1;}

if(m==3){if(map[m].a[4][4]==7&& map[m].a[4][5]==7&& map[m].a[4][6]==7&&

map[m].a[5][4]==7&& map[m].a[5][5]==7&& map[m].a[5][6]==7) return 1;}

if(m==4){if(map[m].a[3][2]==7&& map[m].a[4][1]==7&& map[m].a[4][2]==7&&

map[m].a[5][1]==7&& map[m].a[5][2]==7) return 1;}

return 0;

}

int main( ) //主函數

{ while (1)

{ system("cls");

DrMap( );

while (1)

{ start( );

if(ifwan()){printf("07");break;} //完成後響鈴

}

m+=1;

}

return 0;

}

⑧ 求一個用C語言編寫的推箱子小游戲代碼

若用VC環境下的C語言進行圖像編程,理論上是不行的。不過好在已有高人解決了
壓縮包內有文件源代碼,與相應的插件,按說明安裝即可
親自測試
很抱歉,回答者上傳的附件已失效

⑨ 緊急求救:用C語言編寫推箱子游戲的程序!!

給:
#include"stdio.h"
#include"bios.h"
#define LEFT 75
#define RIGHT 77
#define UPPER 72
#define DOWN 80
#define ESC 27
struct Boxss /*定義箱子結構體,其中包含坐標屬性*/
{
int x,y;
};
union keyboard /*定義讀取鍵盤碼的共用體類型*/
{
unsigned int iKeyInfo;
char chKeyBit[2];
};
int fnGetKey(void) /*定義讀取鍵盤碼的函數*/
{
union keyboard uniKey1; /*定義讀取鍵盤碼的共用體變數*/
while(bioskey(1)==0); /*檢測用戶是否按鍵*/
uniKey1.iKeyInfo=bioskey(0); /*讀取按鍵信息*/
return(uniKey1.chKeyBit[0]==0?uniKey1.chKeyBit[1]:uniKey1.chKeyBit[0]); /*返回ASCII碼或擴充碼*/
}
void main()
{
int iKey,x=11,y=6,tx=11,ty=6; /*x,y為人物移動後坐標,tx,ty為人物移動前坐標*/
struct Boxss Box[4]; /*定義箱子數量*/
int chMap[10][10]={ /*用二維數組定義地圖*/
{0,0,0,0,0,0,0,0,0,0}, /*0表示牆1表示路2表示目標*/
{0,1,0,0,0,0,1,1,1,0},
{0,1,0,2,0,0,1,0,1,0},
{0,1,0,1,0,0,1,0,1,0},
{0,1,1,1,0,0,1,0,1,0},
{0,1,0,0,0,0,1,0,1,0},
{0,1,1,1,1,1,1,0,1,0},
{0,1,0,1,0,0,0,0,2,0},
{0,2,0,1,1,1,1,2,0,0},
{0,0,0,0,0,0,0,0,0,0},
};
int i,j;
Box[0].x=13; /*定義箱子的坐標屬性*/
Box[1].x=11;
Box[2].x=14;
Box[3].x=18;
Box[0].y=8;
Box[1].y=7;
Box[2].y=13;
Box[3].y=7;
while(1) /*反復進行求移動的坐標運算*/
{
for(i=0;i<10;i++) /*輸出新地圖(刷新地圖)*/
{
gotoxy(10,5+i);
for(j=0;j<10;j++)
{
if(chMap[i][j]==0)
printf("#");
if(chMap[i][j]==1)
printf(" ");
if(chMap[i][j]==2)
printf("X");
}
}
j=0; /*判斷是否所有箱子都在目標坐標上*/
for(i=0;i<4;i++)
if(chMap[Box[i].y-5][Box[i].x-10]==2)
j++;
if(j==4) /*如果所有箱子都就位輸出"YOU WIN!"退出*/
{
clrscr();
printf("You Win!");
break;
}
for(i=0;i<4;i++) /*在起始(或移動後)的坐標輸出箱子*/
{
gotoxy(Box[i].x,Box[i].y);
printf("0");
}
gotoxy(x,y); /*在起始(或移動後)的坐標輸出人*/
printf("*\b");
tx=x; /*記錄本次移動前的坐標*/
ty=y;
iKey=fnGetKey();
if(iKey==LEFT&&chMap[y-5][x-1-10]!=0) /*按讀取的按鍵信息改變坐標如果改變的坐標和牆(0)重合則不改變*/
x--;
if(iKey==RIGHT&&chMap[y-5][x+1-10]!=0)
x++;
if(iKey==UPPER&&chMap[y-1-5][x-10]!=0)
y--;
if(iKey==DOWN&&chMap[y+1-5][x-10]!=0)
y++; /*輸入ESC退出並輸出"YOU LOST"*/
if(iKey==ESC)
{
clrscr();
printf("You Lost");
break;
}
for(i=0;i<4;i++) /*如果移動後的人的坐標與箱子坐標重合,則改變箱子坐標向前一格*/
if(Box[i].x==x&&Box[i].y==y)
{
Box[i].x+=(x-tx);
Box[i].y+=(y-ty);
if(chMap[Box[i].y-5][Box[i].x-10]==0) /*如果移動後的箱子坐標會出現在牆上,則使箱子坐標和人坐標都返回移動前的值*/
{
Box[i].x-=(x-tx);
Box[i].y-=(y-ty);
x=tx;
y=ty;
}
break;
}
clrscr();
}
getch();
}

熱點內容
絕地求生未來之役比賽為什麼進不去 發布:2023-08-31 22:07:08 瀏覽:1393
dota2位置什麼意思 發布:2023-08-31 22:00:04 瀏覽:833
lol電競是什麼樣子 發布:2023-08-31 21:58:40 瀏覽:1292
絕地求生八倍鏡的那個圓圈怎麼弄 發布:2023-08-31 21:58:31 瀏覽:1378
lol龍龜一個多少金幣 發布:2023-08-31 21:55:07 瀏覽:742
王者如何改游戲內名稱 發布:2023-08-31 21:55:06 瀏覽:1033
游戲主播打廣告是什麼意思 發布:2023-08-31 21:55:06 瀏覽:1714
絕地求生如何免費拿到ss7賽季手冊 發布:2023-08-31 21:52:13 瀏覽:909
pgg是哪個國家的戰隊lol 發布:2023-08-31 21:52:07 瀏覽:790
一個人的時候才發現游戲很沒意思 發布:2023-08-31 21:49:24 瀏覽:1425