package win;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
//import com.sun.corba.se.impl.javax.rmi.CORBA.Util;
//import com.sun.corba.se.pept.transport.Selector;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.*;
public class elsfk extends Frame {
final int RECT_HEIGHT = 30;
final int RECT_WIDTH = 20;
int keyCode = 0;
int off_x = 10;
int off_y = 0;
int score = 0;
int highScore_easy = 0;
int highScore_mid = 0;
int highScore_hard = 0;
int difficulty = 0;
Panel pl_westPanel ;
Panel pl_eastPanel ;
Panel pl_southPanel = new Panel();
Label lb_nextShape;
Label lb_scoreTitle;
Label lb_highScoreTitle;
Label lb_currentSession;
Label lb_difficultyTitle;
Button bt_start;
String s = new String();
Timer tm = new Timer(true);
TimerTask tt;
boolean gameStart = false;
boolean gameOver = false;
boolean isPause = true;
Font numFont = new Font("Times New Roman",Font.PLAIN,18);
Font hzFont = new Font("宋体",Font.PLAIN,15);
boolean[][] array = new boolean[RECT_WIDTH][RECT_HEIGHT];
boolean[][] rightArray = new boolean[RECT_WIDTH][RECT_HEIGHT];
// _ _
// | |
// |_ _|
int[] shape1_CoordArray ={0,1,1,0,0,0,1,1};
// _
// | |
// | |_
// |_ _|
// _ _ _
// | _ _|
// |_|
// _ _
// |_ |
// | |
// |_|
// _
// _ _| |
// |_ _ _|
int[] shape2_CoordArray ={0,0,0,1,0,1,2,2,
0,0,1,2,1,0,0,0,
0,1,1,1,0,0,1,2,
2,2,1,0,0,1,1,1};
// _
// _| |_
// |_ _ _|
// _
// | |_
// | _|
// |_|
// _ _ _
// |_ _|
// |_|
// _
// _| |
// |_ |
// |_|
//
int[] shape3_CoordArray ={1,0,1,2,0,1,1,1,
0,0,0,1,0,1,2,1,
0,1,2,1,0,0,0,1,
0,1,1,1,1,0,1,2};
// _
// | |
// | |
// | |
// |_|
// _ _ _ _
// |_ _ _ _|
int[] shape4_CoordArray ={0,0,0,0,0,1,2,3,
0,1,2,3,0,0,0,0};
shape shape;
shape nextShape;
class shape{
int[] Coord;//基本坐标
int curType;
int totalTypes;
shape(int[] co){
int i;
totalTypes = co.length/8;
Random rnd = new Random();
curType = rnd.nextInt(totalTypes);
Coord = new int[co.length];
for(i = 0;i < co.length;i++)
{
Coord[i] = co[i];
}
}
public int getCoordX(int i)
{
return Coord[curType * 8 + i];
}
public int getCoordY(int i)
{
return Coord[curType * 8 + 4 +i];
}
public void change()
{
if (curType == totalTypes - 1)
{
curType = 0;
}
else
{
curType++;
}
}
public void changeBack()
{
if (curType == 0)
{
curType = totalTypes - 1;
}
else
{
curType--;
}
}
}
public void gameStart()
{
newShapeStart();
shape = nextShape;
newShapeStart();
tm.schedule(tt, 0,200);
}
public void checkDeleteLine()
{
int i,j,k;
int count = 0;
j = RECT_HEIGHT - 1;
while(j >= 0)
{
count = 0;
for(i = 0;i < RECT_WIDTH;i++)
{
if(array[i][j] == true)
{
count ++;
}
}
if(count == RECT_WIDTH)
{
score += 100;
for(k = j;k > 0;k--)
{
for(i = 0;i < RECT_WIDTH;i++)
{
array[i][k] = array[i][k - 1];
}
}
for(i = 0;i < RECT_WIDTH;i++)
{
array[i][0] = false;
}
}
else
{
j --;
}
}
}
public void newShapeStart()
{
off_y = 0;
off_x = 10;
Random rnd = new Random();
switch(rnd.nextInt(4))
{
case 0:
nextShape = new shape(shape1_CoordArray);;
break;
case 1:
nextShape = new shape(shape2_CoordArray);
break;
case 2:
nextShape = new shape(shape3_CoordArray);
break;
case 3:
nextShape = new shape(shape4_CoordArray);
break;
default:
break;
}
pl_eastPanel.repaint();
}
public void moveOrChange(int code)
{
int i,j;
boolean conflict = false;
if(code != KeyEvent.VK_UP && code != KeyEvent.VK_DOWN&& code != KeyEvent.VK_LEFT&& code != KeyEvent.VK_RIGHT)
{
return;
}
for(i = 0;i < 4;i++)
{
array[off_x + shape.getCoordX(i)][off_y + shape.getCoordY(i)] = false;
}
if(code == KeyEvent.VK_UP)
{
shape.change();
}
else if(code == KeyEvent.VK_DOWN)
{
off_y ++;
}
else if(code == KeyEvent.VK_LEFT)
{
off_x --;
}
else if(code == KeyEvent.VK_RIGHT)
{
off_x ++;
}
for(i = 0;i < 4;i++)
{
if(off_x + shape.getCoordX(i) < 0 || off_x + shape.getCoordX(i) > (RECT_WIDTH - 1) || off_y + shape.getCoordY(i) > (RECT_HEIGHT - 1) || array[off_x + shape.getCoordX(i)][off_y + shape.getCoordY(i)] == true)
{
if(code == KeyEvent.VK_UP)
{
shape.changeBack();
}
else if(code == KeyEvent.VK_DOWN)
{
off_y --;
conflict = true;
}
else if(code == KeyEvent.VK_LEFT)
{
off_x ++;
}
else if(code == KeyEvent.VK_RIGHT)
{
off_x --;
}
break;
}
}
for(i = 0;i < 4;i++)
{
array[off_x + shape.getCoordX(i)][off_y + shape.getCoordY(i)] = true;
}
if(conflict == true)
{
if(off_y < 1){
for(i = 0;i < RECT_WIDTH;i++)
{
for(j = 0;j < RECT_HEIGHT;j++)
{
array[i][j] = false;
}
}
isPause = true;
new ModalDialog("");
writeHighScore(score);
score = 0;
shape = nextShape;
newShapeStart();
isPause = false;
}
else
{
shape = nextShape;
checkDeleteLine();
newShapeStart();
}
}
}
void pauseOrStart()
{
if (bt_start.getLabel() == "Start")
{
if(gameStart == false)
{
gameStart = true;
gameStart();
}
bt_start.setLabel("Pause");
}
else
{
bt_start.setLabel("Start");
}
isPause = !isPause;
}
void readFile2String(StringBuffer sb,String fileName)
{
BufferedReader br;
String s;
try{
br=new BufferedReader(new FileReader(fileName));
while((s=br.readLine())!=null) {
sb.append(s);
}
br.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
void writeString2File(StringBuffer sb,String fileName)
{
BufferedWriter bw;
String s;
try{
bw=new BufferedWriter(new FileWriter(fileName));
bw.write(sb.toString());
bw.close();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
int readHighScore()
{
File file = new File(".\\record.txt");
if(file.exists()){
String sFileName = file.toString();
StringBuffer sFileContent = new StringBuffer();
String s1 = "highScore_easy:";
readFile2String(sFileContent,sFileName);
int highScoreEasyIndex = sFileContent.indexOf(s1);
int len = s1.length();
s1 = sFileContent.substring(highScoreEasyIndex + len);
return Integer.parseInt(s1);
}
return 0;
}
void writeHighScore(int newHighScore)
{
File file = new File(".\\record.txt");
String sFileName = file.toString();
StringBuffer sFileContent = new StringBuffer();
String s1 = "highScore_easy:";
String newS;
newS = String.format("%d", newHighScore);
if(file.exists()){
readFile2String(sFileContent,sFileName);
int highScoreEasyIndex = sFileContent.indexOf(s1);
int len = s1.length();
s1 = sFileContent.substring(highScoreEasyIndex + len);
if (newHighScore > Integer.parseInt(s1))
{
sFileContent.replace(highScoreEasyIndex + len, highScoreEasyIndex + len + s1.length(), newS);
writeString2File(sFileContent,sFileName);
}
}
else
{
try {
file.createNewFile();
sFileContent.append(s1);
sFileContent.append(newS);
writeString2File(sFileContent,sFileName);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public class ModalDialog extends JDialog{
Button bt_ok;
Label lb_gameOver;
ModalDialog(String str){
setModal(true);
setSize(180,120);
setTitle(str);
setLocationRelativeTo(null);//窗口居中
setLayout(null);
setBackground(Color.white);
lb_gameOver = new Label("GAME OVER !!!");
lb_gameOver.setFont(numFont);
lb_gameOver.setForeground(Color.RED);
lb_gameOver.setLocation(20,20);
lb_gameOver.setSize(150,20);
bt_ok = new Button("确认");
bt_ok.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
dispose();
}
});
bt_ok.setLocation(60,50);
bt_ok.setSize(60,30);
add(lb_gameOver);
add(bt_ok);
setVisible(true);
}
}
public elsfk(){
super();
// try {
// FileInputStream in = new FileInputStream(".\record.txt");
// } catch (FileNotFoundException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
// try {
// FileOutputStream out = new FileOutputStream(".\record.txt");
// } catch (FileNotFoundException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
highScore_easy = readHighScore();
requestFocus();
//全局键盘处理
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(new KeyEventPostProcessor()
{
public boolean postProcessKeyEvent(KeyEvent e)
{
if(e.getID() == KeyEvent.KEY_TYPED && (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_SPACE))
{
pauseOrStart();
return true;
}
else if(e.getID() == KeyEvent.KEY_PRESSED && !isPause)
{
Graphics g = pl_westPanel.getGraphics();
keyCode = e.getKeyCode();
int i,j;
moveOrChange(keyCode);
for(i = 0;i < RECT_WIDTH;i++)
{
for(j = 0;j < RECT_HEIGHT;j++)
{
if(array[i][j] == true)
{
g.setColor(Color.red);
g.fill3DRect(1 + i * 10, 1 + j * 10, 10, 10, true);
}
else
{
g.setColor(Color.white);
g.fillRect(1 + i * 10, 1 + j * 10, 10, 10);
}
}
}
return true;
}
else
{
return false;
}
}
}
);
addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){writeHighScore(score);System.exit(0);}});//按下关闭按钮退出
setLayout(null);
setBackground(Color.white);
setSize(300,400);
setTitle("俄罗斯方块");
setLocationRelativeTo(null);//窗口居中
bt_start = new Button("Start");
bt_start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
pauseOrStart();
}
});
bt_start.setLocation(70,10);
bt_start.setSize(60,30);
lb_nextShape = new Label("下个形状:");
lb_nextShape.setFont(hzFont);
lb_nextShape.setForeground(Color.BLUE);
lb_nextShape.setLocation(10, 10);
lb_nextShape.setSize(80,30);
lb_scoreTitle = new Label("得分:");
lb_scoreTitle.setFont(hzFont);
lb_scoreTitle.setForeground(Color.BLUE);
lb_scoreTitle.setLocation(10, 90);
lb_scoreTitle.setSize(80,30);
lb_highScoreTitle = new Label("最高分:");
lb_highScoreTitle.setFont(hzFont);
lb_highScoreTitle.setForeground(Color.BLUE);
lb_highScoreTitle.setLocation(10, 160);
lb_highScoreTitle.setSize(80,30);
lb_difficultyTitle = new Label("难易度:");
lb_difficultyTitle.setFont(hzFont);
lb_difficultyTitle.setForeground(Color.BLUE);
lb_difficultyTitle.setLocation(10, 225);
lb_difficultyTitle.setSize(80,30);
pl_westPanel = new Panel(){
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.drawRect(0,0,RECT_WIDTH * 10 + 1,RECT_HEIGHT * 10 + 1);
}
};
pl_eastPanel = new Panel(){
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
int i,j;
// String s = new String();
g.setColor(Color.red);
g.setFont(numFont);
if (nextShape != null)
{
for(i = 0;i < 4;i++)
{
g.fill3DRect(30 + 10 * nextShape.getCoordX(i), 50 + 10 * nextShape.getCoordY(i), 10, 10, true);
}
}
s = String.format("%d", score);
g.drawString(s, 30, 140);
s = String.format("%d", highScore_easy);
g.drawString(s, 30, 210);
//g.setFont(hzFont);
if(difficulty == 0)
{
g.drawString("easy", 20, 280);
}
else if(difficulty == 1)
{
g.drawString("middle", 20, 280);
}
else if(difficulty == 2)
{
g.drawString("hard", 20, 280);
}
}
};
pl_westPanel.setLocation(8, 30);
pl_westPanel.setSize(RECT_WIDTH * 10 + 2, RECT_HEIGHT * 10 + 2);
add(pl_westPanel);
pl_eastPanel.setLocation(RECT_WIDTH * 10 + 10,30);
pl_eastPanel.setSize(82, RECT_HEIGHT * 10 + 2);
//pl_eastPanel.setBackground(Color.yellow);
add(pl_eastPanel);
//pl_southPanel.setBackground(Color.gray);
pl_southPanel.setLocation(8,RECT_HEIGHT * 10 + 2 + 30);
pl_southPanel.setSize(286, 60);
add(pl_southPanel);
pl_eastPanel.setLayout(null);
pl_eastPanel.add(lb_nextShape);
pl_eastPanel.add(lb_scoreTitle);
pl_eastPanel.add(lb_highScoreTitle);
pl_eastPanel.add(lb_difficultyTitle);
pl_southPanel.setLayout(null);
pl_southPanel.add(bt_start);
setVisible(true);
tt = new TimerTask(){
Graphics g = pl_westPanel.getGraphics();
/* (non-Javadoc)
* @see java.util.TimerTask#run()
*/
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
int j = 0;
do {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
} while (isPause);
moveOrChange(KeyEvent.VK_DOWN);
for(i = 0;i < RECT_WIDTH;i++)
{
for(j = 0;j < RECT_HEIGHT;j++)
{
if(array[i][j] == true)
{
g.setColor(Color.red);
g.fill3DRect(1 + i * 10, 1 + j * 10, 10, 10, true);
}
else
{
g.setColor(Color.white);
g.fillRect(1 + i * 10, 1 + j * 10, 10, 10);
}
}
}
}
};
}
public static void main(String[] args){
new elsfk();
}
}
最近下载更多
luoyanglin LV2
2024年9月14日
jiangqiang LV12
2024年1月16日
zhihong fan LV2
2023年12月26日
1112WHQ LV7
2023年11月3日
微信网友_6699076084797440 LV7
2023年10月30日
luo110012 LV9
2023年5月15日
微笑刺客 LV21
2023年3月7日
522881297 LV1
2022年12月19日
微信网友_6248906393866240 LV1
2022年12月5日
包渤昊 LV2
2022年11月30日

最近浏览