基于java+mysql的swing游戏植物大战僵尸(java+swing)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于java的Swing游戏植物大战僵尸(java+swing)
}
// boardlbl[i][j].setIcon(wood);
// }
for (int i = 0; i < boardlbl.length; i++) {
for (int j = 0; j < boardlbl[i].length; j++) {
board.add(boardlbl[i][j]);
}
}
board.setBounds(40, 85,width-100,height-100);
width=width-100;
height=height-100;
mainwin.add(board,JLayeredPane.DEFAULT_LAYER);
//mainwin.add(board,JLayeredPane.PALETTE_LAYER);
createCard();
return mainwin;
}
}
if(y+1!=length-1&&grid.getBoard()[x][y].equals("21bullet")&&
!("11,12,13,14".contains(grid.getBoard()[x][y+1].substring(0,2)))){
grid.getBoard()[x][y+1]=grid.getBoard()[x][y];
grid.getBoard()[x][y]="00111";
return y+1;
}
if(y+1!=length-1&&grid.getBoard()[x][y].equals("21bullet")&&
("11,12,13,14".contains(grid.getBoard()[x][y+1].substring(0,2)))){
if (isHit(grid.getBoard()[x][y+1])){
grid.getBoard()[x][y+1] = "00111";
return -1;
}
grid.getBoard()[x][y]="00111";
return y+1;
}
return -1;
}
public int moveCorpse(int x,int y){
if (y==0&&("11,12,13,14,15".contains(grid.getBoard()[x][y].substring(0, 2)))) {
fail();
return -1;
}
if (y - 1 != -1
indexwin=new IndexWin(this);
}
public void start(){
SunAdd.stop=false;
this.x=15;
this.y=80;
grid=new Grid(this.x,this.y);
sunValue=50;
win=Win.getWin(this);
timer=new Timer(this);
//grid.start();
indexwin.setVisible(false);
sunAutoProduceThread = new SunAutoProduceThread(this);
moneyEnoughThread = new MoneyEnoughThread(this);
win.toShow();
putSunValue(0);
timer.start();
sunAutoProduceThread.start();
moneyEnoughThread.start();
grid.getBoard()[1][0]="31111";
grid.getBoard()[4][0]="31111";
grid.getBoard()[7][0]="31111";
grid.getBoard()[10][0]="31111";
grid.getBoard()[13][0]="31111";
corpseNum=grid.getNum();
win=Win.getWin(this);
timer=new Timer(this);
//grid.start();
indexwin.setVisible(false);
sunAutoProduceThread = new SunAutoProduceThread(this);
moneyEnoughThread = new MoneyEnoughThread(this);
win.toShow();
putSunValue(0);
timer.start();
sunAutoProduceThread.start();
moneyEnoughThread.start();
grid.getBoard()[1][0]="31111";
grid.getBoard()[4][0]="31111";
grid.getBoard()[7][0]="31111";
grid.getBoard()[10][0]="31111";
grid.getBoard()[13][0]="31111";
corpseNum=grid.getNum();
}
public void newCorpseThread(){
fail();
return -1;
}
if (y - 1 != -1
&& !("01,02,03,04,05".contains(grid.getBoard()[x][y-1].substring(0, 2)))
&& ("11,12,13,14,15".contains(grid.getBoard()[x][y].substring(0, 2)))) {
grid.getBoard()[x][y-1] = grid.getBoard()[x][y ];
grid.getBoard()[x][y] = "00111";
return y-1;
}
if (y - 1 != -1
&& ("01,02,03,04,05".contains(grid.getBoard()[x][y-1].substring(0, 2)))
&& ("11,12,13,14,15".contains(grid.getBoard()[x][y ].substring(0, 2)))) {
plantList.get(x * 100 + y-1).setHp(plantList.get(x * 100 + y-1).getHp() - 1);
if (plantList.get(x * 100 + y-1).getHp() == 0) {
grid.getBoard()[x][y-1] = "00111";
}
return y;
}
return -1;
}
private Map<Integer, Plant> plantList=new HashMap<Integer, Plant>();
private Map<Integer,CarThread> carList=new HashMap<Integer, CarThread>();
private Map<String,CorpseMoveThread> corpseList=new HashMap<String, CorpseMoveThread>();
private Map<Integer,ProduceBullet> produceBulletTimer=new HashMap<Integer,ProduceBullet>();
public Controller(){
indexwin=new IndexWin(this);
}
public void start(){
SunAdd.stop=false;
this.x=15;
this.y=80;
grid=new Grid(this.x,this.y);
sunValue=50;
win=Win.getWin(this);
timer=new Timer(this);
//grid.start();
indexwin.setVisible(false);
sunAutoProduceThread = new SunAutoProduceThread(this);
moneyEnoughThread = new MoneyEnoughThread(this);
win.toShow();
putSunValue(0);
timer.start();
sunAutoProduceThread.start();
moneyEnoughThread.start();
grid.getBoard()[1][0]="31111";
grid.getBoard()[4][0]="31111";
public MyLabel(int i,int j,final Controller controller){
this(controller);
this.x = i;
this.y = j;
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
if(controller.isUproot()){
controller.uprootPlant(x, y);
}
else{
controller.addPlant(x,y);
}
}
});
}
public void setlable(int x) {
switch (x) {
case 21:
setIcon(bb);
lbl1.setBounds(330, 50, img1.getIconWidth(), img1.getIconHeight());
lbl2.setBounds(340, 150, img2.getIconWidth(), img2.getIconHeight());
lbl3.setBounds(340, 250, img3.getIconWidth(), img3.getIconHeight());
main.add(lbl1, 0);
main.add(lbl2, 0);
main.add(lbl3, 0);
// 选项,帮助,退出
final ImageIcon option1 = new ImageIcon(
IndexWin.class
.getResource("/plantvsplant/index/SelectorScreen_Options1.png"));
final ImageIcon help1 = new ImageIcon(
IndexWin.class
.getResource("/plantvsplant/index/SelectorScreen_Help1.png"));
final ImageIcon quit1 = new ImageIcon(
IndexWin.class
//corpse12.setImage(corpse12.getImage().getScaledInstance(80, 80, 1));
ImageIcon img2 = new ImageIcon(
IndexWin.class
.getResource("/plantvsplant/index/SelectorScreen_Vasebreaker_button.png"));
ImageIcon img3 = new ImageIcon(
IndexWin.class
.getResource("/plantvsplant/index/SelectorScreen_Challenges_button.png"));
final JLabel lbl1 = new JLabel(img1);
lbl1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
controller.start();
}
public void mouseEntered(MouseEvent e) {
lbl1.setIcon(img11);
}
public void mouseExited(MouseEvent e) {
lbl1.setIcon(img1);
}
});
JLabel lbl2 = new JLabel(img2);
}
public int moveCorpse(int x,int y){
if (y==0&&("11,12,13,14,15".contains(grid.getBoard()[x][y].substring(0, 2)))) {
fail();
return -1;
}
if (y - 1 != -1
&& !("01,02,03,04,05".contains(grid.getBoard()[x][y-1].substring(0, 2)))
&& ("11,12,13,14,15".contains(grid.getBoard()[x][y].substring(0, 2)))) {
grid.getBoard()[x][y-1] = grid.getBoard()[x][y ];
grid.getBoard()[x][y] = "00111";
return y-1;
}
if (y - 1 != -1
&& ("01,02,03,04,05".contains(grid.getBoard()[x][y-1].substring(0, 2)))
&& ("11,12,13,14,15".contains(grid.getBoard()[x][y ].substring(0, 2)))) {
plantList.get(x * 100 + y-1).setHp(plantList.get(x * 100 + y-1).getHp() - 1);
if (plantList.get(x * 100 + y-1).getHp() == 0) {
grid.getBoard()[x][y-1] = "00111";
}
return y;
}
return -1;
}
public void mouseEntered(MouseEvent e) {
lbl1.setIcon(img11);
}
public void mouseExited(MouseEvent e) {
lbl1.setIcon(img1);
}
});
JLabel lbl2 = new JLabel(img2);
JLabel lbl3 = new JLabel(img3);
lbl1.setBounds(330, 50, img1.getIconWidth(), img1.getIconHeight());
lbl2.setBounds(340, 150, img2.getIconWidth(), img2.getIconHeight());
lbl3.setBounds(340, 250, img3.getIconWidth(), img3.getIconHeight());
main.add(lbl1, 0);
main.add(lbl2, 0);
main.add(lbl3, 0);
// 选项,帮助,退出
final ImageIcon option1 = new ImageIcon(
IndexWin.class
.getResource("/plantvsplant/index/SelectorScreen_Options1.png"));
final ImageIcon help1 = new ImageIcon(
IndexWin.class
case 21:
setIcon(bb);
break;
// case 31:
// setIcon(bb);
// break;
case 1:
setIcon(sunfllower);
break;
case 3:
setIcon(pease);
break;
case 5:
setIcon(snowpease);
break;
case 2:
setIcon(bomb);
break;
case 4:
setIcon(potato);
card0.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
if(card0.isEnabled()&& 0==plantid){
plantid=1;
controller.putSunValue(-50);
}else{
if(plantid==1){
controller.putSunValue(50);
plantid=0;
b+=3;
if(a<200)
a+=2;
sun.setBounds(a, b, 80, 80);
if(b>=650)
private Map<Integer, Plant> plantList=new HashMap<Integer, Plant>();
private Map<Integer,CarThread> carList=new HashMap<Integer, CarThread>();
private Map<String,CorpseMoveThread> corpseList=new HashMap<String, CorpseMoveThread>();
private Map<Integer,ProduceBullet> produceBulletTimer=new HashMap<Integer,ProduceBullet>();
public Controller(){
indexwin=new IndexWin(this);
}
public void start(){
SunAdd.stop=false;
this.x=15;
this.y=80;
grid=new Grid(this.x,this.y);
sunValue=50;
win=Win.getWin(this);
timer=new Timer(this);
//grid.start();
indexwin.setVisible(false);
sunAutoProduceThread = new SunAutoProduceThread(this);
moneyEnoughThread = new MoneyEnoughThread(this);
win.toShow();
putSunValue(0);
timer.start();
sunAutoProduceThread.start();
return y+1;
}
if(y+1!=length-1&&grid.getBoard()[x][y].equals("21bullet")&&
!("11,12,13,14".contains(grid.getBoard()[x][y+1].substring(0,2)))){
grid.getBoard()[x][y+1]=grid.getBoard()[x][y];
grid.getBoard()[x][y]="00111";
return y+1;
}
if(y+1!=length-1&&grid.getBoard()[x][y].equals("21bullet")&&
("11,12,13,14".contains(grid.getBoard()[x][y+1].substring(0,2)))){
if (isHit(grid.getBoard()[x][y+1])){
grid.getBoard()[x][y+1] = "00111";
return -1;
}
grid.getBoard()[x][y]="00111";
return y+1;
}
return -1;
}
public int moveCorpse(int x,int y){
private Map<Integer, String> bulletList=new HashMap<Integer, String>();
private SunAutoProduceThread sunAutoProduceThread;
private Map<Integer, Plant> plantList=new HashMap<Integer, Plant>();
private Map<Integer,CarThread> carList=new HashMap<Integer, CarThread>();
private Map<String,CorpseMoveThread> corpseList=new HashMap<String, CorpseMoveThread>();
private Map<Integer,ProduceBullet> produceBulletTimer=new HashMap<Integer,ProduceBullet>();
public Controller(){
indexwin=new IndexWin(this);
}
public void start(){
SunAdd.stop=false;
this.x=15;
this.y=80;
grid=new Grid(this.x,this.y);
sunValue=50;
win=Win.getWin(this);
timer=new Timer(this);
//grid.start();
indexwin.setVisible(false);
sunAutoProduceThread = new SunAutoProduceThread(this);
moneyEnoughThread = new MoneyEnoughThread(this);
grid.getBoard()[10][0]="31111";
grid.getBoard()[13][0]="31111";
corpseNum=grid.getNum();
}
public void newCorpseThread(){
for (int i = 0; i < grid.getBoard().length; i++) {
if("11,12,13,14".contains(grid.getBoard()[i][length-1].substring(0,2))&&
corpseList.get(grid.getBoard()[i][length-1])==null){
corpseList.put(grid.getBoard()[i][length-1], new CorpseMoveThread(this,i ,length-1));
corpseList.get(grid.getBoard()[i][length-1]).start();
return y+1;
}
if(y+1!=length-1&&grid.getBoard()[x][y].equals("21bullet")&&
!("11,12,13,14".contains(grid.getBoard()[x][y+1].substring(0,2)))){
grid.getBoard()[x][y+1]=grid.getBoard()[x][y];
grid.getBoard()[x][y]="00111";
return y+1;
}
if(y+1!=length-1&&grid.getBoard()[x][y].equals("21bullet")&&
("11,12,13,14".contains(grid.getBoard()[x][y+1].substring(0,2)))){
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {