帮我看一下这段代码?

kgarnett210 2011-12-09 04:28:50
package Game;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.*;
import java.awt.ActiveEvent.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.*;
import java.util.List.*;

public class snakeWin extends JPanel implements ActionListener,KeyListener,Runnable {
JButton startGame;
JButton endGame;
int score;
int speed;
boolean start=false;
Random r=new Random();
int rx;
int ry;
ArrayList<snakeMove> list=new ArrayList<snakeMove>();
int temp;

public snakeWin() {
startGame=new JButton("开始");
endGame=new JButton("结束");
setLayout(new FlowLayout(FlowLayout.LEFT));
add(startGame);
add(endGame);
startGame.addActionListener(this);
endGame.addActionListener(this);
this.addKeyListener(this);
}

public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(10,40,400,300);
g.drawString("分数"+score,140,17);
g.drawString("速度"+speed,140,35);
if(start){
g.setColor(Color.white);
g.fillRect(10+rx*10,40+ry*10,10,10);
g.setColor(Color.blue);
for(int i=0;i<list.size();i++){
g.fillRect(10+list.get(i).getX(),40+list.get(i).getY(),10,10);
}
}
}

public void actionPerformed(ActionEvent e) {
if(e.getSource()==startGame){
startGame.setEnabled(false);
start=true;
rx=r.nextInt(40);
ry=r.nextInt(30);
snakeMove tempMove=new snakeMove();
tempMove.setX(20);
tempMove.setY(15);
list.add(tempMove);
requestFocus(true);
Thread thread=new Thread(this);
thread.start();
repaint();
}
if(e.getSource()==endGame){
System.exit(0);
}
}

public void move(int x,int y){
if(partjudge(x,y)){
otherMove();
list.get(0).setX(list.get(0).getX()+x);
list.get(0).setY(list.get(0).getY()+y);
eat();
repaint();
}
else{

}
}

private void otherMove(){
snakeMove tempMove=new snakeMove();
for (int i = 0; i < list.size(); i++) {
if(i==1){
list.get(i).setX(list.get(0).getX());
list.get(i).setY(list.get(0).getY());
}
else if(i>1){
tempMove=list.get(i-1);
list.set(i-1,list.get(i));
list.set(i,tempMove);
}
}
}

public boolean partjudge(int x,int y){
if(!(globaljudge(list.get(0).getX()+x,list.get(0).getY()+y))){
return false;
}
return true;
}

public boolean globaljudge(int x,int y){
if(x<0||x>=400||y<0||y>=300){
return false;
}
return true;
}

private void eat(){
if(rx==list.get(0).getX()&&ry==list.get(0).getY()){
rx = r.nextInt(40);
ry = r.nextInt(30);
snakeMove tempMove=new snakeMove();
tempMove.setX(list.get(list.size()-1).getX());
tempMove.setY(list.get(list.size()-1).getY());
list.add(tempMove);
}
else{

}
}

public void keyPressed(KeyEvent e) {
if(start){
switch (e.getKeyCode()) {
case(KeyEvent.VK_UP):
move(0,-1);
temp=1;
break;
case(KeyEvent.VK_DOWN):
move(0,1);
temp=2;
break;
case(KeyEvent.VK_LEFT):
move(-1,0);
temp=3;
break;
case(KeyEvent.VK_RIGHT):
move(1,0);
temp=4;
break;
default:
break;
}
}
}

@Override
public void keyReleased(KeyEvent e) {

}
@Override
public void keyTyped(KeyEvent e) {

}
@Override
public void run() {
while(start){
switch(temp){
case 1:
move(0,-1);
break;
case 2:
move(0,1);
break;
case 3:
move(-1,0);
break;
case 4:
move(1,0);
break;
default:
move(0,1);
break;
}
repaint();
try{
Thread.sleep(500);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}


}
...全文
28 点赞 收藏 2
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
kgarnett210 2011-12-09
老是吃不到食物啊,蛇直接穿过食物,不生成新的食物,蛇身也不加长
回复
strivehard 2011-12-09

try{
Thread.sleep(500);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}

代码怎么了
回复
相关推荐
发帖
非技术区
创建于2007-09-28

2.3w+

社区成员

Java 非技术区
申请成为版主
帖子事件
创建了帖子
2011-12-09 04:28
社区公告
暂无公告