import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import java.awt.*;
public class Window extends Frame {
private int i;
public int v=0;
public int score = 0; //刚开始的分数为0
public static String Input ; //定义一个字符变量Input
public static int SPEED = 100; //改变SPEED可以改变蛇的速度
public boolean gameOver = false; //把游戏的结束定义一个布尔变量
public static final int BLOCK_SIZE= 10; //每个小正方形
public static final int GAME_WIDE = 800; //窗体的宽度
public static final int GAME_HEIGHT = 600; //窗体的高度
private Font font1 = new Font("黑体", Font.BOLD, 30); //字体设置
private Font font2 = new Font("宋体", Font.BOLD, 50);//字体设置
private Font font3 = new Font("黑体", Font.BOLD, 20);//字体设置
public static final int ROWS = GAME_HEIGHT/BLOCK_SIZE;
public static final int COLUMNS = GAME_WIDE/BLOCK_SIZE;
private static final Object message = "亲,如果你想关闭窗口,请按F1;按数字键1~5可以改变蛇的行进速度。";
PaintThread paintThread = new PaintThread(); //添加一个线程
Snake s = new Snake(this); //添加一条蛇
Food e = new Food(); //添加食物
Wind m = new Wind(); //添加消息提示框
Image offScreenImage = null; //引入双缓冲
/*
* 添加几堵墙,并设定墙的坐标、大小
*/
Walls wall1= new Walls(300,200,20,200,1); //添加墙1,并设定墙1的位置坐标
Walls wall2 = new Walls(360,400,200,20,2); //添加墙2,并设定墙2的位置坐标
Walls wall3= new Walls(250,400,20,150,3); //添加墙3,并设定墙3的位置坐标
Walls wall4= new Walls(100,100,20,211,4);
Walls wall5= new Walls(520,300,200,20,5);
Walls wall6= new Walls(500,100,20,100,5);
Walls wall7= new Walls(100,100,200,20,7);
Walls wall8= new Walls(400,300,80,20,8);
/*
* 贪吃蛇游戏的主窗口
*/
public void launchFrame(){
this.setSize(GAME_WIDE,GAME_HEIGHT); //设定窗口的宽度、高度
//让框架在屏幕的位置居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width; //得到屏幕的宽度
int screenHeight = screenSize.height; //得到屏幕的高度
int x = (screenWidth - this.getWidth())/2; //算出框架左上角点的横坐标
int y = (screenHeight - this.getHeight())/2;//算出框架左上角点的纵坐标
this.setLocation(x,y); //设定窗口左上角点的位置
this.setResizable(false); //禁止修改窗体的大小
this.setUndecorated(true); //消除边框不修饰事件
this.setVisible(true); //让窗口显示出来
/** 设置窗口关闭事件*/
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.addKeyListener(new KeyMonitor()); // 添加键盘监听
new Thread(paintThread).start(); // 添加线程重画事件
}
public void paint(Graphics g){
g.setColor(Color.gray); //设定游戏窗口的背景颜色
g.fillRect(30, 60, GAME_WIDE-60, GAME_HEIGHT-90);
g.setColor(Color.white); //窗口边框的颜色
g.fillRect(0,0, GAME_WIDE, 60); //窗口的上边框区域
g.fillRect(0,570, GAME_WIDE, 30); //窗口的下边框区域
g.fillRect(0,60,30,GAME_HEIGHT-90); //窗口的左边框区域
g.fillRect(770,60, 30,GAME_HEIGHT-90); //窗口的右边框区域
g.setColor(Color.getHSBColor(255,98,98));
g.fillRect(250,10,300,40); //窗口的红边框的位置、大小
g.fillRect(0,100,30,300); //窗口的红边框的位置、大小
g.fillRect(770,100,30,300); //窗口的红边框的位置、大小
g.setColor(Color.blue); //添加网格线,并设定网格线的颜色
/*
* 利用for循环画出网格线
*/
for(i=1;i<ROWS - 7;i++){ //画出横线
g.drawLine(BLOCK_SIZE*3, BLOCK_SIZE*(i+5),
GAME_WIDE-BLOCK_SIZE*3, BLOCK_SIZE*(i+5));
}
for(i=1;i<COLUMNS-4;i++){ //画出竖线
g.drawLine(BLOCK_SIZE*(i+2),BLOCK_SIZE*6,
BLOCK_SIZE*(i+2),GAME_HEIGHT-BLOCK_SIZE*3);
}
/*
* 设置分数的位置及字体的颜色,位置坐标
*/
g.setColor(Color.blue);
g.drawString("分数:"+ score, 30, 50);
Random TT = new Random(); //随机数产生器
g.setFont(font1); //设置边框的字体
g.setColor(Color.getHSBColor(TT.nextInt(255),
TT.nextInt(255),TT.nextInt(255)));
//画出对联“龙去神威在 蛇来紫气生 ”
g.drawString("喜",265,40);
g.drawString("迎",340,40);
g.drawString("新",415,40);
g.drawString("春",490,40);
g.drawString("龙",0,150);
g.drawString("去",0,200);
g.drawString("神",0,250);
g.drawString("威",0,300);
g.drawString("在",0,350);
g.drawString("蛇",769,150);
g.drawString("来",769,200);
g.drawString("紫",769,250);
g.drawString("气",769,300);
g.drawString("生",769,350);
/*
* 判断游戏是否结束,如果游戏结束
*/
if(gameOver){
for(;v==0;v++){
m.launchFrame();} //m表示游戏结束时的消息提示框
this.paintThread.gameOver(); //线程结束
}
s.eat(e); //蛇吃掉食物e
e.draw(g); //画出食物e
s.draw(g); //画出蛇
//画出三堵墙
wall1.draw(g);
wall2.draw(g);
//蛇是否撞到墙
s.zq(wall1);
s.zq(wall2);
if(score>10){
wall3.draw(g);
s.zq(wall3);
}
if(score>20){
wall4.draw(g);
s.zq(wall4);
}
if(score>30){
wall5.draw(g);
s.zq(wall5);
}
if(score>40){
wall6.draw(g);
s.zq(wall6);
}
if(score>50){
wall7.draw(g);
s.zq(wall7);
}
if(score>65){
wall8.draw(g);
s.zq(wall8);
}
}
/**
*添加键盘监听事件
*/
public void actionPerformed(ActionEvent e) {
}
/**
* 蛇的生死
*/
public void stop(){
gameOver = true;
}
/**
* 添加一个线程
* @author Zhang Huijun
*
*/
private class PaintThread implements Runnable{
private boolean running = true;
public void run() {
while(running){
repaint();
try{
Thread.sleep(SPEED-score/2); //改变SPEED的大小可以改变蛇的移动速度
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
public void gameOver(){
running = false; //如果gameOver,把running设为false
}
}
//得到键盘输入的指令
private class KeyMonitor extends KeyAdapter{
public void keyPressed(KeyEvent e) {
s.KeyPressed(e);
}
}
public int getScore() {
return score;
}
public void setSore(int score) {
this.score = score;
}
public class Wind extends JPanel {
Random hh = new Random();
//Window w;
//设定游戏结束窗口的各项信息
public void launchFrame (){
JFrame frame = new JFrame("Message");
frame.getContentPane().add( new Wind());//返回窗体的对象
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,300); //窗口的大小
//让小窗口居中显示
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;
int x = (screenWidth - frame.getWidth())/2;
int y = (screenHeight - frame.getHeight())/2;
frame.setLocation(x,y);
frame.setVisible(true);
}
protected void paintComponent(Graphics g){
super.paintComponents(g);
//得到当前设置的字体
g.setFont(font2); //设置字体
g.setColor(Color.red ); //设置颜色
g.drawString("游戏结束!",80,100); //设置字的位置、内容
g.setFont(font3); //设置字体
g.setColor(Color.getHSBColor(100,100,0)); //设置字体的颜色
g.drawString(Input+"才得了"+score+"分!",80,150); //设置字的位置、内容
g.drawString("小样,要不要再来一次??" , 80, 180); //设置字的位置、内容
}
}
/**
* 重写update方法,利用双缓冲消除屏幕画面闪烁现象
*/
public void update(Graphics g) {
if(offScreenImage == null){
offScreenImage = this.createImage(GAME_WIDE,GAME_HEIGHT);
}
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage,0,0,null);
}
/**
* 这个类是贪吃蛇游戏的主窗口
* @param Zhang Huijun
*/
public static void main(String[