java游戏--拼图
2010-07-31 14:17
155 查看
package lession.game.game1;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ppuzzle extends Applet{
Image imgppuzzle,buffer;
Point fifteen=new Point(3,3);
Canvas screen;
Graphics gs,gb;
Button bstart=new Button("开始游戏");
Button bsee=new Button("正确图象");
boolean running;
int sx,sy;
int[][]map={{0,4,8,12},{1,5,9,13},{2,6,10,14},{3,7,11,15}};
public void init(){
PrepareImage();
sx=imgppuzzle.getWidth(this)/4; //?????????????????????????????
sy=imgppuzzle.getHeight(this)/4;
this.setBackground(Color.blue);
InitScreen();
InitButtons();
add(screen);
add(bstart);
add(bsee);
this.setSize(sx*6,sy*4+10);
// this.setVisible(true);
//System.out.println("ddddddd");
}
public void PrepareImage(){
/*Class cl=this.getClass();
URL url=cl.getResource("2.gif");
imgppuzzle=getImage(url);
*/
imgppuzzle=getImage(getCodeBase(),"image/2.gif");
MediaTracker mt=new MediaTracker(this);
mt.addImage(imgppuzzle,0);
try{
mt.waitForAll();
}catch(Exception e){}
buffer=createImage(imgppuzzle.getWidth(this),imgppuzzle.getHeight(this));
gb=buffer.getGraphics();
}
public void InitMap(){
Random rnd=new Random();
int temp,x1,y1,x2,y2;
for(int i=0;i<100;i++){
x1=rnd.nextInt(4);
x2=rnd.nextInt(4);
y1=rnd.nextInt(4);
y2=rnd.nextInt(4);
temp=map[x1][y1];
map[x1][y1]=map[x2][y2];
map[x2][y2]=temp;
}
outer:
for(int j=0;j<4;j++)
for(int i=0;i<4;i++){
if(map[i][j]==15){
fifteen.setLocation(i,j); //设置第15快图象的显示位置
break outer;
}
}
}
public void InitScreen(){
screen=new Canvas(){
public void paint(Graphics g){
if(gs==null) gs=getGraphics();
if(running) DrawScreen();
else //如果游戏没开始显示整个图象
g.drawImage(imgppuzzle,0,0,this);
}
};
screen.setSize(imgppuzzle.getWidth(this),imgppuzzle.getHeight(this));
screen.addMouseListener(new MouseAdapter (){
public void mousePressed(MouseEvent me){
// System.out.println("选择了块1");
if(running==false) return;
// System.out.println("选择了块2");
int x=me.getX()/sx;
int y=me.getY()/sy;
int fx=(int)fifteen.getX();
int fy=(int)fifteen.getY();
// System.out.println(x+","+y+" "+fx+","+fy);
if((Math.abs(fx-x)+Math.abs(fy-y))>=2) return;
// System.out.println("选择了块3");
if(map[x][y]==15) return ;
// System.out.println("选择了块4");
map[fx][fy]=map[x][y]; //交换两快图象的位置 重新设置
map[x][y]=15;
fifteen.setLocation(x,y);
//System.out.println("选择了块");
DrawScreen();
}
});
}
public void InitButtons(){
bstart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
InitMap();
DrawScreen();
running=true;
bsee.setLabel("显示图象");
}
});
bsee.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(bsee.getLabel().equals("继续")){//??????????????????????????????
DrawScreen();
bsee.setLabel("显示图象");
}
else{
gs.drawImage(imgppuzzle,0,0,screen);
bsee.setLabel("继续");
}
}
});
}
public void DrawScreen(){
gb.clearRect(0,0,sx*4,sy*4);
for(int j=0;j<4;j++)
for(int i=0;i<4;i++){
if(map[i][j]!=15) DrawSegment(map[i][j],i,j);
}
gs.drawImage(buffer,0,0,screen);
}
public void DrawSegment(int seg,int x,int y){
int dx=seg%4 *sx;
int dy=seg/4 *sy;
gb.drawImage(imgppuzzle,x*sx,y*sy,x*sx+sx-1,y*sy+sy-1,dx,dy,dx+sx-1,dy+sy-1,screen);
}
}
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ppuzzle extends Applet{
Image imgppuzzle,buffer;
Point fifteen=new Point(3,3);
Canvas screen;
Graphics gs,gb;
Button bstart=new Button("开始游戏");
Button bsee=new Button("正确图象");
boolean running;
int sx,sy;
int[][]map={{0,4,8,12},{1,5,9,13},{2,6,10,14},{3,7,11,15}};
public void init(){
PrepareImage();
sx=imgppuzzle.getWidth(this)/4; //?????????????????????????????
sy=imgppuzzle.getHeight(this)/4;
this.setBackground(Color.blue);
InitScreen();
InitButtons();
add(screen);
add(bstart);
add(bsee);
this.setSize(sx*6,sy*4+10);
// this.setVisible(true);
//System.out.println("ddddddd");
}
public void PrepareImage(){
/*Class cl=this.getClass();
URL url=cl.getResource("2.gif");
imgppuzzle=getImage(url);
*/
imgppuzzle=getImage(getCodeBase(),"image/2.gif");
MediaTracker mt=new MediaTracker(this);
mt.addImage(imgppuzzle,0);
try{
mt.waitForAll();
}catch(Exception e){}
buffer=createImage(imgppuzzle.getWidth(this),imgppuzzle.getHeight(this));
gb=buffer.getGraphics();
}
public void InitMap(){
Random rnd=new Random();
int temp,x1,y1,x2,y2;
for(int i=0;i<100;i++){
x1=rnd.nextInt(4);
x2=rnd.nextInt(4);
y1=rnd.nextInt(4);
y2=rnd.nextInt(4);
temp=map[x1][y1];
map[x1][y1]=map[x2][y2];
map[x2][y2]=temp;
}
outer:
for(int j=0;j<4;j++)
for(int i=0;i<4;i++){
if(map[i][j]==15){
fifteen.setLocation(i,j); //设置第15快图象的显示位置
break outer;
}
}
}
public void InitScreen(){
screen=new Canvas(){
public void paint(Graphics g){
if(gs==null) gs=getGraphics();
if(running) DrawScreen();
else //如果游戏没开始显示整个图象
g.drawImage(imgppuzzle,0,0,this);
}
};
screen.setSize(imgppuzzle.getWidth(this),imgppuzzle.getHeight(this));
screen.addMouseListener(new MouseAdapter (){
public void mousePressed(MouseEvent me){
// System.out.println("选择了块1");
if(running==false) return;
// System.out.println("选择了块2");
int x=me.getX()/sx;
int y=me.getY()/sy;
int fx=(int)fifteen.getX();
int fy=(int)fifteen.getY();
// System.out.println(x+","+y+" "+fx+","+fy);
if((Math.abs(fx-x)+Math.abs(fy-y))>=2) return;
// System.out.println("选择了块3");
if(map[x][y]==15) return ;
// System.out.println("选择了块4");
map[fx][fy]=map[x][y]; //交换两快图象的位置 重新设置
map[x][y]=15;
fifteen.setLocation(x,y);
//System.out.println("选择了块");
DrawScreen();
}
});
}
public void InitButtons(){
bstart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
InitMap();
DrawScreen();
running=true;
bsee.setLabel("显示图象");
}
});
bsee.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
if(bsee.getLabel().equals("继续")){//??????????????????????????????
DrawScreen();
bsee.setLabel("显示图象");
}
else{
gs.drawImage(imgppuzzle,0,0,screen);
bsee.setLabel("继续");
}
}
});
}
public void DrawScreen(){
gb.clearRect(0,0,sx*4,sy*4);
for(int j=0;j<4;j++)
for(int i=0;i<4;i++){
if(map[i][j]!=15) DrawSegment(map[i][j],i,j);
}
gs.drawImage(buffer,0,0,screen);
}
public void DrawSegment(int seg,int x,int y){
int dx=seg%4 *sx;
int dy=seg/4 *sy;
gb.drawImage(imgppuzzle,x*sx,y*sy,x*sx+sx-1,y*sy+sy-1,dx,dy,dx+sx-1,dy+sy-1,screen);
}
}
相关文章推荐
- Java 简单拼图游戏(实现音乐播放功能)
- java雷电游戏源码项目
- 人生如梦游戏间,RPG游戏开源开发讲座(JAVA篇)[3]——邯郸学步
- java编写的猜数字游戏
- 微信游戏打飞机游戏制作(java模拟微信打飞机游戏)
- 【开源java游戏框架libgdx专题】-14-系统控件-Skin类
- Java小游戏之控制台小游戏
- java游戏的安装
- java 用栈的原理(穷举)实现迷宫游戏
- Java 游戏编程基础 做个碰撞的小球
- Head First Java (第二版)学习记录 5 - 编写程序 DotCom 初级游戏
- 人生如梦游戏间,RPG游戏开源开发讲座(JAVA篇)[0]——月晕础润 推荐
- 打字母的游戏&Java入门
- Java 游戏 扫雷 Applet 多媒体版
- Python加pyGame实现的简单拼图游戏实例
- 新Java技术工具亮相游戏开发商大会
- Introduction to Java Programming编程题7.11<硬币游戏:显示正反面>
- Android自定义控件:做一个拼图游戏
- 关于java设计模式与极品飞车游戏的思考
- (2)面向对象开发_java石头剪刀布小游戏大厅之人类玩家-初学