java键盘事件代码_请教键盘事件问题

请教键盘事件问题

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class Snake extends JFrame

{

int x,y;

int X,Y;

int loc;

int i=20;

static int pointX=20;

static int pointY=20;

ArrayList list;

ArrayList array;

public Snake()

{

Container c=getContentPane();

list=new ArrayList();

array=new ArrayList();

setSize(800,600);

x=300;y=200;

X=20;Y=30;

setLocation(x,y);

show();

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

public void drawSnake()

{

addKeyListener(new KeyAdapter()

{

public void keyPressed(KeyEvent e)

{

loc=e.getKeyCode();

}

});

while(true){

if(X+i<750)

{

if(loc==40){list.clear();list.add(new MySnake(X+i,y));}

else

{

list.clear();

list.add(new MySnake(X+i,Y));

i=i+20;

}

try

{

Thread.sleep(300);

repaint();

}

catch (Exception e)

{

return;

}

}

else

{

JOptionPane.showMessageDialog(this,"you arg lose","error message",1);

System.exit(0);

}

}

}

public void paint(Graphics g)

{

super.paint(g);

Iterator it=list.iterator();

while(it.hasNext())

{

MySnake snake=(MySnake)it.next();

snake.drawMe(g);

}

}

public static void main(String args[])

{

Snake s=new Snake();

s.drawSnake();

}

}

class MySnake

{

int X,Y;

static int pointX=22;

static int pointY=44;

int j=0;

public MySnake(int X,int Y)

{

this.X=X;this.Y=Y;

}

public void drawMe(Graphics g)

{

g.setColor(Color.red);

g.fillRect(X,Y+pointY*j,20,20);

g.fillRect(X+pointX,Y+pointY*j,20,20);

g.fillRect(X+pointY,Y+pointY*j,20,20);

}

};

在这里我点了向下的键,为什么整个蛇都往下跑了啊?----------------解决方案--------------------------------------------------------

你这个代码写的风格一点都不好

你一些常量,你不应该直接写他们的值写出来

还有,起一个方法不应该在此方法里面一个无限循环

这样不好,最好的方法 是起一个内部线程容易掌握一点

并且也容易改一点

----------------解决方案--------------------------------------------------------

代码的风格和常量等都可以改,我最想知道的:如何用键盘控制蛇的方向呢?怎么把蛇画出来呢?

----------------解决方案--------------------------------------------------------

注册键盘事件啊

然后根据不同的按键生成不同的方向

你可以定义四个方向的常量值

然后针对不同的值做不同的操作

你定义一个类叫Snake

它面里有以下的方法

move(int dir);//实现其向哪个方法走

draw(Graphics g)//实现把它自己画出来

这样就是很好了吗

----------------解决方案--------------------------------------------------------

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class Snake extends JFrame implements Runnable

{

int x,y;

int X,Y;

int loc;

int i=20;

int j=0;

static int pointX=20;

static int pointY=20;

ArrayList list;

ArrayList array;

Thread th;

public Snake()

{

Container c=getContentPane();

list=new ArrayList();

array=new ArrayList();

setSize(800,600);

x=300;y=200;

X=20;Y=30;

setLocation(x,y);

show();

th=new Thread(this);

th.start();

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

public void run()

{

addKeyListener(new KeyAdapter()

{

public void keyPressed(KeyEvent e)

{

loc=e.getKeyCode();

}

});

while(true){

list.clear();

list.add(new MySnake(X+i,Y+j));

if(loc==39)i=i+20;

if(loc==37)i=i-20;

if(loc==40)j=j+20;

if(loc==38)j=j-20;

try

{

Thread.sleep(300);

repaint();

}

catch (Exception e)

{

return;

}

if(X+i>740||X+i<0||Y+j>550||Y+j<0)

{

JOptionPane.showMessageDialog(this,"you arg lose","error message",1);

System.exit(0);

}

}

}

public void paint(Graphics g)

{

super.paint(g);

Iterator it=list.iterator();

while(it.hasNext())

{

MySnake snake=(MySnake)it.next();

snake.drawMe(g);

}

}

public static void main(String args[])

{

new Snake();

}

}

class MySnake

{

int X,Y;

public MySnake(int X,int Y)

{

this.X=X;this.Y=Y;

}

public void drawMe(Graphics g)

{

g.setColor(Color.red);

g.fillRect(X,Y,20,20);

g.fillRect(X+22,Y,20,20);

g.fillRect(X+44,Y,20,20);

}

};

我又重新改了一下,可以用鼠标控制,不过我看的很别扭.倒是很象积木样的.

请指教一下!!!

----------------解决方案--------------------------------------------------------

不好意思,说错了.是用键盘控制.但是往上和往下的时候,处理的不好.......

请高手指教

----------------解决方案--------------------------------------------------------

不可以拐弯 遗憾那!!

----------------解决方案--------------------------------------------------------

你在移动的时候是一起画的方块,上下移动当然一起动了.

应该改一下移动的算法

----------------解决方案--------------------------------------------------------

我不是说了吗?

你要在snack类是再写一个move的方法,该方法接受一个参数,此参数代表四个方向

然后你针对不同的方向,在move里把自己的座标改一下,然后再调用自己的draw方法

这样不就很容易实现了吗?

----------------解决方案--------------------------------------------------------

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.*;

public class Snake extends JFrame implements Runnable

{

boolean bool=false;

int x,y;

int X,Y;

int loc;

int i=20;

int j=0;

static int pointX=20;

static int pointY=20;

ArrayList list;

ArrayList array;

Thread th;

public Snake()

{

Container c=getContentPane();

list=new ArrayList();

array=new ArrayList();

setSize(800,600);

x=300;y=200;

X=20;Y=30;

setLocation(x,y);

show();

th=new Thread(this);

th.start();

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit(0);

}

});

}

public void run()

{

addKeyListener(new KeyAdapter()

{

public void keyPressed(KeyEvent e)

{

loc=e.getKeyCode();

}

});

while(true){

list.clear();

list.add(new MySnake(X+i,Y+j));

if(loc==39){i=i+20;bool=false;}

if(loc==37){i=i-20;;bool=false;}

if(loc==40){j=j+20;;bool=true;}

if(loc==38){j=j-20;;bool=true;}

try

{

Thread.sleep(300);

repaint();

}

catch (Exception e)

{

return;

}

if(X+i>740||X+i<0||Y+j>550||Y+j<0)

{

JOptionPane.showMessageDialog(this,"you arg lose","error message",1);

System.exit(0);

}

}

}

public void paint(Graphics g)

{

super.paint(g);

Iterator it=list.iterator();

while(it.hasNext())

{

MySnake snake=(MySnake)it.next();

if(bool==false)

snake.drawMe(g);

if(bool==true)

snake.drawHe(g);

}

}

public static void main(String args[])

{

new Snake();

}

}

class MySnake

{

int X,Y;

public MySnake(int X,int Y)

{

this.X=X;this.Y=Y;

}

public void drawMe(Graphics g)

{

g.setColor(Color.red);

g.fillRect(X,Y,20,20);

g.fillRect(X+22,Y,20,20);

g.fillRect(X+44,Y,20,20);

}

public void drawHe(Graphics g)

{

g.setColor(Color.red);

g.fillRect(X,Y,20,20);

g.fillRect(X,Y+22,20,20);

g.fillRect(X,Y+44,20,20);

}

};

重新改了一下.终于可以用键盘顺利地控制那条蛇了,不过我怎么越看越别扭啊!总感觉少了点什么!!!!

请各位老兄指教下哈!!!!!!

----------------解决方案--------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值