当前位置:   article > 正文

java打地鼠项目介绍,打地鼠---Java

打地鼠项目描述

0818b9ca8b590ca3270a3433284dd417.png

//打地鼠,基本就是  对图片处理,可能会出现重叠现象,首先添加一个背景 图片,然后添加地鼠(随机生成),之后就是面板(统计游戏次数的图片),等级的提升就是对//Timer 的延迟设置处理

package cn.hncu.games; import java.applet.Applet; import java.applet.AudioClip; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Font; import java.awt.Image; import java.awt.Point; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.net.MalformedURLException; import java.net.URL; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingConstants; import javax.swing.Timer; public class Kitmouse2 extends JFrame implements ActionListener, MouseListener{ String dir ="./images/"; Toolkit tk; JLabel showtimelabel,hittimes,currrntlevel; Timer time; int delay=1000; Random random; JLabel jlbmosue; JPanel panel; int grade=1,hit=0,show=0; boolean isHit=false;//防止多次点击,点击次数增加; public Kitmouse2(){ this.setTitle("打地鼠"); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setBounds(200, 200, 438, 375); setbk();//添加背景图片 setmousecurson(); dichu(); settu(); this.setVisible(true); time =new Timer(delay, this);//启动定时器 random=new Random(); time.start(); } //设置背景 private void setbk() { ((JPanel)(this.getContentPane())).setOpaque(false); ImageIcon bgicon =new ImageIcon(dir+"beijing.jpg"); JLabel bgleb =new JLabel(bgicon); bgleb.setBounds(0 , 0, bgicon.getIconWidth(), bgicon.getIconHeight()); this.getLayeredPane().add(bgleb, new Integer(Integer.MIN_VALUE)); } //设置鼠标图标 private void setmousecurson() { Curson(1); } private void Curson(int i) { Image mouseImage; tk =Toolkit.getDefaultToolkit(); if(i==1){ mouseImage =tk.createImage(dir+"chui1.png"); }else{ mouseImage =tk.createImage(dir+"chui2.png"); } Cursor myCurson =tk.createCustomCursor(mouseImage, new Point(10, 10), "xx"); this.setCursor(myCurson); } //设置记录分数,次数的图标 private void settu() { panel =new JPanel(); panel.setOpaque(false); //panel.setPreferredSize(new Dimension(438, 375)); this.getContentPane().add(panel); //出现次数图标 showtimelabel =new JLabel("0", new ImageIcon(dir+"chuxiancishu.png"), SwingConstants.CENTER); showtimelabel.setFont(new Font("hha", Font.BOLD, 20)); panel.add(showtimelabel); hittimes =new JLabel("0", new ImageIcon(dir+"dazhongcishu.png"), SwingConstants.CENTER); hittimes.setFont(new Font("hha", Font.BOLD, 20));//设置字体的大小 panel.add(hittimes); //当前等级 currrntlevel =new JLabel("1", new ImageIcon(dir+"dangqiandengji.png"), SwingConstants.CENTER); currrntlevel.setFont(new Font("hha", Font.BOLD, 20));//设置字体的大小 panel.add(currrntlevel); } public AudioClip loadSound(String name) { URL url=null; try { url=new URL("file:"+name); } catch (MalformedURLException e) { e.printStackTrace(); } return Applet.newAudioClip(url); } //地鼠出现 private void dichu() { ImageIcon ic=new ImageIcon(dir+"dishu.png"); jlbmosue= new JLabel(ic); jlbmosue.setLocation(100, 100); jlbmosue.setSize(80,80); jlbmosue.setVisible(false); this.getContentPane().add(jlbmosue); jlbmosue.addMouseListener(this); } public static void main(String[] args) { Kitmouse2 k2= new Kitmouse2(); //AudioClip k1 =k2.loadSound("F:\\ex\\javamusic\\1.wav"); //k1.play(); } @Override public void actionPerformed(ActionEvent e) { int num =random.nextInt(9);//产生随机数【0,9); switch (num){ case 0: jlbmosue.setLocation(56, 63); break; case 1: jlbmosue.setLocation(322, 204); break; case 2: jlbmosue.setLocation(185, 204); break; case 3: jlbmosue.setLocation(48, 203); break; case 4: jlbmosue.setLocation(298, 133); break; case 5: jlbmosue.setLocation(162, 133); break; case 6: jlbmosue.setLocation(22, 133); break; case 7: jlbmosue.setLocation(311, 63); break; case 8: jlbmosue.setLocation(186, 63); break; } isHit=true; jlbmosue.setIcon(new ImageIcon(dir+"dishu.png")); show++; showtimelabel.setText(""+show); jlbmosue.setVisible(true); if(show>15){ int b =JOptionPane.showConfirmDialog(this, "你输了,还要再来吗?", "很遗憾",JOptionPane.YES_NO_OPTION ); if(b==JOptionPane.YES_OPTION){ time.stop(); grade=1; show=0; hit=0; delay=1000; hittimes.setText(""+hit); showtimelabel.setText(""+show); currrntlevel.setText(""+grade); time.setDelay(delay); time.start(); }else{ System.exit(0); } } } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mousePressed(MouseEvent e) { Curson(2); if(e.getSource()==jlbmosue){ jlbmosue.setIcon(new ImageIcon(dir+"datou.png")); if(isHit){ hit++; hittimes.setText(""+hit); isHit=false; } //AudioClip aa=new Audi if(hit>7){ delay -=100; if(delay<20){ jlbmosue.setVisible(false); time.stop(); int a =JOptionPane.showConfirmDialog(this,"您通关了,好样的,加油,还要来吗?", "恭喜您", JOptionPane.YES_NO_OPTION); if(a==JOptionPane.YES_OPTION){ grade=0; delay=1000; }else{ System.exit(0); } } grade ++; hit=0; show=0; time.stop(); jlbmosue.setVisible(false); JOptionPane.showMessageDialog(this, "恭喜您过关了,下面进入有难度的第"+grade+"关,加油哦"); hittimes.setText(""+hit); showtimelabel.setText(""+show); currrntlevel.setText(""+grade); time.setDelay(delay); time.start(); }} } @Override public void mouseReleased(MouseEvent e) { Curson(1); } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号