[Java] 纯文本查看 复制代码
package olddriver;
import javax.swing.*;
import java.awt.*;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.ReentrantLock;
import static java.util.concurrent.Executors.newFixedThreadPool;
public class OnScreen extends JFrame implements KeyListener{
public static final ReentrantLock lock=new ReentrantLock();
private static final int FRONTLINE =34, LEFTLINE =32;
private static int playerpoint= LEFTLINE /2-3;
private static final int HIGH = FRONTLINE -2;
private static ArrayList<Boolean> monster_row1 = new ArrayList<>();
private static ArrayList<Boolean> monster_row2 = new ArrayList<>();
private static ArrayList<Boolean> monster_row3 = new ArrayList<>();
private static ArrayList<Boolean> monster_row4 = new ArrayList<>();
private static ArrayList<Boolean> monster_row5 = new ArrayList<>();
private static ArrayList<Boolean> monster_row6 = new ArrayList<>();
private static ArrayList<Boolean> monster_list = new ArrayList<>();
private static ArrayList<ArrayList<Boolean>> monster_row =new ArrayList<>();
private static int ball_row = LEFTLINE /2;
private static int ball_list = HIGH -1;
private static boolean gamestart=false;
private static int gamespeed=100;
private static boolean gameover=true;
private static boolean hardselect=false;
private static final String info="哲学版打方块5.0版本 \n 请选择游戏难度 \n 数字键:0-6 \n 其它键为默认难度 \n 0最快 6最慢 \n 左右键或者AD键控制方向";
private static ExecutorService gameline=newFixedThreadPool(2);
private Center center=new Center();
private FlushWindow flushWindow=new FlushWindow();
private static JMenuBar jMenuBar=new JMenuBar();
private static JMenu jMenu1=new JMenu("文件");
private static JMenuItem jMenuItem1=new JMenuItem("打开历史记录");
private static File savefile=new File("abc.save");
private static FileInputStream load;
JPanel jp1;
static JTextPane jt1;
public OnScreen(){
jp1=new JPanel();
jt1=new JTextPane();
jp1.add(jt1);
jt1.setPreferredSize(new Dimension(450,590));
jt1.setEditable(false);
jt1.setFont(new Font("黑体", Font.BOLD,30));
jt1.addKeyListener(this);
this.add(jp1, BorderLayout.CENTER);
this.setSize(500,690);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(800,360);
this.setResizable(false);
this.setVisible(true);
this.setTitle("哲学版打方块5.0!!!");
this.setJMenuBar(jMenuBar);
jMenuBar.add(jMenu1);
jMenu1.add(jMenuItem1);
if (!savefile.exists()) {
try {
savefile.createNewFile();
} catch (IOException e) {
}
} else{
try {
load=new FileInputStream(savefile);
} catch (FileNotFoundException e) {
}
}
}
public static void main(String[] args) {
gameInit();
OnScreen onScreen=new OnScreen();
jt1.setText(info);
// playerpoint=LEFTLINE-6;
/*//左右移动小测试
boolean left=false;
boolean right=false;
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (playerpoint<LEFTLINE-6 && !right){
playerpoint++;
} else if (playerpoint==LEFTLINE-6){
playerpoint--;
right=true;
} else if(playerpoint==1){
playerpoint++;
right=false;
} else if (right && playerpoint>1){
playerpoint--;
}
onScreen.jt1.setText(onScreen.flushScreen(playerpoint));
}*/
//
//刷新画面
// onScreen.center();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (!gamestart && !hardselect) {
gameHard((char)e.getKeyCode());
hardselect=true;
}
if (!gamestart && hardselect){
jt1.setFont(new Font("黑体", Font.BOLD,12));
gamestart=true;
gameover=true;
gameline.submit(center);
gameline.submit(flushWindow);
playerpoint++;
}
if ((e.getKeyCode()==KeyEvent.VK_LEFT || e.getKeyCode()==KeyEvent.VK_A) && playerpoint>1 && gameover){
playerpoint--;
}
if((e.getKeyCode()==KeyEvent.VK_RIGHT || e.getKeyCode()==KeyEvent.VK_D) && playerpoint< LEFTLINE -6 && gameover){
playerpoint++;
}
}
@Override
public void keyReleased(KeyEvent e) {
if ((e.getKeyCode()==KeyEvent.VK_LEFT || e.getKeyCode()==KeyEvent.VK_A) && playerpoint>1 && gameover){
playerpoint--;
} else if((e.getKeyCode()==KeyEvent.VK_RIGHT || e.getKeyCode()==KeyEvent.VK_D) && playerpoint< LEFTLINE -6 && gameover){
playerpoint++;
}
}
public static String flushScreen(int playerpoint){
String temp="";
for (int i = 0; i <= FRONTLINE; i++) {
for (int i1 = 0; i1 <= LEFTLINE; i1++) {
if(i == 0 || i == FRONTLINE) {
temp=(temp+"□");
} else if(i1 == 0 || i1 == LEFTLINE) {
temp=(temp+"□");
} else if(i == HIGH && i1 == playerpoint) {
temp=(temp+"======");
i1 += 5;
} else if(monster_list.get(i) && monster_row.get(i>0&&i<6?i-1:5).get(i1)) {
temp=(temp+"♂");
} else if(ball_list==i && ball_row==i1){
temp=(temp+"Ο");
} else {
temp=(temp+" ");
}
}
temp=(temp+"\n");
}
return temp;
}
public static class Center implements Runnable{
@Override
public void run() {
boolean left_wall=true;
boolean right_wall=false;
boolean front_wall=false;
boolean kick=true;
boolean dead=false;
boolean kill_monster=false;
synchronized (lock){
ArrayList<Integer> temp=new ArrayList<>();
while (gameover) {
temp.clear();
for (int i = 0; i < 6; i++) {
temp.add(playerpoint+i);
}
int point=temp.indexOf(ball_row);
try {
Thread.sleep(gamespeed);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(ball_list==FRONTLINE-1){
dead=true;
}
if(ball_row==LEFTLINE-1){
right_wall=true;
left_wall=false;
}
if(ball_list==1){
front_wall=true;
kick=false;
}
if(ball_row==1){
right_wall=false;
left_wall=true;
}
if(ball_list<7){
if(monster_row.get(ball_list-1).get(ball_row-1)){
monster_row.get(ball_list-1).set(ball_row-1,false);
if(monster_row.get(ball_list).get(ball_row-1)){
monster_row.get(ball_list).set(ball_row-1,false);
}
kill_monster=true;
kick=false;
}
}
if(ball_list==HIGH-1 && (point != -1)){
kick=true;
front_wall=false;
kill_monster=false;
if(point==0) {
if(left_wall) {
right_wall = true;
left_wall = false;
}
}
if(point==5) {
if(right_wall) {
right_wall = false;
left_wall = true;
}
}
}
if(dead){
gameover=false;
jt1.setText("你死啦!! \n 莫慌!5秒后游戏重新开始。。");
jt1.setFont(new Font("黑体",Font.BOLD,25));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
gameOver();
break;
}
if(left_wall){
ball_row++;
}
if(right_wall){
ball_row--;
}
if(front_wall){
ball_list++;
}
if(kick){
ball_list--;
}
if(kill_monster){
ball_list++;
}
jt1.setText(flushScreen(playerpoint));
}
}
}
}
private static void init(boolean b){
monster_row1.add(b);
monster_row2.add(b);
monster_row3.add(b);
monster_row4.add(b);
monster_row5.add(b);
}
public static void gameHard(char hard){
switch (hard){
case '0':
gamespeed=33;
break;
case '1':
gamespeed=66;
break;
case '2':
gamespeed=100;
break;
case '3':
gamespeed=200;
break;
case '4':
gamespeed=300;
break;
case '5':
gamespeed=400;
break;
case '6':
gamespeed=500;
break;
case 96:
gamespeed=33;
break;
case 97:
gamespeed=66;
break;
case 98:
gamespeed=100;
break;
case 99:
gamespeed=200;
break;
case 100:
gamespeed=300;
break;
case 101:
gamespeed=400;
break;
case 102:
gamespeed=500;
break;
default:
gamespeed=200;
break;
}
}
public static class FlushWindow implements Runnable{
@Override
public void run() {
synchronized (lock) {
while (gameover) {
jt1.setText(flushScreen(playerpoint));
try {
Thread.sleep(33);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void gameOver() {
gameInit();
jt1.setText(info);
}
public static void gameInit(){
playerpoint= LEFTLINE /2-3;
monster_row1 = new ArrayList<>();
monster_row2 = new ArrayList<>();
monster_row3 = new ArrayList<>();
monster_row4 = new ArrayList<>();
monster_row5 = new ArrayList<>();
monster_row6 = new ArrayList<>();
monster_list = new ArrayList<>();
monster_row =new ArrayList<>();
ball_row = LEFTLINE /2;
ball_list = HIGH -1;
gamespeed=100;
init(false);
for (int i=1;i<LEFTLINE;i++){
init(true);
}
init(false);
for (int i=0;i<=LEFTLINE;i++) {
monster_row6.add(false);
}
monster_list.add(false);
for (int i = 1; i < 6; i++) {
monster_list.add(true);
}
for (int i = 6; i < FRONTLINE; i++) {
monster_list.add(false);
}
monster_row.add(monster_row1);
monster_row.add(monster_row2);
monster_row.add(monster_row3);
monster_row.add(monster_row4);
monster_row.add(monster_row5);
monster_row.add(monster_row6);
gamestart=false;
gameover=false;
hardselect=false;
}
}