100分求助,按完OneMoreTime钮按后,再用键盘改变方向就不好使了

wangdong20 2012-01-29 09:05:54
RT

package 贪食蛇;
import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.*;

public class TestSnake extends JFrame
implements ActionListener{
private JButton jbtOneMoreTime=new JButton("One more time");
private JButton jbtExit=new JButton("Exit");
TableLabel tl=new TableLabel();

public TestSnake(){

JPanel panel=new JPanel();
panel.setLocation(new Point(220,150));

panel.setLayout(new GridLayout(2,1,5,5));
panel.add(jbtOneMoreTime);
panel.add(jbtExit);

add(tl,BorderLayout.CENTER);
add(panel,BorderLayout.EAST);


//Register listener with buttons
jbtOneMoreTime.addActionListener(this);
jbtExit.addActionListener(this);
}



public void actionPerformed(ActionEvent e){
if(e.getSource()==jbtOneMoreTime){
Snakes s=tl.getSnakes();
s.setFocusable(true);
jbtOneMoreTime.setFocusable(false);

s.initial();
}

else if(e.getSource()==jbtExit)
System.exit(0);
}

public static void main(String[] args) {
// TODO 自动生成方法存根
TestSnake frame=new TestSnake();

frame.setTitle("Snakes1");
frame.setSize(360,260);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

}




Snakes类

package 贪食蛇;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

import javax.swing.*;

public class Snakes extends JPanel{
/**蛇的头*/
private Point head=new Point(50,50);
/**蛇的身体*/
private ArrayList<Point> body=new ArrayList<Point>();
/**蛇的方向*/
private int oldDirection=RIGHT;
private int newDirection=LEFT;
/**蛇的生命状况*/
boolean live=true;
/**方向上*/
public static final int UP=1;
/**方向下*/
public static final int DOWN=-1;
/**方向左*/
public static final int LEFT=2;
/**方向右*/
public static final int RIGHT=-2;
/**分数*/
private int score=0;
/**蛇的运动速度*/
private int speed;

public Snakes(){
head=new Point(50,50);
body=new ArrayList<Point>();
oldDirection=RIGHT;
newDirection=LEFT;
live=true;
score=0;
//body加上蛇的头部
body.add(head);
//body加上蛇的其他部分
for(int i=40;i>=20;i-=10){
body.add(new Point(i,50));
}

addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_UP: newDirection=UP; break;
case KeyEvent.VK_DOWN: newDirection=DOWN; break;
case KeyEvent.VK_LEFT: newDirection=LEFT; break;
case KeyEvent.VK_RIGHT: newDirection=RIGHT;break;
default: break;
}
}
});
}

/**蛇的运动*/
public void move(){
if(live){
for(int i=body.size()-1;i>=1;i--){
bodyMove(body.get(i-1),body.get(i));
}
switch(oldDirection){
case UP: head.translate(0, -10); break;
case DOWN: head.translate(0, 10); break;
case LEFT: head.translate(-10, 0); break;
case RIGHT: head.translate(10, 0); break;
default: break;
}
}
}

/**改变蛇的运动方向*/
public void changeDirection(){
if(live){
if(oldDirection+newDirection!=0){
oldDirection=newDirection;
}
}
}

/**蛇有没有吃到苹果*/
public boolean isEatApple(Apple a){
if(head.equals(a.getPoint())){
score+=10;
return true;
}
else
return false;
}

/**获取蛇头*/
public Point getHead(){
return head;
}

/**获取newDIrection*/
public int getNewDirection(){
return newDirection;
}

/**比较两个Point的相对位置,并将后一个Point移动到前一个Point的位置*/
public void bodyMove(Point previous,Point current){
if(current.getX()!=previous.getX()){
int temp=(int) (previous.getX()-current.getX());
current.translate(temp, 0);
}

if(current.getY()!=previous.getY()){
int temp=(int) (previous.getY()-current.getY());
current.translate(0,temp);
}
}

/**获取body*/
public ArrayList<Point> getBody(){
return body;
}

/**获取蛇的尾巴*/
public Point getTail(){
return body.get(body.size()-1);
}

/**检查有没有吃到自己*/
public boolean isEatItself(){
for(int i=1;i<body.size()-1;i++){
if(head.equals(body.get(i)))
return true;
}
return false;
}

/**判断蛇有没有撞墙*/
public boolean isBeyondBound(){
if(head.x>=220||head.y>=220||
head.x<0||head.y<0)
return true;
else
return false;
}

public int getScore(){
return score;
}

public void initial(){
head=new Point(50,50);
body=new ArrayList<Point>();
oldDirection=RIGHT;
newDirection=LEFT;
live=true;
score=0;
//body加上蛇的头部
body.add(head);
//body加上蛇的其他部分
for(int i=40;i>=20;i-=10){
body.add(new Point(i,50));
}

this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
switch(e.getKeyCode()){
case KeyEvent.VK_UP: newDirection=UP; break;
case KeyEvent.VK_DOWN: newDirection=DOWN; break;
case KeyEvent.VK_LEFT: newDirection=LEFT; break;
case KeyEvent.VK_RIGHT: newDirection=RIGHT;break;
default: break;
}
}
});
}

}

...全文
365 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangdong20 2012-01-29
  • 打赏
  • 举报
回复
[Quote=引用楼主 wangdong20 的回复:]
RT

Java code


package 贪食蛇;
import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.*;

publ……
[/Quote]
果然是这样,高手就是高手啊
MiceRice 2012-01-29
  • 打赏
  • 举报
回复
焦点丢失,你需要在jbtOneMoreTime事件处理代码段的最后一句话,让你的蛇Snakes重新得到focus,也就是在: s.initial(); 和 } 之间,增加s.requestFocus();


62,633

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧