java拼图游戏完整代码

合集下载

拼图游戏源代码

拼图游戏源代码

//*关卡开始前 booleanfirstStart=true;//是否首次运行标志 musicNum=1;//音乐不够,以此变量做两首歌更换标志 privatevoiddoStartPa ...{ //*关卡化 lm=LayerManager; puzzlePic=creatPuzzlePic; //*测试用 //puzzlePic=creatTestPuzzlePic;//测试用 lm.append(puzzlePic);
//播放声音 //西门子模拟器不支持下面这段代码,但实体机子完美支持 try ...{ InputStreamin; //in=getClass.getResourceAsStream(\"/res/music\"+stageNum+\".mid\");这是有5首音乐时候使用 //只有两首歌时候使用 (musicNum1) ...{ in=getClass.getResourceAsStream(\"/res/music1.mid\"); [Page] musicNum=2; } ...{ in=getClass.getResourceAsStream(\"/res/music2.mid\"); musicNum=1; } player=Manager.createPlayer(in,\"audio/midi\"); player.LoopCount(10); player.start; } catch(IOExceptione)...{}
/***//**储存损耗时间,普通模式用到*/ privatelongusedTime; [Page] /***//**储存单关通过最短时间*/ privatelongquicklyPassOneStageTime; /***//**游戏过程暂停状态所用标志*/ privatebooleanisGamePaused; /***//**储存显示对象*/ privateDisplaydisplay;

Java语言实现拼图游戏源代码

Java语言实现拼图游戏源代码

Java语言实现拼图游戏源代码/** JA V A小游戏-拼图我做的第一个小游戏* Cell类是继承的按钮类,并加上相应图形,形成方格*MyCanvas是一个面板,加载Cell类的对象(方格),是这三个类中的核心*/import java.awt.Rectangle;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JOptionPane;import javax.swing.JPanel;import java.awt.BorderLayout;import java.awt.Button;import java.awt.Choice;import java.awt.Color;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.Icon;import javax.swing.JButton;public class MyMainFrame extends JFrame implements ActionListener { MyCanvas myCanvas;JPanel panelNorth,panelPreview;//定义上方的面板,及预览所需的面板Button start,preview,set;//定义开始,预览,设定按钮Container container;//容器,得到内容面板public MyMainFrame() {//初使化container=this.getContentPane();start=new Button("开始");start.addActionListener(this);preview=new Button("预览");preview.addActionListener(this);set = new Button("设置");set.addActionListener(this);panelPreview=new JPanel();panelPreview.setLayout(null);Icon icon=new ImageIcon("pic_"+MyCanvas.pictureID+".jpg");JLabel label=new JLabel(icon);label.setBounds(0,0,300,300);panelPreview.add(label);panelNorth=new JPanel();panelNorth.setBackground(Color.red);panelNorth.add(start);panelNorth.add(preview);panelNorth.add(set);myCanvas=new MyCanvas();container.add(myCanvas,BorderLayout.CENTER);container.add(panelNorth,BorderLayout.NORTH);this.setTitle("拼图小游戏-明");this.setLocation(300,200);this.setSize(308,365);this.setResizable(false);this.setVisible(true);this.setDefaultCloseOperation(3);}public static void main(String[] args) {// TODO 自动生成方法存根new MyMainFrame();}public void actionPerformed(ActionEvent arg0) {//对三个按钮事件的处理// TODO 自动生成方法存根Button button=(Button)arg0.getSource();if(button==start){myCanvas.Start();}else if(button==preview){if(button.getLabel()=="预览"){container.remove(myCanvas);container.add(panelPreview);panelPreview.updateUI();container.repaint();button.setLabel("返回");}else{container.remove(panelPreview);container.add(myCanvas);container.repaint();button.setLabel("预览");}}else if(button==set){//修改所选图片Choice pic = new Choice();pic.add("小猫");pic.add("小猪");pic.add("云");pic.add("QQ");pic.add("卡通");pic.add("花");int i=JOptionPane.showConfirmDialog(this, pic, "选择图片", JOptionPane.OK_CANCEL_OPTION);if(i==JOptionPane.YES_OPTION){MyCanvas.pictureID=pic.getSelectedIndex()+1;myCanvas.reLoadPictrue();Icon icon=new ImageIcon("pic_"+MyCanvas.pictureID+".jpg");JLabel label=new JLabel(icon);label.setBounds(0,0,300,300);panelPreview.removeAll();panelPreview.add(label);panelPreview.repaint();}}}}class MyCanvas extends JPanel implements MouseListener {boolean hasAddActionListener=false;//设置方格的动作监听器的标志位,TRUE为已经添加上动作事件,FALSE是尚未添加动作事件Cell cell[];//定义方格Rectangle cellNull;//定义空方格区域public static int pictureID=1;//当前选择的图片代号public MyCanvas() {this.setLayout(null);this.setSize(400,400);cellNull=new Rectangle(200,200,100,100);//空方格区域在第三行每三列cell=new Cell[9];Icon icon;for (int i = 0; i < 3; i++) {//为9个方格加载图片,并初使化坐标,形成三行三列for(int j=0;j<3;j++){icon=new ImageIcon("pic_"+pictureID+"_"+(i*3+j+1)+".jpg");cell[i*3+j]=new Cell(icon);cell[i*3+j].setLocation(j*100,i*100);this.add(cell[i*3+j]);}}this.remove(cell[8]);//移除最后一个多余的方格}文章转自上海青大实训:。

java语言实现拼图游戏源代码

java语言实现拼图游戏源代码

java语言实现拼图游戏源代码Java语言实现拼图游戏源代码 /** JAVA小游戏,拼图我做的第一个小游戏* Cell类是继承的按钮类,并加上相应图形,形成方格*MyCanvas是一个面板,加载Cell类的对象(方格),是这三个类中的核心 */ import java.awt.Rectangle; import java.awt.event.MouseEvent; import java.awt.event.MouseListener;import javax.swing.Icon;import javax.swing.ImageIcon; import javax.swing.JOptionPane; import javax.swing.JPanel; import java.awt.BorderLayout; import java.awt.Button;import java.awt.Choice;import java.awt.Color;import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;import javax.swing.Icon;import javax.swing.ImageIcon; import javax.swing.JFrame; importjavax.swing.JLabel; import javax.swing.JOptionPane; importjavax.swing.JPanel; import javax.swing.Icon;import javax.swing.JButton;public class MyMainFrame extends JFrame implements ActionListener { MyCanvas myCanvas;JPanel panelNorth,panelPreview;//定义上方的面板,及预览所需的面板Button start,preview,set;//定义开始,预览,设定按钮Container container;//容器,得到内容面板public MyMainFrame() {//初使化container=this.getContentPane(); start=new Button("开始");start.addActionListener(this); preview=new Button("预览");preview.addActionListener(this); set = new Button("设置");set.addActionListener(this); panelPreview=new JPanel();panelPreview.setLayout(null); Icon icon=newImageIcon("pic_"+MyCanvas.pictureID+".jpg");JLabel label=new JLabel(icon); label.setBounds(0,0,300,300); panelPreview.add(label);panelNorth=new JPanel();panelNorth.setBackground(Color.red); panelNorth.add(start);panelNorth.add(preview);panelNorth.add(set);myCanvas=new MyCanvas();container.add(myCanvas,BorderLayout.CENTER);container.add(panelNorth,BorderLayout.NORTH);this.setTitle("拼图小游戏-明");this.setLocation(300,200);this.setSize(308,365);this.setResizable(false);this.setVisible(true);this.setDefaultCloseOperation(3); }public static void main(String[] args) {// TODO 自动生成方法存根new MyMainFrame();}public void actionPerformed(ActionEvent arg0) {//对三个按钮事件的处理 // TODO 自动生成方法存根Button button=(Button)arg0.getSource(); if(button==start){myCanvas.Start();}else if(button==preview){if(button.getLabel()=="预览"){container.remove(myCanvas); container.add(panelPreview); panelPreview.updateUI();container.repaint();button.setLabel("返回");}else{container.remove(panelPreview); container.add(myCanvas); container.repaint();button.setLabel("预览");}}else if(button==set){//修改所选图片Choice pic = new Choice(); pic.add("小猫");pic.add("小猪");pic.add("云");pic.add("QQ");pic.add("卡通");pic.add("花");int i=JOptionPane.showConfirmDialog(this, pic, "选择图片",JOptionPane.OK_CANCEL_OPTION);if(i==JOptionPane.YES_OPTION){ MyCanvas.pictureID=pic.getSelectedIndex() +1;myCanvas.reLoadPictrue(); Icon icon=newImageIcon("pic_"+MyCanvas.pictureID+".jpg");JLabel label=new JLabel(icon); label.setBounds(0,0,300,300); panelPreview.removeAll(); panelPreview.add(label);panelPreview.repaint();}}}}class MyCanvas extends JPanel implements MouseListener {boolean hasAddActionListener=false;//设置方格的动作监听器的标志位,TRUE为已经添加上动作事件,FALSE是尚未添加动作事件Cell cell[];//定义方格Rectangle cellNull;//定义空方格区域public static int pictureID=1;//当前选择的图片代号public MyCanvas() {this.setLayout(null);this.setSize(400,400);cellNull=new Rectangle(200,200,100,100);//空方格区域在第三行每三列cell=new Cell[9];Icon icon;for (int i = 0; i < 3; i++) {//为9个方格加载图片,并初使化坐标,形成三行三列 for(int j=0;j<3;j++){icon=new ImageIcon("pic_"+pictureID+"_"+(i*3+j+1)+".jpg");cell[i*3+j]=new Cell(icon); cell[i*3+j].setLocation(j*100,i*100); this.add(cell[i*3+j]);}}this.remove(cell[8]);//移除最后一个多余的方格}文章转自上海青大实训:谣言:吃太咸了会得病,导语:“人体每日摄入食盐不应过多,否则易患多种疾病。

拼图游戏代码

拼图游戏代码

Option ExplicitDim cpost%, cposl%, nowpost%, nowposl%, changet%, changel%, medium%, forno1%, forno2%, no(1 To 9) As Integer, steps%, timecount%, score%Dim checkwhetherdisorder%, checkdisordering%, disorderno%, continuedisorderSub checkcommand()If (Command1.Top = 0) And (Command1.Left = 0) _And (Command2.Top = 0) And (Command2.Left = 2000) _And (Command3.Top = 0) And (Command3.Left = 4000) _And (Command4.Top = 2000) And (Command4.Left = 0) _And (Command5.Top = 2000) And (Command5.Left = 2000) _And (Command6.Top = 2000) And (Command6.Left = 4000) _And (Command7.Top = 4000) And (Command7.Left = 0) _And (Command8.Top = 4000) And (Command8.Left = 2000) _And (recognize.Top = 4000) And (recognize.Left = 4000) ThenTimer1.Interval = 0If checkdisordering = 1 Then GoTo line1MsgBox ("恭喜你已经解出了正确的顺序")score = 500 - steps - timecountMsgBox ("您本次的得分为" & Str(score))checkwhetherdisorder = 0line1:End IfEnd SubPrivate Sub Form_Activate()MsgBox ("如果你已经了解规则,请点击关闭游戏规则")start.Enabled = FalseEnd SubPrivate Sub form_Load()changet = 0changel = 0steps = 0timecount = 0score = 0checkwhetherdisorder = 0Call orders_Clickcheckdisordering = 0End SubPrivate Sub Command1_Click()If (checkwhetherdisorder = 0) Or (Picture2.Visible = True) Then GoTo line0cpost = recognize.Topcposl = recognize.Leftnowpost = Command1.Topnowposl = Command1.LeftIf Abs(cpost - nowpost) = 2000 Then changet = 1If Abs(cposl - nowposl) = 2000 Then changel = 1If ((Abs(cpost - nowpost) = 2000) And (Abs(cposl - nowposl) = 4000)) Or ((Abs(cpost - nowpost) = 4000) And (Abs(cposl - nowposl) = 2000)) Thenchanget = 0changel = 0End Ifmedium = cpostcpost = nowpostnowpost = mediummedium = cposlcposl = nowposlnowposl = mediumIf changet + changel = 1 ThenCommand1.Move nowposl, nowpostrecognize.Move cposl, cpoststeps = steps + 1stepsdisplay.Clsstepsdisplay.Print "已进行步数"; stepsEnd Ifchanget = 0changel = 0Timer1.Interval = 1000checkcommandline0: If Picture2.Visible = True Then MsgBox ("点击关闭游戏规以开始游戏")End SubPrivate Sub Command2_Click()If (checkwhetherdisorder = 0) Or (Picture2.Visible = True) Then GoTo line0cpost = recognize.Topcposl = recognize.Leftnowpost = Command2.Topnowposl = Command2.LeftIf Abs(cpost - nowpost) = 2000 Then changet = 1If Abs(cposl - nowposl) = 2000 Then changel = 1If ((Abs(cpost - nowpost) = 2000) And (Abs(cposl - nowposl) = 4000)) Or ((Abs(cpost - nowpost) = 4000) And (Abs(cposl - nowposl) = 2000)) Thenchanget = 0changel = 0End Ifmedium = cpostcpost = nowpostnowpost = mediummedium = cposlcposl = nowposlnowposl = mediumIf changet + changel = 1 ThenCommand2.Move nowposl, nowpostrecognize.Move cposl, cpoststeps = steps + 1stepsdisplay.Clsstepsdisplay.Print "已进行步数"; stepsEnd Ifchanget = 0changel = 0Timer1.Interval = 1000checkcommandline0: If Picture2.Visible = True Then MsgBox ("点击关闭游戏规以开始游戏")End SubPrivate Sub Command3_Click()If (checkwhetherdisorder = 0) Or (Picture2.Visible = True) Then GoTo line0cpost = recognize.Topcposl = recognize.Leftnowpost = Command3.Topnowposl = Command3.LeftIf Abs(cpost - nowpost) = 2000 Then changet = 1If Abs(cposl - nowposl) = 2000 Then changel = 1If ((Abs(cpost - nowpost) = 2000) And (Abs(cposl - nowposl) = 4000)) Or ((Abs(cpost - nowpost) = 4000) And (Abs(cposl - nowposl) = 2000)) Thenchanget = 0changel = 0End Ifmedium = cpostcpost = nowpostnowpost = mediummedium = cposlcposl = nowposlnowposl = mediumIf changet + changel = 1 ThenCommand3.Move nowposl, nowpostrecognize.Move cposl, cpoststeps = steps + 1stepsdisplay.Clsstepsdisplay.Print "已进行步数"; stepsEnd Ifchanget = 0changel = 0Timer1.Interval = 1000checkcommandline0: If Picture2.Visible = True Then MsgBox ("点击关闭游戏规以开始游戏")End SubPrivate Sub Command4_Click()If (checkwhetherdisorder = 0) Or (Picture2.Visible = True) Then GoTo line0cpost = recognize.Topcposl = recognize.Leftnowpost = Command4.Topnowposl = Command4.LeftIf Abs(cpost - nowpost) = 2000 Then changet = 1If Abs(cposl - nowposl) = 2000 Then changel = 1If ((Abs(cpost - nowpost) = 2000) And (Abs(cposl - nowposl) = 4000)) Or ((Abs(cpost - nowpost) = 4000) And (Abs(cposl - nowposl) = 2000)) Thenchanget = 0changel = 0End Ifmedium = cpostcpost = nowpostnowpost = mediummedium = cposlcposl = nowposlnowposl = mediumIf changet + changel = 1 ThenCommand4.Move nowposl, nowpostrecognize.Move cposl, cpoststeps = steps + 1stepsdisplay.Clsstepsdisplay.Print "已进行步数"; stepsEnd Ifchanget = 0changel = 0Timer1.Interval = 1000checkcommandline0: If Picture2.Visible = True Then MsgBox ("点击关闭游戏规以开始游戏")End SubPrivate Sub Command5_Click()If (checkwhetherdisorder = 0) Or (Picture2.Visible = True) Then GoTo line0cpost = recognize.Topcposl = recognize.Leftnowpost = Command5.Topnowposl = Command5.LeftIf Abs(cpost - nowpost) = 2000 Then changet = 1If Abs(cposl - nowposl) = 2000 Then changel = 1If ((Abs(cpost - nowpost) = 2000) And (Abs(cposl - nowposl) = 4000)) Or ((Abs(cpost - nowpost) = 4000) And (Abs(cposl - nowposl) = 2000)) Thenchanget = 0changel = 0End Ifmedium = cpostcpost = nowpostnowpost = mediummedium = cposlcposl = nowposlnowposl = mediumIf changet + changel = 1 ThenCommand5.Move nowposl, nowpostrecognize.Move cposl, cpoststeps = steps + 1stepsdisplay.Clsstepsdisplay.Print "已进行步数"; stepsEnd Ifchanget = 0changel = 0Timer1.Interval = 1000checkcommandline0: If Picture2.Visible = True Then MsgBox ("点击关闭游戏规以开始游戏")End SubPrivate Sub Command6_Click()If (checkwhetherdisorder = 0) Or (Picture2.Visible = True) Then GoTo line0cpost = recognize.Topcposl = recognize.Leftnowpost = Command6.Topnowposl = Command6.LeftIf Abs(cpost - nowpost) = 2000 Then changet = 1If Abs(cposl - nowposl) = 2000 Then changel = 1If ((Abs(cpost - nowpost) = 2000) And (Abs(cposl - nowposl) = 4000)) Or ((Abs(cpost - nowpost) = 4000) And (Abs(cposl - nowposl) = 2000)) Thenchanget = 0changel = 0End Ifmedium = cpostcpost = nowpostnowpost = mediummedium = cposlcposl = nowposlnowposl = mediumIf changet + changel = 1 ThenCommand6.Move nowposl, nowpostrecognize.Move cposl, cpoststeps = steps + 1stepsdisplay.Clsstepsdisplay.Print "已进行步数"; stepsEnd Ifchanget = 0changel = 0Timer1.Interval = 1000checkcommandline0: If Picture2.Visible = True Then MsgBox ("点击关闭游戏规以开始游戏")End SubPrivate Sub Command7_Click()If (checkwhetherdisorder = 0) Or (Picture2.Visible = True) Then GoTo line0cpost = recognize.Topcposl = recognize.Leftnowpost = Command7.Topnowposl = Command7.LeftIf Abs(cpost - nowpost) = 2000 Then changet = 1If Abs(cposl - nowposl) = 2000 Then changel = 1If ((Abs(cpost - nowpost) = 2000) And (Abs(cposl - nowposl) = 4000)) Or ((Abs(cpost - nowpost) = 4000) And (Abs(cposl - nowposl) = 2000)) Thenchanget = 0changel = 0End Ifmedium = cpostcpost = nowpostnowpost = mediummedium = cposlcposl = nowposlnowposl = mediumIf changet + changel = 1 ThenCommand7.Move nowposl, nowpostrecognize.Move cposl, cpoststeps = steps + 1stepsdisplay.Clsstepsdisplay.Print "已进行步数"; stepsEnd Ifchanget = 0changel = 0Timer1.Interval = 1000checkcommandline0: If Picture2.Visible = True Then MsgBox ("点击关闭游戏规以开始游戏")End SubPrivate Sub Command8_Click()If (checkwhetherdisorder = 0) Or (Picture2.Visible = True) Then GoTo line0cpost = recognize.Topcposl = recognize.Leftnowpost = Command8.Topnowposl = Command8.LeftIf Abs(cpost - nowpost) = 2000 Then changet = 1If Abs(cposl - nowposl) = 2000 Then changel = 1If ((Abs(cpost - nowpost) = 2000) And (Abs(cposl - nowposl) = 4000)) Or ((Abs(cpost - nowpost) = 4000) And (Abs(cposl - nowposl) = 2000)) Thenchanget = 0changel = 0End Ifmedium = cpostcpost = nowpostnowpost = mediummedium = cposlcposl = nowposlnowposl = mediumIf changet + changel = 1 ThenCommand8.Move nowposl, nowpostrecognize.Move cposl, cpoststeps = steps + 1stepsdisplay.Clsstepsdisplay.Print "已进行步数"; stepsEnd Ifchanget = 0changel = 0Timer1.Interval = 1000checkcommandline0: If Picture2.Visible = True Then MsgBox ("点击关闭游戏规则即可使用")End SubPrivate Sub start_Click()Dodisorderno = Val(InputBox("请输入要打乱的程度(建议在30到2000之间)", , 200, 5500, 3000)) If disorderno = 0 Then Exit SubIf disorderno < 0 Then MsgBox ("输入无效,重新输入")If disorderno > 2000 Thencontinuedisorder = MsgBox("你确定此打乱程度吗?这可能会超出你的计算机的运算能力,请谨慎使用!", vbYesNo)If continuedisorder = vbYes ThenGoTo line1ElseExit SubEnd IfEnd IfIf disorderno > 0 And disorderno < 30 Then MsgBox ("数值过小无打乱效果,请重新输入") line1: Loop Until disorderno >= 30checkdisordering = 1stepsdisplay.Clstimedisplay.Clscheckwhetherdisorder = 1line0: RandomizeFor forno2 = 1 To disordernoFor forno1 = 1 To 9no(forno1) = Int(9 * Rnd + 1)Select Case no(forno1)Case 1Command1_ClickCase 2Command2_ClickCase 3Command3_ClickCase 4Command4_ClickCase 5Command5_ClickCase 6Command6_ClickCase 7Command7_ClickCase 8Command8_ClickCase 9End Selectsteps = 0Timer1.Interval = 0Next forno1Next forno2If checkwhetherdisorder = 0 Then GoTo line0checkdisordering = 0timecount = -1End SubPrivate Sub Timer1_Timer()timecount = timecount + 1timedisplay.Clstimedisplay.Print "用时"; timecount; "秒"End SubPrivate Sub orders_Click()Picture2.Visible = Trueorders.Enabled = Falsecloseorders.Enabled = TruePicture2.Print "本游戏是比拼谁将8个数字块最快还原为最初的顺序" Picture2.Print "点击“打乱”计算机会自动打乱8个数字块"Picture2.Print "点击数字块可以使之移动到相邻的空白位置上" Picture2.Print "从你开始改变数字块的顺序,会自动计时."Picture2.Print "当你完成的时候,计算机会对你的表现打分"Picture2.Print "现在就开始吧!"Picture2.Print "阅读完成后点击关闭游戏规则"start.Enabled = FalseEnd SubPrivate Sub closeorders_Click()Picture2.Visible = Falseorders.Enabled = Truecloseorders.Enabled = FalsePicture2.Clsstart.Enabled = TrueEnd Sub。

拼图游戏源代码

拼图游戏源代码

Option ExplicitDim imageid(8) As Integer '记录图片排列的顺序的数组Dim position As Integer '被点击的图片位置Dim xposition As Integer '空图片的位置Dim movetimes As Integer '记录移动次数Dim calposition As Integer '用于计算位置Dim started As Boolean '是否开始Dim win As Boolean '是否胜利Dim pic(9) As Picture '存储图像的数组对象Dim pic1 As PicturePrivate Sub Command1_Click()Dim i As IntegerDim temp(8) As IntegerDim chosennumber As IntegerFor i = 0 To 7temp(i) = iNextRandomizeFor i = 7 To 0 Step -1chosennumber = Int(i * Rnd)imageid(7 - i) = temp(chosennumber)temp(chosennumber) = temp(i)NextFor i = 0 To 7Picture1(i).Picture = pic(imageid(i))NextPicture1(9).Picture = pic(8)Picture1(8).Picture = pic(9)xposition = 8started = Truemovetimes = 0End SubPrivate Sub Form_Load()Dim i As IntegerFor i = 0 To 8Set pic(i) = Picture1(i).PictureNextEnd SubPrivate Sub Picture1_Click(Index As Integer)Dim i As IntegerDim response As Integerposition = IndexIf started = False ThenExit SubEnd IfIf (position <> 0) And (position <> 3) And (position <> 6) Thencalposition = position - 1If calposition = xposition ThencvaleEnd IfEnd IfIf (position <> 2) And (position <> 5) And (position <> 9) Thencalposition = position + 1If calposition = xposition ThencvaleEnd IfEnd IfText1.Text = Str(position)If position <= 5 Thencalposition = position + 3Text2.Text = Str(calposition)If calposition = xposition ThencvaleEnd IfEnd IfIf position >= 3 And position <> 9 Thencalposition = position - 3If calposition = xposition ThencvaleEnd IfEnd Ifwin = TrueFor i = 0 To 7If imageid(i) <> i Thenwin = FalseExit ForEnd IfNextIf win = True ThenMsgBox "恭喜恭喜!您很聪明" & "共移动" & vbCrLf & movetimes & "次", vbOKOnly started = FalseEnd IfEnd SubPrivate Sub cvale()Set pic1 = Picture1(position).PicturePicture1(position).Picture = Picture1(calposition).Picture Picture1(calposition).Picture = pic1xposition = positionEnd Sub。

积木游戏程序源代码

积木游戏程序源代码

积木游戏程序源代码一、HTML网页文件名:JMYX6.html<html><head><title>积木游戏JMYX6</title></head><body><applet code="JMYX6.class" width=500 height=500></applet></body></html>二、源代码文件名:JMYX6.javaimport java.applet.*;import java.awt.*;import java.awt.event.*;public class JMYX6 extends Applet implements KeyListener,ActionListener{ Label label=new Label("鼠标点击任一积木,然后用光标键移动它。

");Button b[ ]=new Button[40];int x[ ]=new int[40];int y[ ]=new int[40];String s[ ]=new String[40];public void init( ){ setLayout(null);add(label);label.setBounds(0,0,250,30);int k1=70,k2=70,k3=70,k4=70;for(int i=0;i<40;i++){ b[i]=new Button(String.valueOf(i));if(i%3==0) b[i].setBackground(Color.red);if(i%3==1) b[i].setBackground(Color.blue);if(i%3==2) b[i].setBackground(Color.yellow);b[i].addKeyListener(this); //按钮获得键盘事件监视器b[i].addActionListener(this);add(b[i]);if(i%4==0) { b[i].setBounds(k1,40,30,30);k1=k1+31; }else if(i%4==1) { b[i].setBounds(k2,71,30,30);k2=k2+31; }else if(i%4==2) { b[i].setBounds(k3,102,30,30);k3=k3+31; }else if(i%4==3) { b[i].setBounds(k4,133,30,30);k4=k4+31; } }for(int i=0;i<40;i++){ x[i]=b[i].getBounds( ).x;y[i]=b[i].getBounds( ).y; //获得按钮左上角的x,y 坐标}}public void keyTyped(KeyEvent e){ }public void keyPressed(KeyEvent e){ if(e.getKeyCode( )==KeyEvent.VK_UP){ for(int i=0;i<=39;i++){ if(s[i].equals(String.valueOf(i))){ y[i]=y[i]-2;if(y[i]<=0) y[i]=0;b[i].setLocation(x[i],y[i]);}}}else if(e.getKeyCode( )==KeyEvent.VK_DOWN){ for(int i=0;i<=39;i++){ if(s[i].equals(String.valueOf(i))){ y[i]=y[i]+2;if(y[i]>=300) y[i]=300;b[i].setLocation(x[i],y[i]);}}}else if(e.getKeyCode( )==KeyEvent.VK_LEFT){ for(int i=0;i<=39;i++){ if(s[i].equals(String.valueOf(i))){ x[i]=x[i]-2;if(x[i]<=0) x[i]=0;b[i].setLocation(x[i],y[i]);}}}else if(e.getKeyCode( )==KeyEvent.VK_RIGHT){ for(int i=0;i<=39;i++){ if(s[i].equals(String.valueOf(i))){ x[i]=x[i]+2;if(x[i]>=300) x[i]=300;b[i].setLocation(x[i],y[i]);}}}}public void keyReleased(KeyEvent e){ }public void actionPerformed(ActionEvent e){ for(int i=0;i<40;i++){ if(e.getSource( )==b[i]){ for(int j=0;j<=39;j++)s[j]=b[i].getLabel( );}}}}。

java拼图游戏报告

java拼图游戏报告

目录1 需求分析 (2)1.1功能需求 (2)1.2系统需求 (2)2游戏详细设计 (3)2.1 程序类主要功能介绍 (3)2.2 程序源代码 (3)1)Arg.java (3)2) ButtonClick.java (4)3) CardFrame.java (5)4) GameOver.java (7)5) Grades.java (8)6) Matrix.java (11)7) MenuFrame.java (12)8) point.java (18)9) Puzzle.java (19)10) ShowImage.java (22)11) Split.java (23)12)FileTest.java (24)13)MusicPlay.java (25)3测试过程 (27)1 需求分析1.1功能需求1)让玩家能够用鼠标控制进行菜单的选择2)玩家可以通过点击空白区域来移动有图方块3)给玩家提供暂停游戏的功能4)提供图片预览,方便玩家查看5)提供图片导入功能,是玩家可以用自己喜爱的图片进行游戏6)提供背景音乐功能,优化玩家的游戏氛围7)提供游戏记录功能,让玩家可以不断挑战自己的水平,激发游戏乐趣8)提供关卡难度选择,使得游戏适应人群更为广泛9)提供游戏帮助功能,帮助玩家更快适应游戏10)游戏成功后能给出提示1.2系统需求1)性能需求:实时性好,可靠性强2)设计约束:1.开发工具:jdk 6.0以上2.运行环境:windows xp以上3.满足所定义的各种需求2游戏详细设计2.1 程序类主要功能介绍该游戏程序中包括11个类。

包括:ButtonClick.class、CardFrame.class、GameOver.class、Grades.class、Matrix.class、MenuFrame.class、point.class、Puzzle.class、ShowImage.class、Split.class、FileTest.class、MusicPlay.class。

java课程设计拼图游戏

java课程设计拼图游戏

import java.awt.*;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.image.BufferedImage;import javax.swing.*;public class Pintu extends JFrame implements KeyListener{PicPanel picPanel;JLabel statusText = new JLabel("");JLabel statusimg = new JLabel("yuantu");public static void main(String[] args){Image img = Toolkit.getDefaultToolkit().getImage("D:/tierui/jsp/Pintu/src/img/pintu.jpg");Pintu pintu1 = new Pintu(img);pintu1.setVisible(true);}public Pintu(Image img) throws HeadlessException{picPanel = new PicPanel(img, statusText,statusimg);this.setLayout(new BorderLayout());add(picPanel, BorderLayout.CENTER);add(statusText, BorderLayout.SOUTH);setTitle("拼图游戏");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setSize(600, 400);Dimension size = Toolkit.getDefaultToolkit().getScreenSize();//获得屏幕的标尺int screenWidth = size.width;int screenHeight = size.height;int x = (screenWidth - this.getWidth())/2;int y = (screenHeight - this.getHeight())/2;this.setLocation(x, y);addKeyListener(this);}public void keyPressed(KeyEvent e){int nDirection = picPanel.DIRECTION_NONE;switch (e.getKeyCode()){case KeyEvent.VK_DOWN:nDirection = picPanel.DIRECTION_DOWN;break;case KeyEvent.VK_UP:nDirection = picPanel.DIRECTION_UP;break;case KeyEvent.VK_LEFT:nDirection = picPanel.DIRECTION_LEFT;break;case KeyEvent.VK_RIGHT:nDirection = picPanel.DIRECTION_RIGHT;break;case KeyEvent.VK_F1: // F1键按下,重新开始游戏picPanel.initData();repaint();return;case KeyEvent.VK_Y: // 显示原图if (picPanel.bOnShowAll)picPanel.bOnShowAll = false;elsepicPanel.bOnShowAll = true;repaint();return;default:return;}boolean bCanMove = picPanel.move(nDirection);if (bCanMove){picPanel.nStep++;repaint();}}public void keyReleased(KeyEvent e){}public void keyTyped(KeyEvent e){}}class PicPanel extends JPanel implements MouseListener, Runnable{ BufferedImage[] m_Image = new BufferedImage[9]; // 9个用来装入拼图的图片对象Image m_ImgAll; // 总的大图片int m_nImageNo[][] = new int[3][3]; // 标志现在各个拼图的排列情况final int NO_IMAGE = -1; // 此位置没有拼图final int IMAGE_WIDTH = 120; // 每张拼图的宽final int IMAGE_HEIGHT = 120; // 每张拼图的高final int DIRECTION_UP = 1;final int DIRECTION_DOWN = 2;final int DIRECTION_LEFT = 3;final int DIRECTION_RIGHT = 4;final int DIRECTION_NONE = -1;final int DELTAX = 120; // 标志提示信息区的宽度Thread thTimer; // 计时器线程int nTime = 0; // 已经玩过的时间,以秒为单位boolean bWantStartNewGame = false; // 游戏是否结束,是否需要开始新游戏int nStep = 0; // 已经走的步数int nScore = 0; // 玩家所得的分数int m_nNumOfImg = 0; // 拼图底图所使用的图片的个数String m_sImgName = null; // 记录拼图底图的名字boolean bOnShowAll = false; // 预览的开关JLabel statusText;JPanel panel=new JPanel();public PicPanel(Image img, JLabel statusText,JLabel statusimg){this.statusText = statusText;setBackground(Color.white);m_sImgName = "pintu.jpg";MediaTracker mediaTracker = new MediaTracker(this); // 建立一个监视器.MediaTracker 类是一个跟踪多种媒体对象状态的实用工具类try{// 装载总的大图片m_ImgAll = Toolkit.getDefaultToolkit().getImage("D:/tierui/jsp/Pintu/src/img/" + m_sImgName);mediaTracker.addImage(m_ImgAll, 1);//addImage方法-向此媒体跟踪器正在跟踪的图像列表添加一个图像。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

// Cell.javapackage cn.itcast.picture.ui;import javax.swing.Icon;import javax.swing.JButton;/** 图片小方格类*/public class Cell extends JButton {//带有图片的小方格public Cell(Icon icon) {super(icon);//设置小方格大小this.setSize(150, 150);}//带有图片和文字的小方格public Cell(String text, Icon icon) {super(text, icon);//设置小方格大小this.setSize(150, 150);this.setHorizontalTextPosition(CENTER);//设置文字水平居中显示this.setVerticalTextPosition(CENTER);//设置文字垂直居中显示}//public void move(String direction) {switch (direction) {case "UP":this.setLocation(this.getBounds().x,this.getBounds().y-150);break;case "DOWN":this.setLocation(this.getBounds().x,this.getBounds().y+150);break;case "LEFT":this.setLocation(this.getBounds().x-150,this.getBounds().y);break;case "RIGHT":this.setLocation(this.getBounds().x+150,this.getBounds().y);break;default:break;}}}//PictureCanvas.javapackage cn.itcast.picture.ui;import java.awt.Rectangle;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import javax.swing.ImageIcon;import javax.swing.JOptionPane;import javax.swing.JPanel;/** 拼图类*/public class PictureCanvas extends JPanel implements MouseListener { //静态变量public static int pictureID = 1;//图片IDpublic static int stepNum = 0;//步数private Cell[] cell;private boolean hasAddActionListener = false;//表示是否为小方格添加了点击监听,有为trueprivate Rectangle nullCell;//构造方法public PictureCanvas() {initCanvas();}//初始化public void initCanvas(){//设置拼图区的位置this.setLayout(null);//帧布局//创建12个小方格,并添加到拼图区cell = new Cell[12];for(int i = 0;i < 4;i++){for(int j = 0;j < 3;j++){//加载图片int num = i*3+j;ImageIcon icon = new ImageIcon("picture/"+pictureID+"_"+(num+1)+".gif");//创建图片小方格cell[num] = new Cell(icon);//指定显示位置cell[num].setLocation(150*j+20, i*150+20);//把图片小方格添加到拼图区this.add(cell[num]);}}//删除第12个小方格this.remove(cell[11]);nullCell = new Rectangle(320, 470, 150, 150);hasAddActionListener = false;}//重新加载图片,并添加数字提示public void reloadPictureAddNumber() {for(int i = 0;i < 4;i++){for(int j = 0;j < 3;j++){int num = i*3+j;ImageIcon icon = new ImageIcon("picture/"+pictureID+"_"+(num+1)+".gif");cell[num].setIcon(icon);cell[num].setText(num+1+"");cell[num].setVerticalTextPosition(getY()/2);cell[num].setHorizontalTextPosition(getX()/2);}}}//重新加载图片,并清除数字提示public void reloadPictureClearNumber() {for(int i = 0;i < 4;i++){for(int j = 0;j < 3;j++){int num = i*3+j;ImageIcon icon = new ImageIcon("picture/"+pictureID+"_"+(num+1)+".gif");cell[num].setIcon(icon);cell[num].setText("");}}}//start 对小方格重新排序public void start() {//如果没有给小方格添加监听,则添加监听if(!hasAddActionListener){for(int i=0;i<11;i++){cell[i].addMouseListener(this);}hasAddActionListener=true;}//当第一个小方格离左上角比较近的话,进行空方格与就近方格的随机交换while(cell[0].getBounds().x<=170 && cell[0].getBounds().y<=170){ //获取空方格的位置int nullX = nullCell.getBounds().x;int nullY = nullCell.getBounds().y;//随机产生一个方向,进行空方格的移动互换//产生0-3之间的随机数,代表交换移动方向int direction = (int)(Math.random()*4);switch (direction){case 0://空方格向左交换移动,原左侧方格向右移动nullX -= 150;cellMove(nullX,nullY,"RIGHT");break;case 1://空方格向右交换移动,原右侧方格向左移动nullX += 150;cellMove(nullX, nullY, "LEFT");break;case 2://空方格向上交换移动,原上方方格向下移动nullY -= 150;cellMove(nullX, nullY, "DOWN");break;case 3://空方格向下交换移动,原下方方格向上移动nullY += 150;cellMove(nullX, nullY, "UP");break;}}}private void cellMove(int nullX, int nullY, String direction) {for(int i=0;i<11;i++){if(cell[i].getBounds().x == nullX && cell[i].getBounds().y == nullY){ //当前方格移动cell[i].move(direction);//空方格移动(nullX,nullY)nullCell.setLocation(nullX,nullY);break;}}}@Overridepublic void mouseClicked(MouseEvent e) {}@Overridepublic void mousePressed(MouseEvent e) {//获取当前点击的小方格Cell button = (Cell)e.getSource();//获取所点击方格的x,yint clickX = button.getBounds().x;int clickY = button.getBounds().y;//获取空方格的x,yint nullX = nullCell.getBounds().x;int nullY = nullCell.getBounds().y;//比较如果满足条件,则互换位置if(clickX == nullX && clickY - nullY == 150){//点击为空方格下方方格button.move("UP");}else if (clickX == nullX && clickY - nullY == -150) {//点击为空方格上方方格button.move("DOWN");}else if (clickX - nullX == 150 && clickY == nullY) {//点击为空方格右方方格button.move("LEFT");}else if (clickX - nullX == -150 && clickY == nullY) {//点击为空方格左方方格button.move("RIGHT");}else {return;}//更新空方格位置nullCell.setLocation(clickX, clickY);//拼图区重新绘制this.repaint();//更新游戏移动步数stepNum++;PictureMainFrame.step.setText("步数:" + stepNum);//判断若完成,则提示完成if(isFinish()){JOptionPane.showMessageDialog(this, "恭喜你完成了,共用"+stepNum+"步。

相关文档
最新文档