贪吃蛇
运行之后蛇块无法与食物块重叠导致无法吃食物……为什么??
package greedsnake;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class SnakeWindow extends JFrame {
public SnakeWindow(){
setTitle("贪吃蛇");
setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(450, 440);
this.setVisible(true);
mainPanel main=new mainPanel();
this.getContentPane().add(BorderLayout.CENTER,main);
}
public static void main(String[] args){
new SnakeWindow();
}
}
package greedsnake;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class mainPanel extends JPanel{
JButton start,stop;
int fenshu=0;
boolean starto=false;
Random r=new Random();
int sx=0,sy=0;
int temp=1;
JDialog dialog=new JDialog();
JLabel label=new JLabel("game over!");
JButton button=new JButton("OK");
Thread thread;
ArrayList<SnakeAct> list=new ArrayList<SnakeAct>();
public mainPanel() {
this.setLayout(new FlowLayout(FlowLayout.LEFT));
dialog.setLayout(new GridLayout(2,1));
dialog.add(label);
dialog.add(button);
dialog.setVisible(false);
button.addActionListener(new buttonListener());
start=new JButton("开始");
this.add(start);
start.addActionListener(new StartActionListener());
stop=new JButton("退出");
this.add(stop);
stop.addActionListener(new StopActionListener());
this.addKeyListener(new MyKeyListener());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(10, 40, 400, 350);
g.drawString("分数:"+fenshu, 200, 25);
if(starto){
g.setColor(Color.BLUE);
g.fillRect(10+sx, 40+sy, 10, 10);
for(int i=0;i<list.size();i++){
g.setColor(Color.RED);
g.fillRect(10+list.get(i).getX()*10, 40+list.get(i).getY()*10, 10, 10);
}
}
}
public void move(int x,int y){
otherMove();
if(minOk(x, y)&&maxOk(x, y)){
list.get(0).setY(list.get(0).getY()+y);
list.get(0).setX(list.get(0).getX()+x);
eat();
repaint();
}else{ //死亡方法
dialog.setVisible(true);
dialog.setSize(200, 100);
dialog.setLocation(200, 200);
thread=null;
}
}
public void eat(){
if((sx==list.get(0).getX())&&(sy==list.get(0).getY())){
fenshu+=10;
sx=r.nextInt(390);
sy=r.nextInt(340);
SnakeAct temp=new SnakeAct();
temp.setX(list.get(list.size()-1).getX());
temp.setY(list.get(list.size()-1).getY());
list.add(temp);
}
}
public void otherMove(){
SnakeAct temp=new SnakeAct();
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){
temp=list.get(i-1);
list.set(i-1, list.get(i));
list.set(i, temp);
}
}
}
public boolean minOk(int x,int y){
if((list.get(0).getX()+x<0)||(list.get(0).getY()+y<0)){
return false;
}
for(int i=0;i<list.size();i++){
if(i>0&&list.get(0).getX()==list.get(i).getX()&&list.get(0).getY()==list.get(i).getY()){
return false;
}
}
return true;
}
public boolean maxOk(int x,int y){
if((list.get(0).getX()+x>=40)||(list.get(0).getY()+y>=35)){
return false;
}else{
return true;
}
}
public class StartActionListener implements ActionListener,Runnable{
public void actionPerformed(ActionEvent e) {
starto=true;
start.setEnabled(false);
sx=r.nextInt(390);
sy=r.nextInt(340);
SnakeAct act=new SnakeAct();
act.setX(30);
act.setY(15);
list.add(act);
requestFocus();
thread=new Thread(this);
thread.start();
repaint();
}
public void run(){
while(starto){
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:
break;
}
repaint();
try{
Thread.sleep(400);
}catch(Exception e){
e.printStackTrace();
}
}
}
}
public class StopActionListener implements ActionListener{
public void actionPerformed(ActionEvent e){
System.exit(0);
}
}
public class buttonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
list.clear();
fenshu=0;
starto=false;
start.setEnabled(true);
dialog.setVisible(false);
repaint();
}
}
public class MyKeyListener implements KeyListener{
public void keyPressed(KeyEvent e){
if(starto){
int a=e.getKeyCode();
switch(a){
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 keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
}
package greedsnake;
public class SnakeAct {
private int x;
private int y;
public void setX(int ax){
this.x=ax;
}
public void setY(int ay){
this.y=ay;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
}
[解决办法]
- Java code
//看到代码后蛋痛了public class NewClass { public static void main(String[] args) { new SnakeWindow().setVisible(true); }}class SnakeWindow extends JFrame { public SnakeWindow() { setTitle("贪吃蛇"); setLocation(200, 200); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(450, 440); this.setVisible(true); mainPanel main = new mainPanel(); this.getContentPane().add(BorderLayout.CENTER, main); }}class mainPanel extends JPanel { JButton start, stop; int fenshu = 0; boolean starto = false; Random r = new Random(); int sx = 0, sy = 0; int temp = 1; JDialog dialog = new JDialog(); JLabel label = new JLabel("game over!"); JButton button = new JButton("OK"); Thread thread; ArrayList<SnakeAct> list = new ArrayList<SnakeAct>(); public mainPanel() { this.setLayout(new FlowLayout(FlowLayout.LEFT)); dialog.setLayout(new GridLayout(2, 1)); dialog.add(label); dialog.add(button); dialog.setVisible(false); button.addActionListener(new buttonListener()); start = new JButton("开始"); this.add(start); start.addActionListener(new StartActionListener()); stop = new JButton("退出"); this.add(stop); stop.addActionListener(new StopActionListener()); this.addKeyListener(new MyKeyListener()); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawRect(10, 40, 400, 350); g.drawString("分数:" + fenshu, 200, 25); if (starto) { g.setColor(Color.BLUE); //这里 [color=#FF0000]g.fillRect(10 + sx * 10, 40 + 10 * sy, 10, 10);[/color] for (int i = 0; i < list.size(); i++) { g.setColor(Color.RED); g.fillRect(10 + list.get(i).getX() * 10, 40 + list.get(i).getY() * 10, 10, 10); } } } public void move(int x, int y) { if (minOk(x, y) && maxOk(x, y)) { //先把要走的位置加上 [color=#FF0000]list.add(0, new SnakeAct(list.get(0).getX() + x, list.get(0).getY() + y));[/color] //如果要走的位置不是吃的点,则删除最后一个点,实现了前进 //否则则吃掉一个点 if (sx != list.get(0).getX() || sy != list.get(0).getY()) { [color=#FF0000]list.remove(list.size() - 1);[/color] } else { //生成的新位置不在蛇身上 [color=#FF0000]while (true) { sx = r.nextInt(40);//蛇走的位置为0-39,0-34 sy = r.nextInt(35);//蓝点的位置也应该一样 boolean flag = true; for (SnakeAct sa : list) { if (sa.getX() == sx && sa.getY() == sy) { flag = false; break; } } if (flag) { break; } }[/color] } repaint(); } else { //死亡方法 dialog.setVisible(true); dialog.setSize(200, 100); dialog.setLocation(200, 200); thread = null; } } public boolean minOk(int x, int y) { if ((list.get(0).getX() + x < 0) || (list.get(0).getY() + y < 0)) { return false; } for (int i = 0; i < list.size(); i++) { if (i > 0 && list.get(0).getX() == list.get(i).getX() && list.get(0).getY() == list.get(i).getY()) { return false; } } return true; } public boolean maxOk(int x, int y) { if ((list.get(0).getX() + x >= 40) || (list.get(0).getY() + y >= 35)) { return false; } else { return true; } } class StartActionListener implements ActionListener, Runnable { public void actionPerformed(ActionEvent e) { starto = true; start.setEnabled(false); [color=#FF0000]sx = r.nextInt(40); sy = r.nextInt(35);[/color] SnakeAct act = new SnakeAct(); act.setX(30); act.setY(15); list.add(act); requestFocus(); thread = new Thread(this); thread.start(); repaint(); } public void run() { while (starto) { 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: break; } repaint(); try { Thread.sleep(400); } catch (Exception e) { e.printStackTrace(); } } } } public class StopActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public class buttonListener implements ActionListener { public void actionPerformed(ActionEvent e) { list.clear(); fenshu = 0; starto = false; start.setEnabled(true); dialog.setVisible(false); repaint(); } } public class MyKeyListener implements KeyListener { public void keyPressed(KeyEvent e) { if (starto) { int a = e.getKeyCode(); switch (a) { 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 keyTyped(KeyEvent e) {// TODO Auto-generated method stub } @Override public void keyReleased(KeyEvent e) {// TODO Auto-generated method stub } }}class SnakeAct { private int x; private int y; public SnakeAct() { } public SnakeAct(int x, int y) { this.x = x; this.y = y; } public void setX(int ax) { this.x = ax; } public void setY(int ay) { this.y = ay; } public int getX() { return x; } public int getY() { return y; }}
