📄 game.java
字号:
/*
* Game.java
*
* Created on 2007年3月12日, 下午8:58
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package tetrasquare;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.Random;
import java.util.Stack;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;
/**
*
* @author Caviar
*/
public class Game implements Serializable{
/** Creates a new instance of Game */
public Game(mainFrame f) {
this.frm=f;
}
public void newGame(){
frm.a[xkey][ykey].setRect(0);
xkey=0;ykey=0;
frm.a[0][0].setRect(1);
refresh();
level=1;grid_left=0;
empty_grid=0;score=0;
piecesleft=level*10;
isSaved=false;
isStart=true;
placeGrayChess();
randomShape();
paintInNextShape(shapes,direction);
frm.setStatistics();
}
public void placeShape(int xh,int yh){
nextshape=new Shape(xh,yh,shapes,direction);
if(canPut(nextshape)){
if(level==43){ //如果到43级了,那就不再放红色,全都显示为灰色
frm.a[nextshape.x1][nextshape.y1].setBlockColor(-1);
frm.a[nextshape.x2][nextshape.y2].setBlockColor(-1);
frm.a[nextshape.x3][nextshape.y3].setBlockColor(-1);
frm.a[nextshape.x4][nextshape.y4].setBlockColor(-1);
}
else{
paintShape(nextshape);
frm.g.vanish();
}
lastShapePut=true;
score+=1;
refresh();//刷新右下角下一个图形区域
randomShape();//随机生成下一个图形
paintInNextShape(shapes,direction);//画下一个图形
piecesleft--;
frm.setStatistics();
} else lastShapePut=false;
if(frm.g.isOver()&&piecesleft!=0) {//判断是否已经不能将最后一块加入,并弹出对话框让用户选择是否重新游戏
gameOver();
}
if(piecesleft==0&&level==43){
gameOver();
}
if(piecesleft==0){
nextLevel();
}
}
public void nextLevel(){
level++;
if(level>43)frm.g.gameOver();
piecesleft=level*10;
for(int i=0;i<8;i++)
for(int j=0;j<8;j++){
if(frm.a[i][j].getBlockColor()==1)
grid_left++;
else if(frm.a[i][j].getBlockColor()==0)
empty_grid++;
}
score+=10*empty_grid;
empty_grid=0;
placeGrayChess();//应该有个过关提示,还没想好做成什么样子
randomShape();
paintInNextShape(shapes,direction);
frm.setStatistics();
}
public void vanish(){
for(int i=1;i<7;i++)
for(int j=1;j<7;j++){
if(frm.a[i-1][j-1].getBlockColor()==1&&frm.a[i-1][j].getBlockColor()==1&&frm.a[i-1][j+1].getBlockColor()==1&&
frm.a[i][j+1].getBlockColor()==1&&frm.a[i+1][j+1].getBlockColor()==1&&
frm.a[i+1][j].getBlockColor()==1&&frm.a[i+1][j-1].getBlockColor()==1&&
frm.a[i][j-1].getBlockColor()==1&&frm.a[i][j].getBlockColor()==1){
vanish3Mark.push(new Point(i,j));
}
}
score+=10*vanish3Mark.size()*level;
frm.setStatistics();
while(!vanish3Mark.empty()){
Point p=(Point)vanish3Mark.pop();
for(int i=(int)(p.getX()-1);i<(int)(p.getX()+2);i++)
for(int j=(int)(p.getY()-1);j<(int)(p.getY()+2);j++){
frm.a[i][j].setBlockColor(0);
}
}
}
public void rotate(){
direction++;
frm.g.refresh();
frm.g.paintInNextShape(shapes,direction);
}
public void gameOver(){
isStart=false;
int j=JOptionPane.showConfirmDialog(frm,"游戏已结束,还要重开一局吗?","游戏结束",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE);
if (j == JOptionPane.YES_OPTION) {
//如果目前的分数大于本用户已获得的最高分,那么把这个分保存为最高分
if(score>frm.hs.users[user-1].score)
frm.hs.setUserInfo(score,level,piecesleft,user);
frm.hs.setTop(new UserInfo(score,level,piecesleft,user));
frm.hs.displayTop10();
frm.hs.saveRecord();
frm.g.newGame();
}
}
public void refresh(){
for(int i=0;i<5;i++)
for(int j=0;j<5;j++){
frm.b[i][j].setBlockColor(0);
}
}
public void paintShape(Shape sh){
paintChessman(sh.x1,sh.y1,1);
paintChessman(sh.x2,sh.y2,1);
paintChessman(sh.x3,sh.y3,1);
paintChessman(sh.x4,sh.y4,1);
}
public boolean canPut(Shape s){
if(s.x1>=0&&s.x1<8&&s.x2>=0&&s.x2<8&&s.x3>=0&&s.x3<8&&s.x4>=0&&s.x4<8&&s.y1>=0&&s.y1<8&&s.y2>=0&&s.y2<8&&s.y3>=0&&s.y3<8&&s.y4>=0&&s.y4<8&&
frm.a[s.x1][s.y1].getBlockColor()==0&&frm.a[s.x2][s.y2].getBlockColor()==0&&frm.a[s.x3][s.y3].getBlockColor()==0&&frm.a[s.x4][s.y4].getBlockColor()==0)
return true;
return false;
}
public void randomShape(){
shapes=(int)(Math.random()*6+2);
direction=(int)(Math.random()*3+2);
}
private void placeGrayChess(){
for(int j=0;j<8;j++){
for(int i=0;i<8;i++){
paintChessman(i,j,0);
}
}
for(int i=0;i<(int)(grid_left*(0.15+level*0.02));i++){
int x=(int)(Math.random()*100%8);
int y=(int)(Math.random()*100%8);
//如果有重复重新生成一个随机数
if(frm.a[x][y].getBlockColor()!=0){
i--;continue;
} else {
paintChessman(x,y,-1);
}
}
}
public void saveGame(){
if(!isStart) return;
try{
JFrame frame = new JFrame();
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new filefilter());
chooser.setAcceptAllFileFilterUsed(false);
int ret = chooser.showDialog(frame,"保存游戏");
if (ret != JFileChooser.APPROVE_OPTION) {
return;
}
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
game[i][j]=frm.a[i][j].getBlockColor();
FileVar.push(game);
FileVar.push(level);
FileVar.push(score);
FileVar.push(piecesleft);
FileVar.push(nextshape);
FileVar.push(isStart);
FileVar.push(shapes);
FileVar.push(direction);
File f = chooser.getSelectedFile();
if(!f.getName().endsWith(".lyb"))
if(!f.renameTo(new File(f.getName()+".lyb")))
System.out.println(f.getName());
FileOutputStream fileOut =new FileOutputStream(f);
ObjectOutputStream objOut=new ObjectOutputStream(fileOut);
objOut.writeObject(FileVar);
objOut.close();
fileOut.close();
isSaved=true;
} catch(Exception e){
System.out.println(e.getMessage());
}
}
public void loadGame(){
try{
JFrame frame = new JFrame();
JFileChooser chooser = new JFileChooser();
chooser.addChoosableFileFilter(new filefilter());
int ret = chooser.showDialog(frame,"继续游戏");
if (ret != JFileChooser.APPROVE_OPTION) {
return;
}
File f = chooser.getSelectedFile();
if (f.isFile() && f.canRead()){
FileInputStream fis = new FileInputStream(f);
ObjectInputStream is = new ObjectInputStream(fis);
FileVar=(Stack)is.readObject();
direction=Integer.parseInt(FileVar.pop().toString());
shapes=Integer.parseInt(FileVar.pop().toString());
isStart=Boolean.parseBoolean(FileVar.pop().toString());
nextshape=(Shape)FileVar.pop();
piecesleft=Integer.parseInt(FileVar.pop().toString());
score=Integer.parseInt(FileVar.pop().toString());
level=Integer.parseInt(FileVar.pop().toString());
game=(int[][])FileVar.pop();
for(int i=0;i<8;i++)
for(int j=0;j<8;j++)
frm.a[i][j].setBlockColor(game[i][j]);
frm.setStatistics();
paintInNextShape(shapes,direction);
if(frm.g.isOver()) frm.g.gameOver();
is.close();
fis.close();
isSaved=false;
}
} catch(Exception e){
System.out.print(e.getMessage());
}
}
//Shape为图形内部类,1是7型,2是反7,3是正Z,4是反Z,5是田,6是|,7是T型
public void paintInNextShape(int shape,int di){
int x = 0,y = 0;
if(shape==1&&di%4==0){
x=2;y=1;
}
if(shape==1&&di%4==1){
x=3;y=2;
}
if(shape==1&&di%4==2){
x=2;y=3;
}
if(shape==1&&di%4==3){
x=1;y=1;
}
if(shape==2&&di%4==0){
x=2;y=1;
}
if(shape==2&&di%4==1){
x=3;y=1;
}
if(shape==2&&di%4==2){
x=2;y=3;
}
if(shape==2&&di%4==3){
x=1;y=2;
}
if(shape==3&&di%4==0){
x=2;y=1;
}
if(shape==3&&di%4==1){
x=1;y=2;
}
if(shape==3&&di%4==2){
x=2;y=1;
}
if(shape==3&&di%4==3){
x=1;y=2;
}
if(shape==4&&(di%4==0||di%4==2)){
x=2;y=1;
}
if(shape==4&&(di%4==1||di%4==3)){
x=2;y=2;
}
if(shape==5){
x=2;y=1;
}
if(shape==6&&(di%4==0||di%4==2)){
x=2;y=1;
}
if(shape==6&&(di%4==1||di%4==3)){
x=1;y=2;
}
if(shape==7){
x=2;y=2;
}
Shape s=new Shape(x,y,shape,di);
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
frm.b[i][j].setBlockColor(0);
frm.b[s.x1][s.y1].setBlockColor(2);
frm.b[s.x2][s.y2].setBlockColor(1);
frm.b[s.x3][s.y3].setBlockColor(1);
frm.b[s.x4][s.y4].setBlockColor(1);
}
public void paintChessman(int x,int y,int c){
frm.a[x][y].setBlockColor(c);
}
public boolean isOver(){
for(int i=0;i<8;i++)
for(int j=0;j<8;j++){
if(frm.a[i][j].getBlockColor()==0){
for(int k=1;k<5;k++){
Shape test=new Shape(i,j,shapes,k);
if(canPut(test)) return false;
}
}
}
return true;
}
public void movCursor(int dir){
frm.a[xkey][ykey].setRect(0);
if(dir==1) if(ykey-1>=0){
ykey--;
}
if(dir==2) if(xkey+1<8) {
xkey++;
}
if(dir==3) if(ykey+1<8) {
ykey++;
}
if(dir==4) if(xkey-1>=0) {
xkey--;
}
frm.a[xkey][ykey].setRect(1);
}
public void setUser(int u){
this.user=u;
}
public class filefilter extends FileFilter{
public boolean accept(File f) {
return (f.isDirectory()||f.getName().endsWith(".lyb"));
}
public String getDescription() {
return ("TetraSquare 游戏存档 (*.lyb)");
}
}
protected mainFrame frm;
public Shape nextshape;
public int x=0,y=0;
public int xkey=0,ykey=0;
public int[][] game=new int[8][8]; //读取进度时用来保存棋子位置
public int level=1,piecesleft=0,grid_left=0,empty_grid=0;
public int score;
public int shapes;
private int user=1;
public int direction;
public boolean isStart=false,lastShapePut=false,isSaved=false;
private Stack vanish3Mark=new Stack();
private Stack FileVar=new Stack();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -