singlegameface.java
来自「软件工程实践课程的答案哦」· Java 代码 · 共 1,209 行 · 第 1/3 页
JAVA
1,209 行
this.gameFace.gameOver();
setScoreList();//set the ScoreList
}
public void setScoreList()
{
ObjectInputStream in;
ObjectOutputStream out;
//System.out.println("我在这里GetScoreList");
//LinkedList sList=new LinkedList();
InformationList sList=new InformationList();
try{
in=new ObjectInputStream(new FileInputStream("score.friend"));
sList=(InformationList)in.readObject();
//InformationList sList=new InformationList();
//sList.reLive();
if(in!=null)
{
//System.out.println("in yijing da kai le ");
sList=(InformationList)in.readObject();
}
//else sList=new LinkedList();
/* if(sList.getSize()!=0)
{
for(int i=0;i<sList.getSize();i++)
{
System.out.println("Kaishi Row: "+i+"Name :"+((Information)sList.search(i)).getUserName()+"Score: "+((Information)sList.search(i)).getScore());
}
}
int index=-1;
System.out.println("gameFace.score: "+gameFace.score);
while(((Information)sList.get(index+1)).getScore()<=gameFace.score&&index<sList.size()-1)
{
System.out.println(((Information)sList.get(index+1)).getScore());
index++;
}
System.out.println("Index: "+index);
if(index==-1)
index=0;*/
//sList.add(index,new Information("",gameFace.score));
sList.insert(new Information("",gameFace.score));
//sList.displayList();
in.close();
if(sList.getSize()!=0)
{
Win win=new Win();
win.init();
win.start();
win=null;
//JOptionPane.showMessageDialog(null,"Congratulations! You rank the TOP 5!");
//KillerList kList=new KillerList(rank);
Score obj=new Score(sList);
}
}catch(Exception q)
{
//System.out.println("yichangle ");
Win win=new Win();
win.init();
win.start();
win=null;
//JOptionPane.showMessageDialog(null,"It's the first time you experience our game.\n You will love it! Good Luck!");
//InformationList sList=new InformationList();
//sList=new LinkedList();
try{
//in=new ObjectInputStream(new FileInputStream("score.fiend"));
//sList=(LinkedList)in.readObject();
//in.close();
sList.insert(new Information("",gameFace.score));
out=new ObjectOutputStream(new FileOutputStream("score.friend"));
//ScoreList sList=new ScoreList();
//sList.add(new Information("",gameFace.score));
//sList.displayList();
//sList.s1=gameFace.score;
out.writeObject(sList);
out.flush();
out.close();
}catch(FileNotFoundException t) {}
//catch(ClassNotFoundException ev){}
catch(IOException r) {}
//System.out.println("Setting up new record succeed!");
//KillerList kList=new KillerList(1);
Score obj=new Score(sList);
}
}
}
class SinglePaintPanel extends JPanel implements KeyListener
{
URL blueURL = getClass().getResource("pics\\blue.jpg");
ImageIcon blue = new ImageIcon(blueURL);
URL yellowURL = getClass().getResource("pics\\yellow.jpg");
ImageIcon yellow = new ImageIcon(yellowURL);
URL blueGreenURL =getClass().getResource("pics\\blueGreen.jpg");
ImageIcon blueGreen = new ImageIcon(blueGreenURL);
URL greenURL =getClass().getResource("pics\\green.jpg");
ImageIcon green = new ImageIcon(greenURL);
URL redURL = getClass().getResource("pics\\red.jpg");
ImageIcon red = new ImageIcon(redURL);
URL purpleURL = getClass().getResource("pics\\purple.jpg");
ImageIcon purple = new ImageIcon(purpleURL);
URL grayURL = getClass().getResource("pics\\gray.jpg");
ImageIcon gray = new ImageIcon(grayURL);
URL teamURL = getClass().getResource("pics\\team.jpg");
ImageIcon team = new ImageIcon(teamURL);
URL teamTagURL = getClass().getResource("pics\\teamTag.jpg");
ImageIcon teamTag = new ImageIcon(teamTagURL);
int horizonal = 0;
int vertical = 0;
static int fallTag = 0;
static int fallTag2 = 0;
int size = SingleGameFace.unitSize;
SingleGameFace gameFace;
public synchronized void paintComponent(Graphics g)
{
super.paintComponent(g);
if(gameFace.gameStart)
{ //get non-zero location and paint it
SingleLOV lov = gameFace.lovList.head;
g.drawImage(team.getImage(),5,120,Color.black,this);
while(lov!=null)
{
this.paintRec(g,lov.x,lov.y,lov.color);
lov = lov.next;
}
}
else if(gameFace.gameOver)
{
SingleLOV lov = gameFace.lovList.head;
while(lov!=null)
{
this.paintRec(g,lov.x,lov.y,-1);
lov = lov.next;
}
g.setFont(new Font("ScanSerif",Font.BOLD,55));
g.setColor(Color.green);
g.drawString("GAME",75,300);
g.drawString("OVER",75,370);
}
else
{
g.drawImage(teamTag .getImage(),5,120,Color.black,this);
g.setColor(Color.WHITE);
g.drawLine(0,size*3,350,size*3);
// this.setBackground(Color.BLACK);
}
if(gameFace.isPaused==1)
{
g.setFont(new Font("ScanSerif",Font.BOLD,55));
g.setColor(Color.green);
g.drawString("PAUSED",60,300);
}
}
/**paint the square in location(y,x)
* */
private void paintRec(Graphics g,int x ,int y,int color)
{
g.setColor(Color.WHITE);
g.drawLine(0,size*3,350,size*3);
switch(color)
{
case -1:
g.drawImage(gray.getImage(),y*size,x*size,Color.black,this);
break;
case 1:
g.drawImage(red.getImage(),y*size,x*size,Color.black,this);
// g.setColor(Color.red);
break;
case 2:
g.drawImage(blueGreen.getImage(),y*size,x*size,Color.black,this);
// g.setColor(Color.green);
break;
case 3:
g.drawImage(yellow.getImage(),y*size,x*size,Color.black,this);
// g.setColor(Color.yellow);
break;
case 4:
g.drawImage(blue.getImage(),y*size,x*size,Color.black,this);
// g.setColor(Color.blue);
break;
case 5:
g.drawImage(purple.getImage(),y*size,x*size,Color.black,this);
// g.setColor(Color.pink);
break;
case 6:
g.drawImage(green.getImage(),y*size,x*size,Color.black,this);
// g.setColor(Color.pink);
}
//g.fill3DRect(y*size,x*size,size,size,true);
}
public void setGameFace(SingleGameFace gameFace)
{
this.gameFace = gameFace;
}
public void leftMove()
{
if(gameFace.speed!=0&&!gameFace.bar.checkBottom())
gameFace.bar.left();
else if(gameFace.speed==0)
gameFace.bar.left();
this.repaint();
}
public void rightMove()
{
if(gameFace.speed!=0&&!gameFace.bar.checkBottom())
gameFace.bar.right();
else if(gameFace.speed==0)
gameFace.bar.right();
this.repaint();
}
public void shift()
{
if(gameFace.speed!=0&&!gameFace.bar.checkBottom())
gameFace.bar.shift();
else if(gameFace.speed==0)
gameFace.bar.shift();
this.repaint();
}
public void downMove()
{
gameFace.bar.dropOneStep();
this.repaint();
}
/*public void setStart(boolean isStart)
{
this.isStart = isStart;
}*/
public synchronized void down()
{
gameFace.bar.drop();
this.repaint();
}
/////////keyListener///////////////
public void keyTyped(KeyEvent e)
{
}
public void keyPressed(KeyEvent e)
{
if((gameFace.gameStart&& fallTag == 0)||gameFace.speed!=0&&fallTag2==0)
switch(e.getKeyCode())
{
case KeyEvent.VK_DOWN:
if(gameFace.speed==0)
gameFace.timer.resume();
fallTag = 1;
break;
case KeyEvent.VK_LEFT :
this.leftMove();
break;
case KeyEvent.VK_RIGHT :
this.rightMove();
break;
case KeyEvent.VK_UP:
this.shift();
this.repaint() ;break;
case KeyEvent.VK_SPACE:
fallTag=1;
fallTag2=1;
this.down();
this.repaint();
gameFace.timer.resume();
break;
}
}
public void keyReleased(KeyEvent e)
{
}
}
/**The panel displaying the next bar during the game
*
*
*/
class SinglePaintNextPanel extends JPanel
{
SingleGameFace gameFace;
final static int size = 20;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(gameFace.gameStart)
{
SingleColorSet colorSet = gameFace.numCreator.getNext();
int[] colors = new int[3];
colors[0] = colorSet.getColor0();
colors[1] = colorSet.getColor1();
colors[2] = colorSet.getColor2();
for(int i=0;i<3;i++)
{
paintNext(g,i+1,3,colors[i]);
}
}
else
{
}
}
public void paintNext(Graphics g,int x, int y,int color)
{
switch(color)
{
case 1:
g.setColor(new Color(255,23,8));
break;
case 2:
g.setColor(new Color(6,190,254));
break;
case 3:
g.setColor(new Color(254,240,14));
break;
case 4:
g.setColor(new Color(8,83,254));
break;
case 5:
g.setColor(new Color(240,3,255));
break;
case 6:
g.setColor(new Color(50,254,3));
break;
}
g.fill3DRect(50,x*size,size,size,true);
}
public void setGameFace(SingleGameFace gameFace)
{
this.gameFace = gameFace;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?