package console;
/**
* @author: huluhulu~
* @date: 2022-06-28 11:02
* @description: 本文件为Console.java 主要提供了游戏的界面
* @version: 1.0
*/
import data.Source;
import object.*;
import position.Pos;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import static object.Dir.*;
import static object.Dir.DOWN;
public class Console extends Panel implements KeyListener, ActionListener {
//蛇和食物
private Snake snake;
private Food food;
private boolean start;//判断游戏是否开始
private boolean fail; //判断游戏是否失败结束
private boolean stop; //判断游戏是否暂停
private boolean fast; //判断游戏是否暂停
//游戏界面的大小:800*800
private int console_x,console_y;
//一节的大小
private int block_x,block_y;
//当前分数 与最大分数
private int score;
private int maxScore;
//双缓冲:避免gui闪烁
private Image iBuffer;
private Graphics gBuffer;
//计时器
Timer timer;
Timer fastTimer;
//无参构造函数
public Console(){
this.setFocusable(true);
this.addKeyListener(this); //添加键盘监听
//初始化蛇和食物
this.snake=new Snake();
this.food=new Food();
//蛇和食物不重叠
while(snake.isInSnake(food.getPos())){
food.genFood();
}
System.out.println("food pos:"+food.getPos().x+","+food.getPos().y);
//初始化游戏状态
this.start=false;
this.fail=false;
this.stop=false;
this.fast=false;
//初始化游戏界面
this.console_x=800;
this.console_y=800;
//初始化一节的大小
this.block_x=10;
this.block_y=10;
//启动计时器
timer = new Timer(100,this);
fastTimer= new Timer(50,this);
timer.start();
}
//带参构造函数
public Console(int console_x,int console_y,int block_x,int block_y,int time){
this.setFocusable(true);
this.addKeyListener(this); //添加键盘监听
//初始化蛇和食物
this.snake=new Snake(console_x,console_y,block_x,block_y);
this.food=new Food(console_x,console_y,block_x,block_y);
//蛇和食物不重叠
while(snake.isInSnake(food.getPos())){
food.genFood();
}
System.out.println("food pos:"+food.getPos().x+","+food.getPos().y);
//初始化游戏状态
this.start=false;
this.fail=false;
this.stop=false;
this.fast=false;
//初始化游戏界面
this.console_x=console_x;
this.console_y=console_y;
//初始化一节的大小
this.block_x=block_x;
this.block_y=block_y;
//启动计时器
timer = new Timer(time,this);
fastTimer= new Timer(time/2,this);
timer.start();
}
private void init(){
snake.init();
food.genFood();
this.score=0;
//蛇和食物不重叠
while(snake.isInSnake(food.getPos())){
food.genFood();
}
//初始化游戏状态
this.start=false;
this.stop=false;
this.fast=false;
//启动计时器
timer.start();
}
//绘图函数
public void paint(Graphics g) {
if(iBuffer==null)
{
iBuffer=createImage(this.getSize().width,this.getSize().height);
gBuffer=iBuffer.getGraphics();
}
super.paint(gBuffer);
this.setBackground(Color.white);//设置背景板为白色
//绘制游戏区域
gBuffer.setColor(Color.GRAY);
gBuffer.fillRect(0,0,this.console_x+block_x,this.console_y+block_y);
//获取蛇的位置
ArrayList<Pos> snakePos=snake.getSnakePos();
int len=snake.getLength();
//获取当前蛇头方向
Dir headDir=snake.getDir();
//获取食物位置
Pos foodPos=food.getPos();
//画贪吃蛇头部
switch (headDir){
case UP :
Source.up.paintIcon(this,gBuffer,snakePos.get(0).x+block_x,snakePos.get(0).y+block_y);
break;
case DOWN :
Source.down.paintIcon(this,gBuffer,snakePos.get(0).x+block_x,snakePos.get(0).y+block_y);
break;
case LEFT :
Source.left.paintIcon(this,gBuffer,snakePos.get(0).x+block_x,snakePos.get(0).y+block_y);
break;
case RIGHT :
Source.right.paintIcon(this,gBuffer,snakePos.get(0).x+block_x,snakePos.get(0).y+block_y);
break;
default:
System.out.println("error head dir");
break;
}
//画贪吃蛇身体
for(int i=1;i<len;i++){
Source.body.paintIcon(this,gBuffer,snakePos.get(i).x+block_x,snakePos.get(i).y+block_y);
}
//画食物
Source.food.paintIcon(this,gBuffer,foodPos.x+block_x,foodPos.y+block_y);
if(fail){//失败绘制失败提示
gBuffer.setColor(Color.GREEN);
gBuffer.setFont(new Font("幼圆",Font.BOLD,50));
gBuffer.drawString("游戏失败",this.console_x/2-100,this.console_y/2-25);
gBuffer.drawString("得分:"+score,this.console_x/2-100,this.console_y/2+25);
}
if(!start){//游戏未开始
gBuffer.setColor(Color.GREEN);
gBuffer.setFont(new Font("幼圆",Font.BOLD,20));
gBuffer.drawString("空格键开始/暂停游戏",(this.console_x-200)/2,(this.console_y-40)/2);
gBuffer.drawString("space to start/stop:",(this.console_x-200)/2,(this.console_y-40)/2+20);
}
if(stop){//游戏暂停
gBuffer.setColor(Color.GREEN);
gBuffer.setFont(new Font("幼圆",Font.BOLD,20));
gBuffer.drawString("空格键继续游戏",(this.console_x-140)/2,(this.console_y-40)/2);
gBuffer.drawString("space to continue",(this.console_x-200)/2,(this.console_y-40)/2+20);
}
g.drawImage(iBuffer, 0, 0, this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(!start || fail || stop){//不需要更新游戏界面
if(stop)
System.out.println("stoped");
repaint();//调用绘图是因为需要绘制失败界面
if(fast){//这里是保证下一次有计时器能触发该函数
fastTimer.start();
}
else{
timer.start();
}
return;
}
//蛇移动
if(!snake.Move()){//蛇撞到自己
fail=true;//游戏失败
if(fast){
fastTimer.start();
}
else{
timer.start();
}
return;//结束函数
}
//蛇吃到食物
if(Pos.isEqual(food.getPos(),snake.getPos())) {
//移动
if(!snake.Eat()) {
fail=true;//游戏失败
if(fast){
fastTimer.start();
}
else{
timer.start();
}
return; //结束函数
}
//生成新的食物
food.genFood();
//蛇和食物不重叠
while(snake.isInSnake(food.getPos())){
food.genFood();
}
System.out.println("food pos:"+food.getPos().x+","+food.getPos().y);
评论0