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

📄 gamepanel.java

📁 本文可以良好的练习打字
💻 JAVA
字号:
package type;

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.net.URL;


public class GamePanel extends JPanel implements Runnable{
	private static String str="synchronized";
	//Syn syn=new Syn();
	private static int allCharCount;//最一次最多在Frame上显示的字符数
	private static int MaxCharCount=5;//最大的字符个数
	private static int MaxBallCount=3;//屏幕是的最大彩球数量
	static boolean GameOver=false;//游戏是否结束
	private int hasBallCount;//屏幕中现在彩球数量
	private static int speed;//速度
	private static int baseSpeed;//基本速度
	private static int start_pos;//开始位置
	private static int stupSpeed=1;//速度增量
	private static int maxSpeed=12;//最大速度
	private boolean isDown=false;//是否可以下落
	
	private static Image img;
	
	private static AudioClip mySong;
	private static AudioClip chomp;//矩形被吃的声音
	private static AudioClip dangerMusic;//危险提示音
	static AudioClip gameOver;//游戏结束音
	
	private static int dangerDistance=100;//提示距离
	private boolean flag=false;//是否在可显示区域内
	private static int reshTime;//刷新时间
	private char int_char;//字符
	private int pos;//位置
	private Thread thread=null;
	private  int showAllCount;//面板总共显示的字符数
	private  int hitsCount;//被击落的字符数
	boolean imageflag=false;
	private int this_width;
	private int this_heigth;
	private static Font this_font=new Font("Arial",Font.BOLD,20);
	
	static Random random=new Random();
	
	
	public static void reSetStaticCount(){
		GamePanel.allCharCount=0;
		GamePanel.GameOver=false;
	}
	public static void setMusic(){
		img=Toolkit.getDefaultToolkit().getImage("image/explode.gif");
		
		try{
			Class this_class;
			this_class=Class.forName("type.GamePanel");
			
			URL this_url_over=this_class.getResource("../audio/gameover.wav");
			gameOver=Applet.newAudioClip(this_url_over);
			
			URL this_url=this_class.getResource("../audio/firework.au");
			mySong=Applet.newAudioClip(this_url);
			
			URL this_url_chomp=this_class.getResource("../audio/chomp.au");
			chomp=Applet.newAudioClip(this_url_chomp);
			
			URL this_url_danger=this_class.getResource("../audio/danger.au");
			dangerMusic=Applet.newAudioClip(this_url_danger);
			
		}catch(Exception e){
			e.printStackTrace();
		}	
	}
	public GamePanel(String title,int reshTime,int speed){
		super();
		this.setName(title);
		this.reshTime=reshTime*100;
		this.speed=speed;
		this.baseSpeed=speed;
		this.hasBallCount=GamePanel.MaxBallCount;
		start_pos=5;
		this.setFont(GamePanel.this_font);
	}
	       
	public GamePanel(String title){
		this(title,1,2);
	}
	
	public void run(){
		while(true){
			System.out.println(Thread.currentThread().getName()+"运行中");
			
			this.setPostion();
			this.repaint();
			
			try{
				Thread.sleep(reshTime);
			}catch(Exception e){
				e.printStackTrace();
			}		
		}
	}
	
	
	private void setPostion(){
		if(GamePanel.GameOver){
			this.reset();	//游戏结束
			return;
		}
		
		System.out.println("字符数:"+GamePanel.allCharCount);
		
		if(this.canDown()){
			pos=pos+speed;
			if(pos>=start_pos&&flag==false){
				this.showAllCount++;
				flag=true;
			}
			if(this.IsDanger()){
				chomp.play();
				if(--this.hasBallCount<1){
					GamePanel.GameOver=true;
				}
				this.reset();
			}
		}
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		
		if(this.hasNomarl()){
			g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
			
			if(this.imageflag){
				g.drawImage(img,(this.getWidth()/2-10),pos-10,20,20,null);
				mySong.play();
				this.hitsCount++;
				this.reset();
				imageflag=false;
			}else{
				g.drawString(String.valueOf((char)this.int_char),this.getWidth()/2,pos);
			}
			
		}
		//画彩球
		g.setColor(Color.GREEN);
		int this_x,this_y;
		this_width=this.getWidth()-10;
		this_heigth=15;
		this_x=(this.getWidth()-this_width)/2;
		
		for(int i=0;i<this.hasBallCount;i++){
			this_y=this.getHeight()-this_heigth*(i+1)-10;
			
			if((this_y-this.pos)<=GamePanel.dangerDistance){
				dangerMusic.play();
				g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
			}
			
			g.fill3DRect(this_x,this_y,this_width,this_heigth,true);
		}	
	}
	
	public boolean hasNomarl(){
		if(pos>=start_pos&&pos<=this.getHeight()){
			return true;
		}
		return false;
	}
	
	public void reset(){	
		if(GamePanel.allCharCount>0)
			GamePanel.allCharCount--;
			
		this.pos=0;
		flag=false;
		this.setRandomChar();
	   	//System.out.println("字符数:"+GamePanel.allCharCount);
	}
	
	
	public void Down(boolean isDown){
		this.isDown=isDown;
	}
	
	public boolean getDown(){
		return this.isDown;
	}
	
	public void start(){
		if(thread!=null){
			return;
		}
		
		this.hitsCount=0;
		this.showAllCount=0;
		this.setRandomChar();
		
		thread=new Thread(this);
		thread.start();
		
	}
	
	public void resume() {		//恢复
		if(thread!=null){
			thread.resume();
		}
	}
	
	public void suspend(){		// 悬挂
		if(thread!=null){
			thread.suspend();
		}
		
	}
	
	public void stop(){
		if(thread!=null){
			this.int_char=32;	//空格
			this.pos=0;
			this.hasBallCount=this.MaxBallCount;
			thread.stop();
			thread=null;
			this.repaint();
			
			System.out.println("线程关闭:");
			if(thread==null){
				System.out.println("thread=null");
			}
		}
	}
	
	public int getPos(){
		return this.pos;
	}
	
	public char getChar(){
		return this.int_char;
	}
	
	public static boolean addSpeed(int level){
		if((GamePanel.baseSpeed+GamePanel.stupSpeed*level)>GamePanel.maxSpeed){
			return false;
		}
		GamePanel.speed=GamePanel.baseSpeed+GamePanel.stupSpeed*level;
		return true;
	}
	
	public int getShowAllCount(){
		return this.showAllCount;
	}
	
	public int getHitsCount(){
		return this.hitsCount;
	}
	
	
	
	private boolean IsDanger(){
		if((pos>=(this.getHeight()-this_heigth*this.hasBallCount-10))&&flag==true){
			return true;
		}
		return false;
	}
	
	private boolean canDown(){
		synchronized(GamePanel.str){
			if(this.pos!=0&&!this.imageflag&&this.isDown){
				return true;
			}
			if(this.pos==0&&GamePanel.allCharCount<MaxCharCount&&this.isDown){
				GamePanel.allCharCount++;
				System.out.println("Char : "+String.valueOf((char)this.int_char)+" : pos"+this.pos+" allCharCount:"+GamePanel.allCharCount);
				return true;
			}
	
			return false;
		}
	}
	
	
	
	private void setRandomChar(){
		int temp;
		while(true){
			temp=random.nextInt(255);
			if((48<=temp&&temp<=57)||(temp>=65&&temp<=90)||(temp>=97&&temp<=122)){
				this.int_char=(char)temp;
				break;
			}
		}
	}
	
	
	private void explode(Graphics g){
	
	}
	
	private void paintGlobule(Graphics g){
		
	}
	
}
















⌨️ 快捷键说明

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