📄 play.java
字号:
package type;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.net.URL;
class GamePanel extends JPanel implements Runnable{
static String str="synchronized";
//Syn syn=new Syn();
static int allCharCount;//最一次最多在Frame上显示的字符数
static int MaxCharCount=5;//最大的字符个数
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;//是否可以下落
static Image img;
static AudioClip mySong;
static AudioClip chomp;//矩形被吃的声音
static AudioClip dangerMusic;//危险提示音
static AudioClip gameOver;//游戏结束音
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);
public static Random random=new Random();
public static void reSetStaticCount(){
GamePanel.allCharCount=0;
GamePanel.GameOver=false;
}
public static void setMusic(){
img=Toolkit.getDefaultToolkit().getImage("explode.gif");
try{
Class this_class;
this_class=Class.forName("GamePanel");
URL this_url_over=this_class.getResource("gameover.wav");
gameOver=Applet.newAudioClip(this_url_over);
URL this_url=this_class.getResource("firework.au");
mySong=Applet.newAudioClip(this_url);
URL this_url_chomp=this_class.getResource("chomp.au");
chomp=Applet.newAudioClip(this_url_chomp);
URL this_url_danger=this_class.getResource("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){
}
}
public class Play extends JFrame{
Container c=null;
GamePanel[] games;
JLabel[] labels;
JButton jbtn_start;
JButton jbtn_set;
JButton jbtn_pause;
JButton jbtn_exit;
private long startTime;//开始游戏时间
private long currentTime;//当前时间
private URL this_url=null;//按键错误提示声地址
private AudioClip errSound=null;//按键错误提示声
private AudioClip backSound=null;
static AudioClip startMusic;//游戏开始音
static AudioClip addLevel;//玩家升级音
static AudioClip subLevel;//玩家降级音
int curLevel;//玩家当前的级别
int rate; //分数
int time; //时间
int mark; //记分
int level; //级别
int errCount;//错误
int baseMark=20;//系统默认值分数大于此值时则增加速度
private boolean IsStarted=false;//是否开始游戏
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -