程序运行效果
这里只实现了,豌豆射手射击僵尸功能,其他功能比较简单,可自行补充
主类:
package nine.eleven;
import java.awt.Color;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class run extends JPanel{
public static coordinate coor=new coordinate();
public static JFrame jframe=new JFrame();
public static ImageIcon back=new ImageIcon("nine_eleven_image/background.jpg");
public static ImageIcon bea=new ImageIcon("nine_eleven_image/bean.gif");
public static ImageIcon pea=new ImageIcon("nine_eleven_image/peas.gif");
public static ImageIcon cor=new ImageIcon("nine_eleven_image/Corpse.gif");
public static ImageIcon beanone=new ImageIcon("nine_eleven_image/bean-one.gif");
public static JLabel bean=new JLabel();
public static JLabel background=new JLabel();
public static JLabel corpse=new JLabel();
public static void main(String[] args){
new run();
}
public run(){
jframe.setSize(back.getIconWidth(),back.getIconHeight());
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setLocationRelativeTo(null);
jframe.setVisible(true);
this.setLayout(null);
jframe.add(this);
bean.setBounds(coor.x[0],coor.y[0] , 24, 24);
bean.setIcon(bea);
add(bean);
JLabel peas=new JLabel();
peas.setIcon(pea);
peas.setBounds(coor.x[1], coor.y[1], pea.getIconWidth()-2,pea.getIconHeight());
add(peas);
corpse.setIcon(cor);
corpse.setBounds(coor.x[2], coor.y[2], cor.getIconWidth(),cor.getIconHeight());
add(corpse);
Timer ct=new Timer();
TimerTask cs=new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
coor.x[2]-=1;
corpse.setIcon(cor);
corpse.setBounds(coor.x[2], coor.y[2], cor.getIconWidth()+50,cor.getIconHeight());
}
};
ct.schedule(cs, 200L,30L);
Timer t=new Timer();
TimerTask s=new TimerTask(){
@Override
public void run() {
// TODO Auto-generated method stub
coor.x[0]+=2;
bean.setBounds(coor.x[0],coor.y[0] , 24, 24);
bean.setIcon(bea);
if(coor.x[0] >= coor.x[2]){
bean.setIcon(beanone);
bean.setBounds(coor.x[0],coor.y[0] , beanone.getIconWidth(), beanone.getIconHeight());
}
if(coor.x[0] > coor.x[2]+20){
coor.hit+=1;
coor.x[0]=230;
if(coor.hit==5){
coor.hit=0;
coor.x[2]=1420;
}
}
repaint();
}
};
t.schedule(s, 200L,10L);
background.setSize(back.getIconWidth(),back.getIconHeight());
background.setIcon(back);
add(background);
}
}
副类:
package nine.eleven;
public class coordinate {
public static int[] x=new int[10];
public static int[] y=new int[10];
public static int hit=0;
public coordinate(){
y[0]=280;
x[0]=270;
y[1]=270;
x[1]=250;
y[2]=220;
x[2]=1420;
}
}
素材链接:https://download.csdn.net/download/qq_41743240/12342304
如果链接无效,可访问我的主页寻找资源