先说一个 扫雷的吧,,没办法给你源程序,
就给你说制作过程吧,
打开VB60
在窗体的上半部分,用pictureBOX控件画一定的区域用来模仿WINDOWS那样的,里面显示雷数,时间,以及开始,,并且命名为picture1
在画好后,在picture1里 放4个控件,text1,text2,commandbutton(按钮),timer1并且给这4个控件属性定义属性值:text1和text2的BackColor属性为黑色,ForeColor为红色,FONT属性改为小二号。text1 的text属性为10,text2的text属性为0
按钮控件的名称属性改为C1,Caption属性改为开始。FONT属性改为小二号
Timer1 属性 Enabled 为 False, Interval为1000
再Picture1的下面再用Picturebox控件画一定的区域做为雷区。将这个控件的名称属性改为P ,AutoRedraw属性改为True
然后再把控件P(雷区)里 放一个按钮(数组)控件,其属性:名称改为C,Caption为空
(怎么建数组控件?就是在该控件上点右键选择复制,在空白区选择粘贴,系统会提示你是否建立数组控件,你选是就OK了) 它变成数组控件后,两个控件名称一样都是C 但是会有一个C(0)和C(1) 你把(1)的删除就行了,在C(0)控件的属性style改为1,
做完以上的,,只需要把以下代码复制到代码区即刻运行
这是字符界面的扫雷:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windowsh>
#include <conioh>
// defines
#define KEY_UP 0xE048
#define KEY_DOWN 0xE050
#define KEY_LEFT 0xE04B
#define KEY_RIGHT 0xE04D
#define KEY_ESC 0x001B
#define KEY_1 '1'
#define KEY_2 '2'
#define KEY_3 '3'
#define GAME_MAX_WIDTH 100
#define GAME_MAX_HEIGHT 100
// Strings Resource
#define STR_GAMETITLE "ArrowKey:MoveCursor Key1:Open \
Key2:Mark Key3:OpenNeighbors"
#define STR_GAMEWIN "Congratulations! You Win! Thank you for playing!\n"
#define STR_GAMEOVER "Game Over, thank you for playing!\n"
#define STR_GAMEEND "Presented by yzfy Press ESC to exit\n"
//-------------------------------------------------------------
// Base class
class CConsoleWnd
{
public:
static int TextOut(const char);
static int GotoXY(int, int);
static int CharOut(int, int, const int);
static int TextOut(int, int, const char);
static int GetKey();
public:
};
//{{// class CConsoleWnd
//
// int CConsoleWnd::GetKey()
// Wait for standard input and return the KeyCode
//
int CConsoleWnd::GetKey()
{
int nkey=getch(),nk=0;
if(nkey>=128||nkey==0)nk=getch();
return nk>0nkey256+nk:nkey;
}
//
// int CConsoleWnd::GotoXY(int x, int y)
// Move cursor to (x,y)
// Only Console Application
//
int CConsoleWnd::GotoXY(int x, int y)
{
COORD cd;
cdX = x;cdY = y;
return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cd);
}
//
// int CConsoleWnd::TextOut(const char pstr)
// Output a string at current position
//
int CConsoleWnd::TextOut(const char pstr)
{
for(;pstr;++pstr)putchar(pstr);
return 0;
}
//
// int CConsoleWnd::CharOut(int x, int y, const int pstr)
// Output a char at (x,y)
//
int CConsoleWnd::CharOut(int x, int y, const int pstr)
{
GotoXY(x, y);
return putchar(pstr);
}
//
// int CConsoleWnd::TextOut(const char pstr)
// Output a string at (x,y)
//
int CConsoleWnd::TextOut(int x, int y, const char pstr)
{
GotoXY(x, y);
return TextOut(pstr);
}
//}}
//-------------------------------------------------------------
//Application class
class CSLGame:public CConsoleWnd
{
private:
private:
int curX,curY;
int poolWidth,poolHeight;
int bm_gamepool[GAME_MAX_HEIGHT+2][GAME_MAX_WIDTH+2];
public:
CSLGame():curX(0),curY(0){poolWidth=poolHeight=0;}
int InitPool(int, int, int);
int MoveCursor(){return CConsoleWnd::GotoXY(curX, curY);}
int DrawPool(int);
int WaitMessage();
int GetShowNum(int, int);
int TryOpen(int, int);
private:
int DFSShowNum(int, int);
private:
const static int GMARK_BOOM;
const static int GMARK_EMPTY;
const static int GMARK_MARK;
};
const int CSLGame::GMARK_BOOM = 0x10;
const int CSLGame::GMARK_EMPTY= 0x100;
const int CSLGame::GMARK_MARK = 0x200;
//{{// class CSLGame:public CConsoleWnd
//
// int CSLGame::InitPool(int Width, int Height, int nBoom)
// Initialize the game pool
// If WidthHeight <= nBoom, or nBoom<=0,
// or Width and Height exceed limit , then return 1
// otherwise return 0
//
int CSLGame::InitPool(int Width, int Height, int nBoom)
{
poolWidth = Width; poolHeight = Height;
if(nBoom<=0 || nBoom>=WidthHeight
|| Width <=0 || Width >GAME_MAX_WIDTH
|| Height<=0 || Height>GAME_MAX_HEIGHT
){
return 1;
}
// zero memory
for(int y=0; y<=Height+1; ++y)
{
for(int x=0; x<=Width+1; ++x)
{
bm_gamepool[y][x]=0;
}
}
// init seed
srand(time(NULL));
// init Booms
while(nBoom)
{
int x = rand()%Width + 1, y = rand()%Height + 1;
if(bm_gamepool[y][x]==0)
{
bm_gamepool[y][x] = GMARK_BOOM;
--nBoom;
}
}
// init cursor position
curX = curY = 1;
MoveCursor();
return 0;
}
//
// int CSLGame::DrawPool(int bDrawBoom = 0)
// Draw game pool to Console window
//
int CSLGame::DrawPool(int bDrawBoom = 0)
{
for(int y=1;y<=poolHeight;++y)
{
CConsoleWnd::GotoXY(1, y);
for(int x=1;x<=poolWidth;++x)
{
if(bm_gamepool[y][x]==0)
{
putchar('');
}
else if(bm_gamepool[y][x]==GMARK_EMPTY)
{
putchar(' ');
}
else if(bm_gamepool[y][x]>0 && bm_gamepool[y][x]<=8)
{
putchar('0'+bm_gamepool[y][x]);
}
else if(bDrawBoom==0 && (bm_gamepool[y][x] & GMARK_MARK))
{
putchar('#');
}
else if(bm_gamepool[y][x] & GMARK_BOOM)
{
if(bDrawBoom)
putchar('');
else
putchar('');
}
}
}
return 0;
}
//
// int CSLGame::GetShowNum(int x, int y)
// return ShowNum at (x, y)
//
int CSLGame::GetShowNum(int x, int y)
{
int nCount = 0;
for(int Y=-1;Y<=1;++Y)
for(int X=-1;X<=1;++X)
{
if(bm_gamepool[y+Y][x+X] & GMARK_BOOM)++nCount;
}
return nCount;
}
//
// int CSLGame::TryOpen(int x, int y)
// Try open (x, y) and show the number
// If there is a boom, then return EOF
//
int CSLGame::TryOpen(int x, int y)
{
int nRT = 0;
if(bm_gamepool[y][x] & GMARK_BOOM)
{
nRT = EOF;
}
else
{
int nCount = GetShowNum(x,y);
if(nCount==0)
{
DFSShowNum(x, y);
}
else bm_gamepool[y][x] = nCount;
}
return nRT;
}
//
// int CSLGame::DFSShowNum(int x, int y)
// Private function, no comment
//
int CSLGame::DFSShowNum(int x, int y)
{
if((0<x && x<=poolWidth) &&
(0<y && y<=poolHeight) &&
(bm_gamepool[y][x]==0))
{
int nCount = GetShowNum(x, y);
if(nCount==0)
{
bm_gamepool[y][x] = GMARK_EMPTY;
for(int Y=-1;Y<=1;++Y)
for(int X=-1;X<=1;++X)
{
DFSShowNum(x+X,y+Y);
}
}
else bm_gamepool[y][x] = nCount;
}
return 0;
}
//
// int CSLGame::WaitMessage()
// Game loop, wait and process an input message
// return: 0: not end; 1: Win; otherwise: Lose
//
int CSLGame::WaitMessage()
{
int nKey = CConsoleWnd::GetKey();
int nRT = 0, nArrow = 0;
switch (nKey)
{
case KEY_UP:
{
if(curY>1)--curY;
nArrow=1;
}break;
case KEY_DOWN:
{
if(curY<poolHeight)++curY;
nArrow=1;
}break;
case KEY_LEFT:
{
if(curX>1)--curX;
nArrow=1;
}break;
case KEY_RIGHT:
{
if(curX<poolWidth)++curX;
nArrow=1;
}break;
case KEY_1:
{
nRT = TryOpen(curX, curY);
}break;
case KEY_2:
{
if((bm_gamepool[curY][curX]
& ~(GMARK_MARK|GMARK_BOOM))==0)
{
bm_gamepool[curY][curX] ^= GMARK_MARK;
}
}break;
case KEY_3:
{
if(bm_gamepool[curY][curX] & 0xF)
{
int nb = bm_gamepool[curY][curX] & 0xF;
for(int y=-1;y<=1;++y)
for(int x=-1;x<=1;++x)
{
if(bm_gamepool[curY+y][curX+x] & GMARK_MARK)
--nb;
}
if(nb==0)
{
for(int y=-1;y<=1;++y)
for(int x=-1;x<=1;++x)
{
if((bm_gamepool[curY+y][curX+x]
& (0xF|GMARK_MARK)) == 0)
{
nRT |= TryOpen(curX+x, curY+y);
}
}
}
}
}break;
case KEY_ESC:
{
nRT = EOF;
}break;
}
if(nKey == KEY_1 || nKey == KEY_3)
{
int y=1;
for(;y<=poolHeight;++y)
{
int x=1;
for(;x<=poolWidth; ++x)
{
if(bm_gamepool[y][x]==0)break;
}
if(x<=poolWidth) break;
}
if(! (y<=poolHeight))
{
nRT = 1;
}
}
if(nArrow==0)
{
DrawPool();
}
MoveCursor();
return nRT;
}
//}}
//-------------------------------------------------------------
//{{
//
// main function
//
int main(void)
{
int x=50, y=20, b=100,n; // define width & height & n_booms
CSLGame slGame;
// Init Game
{
CConsoleWnd::GotoXY(0,0);
CConsoleWnd::TextOut(STR_GAMETITLE);
slGameInitPool(x,y,b);
slGameDrawPool();
slGameMoveCursor();
}
while((n=slGameWaitMessage())==0) // Game Message Loop
;
// End of the Game
{
slGameDrawPool(1);
CConsoleWnd::TextOut("\n");
if(n==1)
{
CConsoleWnd::TextOut(STR_GAMEWIN);
}
else
{
CConsoleWnd::TextOut(STR_GAMEOVER);
}
CConsoleWnd::TextOut(STR_GAMEEND);
}
while(CConsoleWnd::GetKey()!=KEY_ESC)
;
return 0;
}
//}}
1建一个类表示单元格,属性:
是否是雷
周围有几个雷
特殊标记1
特殊标记2
2用上面的类建一个二维数组,表示雷区
3初始化方法,根据雷区的大小(nxm格)计算有几个雷,可以参考windows自带扫雷的分布,雷的个数要适当,不然没法玩,要么一点就完了,要么一点就挂了。。。
初始化过程中要更新雷区,记下所有信息
4点击的方法,如果是雷的话就炸了,不是的话,递归的处理:
当前的单元格亮开,如果他周围有雷(雷的个数>0)就直接显示雷的数就是了
如果他的周围没有雷,递归的处理他周围的单元格
每亮开一个单元格都要检测是否游戏已结束
太复杂,我分成几部分试着做一下
首先是画棋盘,定义两个二维数组来表示每个棋子的状态和棋子周围的雷数,用于显示。
//画棋盘 a表示棋子是否已被翻开,b表示附近的雷数bool MakeMap(bool a[9][9],int b[9][9])
{
int i=0,j=0;
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
if(a[i][j]) printf("%d ",b[i][j]);
else printf("# ");
}
printf("\n");
}
return true;
}
天是蓝的地是绿的
安装一个就可以了啊
效果还不错可以自己去试试
不会很麻烦+喂心kk6788333
believehaoxianshengrangrengyong~~~
求采纳谢谢~~~求采纳谢谢~~~求采纳谢谢~~~
求采纳谢谢~~~求采纳谢谢~~~求采纳谢谢~~~
求采纳谢谢~~~求采纳谢谢~~~求采纳谢谢~~~
求采纳谢谢~~~求采纳谢谢~~~求采纳谢谢~~~
求采纳谢谢~~~求采纳谢谢~~~求采纳谢谢~~~
求采纳谢谢~~~求采纳谢谢~~~求采纳谢谢~~~
import javaxswingImageIcon; //程序入口
public class Block {
String name; //名字,比如"雷"或数字
int aroundMineNumber; //周围雷的数目
ImageIcon mineIcon; //雷的图标
boolean isMine=false; //是否是雷
boolean isMark=false; //是否被标记
boolean isOpen=false; //是否被挖开
public void setName(String name) {
thisname=name;
}
//设置周围的雷数
public void setAroundMineNumber(int n) {
aroundMineNumber=n;
}
//获得周围的雷数
public int getAroundMineNumber() {
return aroundMineNumber;
}
public String getName() {
return name;
}
//判断是否是雷
public boolean isMine() {
return isMine;
}
//设置是否为雷
public void setIsMine(boolean b) {
isMine=b;
}
//设置雷的图标
public void setMineIcon(ImageIcon icon){
mineIcon=icon;
}
//获得雷的图标
public ImageIcon getMineicon(){
return mineIcon;
}
//确定雷是否被挖开
public boolean getIsOpen() {
return isOpen;
}
//设置为已经被挖开
public void setIsOpen(boolean p) {
isOpen=p;
}
//返回此处是否已经被标记
public boolean getIsMark() {
return isMark;
}
//设置此处是否已经被标记
public void setIsMark(boolean m) {
isMark=m;
}
import javaxswing;
import javaawt;
public class BlockView extends JPanel{
JLabel blockNameOrIcon; //用来显示Block对象的name、number和mineIcon属性
JButton blockCover; //用来遮挡blockNameOrIcon
CardLayout card; //卡片式布局
BlockView(){
card=new CardLayout();
setLayout(card);
blockNameOrIcon=new JLabel("",JLabelCENTER);
blockNameOrIconsetHorizontalTextPosition(AbstractButtonCENTER);
blockNameOrIconsetVerticalTextPosition(AbstractButtonCENTER);
blockCover=new JButton();
add("cover",blockCover);
add("view",blockNameOrIcon);
}
//给出视觉效果变化
public void giveView(Block block){
// 如果是雷,将对应的图标和文字更改
if(blockisMine){
blockNameOrIconsetText(blockgetName());
blockNameOrIconsetIcon(blockgetMineicon());
}
else {
int n=blockgetAroundMineNumber();
if(n>=1)
blockNameOrIconsetText(""+n);
else
blockNameOrIconsetText(" ");
}
}
public void seeBlockNameOrIcon(){
cardshow(this,"view");
validate();
}
public void seeBlockCover(){
cardshow(this,"cover");
validate();
}
public JButton getBlockCover(){
return blockCover;
}
}
以上就是关于怎么用vb做扫雷程序全部的内容,包括:怎么用vb做扫雷程序、C++扫雷源代码、求 java 扫雷 设计思路等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)