⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.java

📁 大家来找碴Java小游戏。地球人都知道的经典街机游戏
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
				     pass();
				   }
			  }
		
		   
	    }
	public void mouseExited(MouseEvent e1)
		{	if (e1.getSource()==font1)
			{	
				font1.setForeground(new Color(153,51,51));
			}
			if (e1.getSource()==font2)
			{	
				font2.setForeground(new Color(153,51,51));
			}
			if (e1.getSource()==font3)
			{	
				font3.setForeground(new Color(153,51,51));
			}
			if (e1.getSource()==font4)
			{	
				font4.setForeground(new Color(153,51,51));
			}
			
		}
	public void mouseEntered(MouseEvent e2)
		{	if (e2.getSource()==font1)
			{	
				font1.setForeground(Color.red);
			}
			if (e2.getSource()==font2)
			{   
				font2.setForeground(Color.red);
			}
			if (e2.getSource()==font3)
			{
				font3.setForeground(Color.red);
			}
			if (e2.getSource()==font4)
			{
			    font4.setForeground(Color.red);
			}
			
		}
	public void mouseReleased(MouseEvent e){}
	public void mousePressed(MouseEvent e){}
	public void keyTyped(KeyEvent ke){}
	public void keyReleased(KeyEvent ke){}
	public void keyPressed(KeyEvent ke)
		{	
			
			if (panename.isVisible())
			{
				if (ke.getKeyCode()==8)
				{
					switch (playname.length())
					{case 1:playname="";break;
					 case 2:playname=playname.substring(0,1);break;
					 case 3:playname=playname.substring(0,2);break;
					}
						
				}
				if ((ke.getKeyCode()<=90)&&(ke.getKeyCode()>=60)&&(playname.length()<3))
				{	String s=String.valueOf(ke.getKeyChar());
				    playname=playname+s.toUpperCase();
				}
				panename.repaint();
			}
			if (ke.getKeyCode()==27)
			{
				System.exit(0);
			}
		}
	public void actionPerformed(ActionEvent ae)
		{	
			if (numtotal<int1)
			{
				numtotal=numtotal+10;
				numbonus=numbonus-10;
				total.setText(String.valueOf(numtotal));
				snum.setText(String.valueOf(numbonus));
				
			}
			else
			{
				timer.stop();
				Sscores.stop();
			}
		}



}
class paneOption extends JPanel implements MouseListener
{
	public static JLabel f1,f2,f3,b1,b2,b3,b4,b5,low,high,ret;
	AudioClip Sbutton;
	public paneOption()
	{	try
		{
			Sbutton=Applet.newAudioClip(new File("sound/move.wav").toURL());
		}
		catch (MalformedURLException ace) {System.out.println("err");}	
		f1=new JLabel("音效:",JLabel.RIGHT);
		f2=new JLabel("背景音乐:",JLabel.RIGHT);
		f3=new JLabel("游戏难度:",JLabel.RIGHT);
		b1=new JLabel("√",new ImageIcon("photo/button1.png"),JLabel.CENTER);
		b2=new JLabel("√",new ImageIcon("photo/button1.png"),JLabel.CENTER);
		b3=new JLabel("",new ImageIcon("photo/button1.png"),JLabel.CENTER);
		b4=new JLabel("√",new ImageIcon("photo/button1.png"),JLabel.CENTER);
		b5=new JLabel("",new ImageIcon("photo/button1.png"),JLabel.CENTER);
		low=new JLabel("Low");
		high=new JLabel("High");
		ret=new JLabel("返回",new ImageIcon("photo/button2.png"),JLabel.CENTER);
		f1.setFont(new Font("宋体",Font.BOLD,30));
		f2.setFont(new Font("宋体",Font.BOLD,30));
		f3.setFont(new Font("宋体",Font.BOLD,30));
		b1.setFont(new Font("宋体",Font.BOLD,40));
		b2.setFont(new Font("宋体",Font.BOLD,40));
		b3.setFont(new Font("宋体",Font.BOLD,40));
		b4.setFont(new Font("宋体",Font.BOLD,40));
		b5.setFont(new Font("宋体",Font.BOLD,40));
		low.setFont(new Font("宋体",Font.BOLD,20));
		high.setFont(new Font("宋体",Font.BOLD,20));
		ret.setFont(new Font("楷体_GB2312",Font.PLAIN,28));
		f1.setForeground(new Color(153,51,51));
		f2.setForeground(new Color(153,51,51));
		f3.setForeground(new Color(153,51,51));
		b1.setForeground(new Color(153,51,51));
		b2.setForeground(new Color(153,51,51));
		b3.setForeground(new Color(153,51,51));
		b4.setForeground(new Color(153,51,51));
		b5.setForeground(new Color(153,51,51));
		low.setForeground(new Color(153,51,51));
		high.setForeground(new Color(153,51,51));
		ret.setForeground(new Color(204,255,204));
		f1.setBounds(80,50,150,30);
		f2.setBounds(80,110,150,30);
		f3.setBounds(80,170,150,30);
		b1.setBounds(260,40,50,50);
		b2.setBounds(260,100,50,50);
		b3.setBounds(260,160,50,50);
		b4.setBounds(330,160,50,50);
		b5.setBounds(400,160,50,50);
		low.setBounds(270,210,50,30);
		high.setBounds(405,210,50,30);
		ret.setBounds(200,260,100,50);
		b1.setHorizontalTextPosition(0);
		b1.setVerticalTextPosition(0);
		b2.setHorizontalTextPosition(0);
		b2.setVerticalTextPosition(0);
		b3.setHorizontalTextPosition(0);
		b3.setVerticalTextPosition(0);
		b4.setHorizontalTextPosition(0);
		b4.setVerticalTextPosition(0);
		b5.setHorizontalTextPosition(0);
		b5.setVerticalTextPosition(0);
		ret.setHorizontalTextPosition(0);
		ret.setVerticalTextPosition(0);
		ImageIcon icon= new ImageIcon("photo/border.png");
		setBorder(new MatteBorder(32,32,32,32,icon));
		setBackground(new Color(227,230,199));
		setLayout(null);
		addMouseListener(this);
		b1.addMouseListener(this);
		b2.addMouseListener(this);
		b3.addMouseListener(this);
		b4.addMouseListener(this);
		b5.addMouseListener(this);
		ret.addMouseListener(this);
		add(f1);
		add(f2);
		add(f3);
		add(b1);
		add(b2);
		add(b3);
		add(b4);
		add(b5);
		add(low);
		add(high);
		add(ret);
	}
	public void mouseReleased(MouseEvent e){}
	public void mousePressed(MouseEvent e){}
	public void mouseClicked(MouseEvent e)
		{	
			//this.setVisible(false);
			if (e.getSource()==b1)
			{
				Sbutton.play();
				if (b1.getText()==(""))
				  b1.setText("√");
				else
				  b1.setText("");
			}
			if (e.getSource()==b2)
			{
				Sbutton.play();
				if (b2.getText()==(""))
				  b2.setText("√");
				else
				  b2.setText("");
			}
			if ((e.getSource()==b3)||(e.getSource()==b4)||(e.getSource()==b5))
			{	Sbutton.play();
				b3.setText("");
				b4.setText("");
				b5.setText("");
				JLabel l=(JLabel)e.getSource();
				l.setText("√");
			}
			if (e.getSource()==ret)
			{
				this.setVisible(false);
			}
		}
	public void mouseEntered(MouseEvent e)
		{
			if (e.getSource()==ret)
			ret.setForeground(Color.white);
			
			
		}
	public void mouseExited(MouseEvent e)
		{	if (e.getSource()==ret)
			ret.setForeground(new Color(204,255,204));
		}
	
}
class paneName extends JPanel implements MouseListener
{
	JLabel ok;
	public paneName()
	{	
		setBackground(Color.darkGray);
		setLayout(null);
		ok=new JLabel("OK",new ImageIcon("photo/button.png"),JLabel.LEFT);
		ok.setBounds(155,150,100,30);
		ok.setForeground(Color.black);
		ok.setFont(new Font("宋体",Font.BOLD,20));
		ok.setHorizontalTextPosition(0);
	    ok.setVerticalTextPosition(0);
		ok.addMouseListener(this);
		add(ok);

	}

	public void mouseReleased(MouseEvent e){}
	public void mousePressed(MouseEvent e){}
	public void mouseClicked(MouseEvent e)
		{
			int max=0;
			/*
			for (int i=0 ;i<10 ;i++ )
			{
				System.out.println(i+1+" "+paneScores.name[i]+"		"+paneScores.scores[i]);
			}
			*/
			for (int i=0;i<10;i++ )
			{	
				if (paneScores.scores[i]<Mygame.numtotal)
				{
					for (int j=9;j>i ;j-- )
					{
						paneScores.scores[j]=paneScores.scores[j-1];
						paneScores.name[j]=paneScores.name[j-1];
					}
					paneScores.scores[i]=Mygame.numtotal;
					paneScores.name[i]=Mygame.playname;
					break;
				}
				
			}
			/*
			System.out.println("\n");
			for (int i=0 ;i<10 ;i++ )
			{						
				System.out.println(i+1+" "+paneScores.name[i]+"		"+paneScores.scores[i]);
			}
			*/
			
			setVisible(false);
		}
	public void mouseEntered(MouseEvent e)
		{
			ok.setForeground(Color.red);
		}
	public void mouseExited(MouseEvent e)
		{
			ok.setForeground(Color.black);
		}
	

	public void paintComponent(Graphics g)	
	{	
		super.paintComponent(g);
		g.setColor(Color.white);
		g.setFont(new Font("宋体",Font.BOLD,24));
		g.drawString("请输入您的姓名",110,40);
		g.setColor(new Color(227,230,199));
		g.fillRect(100,70,200,60);
		g.setColor(Color.black);
		g.fillRect(110,120,180,3);
		g.setColor(new Color(153,51,51));
		g.setFont(new Font("宋体",Font.BOLD,60));
		g.drawString(Mygame.playname,125,115);
	}
	
}

class paneScores extends JPanel 
{
	public static String name[]=new String[10];
	public static int scores[]=new int[10];
	static File fname;
	JLabel ret;
	public paneScores()
	{	setLayout(null);
		fname = new File("highscores.txt");
		setBackground(new Color(227,230,199));
		ret=new JLabel("返回");
		ret.setFont(new Font("楷体_GB2312",Font.PLAIN,36));
		ret.setForeground(new Color(153,51,51));
		ret.setBounds(300,500,100,50);
		ImageIcon icon= new ImageIcon("photo/border1.png");
		setBorder(new MatteBorder(21,24,21,24,icon));
		readScores();
		add(ret);
	}
	public static void readScores()
	{
		int i=0;
		int j=0;
		int k=1;
		try { FileReader fr = new FileReader(fname);
			  BufferedReader br = new BufferedReader(fr);
			  String s = br.readLine();
			  while (s!=null) 
			  {
				if (k%2==0)
				  {
					scores[i]=Integer.parseInt(s);
					i++;
				  }
				else
				  {
					name[i]=s.trim();
					j++;
				  }
				k++;
				s=br.readLine();
			  }			
			   br.close();// 关闭缓冲读入流及文件读入流的连接.
		     } 
			catch (FileNotFoundException e1) {System.err.println("File not found: " + fname);} 
			catch (IOException e2) {}
	}
	
	public static void writeScores()
	{
		try
		{	
			FileWriter fw = new FileWriter("highscores.txt");
			
			for (int i=0;i<10 ;i++ )
			{
				fw.write(name[i]+"\n");
				fw.write(scores[i]+"\n");
			}
			fw.close();
		}
		catch (IOException e2) {}

	}

	public void paintComponent(Graphics g)	
	  {	
		super.paintComponent(g);
		Graphics2D g2=(Graphics2D)g;
		g2.setPaint(new GradientPaint(0,300,new Color(227,230,199),0,40,Color.white,false));
		g2.fillRect(0,0,705,575);
		
		//g.setColor(new Color(51,153,204));
		g.setColor(new Color(153,51,51));
		g.setFont(new Font("Monotype Corsiva",Font.BOLD,50));
		for (int i=0;i<10 ;i++ )
		{	
			String s;
			if (i==9)
			 s=(i+1)+". ";
			else
			 s="  "+(i+1)+". ";
			
			//g.drawString(s+name[i]+"                           "+scores[i],30,i*50+70);  
			g.drawString(s+name[i]+"                           ",30,i*50+70);  
			g.drawString(String.valueOf(scores[i]),500,i*50+70);  
		}
			
	  }

}





⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -