
正文
java拼图游戏开源代码,c语言拼图游戏代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
急求用JAVA编写的图形化界面拼图小游戏代码!
个人见解,总体需要两个二维数组(一个存储正确图片排列 Array1 String[][],一个随机生成图片排列Array2 String[][]),一个一维数组来存储图片的名称Array3 String[],。
(1)如何实现图片移动
使用带图片的按钮(button =new button(getImage(Array[2][4]))),然后通过单击事件来更改按钮的图片来源。 把被点击的按钮的图片路径更新到空白按钮,并且把被点击的按钮图片更新的成空白。其实就是变换两个的二维数组成员的值。更新Array2中的值,然后重绘按钮
如 Array[2][3]=“3.image”
Array[2][4]=“”
图片3.image右移
Array[2][3]=“”
Array[2][4]=“3.image”
(2)如何判断被单击的网格与空白的网格是否相邻
后台使用一个二维数组Array2来做映射。通过二维数组的下标来判断,如Array[2][3]可以知道Array[2][4]是它右边的那个。
(3)如何实现图片的随机摆放
比如有9个图片,你可以命名1-9,然后初始化一个长度为9的一维String 数组Array3来存储图片的名称,
使用随机函数给二维数组Array2赋值,如Array2[2][3]=Array3[random()],这里要判断这个图片是否已被使用过,可以通过遍历Array2来确定当前Array3这个值是否已经在Array2中了
最后通过Array1 和Array2来比较,用户的拼图是否正确。
语言组织能力有限。讲不太清楚。
相关问答
Q1: 求java小游戏源代码
表1. CheckerDrag.java
// CheckerDrag.javaimport java.awt.*;import java.awt.event.*;public class CheckerDrag extends java.applet.Applet{ // Dimension of checkerboard square. // 棋盘上每个小方格的尺寸 final static int SQUAREDIM = 40; // Dimension of checkerboard -- includes black outline. // 棋盘的尺寸 – 包括黑色的轮廓线 final static int BOARDDIM = 8 * SQUAREDIM + 2; // Dimension of checker -- 3/4 the dimension of a square. // 棋子的尺寸 – 方格尺寸的3/4 final static int CHECKERDIM = 3 * SQUAREDIM / 4; // Square colors are dark green or white. // 方格的颜色为深绿色或者白色 final static Color darkGreen = new Color (0, 128, 0); // Dragging flag -- set to true when user presses mouse button over checker // and cleared to false when user releases mouse button. // 拖动标记 --当用户在棋子上按下鼠标按键时设为true, // 释放鼠标按键时设为false boolean inDrag = false; // Left coordinate of checkerboard's upper-left corner. // 棋盘左上角的左方向坐标 int boardx; // Top coordinate of checkerboard's upper-left corner. //棋盘左上角的上方向坐标 int boardy; // Left coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的左方向坐标 int ox; // Top coordinate of checker rectangle origin (upper-left corner). // 棋子矩形原点(左上角)的上方向坐标 int oy; // Left displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的左方向位移 int relx; // Top displacement between mouse coordinates at time of press and checker // rectangle origin. // 在按键时的鼠标坐标与棋子矩形原点之间的上方向位移 int rely; // Width of applet drawing area. // applet绘图区域的宽度 int width; // Height of applet drawing area. // applet绘图区域的高度 int height; // Image buffer. // 图像缓冲 Image imBuffer; // Graphics context associated with image buffer. // 图像缓冲相关联的图形背景 Graphics imG; public void init () { // Obtain the size of the applet's drawing area. // 获取applet绘图区域的尺寸 width = getSize ().width; height = getSize ().height; // Create image buffer. // 创建图像缓冲 imBuffer = createImage (width, height); // Retrieve graphics context associated with image buffer. // 取出图像缓冲相关联的图形背景 imG = imBuffer.getGraphics (); // Initialize checkerboard's origin, so that board is centered. // 初始化棋盘的原点,使棋盘在屏幕上居中 boardx = (width - BOARDDIM) / 2 + 1; boardy = (height - BOARDDIM) / 2 + 1; // Initialize checker's rectangle's starting origin so that checker is // centered in the square located in the top row and second column from // the left. // 初始化棋子矩形的起始原点,使得棋子在第一行左数第二列的方格里居中 ox = boardx + SQUAREDIM + (SQUAREDIM - CHECKERDIM) / 2 + 1; oy = boardy + (SQUAREDIM - CHECKERDIM) / 2 + 1; // Attach a mouse listener to the applet. That listener listens for // mouse-button press and mouse-button release events. // 向applet添加一个用来监听鼠标按键的按下和释放事件的鼠标监听器 addMouseListener (new MouseAdapter () { public void mousePressed (MouseEvent e) { // Obtain mouse coordinates at time of press. // 获取按键时的鼠标坐标 int x = e.getX (); int y = e.getY (); // If mouse is over draggable checker at time // of press (i.e., contains (x, y) returns // true), save distance between current mouse // coordinates and draggable checker origin // (which will always be positive) and set drag // flag to true (to indicate drag in progress). // 在按键时如果鼠标位于可拖动的棋子上方 // (也就是contains (x, y)返回true),则保存当前 // 鼠标坐标与棋子的原点之间的距离(始终为正值)并且 // 将拖动标志设为true(用来表明正处在拖动过程中) if (contains (x, y)) { relx = x - ox; rely = y - oy; inDrag = true; } } boolean contains (int x, int y) { // Calculate center of draggable checker. // 计算棋子的中心位置 int cox = ox + CHECKERDIM / 2; int coy = oy + CHECKERDIM / 2; // Return true if (x, y) locates with bounds // of draggable checker. CHECKERDIM / 2 is the // radius. // 如果(x, y)仍处于棋子范围内则返回true // CHECKERDIM / 2为半径 return (cox - x) * (cox - x) + (coy - y) * (coy - y) CHECKERDIM / 2 * CHECKERDIM / 2; } public void mouseReleased (MouseEvent e) { // When mouse is released, clear inDrag (to // indicate no drag in progress) if inDrag is // already set. // 当鼠标按键被释放时,如果inDrag已经为true, // 则将其置为false(用来表明不在拖动过程中) if (inDrag) inDrag = false; } }); // Attach a mouse motion listener to the applet. That listener listens // for mouse drag events. //向applet添加一个用来监听鼠标拖动事件的鼠标运动监听器 addMouseMotionListener (new MouseMotionAdapter () { public void mouseDragged (MouseEvent e) { if (inDrag) { // Calculate draggable checker's new // origin (the upper-left corner of // the checker rectangle). // 计算棋子新的原点(棋子矩形的左上角) int tmpox = e.getX () - relx; int tmpoy = e.getY () - rely; // If the checker is not being moved // (at least partly) off board, // assign the previously calculated // origin (tmpox, tmpoy) as the // permanent origin (ox, oy), and // redraw the display area (with the // draggable checker at the new // coordinates). // 如果棋子(至少是棋子的一部分)没有被 // 移出棋盘,则将之前计算的原点 // (tmpox, tmpoy)赋值给永久性的原点(ox, oy), // 并且刷新显示区域(此时的棋子已经位于新坐标上) if (tmpox boardx tmpoy boardy tmpox + CHECKERDIM boardx + BOARDDIM tmpoy + CHECKERDIM boardy + BOARDDIM) { ox = tmpox; oy = tmpoy; repaint (); } } } }); } public void paint (Graphics g) { // Paint the checkerboard over which the checker will be dragged. // 在棋子将要被拖动的位置上绘制棋盘 paintCheckerBoard (imG, boardx, boardy); // Paint the checker that will be dragged. // 绘制即将被拖动的棋子 paintChecker (imG, ox, oy); // Draw contents of image buffer. // 绘制图像缓冲的内容 g.drawImage (imBuffer, 0, 0, this); } void paintChecker (Graphics g, int x, int y) { // Set checker shadow color. // 设置棋子阴影的颜色 g.setColor (Color.black); // Paint checker shadow. // 绘制棋子的阴影 g.fillOval (x, y, CHECKERDIM, CHECKERDIM); // Set checker color. // 设置棋子颜色 g.setColor (Color.red); // Paint checker. // 绘制棋子 g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13); } void paintCheckerBoard (Graphics g, int x, int y) { // Paint checkerboard outline. // 绘制棋盘轮廓线 g.setColor (Color.black); g.drawRect (x, y, 8 * SQUAREDIM + 1, 8 * SQUAREDIM + 1); // Paint checkerboard. // 绘制棋盘 for (int row = 0; row 8; row++) { g.setColor (((row 1) != 0) ? darkGreen : Color.white); for (int col = 0; col 8; col++) { g.fillRect (x + 1 + col * SQUAREDIM, y + 1 + row * SQUAREDIM, SQUAREDIM, SQUAREDIM); g.setColor ((g.getColor () == darkGreen) ? Color.white : darkGreen); } } } // The AWT invokes the update() method in response to the repaint() method // calls that are made as a checker is dragged. The default implementation // of this method, which is inherited from the Container class, clears the // applet's drawing area to the background color prior to calling paint(). // This clearing followed by drawing causes flicker. CheckerDrag overrides // update() to prevent the background from being cleared, which eliminates // the flicker. // AWT调用了update()方法来响应拖动棋子时所调用的repaint()方法。该方法从 // Container类继承的默认实现会在调用paint()之前,将applet的绘图区域清除 // 为背景色,这种绘制之后的清除就导致了闪烁。CheckerDrag重写了update()来 // 防止背景被清除,从而消除了闪烁。 public void update (Graphics g) { paint (g); }}
Q2: java做的拼图游戏怎么实现更换图片哦
编写拼图按钮的监听器类,该类为主类的内部类。
在actionPerformed()方法中,首先获得空白按钮和被单击按钮的所在行和列,
然后判断这两个按钮是否相邻,如果相邻则将被单击按钮显示的图片移动到空白按钮上,并令被单击按钮显示空白图片,以及将在类中声明的空白按钮对象设置为被单击的按钮对象
思路就这样,我给你个示例代码,你参考参考哦:
class ImgButtonAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
String emptyName = emptyButton.getName();
char emptyRow = emptyName.charAt(0);
char emptyCol = emptyName.charAt(1);
JButton clickButton = (JButton) e.getSource();
String clickName =clickButton.getName();
char clickRow = clickName.charAt(0);
char clickCol = clickName.charAt(1);
if(Math.abs(clickRow - emptyRow) + Math.abs(clickCol - emptyCol) == 1) {
emptyButton.setIcon(clickButton.getIcon()) ;
clickButton.setIcon(new ImageIcon("img/00.jpg"));
emptyButton = clickButton;
}
}
}
大概就这样了
希望对你有帮助哈
Q3: 用JAVA编写一个小程序 比如拼图游戏 推箱子 猜数字 。。。。。。请附详细注释 因为要发表 谢谢大家!
我写的一个猜数字游戏,希望对你有用,代码如下:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class GuessNumber {
static int trys, A, B;
static String r, t;
public static int[] MakeGuessNumber(){//随机生成一个无重复数字的四位数
Random r = new Random();
int guess[] = new int[4];
for(int i=0; i4; i++){
guess[i] = r.nextInt(10);
for(int j=i-1; j=0; j--){
if(guess[i]==guess[j]){
i--;
break;}
}
}
return guess;
}
public static String getRundom(){//将此四位数转化为字符串
int guess[]=MakeGuessNumber();
return ""+guess[0]+guess[1]+guess[2]+guess[3];
}
public static void messageDialog(Object o){
JOptionPane.showMessageDialog(null, o);
}
public static void guessNumber(){//主要算法实现部分
r=getRundom();
//System.out.println(r);
JFrame jf=new JFrame();
JButton b1=new JButton("新游戏");
JLabel l1=new JLabel("输入:");
final JTextField jtf=new JTextField(10);
JButton b2=new JButton("提交");
final JTextArea jta=new JTextArea(10,10);
jta.append(" "+"Guess"+" "+"Result"+"\n");
JScrollPane scrollPane=new JScrollPane(jta);
JPanel jp1=new JPanel();
jp1.add(l1);
jp1.add(jtf);
jp1.add(b2);
jf.add(b1,BorderLayout.NORTH);
jf.add(jp1,BorderLayout.CENTER);
jf.add(scrollPane,BorderLayout.SOUTH);
b1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
trys=0;
A=0;
B=0;
jta.setText(" "+"Guess"+" "+"Result"+"\n");
jtf.setText("");
r=getRundom();
//System.out.println(r);
}
});
b2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
t=jtf.getText();
A=0;
B=0;
if(t.length()!=4||t.substring(0, 1).equals(t.substring(1, 2))
||t.substring(0, 1).equals(t.substring(2, 3))
||t.substring(0, 1).equals(t.substring(3, 4))
||t.substring(1, 2).equals(t.substring(2, 3))
||t.substring(1, 2).equals(t.substring(3, 4))
||t.substring(2, 3).equals(t.substring(3, 4))
||!t.matches("[0-9]*"))
messageDialog("Wrong Input!");
else{
jtf.setText("");
trys++;
if(t.substring(0, 1).equals(r.substring(0, 1)))
A++;
if(t.substring(0, 1).equals(r.substring(1, 2)))
B++;
if(t.substring(0, 1).equals(r.substring(2, 3)))
B++;
if(t.substring(0, 1).equals(r.substring(3, 4)))
B++;
if(t.substring(1, 2).equals(r.substring(1, 2)))
A++;
if(t.substring(1, 2).equals(r.substring(0, 1)))
B++;
if(t.substring(1, 2).equals(r.substring(2, 3)))
B++;
if(t.substring(1, 2).equals(r.substring(3, 4)))
B++;
if(t.substring(2, 3).equals(r.substring(2, 3)))
A++;
if(t.substring(2, 3).equals(r.substring(0, 1)))
B++;
if(t.substring(2, 3).equals(r.substring(1, 2)))
B++;
if(t.substring(2, 3).equals(r.substring(3, 4)))
B++;
if(t.substring(3, 4).equals(r.substring(3, 4)))
A++;
if(t.substring(3, 4).equals(r.substring(0, 1)))
B++;
if(t.substring(3, 4).equals(r.substring(1, 2)))
B++;
if(t.substring(3, 4).equals(r.substring(2, 3)))
B++;
jta.append(trys+" "+t+" "+A+"A"+B+"B"+"\n");
if(A==4){
if(trys=4)
messageDialog("You win after "+trys+" trys!");
else if(trys=3)
messageDialog("You win after only "+trys+" trys!");
}
}
}
});
jf.setSize(300, 300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
guessNumber();
}
}
我没有进行详细注释,这个程序挺好理解的,你可以自己再看一下
Q4: 求ready to java 游戏代码
我也是刚学不久,不太会,自己编了一个拼图的小游戏,你看一吧。
import java.lang.Math.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class MainFrame extends JFrame implements ActionListener{ //定义整个框架
private JButton[] jb = new JButton[8];
private JButton jbs = new JButton("开 局");
private JButton jbres = new JButton("重新开始");
private JPanel jp1 = new JPanel();
private JPanel jp2 = new JPanel();
private int[] n = new int[9];
private int[] n1 = new int[9];
private int position = 8,p,q;
private boolean bl,startbl=false;
private JLabel jl = new JLabel();
private int count = 0;
private JLabel jl1 = new JLabel(" "+Integer.toString(0));
public MainFrame(){ //框架的构造方法
int i;
for(int j = 0; j n.length; j++){
n[j] = j;
n1[j] = n[j];
}
for(i = 0; i jb.length; i++){ //给每个按钮赋相应的值,并注监听器
jb[i] = new JButton(Integer.toString(i+1));
jb[i].setFont(new Font("宋体",Font.BOLD,48));
jp2.add(jb[i]);
jb[i].addActionListener(this);
}
for(i = 0; i n.length; i++){
if(n[i] == position)
jp2.add(jl);
else
jp2.add(jb[n[i]]);
}
jp2.setLayout(new GridLayout(3,3));//注册监听器
jbs.addActionListener(this);
jbres.addActionListener(this);
jp1.add(jbs);
jp1.add(jbres);
jp1.add(jl1);
jp1.setLayout(new FlowLayout()); //将jp1设置为流布局
setLayout(new BorderLayout()); //整体布局为边界布局
this.add("North",jp1);
this.add("Center",jp2);
this.setTitle("拼图游戏");
this.setBounds(100,100,300,350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //实现关闭按钮
this.setResizable(false);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){ //实现按钮的事件
if(e.getSource()==jbres){ // 重新开始按钮事件
for(int j = 0; jn.length;j++)
n[j] = n1[j];
reShow();
startbl=true;
count = 0;
jl1.setText(" "+Integer.toString(0));
}
else if(e.getSource()==jbs) //开局按钮事件
this.Init();
else if(startbl){ //按钮1-8移动事件
for(int i = 0; i jb.length; i++)
if(e.getSource() == jb[i]){
//System.out.println(i+1);
for(int a=0;an.length;a++){
if(n[a]==i)
p=a;
if(n[a]==position)
q=a;
}
}
if(p != 0 p != 1 p != 2)
if((p-3) == q)
swap(p,q);
if(p != 0 p != 3 p != 6)
if((p-1) == q)
swap(p,q);
if(p != 2 p != 5 p != 8)
if((p+1) == q)
swap(p,q);
if(p != 6 p != 7 p != 8)
if((p+3) == q)
swap(p,q);
}
}
public void swap(int x,int y){ //按钮1-8与空白图片交换
int z;
z = n[x];
n[x] = n[y];
n[y]=z;
jl1.setText(" "+Integer.toString(++count));
reShow();
win();
}
public void Init(){ //随机产生游戏界面
int i=0,j,x;
boolean bl ;
while(i9){
bl = true;
x=(int)(Math.random()*9);
for(j=0;ji;j++)
if(n[j] == x)
bl=false;
if(bl){
n [i++] = x;
n1[i-1] = x;
}
}
reShow();
startbl=true;
count = 0;
jl1.setText(" "+Integer.toString(0));
}
public void reShow(){ //对游戏界面的重写
for(int i = 0; i n.length; i++){
if(n[i] == position)
jp2.add(jl);
else
jp2.add(jb[n[i]]);
}
jp2.revalidate();
}
public void win(){ //判断是否成功
boolean winbl=true;
for(int i=0;in.length;i++)
if(n[i]!=i)
winbl=false;
if(winbl){
JOptionPane.showMessageDialog(this,"祝贺你,你成功了! "+"你用了"+Integer.toString(count)+"步","",JOptionPane.INFORMATION_MESSAGE);
startbl=false;
}
}
}
public class Collage { // 主函数类
public static void main(String[] args){
new MainFrame();
}
}







