为什么这个蛇有的时候会一下走两格?一卡一卡的 找了好久都没有找出错误....才这么点程序不会是电脑卡造成的吧,求大神帮忙看看
[code=
public class GameTest {
public static void main(String[] args) {
Yard y = new Yard(new Snake());
y.gameBegin();
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Yard extends Frame{
private Snake snake;
//private Egg egg;
//private Wall wall;
public Yard(Snake snake){
super("贪食蛇");
this.snake = snake;
//this.egg = egg;
//this.wall = wall;
this.setBounds(0, 0, util.COLS * util.SIZE, util.ROWS * util.SIZE);
this.setBackground(Color.GRAY);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
this.addKeyListener(new KeyMoniter());
}
private Image ibuffer;
private Graphics gbuffer;
public void update(Graphics g) {
if(ibuffer == null){
ibuffer = this.createImage(this.getSize().width,this.getSize().width);
gbuffer = ibuffer.getGraphics();
}
gbuffer.setColor(getBackground());
gbuffer.fillRect(0, 0, this.getSize().width, this.getSize().height);
paint(gbuffer);
g.drawImage(ibuffer, 0, 0, null);
}
Font font = new Font("宋体", Font.BOLD, 20);
public void paint(Graphics g) {
System.out.println("画出游戏界面和格子不包括窗口");
g.setColor(Color.BLACK);
//画出横线
for(int i = 0; i < util.ROWS; i++){
g.drawLine(0, util.SIZE * i, util.SIZE * util.COLS, util.SIZE * i);
}
//画出竖线
for(int i = 0; i < util.COLS; i++){
g.drawLine(util.SIZE * i, 0, util.SIZE * i, util.SIZE * util.ROWS);
}
g.setFont(font);
g.setColor(Color.red);
g.drawString("score: ", 1 * util.SIZE, 3 * util.SIZE);
g.drawString(util.getScore(), 6 * util.SIZE, 3 * util.SIZE);
//wall.drawMe();
snake.drawMe(g);
//egg.drawMe();
}
private class snakeThread implements Runnable{//蛇不断移动
public void run() {
while(true){
repaint();
snake.move();
try {
Thread.sleep(util.TEMPO);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void start(){
new Thread(new snakeThread()).start();
}
public void gameBegin() {
System.out.println("开始游戏");
start();
}
public class KeyMoniter extends KeyAdapter{
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_UP :
snake.changeDir(util.UP);
break;
case KeyEvent.VK_DOWN :
snake.changeDir(util.DOWN);
break;
case KeyEvent.VK_LEFT :
snake.changeDir(util.LEFT);
break;
case KeyEvent.VK_RIGHT :
snake.changeDir(util.RIGHT);
break;
}
}
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.util.LinkedList;
public class Snake {
private int dir ;//蛇方向
private Yard yard;
public LinkedList<body> bodys = new LinkedList<body>();
public Snake(){
init();
}
private void init(){//初始化方法
for(int i = 0; i < util.INIT; i++){
bodys.addLast(new body(util.C_ROW,util.C_COL+i));
}
dir = util.LEFT;
}
public void move(){
System.out.println("蛇移动");
//去尾
bodys.removeLast();
//加头
body b = bodys.getFirst();
int row = b.ROW;
int col = b.COL;
switch(this.dir){
case util.UP:
if(row < 2){
row = util.ROWS-2;
}else{
row--;
}
break;
case util.DOWN:
if(row > util.ROWS-2){
row = 2;
}else{
row++;
}
break;
case util.LEFT:
if(col < 2){
col = util.COLS-2;
}else{
col--;
}
break;
case util.RIGHT:
if(col > util.COLS-2){
col = 2;
}else{
col++;
}
break;
}
bodys.addFirst(new body(row, col));
}
public void changeMove(){
System.out.println("改变蛇移动速度");
}
public void changeDir(int dir){
System.out.println("蛇改变方向");
this.dir = dir;
}
public void eatEgg(){
System.out.println("蛇吃蛋");
}
public boolean isEatBody(){
System.out.println("判断蛇是否吃到身体");
return false;
}
public void drawMe(Graphics g){
System.out.println("画出蛇");
g.setColor(Color.BLUE);
for(body b: bodys){
g.fill3DRect(b.COL * util.SIZE, b.ROW * util.SIZE, util.SIZE, util.SIZE, true);
}
}
public void die(){
System.out.println("蛇死了");
}
private static class body{//蛇身体类
private int ROW ;
private int COL ;
public body(int rOW, int cOL) {
ROW = rOW;
COL = cOL;
}
public int getROW() {
return ROW;
}
public void setROW(int rOW) {
ROW = rOW;
}
public int getCOL() {
return COL;
}
public void setCOL(int cOL) {
COL = cOL;
}
}
}
//参数类
public class util {
//游戏界面参数
public static final int ROWS = 30;//行
public static final int COLS = 30;//列
public static final int SIZE = 15;
//蛇参数
public static final int INIT = 10; //初始时蛇身长度
//蛇移动速度参数
public static final int TEMPO = 1000;
public static final int SLOW = 1000;
public static final int CENTER = 600;
public static final int FAST = 300;
public static final int MIN = 100;
//蛇初始位置
public static final int C_COL = COLS/2;
public static final int C_ROW = ROWS/2;
//方向参数
public static final int UP = -1;
public static final int DOWN = 1;
public static final int LEFT = -2;
public static final int RIGHT = 2;
//分数
public static Integer score = 0;
public void setScore(int score){
this.score = score;
}
public static String getScore() {
// TODO Auto-generated method stub
return score.toString();
}
}
[/code]