JAVA 开发《贪吃蛇》03-绘制画板,实现键盘监听
面板绘制,实现键盘监听
package com.snake.windows;
import com.snake.model.DieTitle;
import com.snake.model.Food;
import com.snake.model.ScoreSystem;
import com.snake.model.Snake;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import static com.snake.constant.Constant.*;
public class MyPanel extends JPanel implements KeyListener {
Snake snake = new Snake();
Food food = new Food();
DieTitle dieTitle = new DieTitle();
ScoreSystem score = new ScoreSystem();
public MyPanel(){
this.setFocusable(true);
this.addKeyListener(this);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(new Color(0xC3BFC1C1, true));
g.setColor(new Color(0xEAEAEA));
g.fillRect(0,SIZE*4,SIZE*MY_WIDTH,SIZE*(MY_HEIGHT-4));
g.setColor(Color.GRAY);
g.setFont(new Font("微软雅黑", Font.BOLD, 45));
g.drawString("贪吃蛇小游戏", 10, 70);
g.setColor(Color.GRAY);
g.setFont(new Font("微软雅黑", Font.ITALIC, 20));
g.drawString("—By晓龙", 280, 70);
g.setColor(new Color(0xCBCBCD));
for (int i = 0; i <MY_HEIGHT-4 ; i++) {
g.drawLine(0,SIZE*4+i*SIZE,SIZE*MY_WIDTH,SIZE*4+i*SIZE);
}
for (int i = 0; i < MY_WIDTH; i++) {
g.drawLine(i*SIZE,SIZE*4,i*SIZE,SIZE*MY_HEIGHT);
}
g.setColor(new Color(0x0CA3D0));
g.fillOval(food.getFoodX(),food.getFoodY(),SIZE,SIZE);
for (int i = 1; i < snake.getLenght(); i++) { g.drawImage(snake.getBodyIm(),snake.getSnakeX(i),snake.getSnakeY(i),null);
}
g.drawImage(snake.getHeadImR(), snake.getSnakeX(0), snake.getSnakeY(0), null);
if(snake.isStart()==false){
g.setColor(new Color(1));
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("按下空格 开始游戏",SIZE*8,SIZE*MY_HEIGHT/2);
}
if (snake.isFail() == true) {
g.setColor(new Color(0xFF0000));
g.setFont(new Font("微软雅黑", Font.BOLD, 35));
g.drawImage(dieTitle.getDieImage(),dieTitle.getDieTitleImX(),dieTitle.getDieTitleImY(),null);
g.drawString("最终得分:"+score.getScoreNum(),dieTitle.getDieTitleImX()+SIZE*2,dieTitle.getDieTitleImY()-SIZE*2);
g.drawString("最终长度:"+score.getLengthNum(),dieTitle.getDieTitleImX()+SIZE*2,dieTitle.getDieTitleImY()-SIZE*2+45);
}
g.setColor(new Color(0x000000));
g.setFont(new Font("微软雅黑", Font.ITALIC, 20));
g.drawString(score.getScoreName()+score.getScoreNum(),score.getX(),score.getY());
g.drawString(score.getLengthName()+score.getLengthNum(),score.getX(),score.getY()+30);
}
public void snakeRun(){
while (true){
snake.snakeMove();
eat();
die();
try {
Thread.sleep(240);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
private void eat(){
if (snake.getSnakeX(0) == food.getFoodX() && snake.getSnakeY(0) ==food.getFoodY()){
snake.setLenght(snake.getLenght()+1);
food.foodInit();
score.scoreAugment();
}
}
private void die(){
for (int i = snake.getLenght()-1; i >0 ; i--) {
if (snake.getSnakeX(0)==snake.getSnakeX(i) && snake.getSnakeY(0)==snake.getSnakeY(i)){
snake.setFail(true);
}
}
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_SPACE){
if(snake.isFail() == true){
snake.snakeInit();
food.foodInit();
score.scoreInit();
snake.setFail(false);
}else{
snake.setStart(!snake.isStart());
}
}
if(snake.getFx() != 'D'){
if(keyCode == KeyEvent.VK_W || keyCode == KeyEvent.VK_UP){
snake.setFx('U');
snake.setFxNum(0);
}
}
if(snake.getFx() != 'U') {
if (keyCode == KeyEvent.VK_S || keyCode == KeyEvent.VK_DOWN) {
snake.setFx('D');
snake.setFxNum(1);
}
}
if(snake.getFx() != 'R') {
if (keyCode == KeyEvent.VK_A || keyCode == KeyEvent.VK_LEFT) {
snake.setFx('L');
snake.setFxNum(2);
}
}
if(snake.getFx() != 'L') {
if (keyCode == KeyEvent.VK_D || keyCode == KeyEvent.VK_RIGHT) {
snake.setFx('R');
snake.setFxNum(3);
}
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}