拼图游戏

Yard.java

package jigsaw;  
  
import javax.swing.JFrame;  
public class Yard extends JFrame {  
    
    Win win = new Win();  
    public Yard()
    {  
        this.setTitle("朱冠亚的拼图游戏");  
        this.setSize(500,500);  
        this.setLocation(200,100);  
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        this.add(win);  
        this.setVisible(true);  
    }  
    
    public static void main(String args[])  
     {  
         new Yard();  
     }  
}  

Win.java

package jigsaw; 
import java.awt.Color;  
import java.awt.Font;  
import java.awt.Graphics;  
import java.awt.Image;  
import java.awt.event.MouseAdapter;  
import java.awt.event.MouseEvent;  
import java.awt.event.MouseListener;  
import java.io.File;  
import java.io.IOException;  
import java.util.Random;  
 
import javax.imageio.ImageIO;  
import javax.swing.JButton;  
import javax.swing.JPanel;  

public class Win extends JPanel implements MouseListener {  
  
    private int[][] tus = new int[3][3];
    private boolean flag = false;  
    private boolean start;
    private int row, col;  
    Font font = new Font("宋体", Font.BOLD, 50);  
    private JButton b = new JButton("开始");  
  
    public Win() {
    				for (int i = 0; i < 3; i++)  
            	for (int j = 0; j < 3; j++) {  
            	    tus[i][j] = i * 3 + j + 1;  
           	}
           	start = true;
        b.addMouseListener(new MouseAdapter() {  
            public void mouseClicked(MouseEvent e) {  
                if (e.getSource() == b) {  
                    flag = false;  
                    start = false; 
                    disOrder();  
                    repaint();  
                }  
            }  
        });  
        this.add(b);  
        this.addMouseListener(this);  
    }  
  
    private void disOrder()
    {  
  
        for (int i = 0; i < 3; i++)  
            for (int j = 0; j < 3; j++) {  
                tus[i][j] = i * 3 + j + 1;  
            }  
        Random r = new Random();  
        int rand;  
  
        for (int count = 0; count < 200; ) {  
            rand = r.nextInt(4)+1;  
            //UP
						if (rand==1)
						for (int i = 0;i<3;i++)
							for(int j = 0;j<3;j++)
								if(tus[i][j]==9 && i!=0)
								{  
									tus[i][j] = tus[i-1][j];
            			tus[i-1][j] = 9;	
            			count++;
									}
					//DOWN				
					if (rand==2)
						for (int i = 0;i<3;i++)
							for(int j = 0;j<3;j++)
								if(tus[i][j]==9 && i!=2)
								{  
									tus[i][j] = tus[i+1][j];
            			tus[i+1][j] = 9;
            			count++;
									}
					//LEFT				
				  if (rand==3)
						for (int i = 0;i<3;i++)
							for(int j = 0;j<3;j++)
								if(tus[i][j]==9 && j!=0)
								{  
									tus[i][j] = tus[i][j-1];
            			tus[i][j-1] = 9;	
            			count++;
									}
					//RIGHT				
					if (rand==4)
						for (int i = 0;i<3;i++)
							for(int j = 0;j<3;j++)
								if(tus[i][j]==9 && j!=2)
								{  
									tus[i][j] = tus[i][j+1];
            			tus[i][j+1] = 9;	
            			count++;
									}
        }  
    }
    @Override  
    public void paintComponent(Graphics g) {  
        super.paintComponent(g);  
        g.drawRect(100, 100, 300, 300);  
        for (int i = 0; i < 3; i++)  
            for (int j = 0; j < 3; j++) {  
                // tus[i][j] = i*3+j+1;  
                if (!(tus[i][j] == 9 && !start)) {  
                    Image image = null;  
                    try {  
                        String str = "img/tu" + tus[i][j] + ".jpg";  
                        image = ImageIO.read(new File(str));  
                    } catch (IOException e) {  
                        e.printStackTrace();  
                    }  
                    g.drawImage(image, 100 + 100 * j, 100 + 100 * i, 100, 100,  
                            this);  
  
                                    // showImage(g,this,"img/tu"+tus[i][j]+".jpg",100+100*j,100+100*i);  
                }  
            }  
        g.setColor(Color.RED);  
        g.setFont(font);  
        if (flag)  
        g.drawString("你这厉害爸妈造不?", 25, 150);  
  
    }  
  
    /* 
     * void showImage(Graphics g,ShellWin win,String img_source,int x,int y) { 
     * try { Image image=ImageIO.read(new File(img_source)); g.drawImage(image, 
     * x, y, win); } catch (IOException e) { e.printStackTrace(); } } 
     */  
  
    @Override  
    public void mouseClicked(MouseEvent e) {  
        if (flag)  
            return; // 如果已经成功完成  
  
        int x = e.getX();  
        int y = e.getY();  
        row = y / 100 - 1;  
        col = x / 100 - 1; // 得到行数和列数 图片位置  
  
        swap();  
        if (isWin()) {  
            flag = true;  
        }  
        repaint();  
  
    }  
  
    private boolean isWin() {  
        for (int i = 0; i < 3; i++)  
            for (int j = 0; j < 3; j++) {  
                if (tus[i][j] != i * 3 + j + 1) {  
                    return false;  
                }  
            }  
        return true;  
    }  
  
    private void swap() {  
        if (row != 0 && tus[row - 1][col] == 9) {  
            tus[row - 1][col] = tus[row][col];  
            tus[row][col] = 9;  
        }  
  
        else if (row != 2 && tus[row + 1][col] == 9) {  
            tus[row + 1][col] = tus[row][col];  
            tus[row][col] = 9;  
        } else if (col != 0 && tus[row][col - 1] == 9) {  
            tus[row][col - 1] = tus[row][col];  
            tus[row][col] = 9;  
        }  
  
        else if (col != 2 && tus[row][col + 1] == 9) {  
            tus[row][col + 1] = tus[row][col];  
            tus[row][col] = 9;  
        }  
    }  
  
    @Override  
    public void mouseEntered(MouseEvent arg0) {  
        // TODO Auto-generated method stub  
  
    }  
  
    @Override  
    public void mouseExited(MouseEvent arg0) {  
        // TODO Auto-generated method stub  
  
    }  
  
    @Override  
    public void mousePressed(MouseEvent arg0) {  
        // TODO Auto-generated method stub  
  
    }  
  
    @Override  
    public void mouseReleased(MouseEvent arg0) {  
        // TODO Auto-generated method stub  
  
    }  
  
} 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Pygame拼图游戏是一种基于Pygame库开发的经典拼图游戏,主要目的是将乱序的图像碎片重新拼接成完整的原图。与传统的拼图游戏相比,Pygame拼图游戏具有更加生动、立体和互动的特点。 在Pygame拼图游戏中,首先需要准备一张完整的图像。这张图像会被切割成若干个小块,然后随机打乱它们的顺序。游戏开始时,玩家需要通过点击空白块的周围块来移动块的位置,最终将它们按照原图的顺序拼接起来。 Pygame拼图游戏具有以下几个特点。首先,游戏界面以图像为基础,并且使用Pygame库中的相关函数来实现图像的加载和切割等操作。其次,游戏中的块可以通过键盘或鼠标来移动,提供了多种操作方式。此外,游戏还可以设置计时器和计步器,用于记录玩家完成拼图所花费的时间和步数。 Pygame拼图游戏的开发相对简单,但也需要一定的编程基础。开发者需要了解Pygame库的基本用法,如图像加载、事件监听、绘制等。在实现游戏逻辑时,需要设计算法来实现块的移动和碰撞检测等功能。此外,还可以添加音效和背景音乐等元素,提升游戏的趣味性和娱乐性。 总而言之,Pygame拼图游戏是一款简单而有趣的游戏,适合初学者学习Pygame库的使用,也能够提供娱乐和放松的体验。无论是对图像处理和游戏开发有兴趣的人,还是想挑战自己拼图能力的人,都可以尝试开发或者体验这款游戏。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值