📄 playingscence.java
字号:
package pp.scence;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import like.graphics.*;
import like.scence2D.*;
import pp.actor.*;
import java.awt.event.ActionListener;
import java.util.EventListener;
import java.awt.event.MouseListener;
public class PlayingScence extends Scence
{
Map map;
ResourceGroup rGroup;
PlayerGroup playerGroup;
PaoPaoGroup ppGroup;
Image exit_1;
boolean enterButton;
//屏外缓冲
VolatileImage offscreen;
//把所有的地表层和不变背景拼成一张,有利于提升绘制速度
Image tile;
Image backGround;
//Fraise层(泡泡是一种特殊的Fraise)
public Fraise [][]fraises = new Fraise[13][15];
//道具层
//TD留待添加
public Propertity [][] pro = new Propertity[13][15];
//player层
int playerNum = 2;
String name1,name2;
Player [] players;
public PlayingScence(ScenceManger sm)
{
super(sm);
this.setSize(800,600);
this.setUndecorated(true);
this.setVisible(true);//这个不早点写,getGraphics()==null,
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
this.setLocation((device.getDisplayMode().getWidth()-this.getWidth())/2,
(device.getDisplayMode().getHeight()-this.getHeight())/2);
}
public int getPlayerNum() {return this.playerNum;}
public Player getPlayer(int id) {return this.players[id];}
public PaoPaoGroup getPaoPaoGroup() {return this.ppGroup;}
public ResourceGroup getResourceGroup() {return this.rGroup;}
public void init()
{
this.exit_1 = ((ImageIcon)htPros.get("exit_1")).getImage();
if(exit_1==null || exit_1.getWidth(this)<0) System.out.println(1);
this.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if(e.getX()>=662&&e.getX()<=662+108
&&e.getY()>=554&&e.getY()<=554+37)
over();
}
});
this.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseMoved(MouseEvent e)
{
if(e.getX()>=662&&e.getX()<=662+108
&&e.getY()>=554&&e.getY()<=554+37)
enterButton = true;
else enterButton = false;
}
});
this.fullWidth = 800;
this.fullHeight = 600;
//=================载入资源=================
//载入地图
loadMap();
//加载各种Group
rGroup = new ResourceGroup(map.configureName);
rGroup.init(this);
playerGroup = new PlayerGroup();
playerGroup.init(this);
ppGroup = new PaoPaoGroup();
ppGroup.init(this);
//加载背景图片并绘上地表层
loadBackGroundImage();
//==============创建障碍物层=====================
FraiseGroup fGroup = new FraiseGroup();
fGroup.init(this);
for(int i=0;i<13;i++)
for(int j=0;j<15;j++)
{
if(map.fraise[i][j]==0) this.fraises[i][j]=null;
else
{
fraises[i][j] = new Fraise(fGroup,rGroup,rGroup.canMove[map.fraise[i][j]],rGroup.canDestroy[map.fraise[i][j]],map.fraise[i][j]);
fraises[i][j].setPos(20+40*j,40+40*i);
}
}
//=============创建Player层==================
name1 = (String)htPros.get("name1");
name2 = (String)htPros.get("name2");
players = new Player[this.playerNum];
players[0] = new Player(this.playerGroup,this,name1,0);
players[0].setPos(20+map.playersJ[0]*40,40+map.playersI[0]*40);
players[0].setID(0);
players[1] = new Player(this.playerGroup,this,name2,1);
players[1].setPos(20+map.playersJ[1]*40,40+map.playersI[1]*40);
players[1].setID(1);
this.addKeyListener(new PlayerAdapter2(players[0],false));
this.addKeyListener(new PlayerAdapter2(players[1],true));
//this.offscreen = this.createImage(800,600);
this.offscreen = this.createVolatileImage(800,600);
//调用super.init进入全屏模式
super.init();
}
public void start()
{
super.start();
rGroup.getAudio(ResourceGroup.Audio_star).play();
rGroup.getAudio(ResourceGroup.Audio_backGround).loop();
}
public void stop()
{
super.stop();
rGroup.getAudio(ResourceGroup.Audio_backGround).stop();
}
public void over()
{
super.over();
/*try
{Thread.sleep(3000);}
catch(Exception e){e.printStackTrace();}*/
sm.changeScence(new LoginScence(sm));
if(!ifFullScreen) this.dispose();
}
public void update(Graphics g)
{
if(players[0].getDead() && players[1].getDead())
{
this.rGroup.getAudio(ResourceGroup.Audio_win).play();
MessageDialog.showMessageDialog(this,"","平局");
//this.htPros.put("login","平局");
this.over();
}
else if(players[0].getDead())
{
this.rGroup.getAudio(ResourceGroup.Audio_win).play();
MessageDialog.showMessageDialog(this,"","玩家:"+players[1].getName()+" 胜利");
//this.htPros.put("login",players[1].getName()+"胜利,再来一盘请按F1");
this.over();
}
else if(players[1].getDead())
{
this.rGroup.getAudio(ResourceGroup.Audio_win).play();
MessageDialog.showMessageDialog(this,"","玩家:"+players[0].getName()+" 胜利");
//this.htPros.put("login",players[0].getName()+"胜利,再来一盘请按F1");
this.over();
}
for(int k=0;k<this.playerNum;k++)
{
if(!players[k].getDestroyed())
players[k].update();
}
//fraises层
for(int i=0;i<13;i++)
for(int j=0;j<15;j++)
{
if(this.fraises[i][j]!=null)
{
if(fraises[i][j].getDestroyed())
{
if(fraises[i][j].ifPP==false && Math.random()>0.35)
{
pro[i][j]=new Propertity(this);
pro[i][j].setX(fraises[i][j].getX());
pro[i][j].setY(fraises[i][j].getY());
}
fraises[i][j] = null;
}
else
{
pro[i][j] = null;
fraises[i][j].update();//在此更新,needUpdate更改
if(this.fraises[i][j].getNeedUpdate())
{
//System.out.println("cc"+i+","+j+fraises[i][j].destroyed);
fraises[i][j].setNeedUpdate(false);
int vi = fraises[i][j].vi;
int vj = fraises[i][j].vj;
fraises[i+vi][j+vj] = fraises[i][j];
fraises[i][j] = null;
}
}
}
}
//.....
do
{
if(offscreen.validate(getGraphicsConfiguration())
== offscreen.IMAGE_INCOMPATIBLE)
{
offscreen = this.createVolatileImage(800,600);
}
Graphics2D og = (Graphics2D)this.offscreen.getGraphics();
og.drawImage(backGround,0,0,this);
if(enterButton)
{og.drawImage(exit_1,662,554,this);}
} while( offscreen.contentsLost() );
/*Graphics og = this.offscreen.getGraphics();
og.drawImage(this.backGround,0,0,this);
if(enterButton)
{og.drawImage(exit_1,662,554,this);}*/
//道具层
for(int i=0;i<13;i++)
for(int j=14;j>=0;j--)
{
if(pro[i][j]==null) continue;
else if(pro[i][j].getDestroyed()) pro[i][j] = null;
else
{
for(int k=0;k<this.playerNum;k++)
{
if(pro[i][j]!=null&&!players[k].getDestroyed() && players[k].getBounds().contains(pro[i][j].getX()+20,pro[i][j].getY()+20))
{pro[i][j].beDestroyed(players[k]);pro[i][j]=null;}
}
}
}
//.....以下是简单的实现
for(int i=0;i<13;i++)
for(int j=14;j>=0;j--)
{
if(fraises[i][j]!=null&&fraises[i][j].ifPP)
{
do
{
if(offscreen.validate(getGraphicsConfiguration())
== offscreen.IMAGE_INCOMPATIBLE)
{
offscreen = this.createVolatileImage(800,600);
}
Graphics2D og = (Graphics2D)this.offscreen.getGraphics();
fraises[i][j].paint((Graphics2D)og);
} while( offscreen.contentsLost() );
//fraises[i][j].paint((Graphics2D)og);
}
}
for(int i=0;i<13;i++)
for(int j=14;j>=0;j--)
{
if(fraises[i][j]!=null&& this.fraises[i][j].getTemp()==false)
{
do
{
if(offscreen.validate(getGraphicsConfiguration())
== offscreen.IMAGE_INCOMPATIBLE)
{
offscreen = this.createVolatileImage(800,600);
}
Graphics2D og = (Graphics2D)this.offscreen.getGraphics();
fraises[i][j].paint(og);
} while( offscreen.contentsLost() );
//fraises[i][j].paint((Graphics2D)og);
}
if(this.pro[i][j]!=null)
{
do
{
if(offscreen.validate(getGraphicsConfiguration())
== offscreen.IMAGE_INCOMPATIBLE)
{
offscreen = this.createVolatileImage(800,600);
}
Graphics2D og = (Graphics2D)this.offscreen.getGraphics();
this.pro[i][j].paint((Graphics2D)og);
} while( offscreen.contentsLost() );
}
//this.pro[i][j].paint((Graphics2D)og);
for(int k=0;k<this.playerNum;k++)
{
if(!players[k].getDestroyed() && players[k].getCurrIGe()==i && players[k].getCurrJGe()==j)
{
if(j!=0&&fraises[i][j-1]!=null&&this.fraises[i][j-1].ifPP)
{
do
{
if(offscreen.validate(getGraphicsConfiguration())
== offscreen.IMAGE_INCOMPATIBLE)
{
offscreen = this.createVolatileImage(800,600);
}
Graphics2D og = (Graphics2D)this.offscreen.getGraphics();
fraises[i][j-1].paint((Graphics2D)og);
} while( offscreen.contentsLost() );
//fraises[i][j-1].paint((Graphics2D)og);
((PaoPao)fraises[i][j-1]).setPainted(true);
}
if(i!=12&&fraises[i+1][j]!=null&&this.fraises[i+1][j].ifPP)
{
do
{
if(offscreen.validate(getGraphicsConfiguration())
== offscreen.IMAGE_INCOMPATIBLE)
{
offscreen = this.createVolatileImage(800,600);
}
Graphics2D og = (Graphics2D)this.offscreen.getGraphics();
fraises[i+1][j].paint((Graphics2D)og);
} while( offscreen.contentsLost() );
//fraises[i+1][j].paint((Graphics2D)og);
((PaoPao)fraises[i+1][j]).setPainted(true);
}
do
{
if(offscreen.validate(getGraphicsConfiguration())
== offscreen.IMAGE_INCOMPATIBLE)
{
offscreen = this.createVolatileImage(800,600);
}
Graphics2D og = (Graphics2D)this.offscreen.getGraphics();
players[k].paint((Graphics2D)og);
} while( offscreen.contentsLost() );
//players[k].paint((Graphics2D)og);
}
}
}
//============操作说明==================
/*do
{
if(offscreen.validate(getGraphicsConfiguration())
== offscreen.IMAGE_INCOMPATIBLE)
{
offscreen = this.createVolatileImage(800,600);
}
Graphics2D og = (Graphics2D)this.offscreen.getGraphics();
og.setColor(Color.GRAY);
og.setFont(new Font(null,Font.BOLD,15));
og.drawString("玩家一操作:上(R)下(F)左(D)右(G) 放泡泡(A)",160,15);
og.drawString("玩家二操作:上(↑)下(↓)左(←)右(→) 放泡泡(/) ESC退出",160,32);
} while( offscreen.contentsLost() );*/
/*og.setColor(Color.GRAY);
og.setFont(new Font(null,Font.BOLD,15));
og.drawString("玩家一操作:上(R)下(F)左(D)右(G) 放泡泡(A)",160,15);
og.drawString("玩家二操作:上(↑)下(↓)左(←)右(→) 放泡泡(/) ESC退出",160,32);
*/
//.....
paint(g);
}
public void paint(Graphics g)
{
g.drawImage(this.offscreen,0,0,this);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
{
this.over();
}
else if(e.getKeyCode()==KeyEvent.VK_F8)
{
if(this.ifFullScreen)
this.toWindow();
else
this.toFullScreen();
}
}
//加载资源函数
//加载地图
public void loadMap()
{
String mapFile = (String)htPros.get(new String("mapFile"));
try
{
this.map = Map.loadMap(mapFile);
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,"找不到指定的地图文件或文件格式不正确","错误",JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
this.over();
System.exit(0);
}
}
//加载背景图片并绘上地表层
public void loadBackGroundImage()
{
this.backGround = this.createImage(800,600);
Graphics g = backGround.getGraphics();
g.drawImage(this.rGroup.getBackGround(),0,0,this);
for(int i=0;i<13;i++)
for(int j=0;j<15;j++)
{
g.drawImage(this.rGroup.getImage(1,map.tile[i][j]),20+j*40,40+i*40,this);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -