java版一个都不能漏游戏制作
本人花了一天时间,总算是初步完成了自己想要实现的一个游戏,java版一个都不能漏游戏。现在还处于初步制作和测试阶段,今后如有升级,会在这篇博客中修改。下面是代码和截图:
package ctong;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.security.acl.Group;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JRootPane;
import javax.swing.JTextField;
public class Game extends JFrame{
private AudioClip clip;
private JRadioButton easy,normal,hard;
private static int x=0,y1=0,y2=0,y3=0,k=0,t=0,add=2,i =3;
private static JTextField jt1 = new JTextField("0"),jt2 = new JTextField("0");;
private JPanel center;
private static ImageIcon bjicon=new ImageIcon("123.jpg");
private boolean flag=true;
public Game(){
this.setTitle("一个都不能漏");
this.setSize(620, 600);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(3);
this.setFocusableWindowState(true);
center = new JPanel();
center.setBackground(Color.WHITE);
center.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
int X=e.getX();
int Y=e.getY();
if(x<X&&X<x+bjicon.getIconWidth()&&y1<Y&&Y<y1+bjicon.getIconHeight()){
y1=-2*bjicon.getIconHeight();
t+=add;
jt2.setText(t+"");
}
if(x+bjicon.getIconWidth()<X&&X<x+2*bjicon.getIconWidth()&&y2<Y&&Y<y2+bjicon.getIconHeight()){
y2=-2*bjicon.getIconHeight();
t+=add;
jt2.setText(t+"");
}
if(x+2*bjicon.getIconWidth()<X&&X<x+3*bjicon.getIconWidth()&&y3<Y&&Y<y3+bjicon.getIconHeight()){
y3=-2*bjicon.getIconHeight();
t+=add;
jt2.setText(t+"");
}
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
// JOptionPane.showMessageDialog(null, "游戏结束!");
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
});
JPanel east = createPanel();
this.add(center,BorderLayout.CENTER);
this.add(east,BorderLayout.EAST);
this.setIconImage(new ImageIcon("123.jpg").getImage());
this.setUndecorated(true);
this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
this.setVisible(true);
}
//现在模拟的图层上绘制好图片
public void drawpic1(Graphics g){
//新建一个模拟图层对象
BufferedImage buim=new BufferedImage(center.getWidth()/3,center.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics gg=buim.getGraphics();
gg.fillRect(0, 0, center.getWidth(), center.getHeight());
//使图片移动
gg.drawImage(bjicon.getImage(), x, y1, null);
y1+=new Random().nextInt(i)+3;
if(y1-5*bjicon.getIconHeight() >= 0){
y1 = -bjicon.getIconHeight();
jt2.setText(--t+"");
JOptionPane.showMessageDialog(null, "游戏结束!所花时间为:"+k+"得分为:"+t);
System.exit(0);
}
if(t>50&&t<=100){
bjicon = new ImageIcon("789.png");
}
if(100<t&&t<=200){
bjicon = new ImageIcon("456.jpg");
}
if(200<t&&t<=400){
bjicon = new ImageIcon("123.png");
}
if(400<t&&t<800){
bjicon = new ImageIcon("456.png");
}
if(1000<t){
bjicon = new ImageIcon("111.png");
}
//将模拟图层上的图像绘制到中间面板的画布上
g.drawImage(buim, 0, 0, null);
}
//现在模拟的图层上绘制好图片
public void drawpic2(Graphics g){
//新建一个模拟图层对象
BufferedImage buim=new BufferedImage(center.getWidth()/3,center.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics gg=buim.getGraphics();
gg.fillRect(0, 0, center.getWidth(), center.getHeight());
//使图片移动
gg.drawImage(bjicon.getImage(), x, y2, null);
y2+=new Random().nextInt(i)+3;
if(y2-5*bjicon.getIconHeight() >= 0){
y2 = -bjicon.getIconHeight();
jt2.setText(--t+"");
JOptionPane.showMessageDialog(null, "游戏结束!所花时间为:"+k+"得分为:"+t);
System.exit(0);
}
//将模拟图层上的图像绘制到中间面板的画布上
g.drawImage(buim, bjicon.getIconWidth(), 0, null);
}
//现在模拟的图层上绘制好图片
public void drawpic3(Graphics g){
//新建一个模拟图层对象
BufferedImage buim=new BufferedImage(center.getWidth()/3,center.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics gg=buim.getGraphics();
gg.fillRect(0, 0, center.getWidth(), center.getHeight());
//使图片移动
gg.drawImage(bjicon.getImage(), x, y3, null);
y3+=new Random().nextInt(i)+3;
if(y3-5*bjicon.getIconHeight() >= 0){
y3 = -bjicon.getIconHeight();
jt2.setText(--t+"");
JOptionPane.showMessageDialog(null, "游戏结束!所花时间为:"+k+"得分为:"+t);
System.exit(0);
}
//将模拟图层上的图像绘制到中间面板的画布上
g.drawImage(buim, 2*bjicon.getIconWidth(), 0, null);
}
public JPanel createPanel(){
JPanel jpa = new JPanel(new BorderLayout());
JPanel jp1 = new JPanel(new GridLayout(7,1));
JPanel jp2 = new JPanel(new GridLayout(2,2));
JButton start = new JButton("开始游戏");
final JButton help = new JButton("帮助");
easy = new JRadioButton("简单",true);
normal = new JRadioButton("中等");
hard = new JRadioButton("困难");
ButtonGroup group = new ButtonGroup();
group.add(easy);
group.add(normal);
group.add(hard);
JCheckBox music = new JCheckBox("声音(ALT+1)");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Thread mt1 = new Thread(new Runnable() {
public void run() {
while(true){
try{
Thread.sleep(10);
}catch (InterruptedException ex){
ex.printStackTrace();
}
drawpic1(center.getGraphics());
}
}
});
mt1.start();
Thread mt2 = new Thread(new Runnable() {
public void run() {
while(true){
try{
Thread.sleep(10);
}catch (InterruptedException ex){
ex.printStackTrace();
}
drawpic2(center.getGraphics());
}
}
});
mt2.start();
Thread mt3 = new Thread(new Runnable() {
public void run() {
while(true){
try{
Thread.sleep(10);
}catch (InterruptedException ex){
ex.printStackTrace();
}
drawpic3(center.getGraphics());
}
}
});
mt3.start();
Thread tt = new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
while(flag){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jt1.setText(++k+"");
}}
});
tt.start();
}
});
help.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "单击图片使图片不落下去,不同的难度获取不同分数,分数越高可以解锁更多图片!");
}
});
easy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
i=3;
add=2;
}
});
normal.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
i=6;
add=4;
}
});
hard.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
i=9;
add=8;
}
});
music.setMnemonic(KeyEvent.VK_1);//快捷键ALT+1
//监听声音
music.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(e.getStateChange()==1){
try {
File f =new File("2.wav");
clip=Applet.newAudioClip(f.toURL());
clip.loop();
}catch (Exception ex) {
ex.printStackTrace();
}
}
else clip.stop();
}
});
jp1.add(start);
jp1.add(help);
jp1.add(easy);
jp1.add(normal);
jp1.add(hard);
jp1.add(music);
JLabel jla1 = new JLabel("时间:",JLabel.CENTER);
JLabel jla2 = new JLabel("得分:",JLabel.CENTER);
jt1.setEditable(false);
jt2.setEditable(false);
jp2.add(jla1);
jp2.add(jt1);
jp2.add(jla2);
jp2.add(jt2);
jpa.add(jp1,BorderLayout.NORTH);
jpa.add(jp2,BorderLayout.SOUTH);
jpa.setBorder(BorderFactory.createTitledBorder("系统控制区"));
jpa.setCursor(new Cursor(12));
return jpa;
}
public static void main(String[] args) {
new Game();
}
}
制作还在进行中。。。