标题:请教键盘事件问题
取消只看楼主
lw8484654
Rank: 1
等 级:新手上路
帖 子:223
专家分:0
注 册:2005-12-1
 问题点数:0 回复次数:4 
请教键盘事件问题

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);
}
};
在这里我点了向下的键,为什么整个蛇都往下跑了啊?

搜索更多相关主题的帖子: int import 键盘 java ArrayList 
2006-06-23 10:58
lw8484654
Rank: 1
等 级:新手上路
帖 子:223
专家分:0
注 册:2005-12-1
得分:0 

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

2006-06-23 12:08
lw8484654
Rank: 1
等 级:新手上路
帖 子:223
专家分:0
注 册:2005-12-1
得分:0 

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);
}
};
我又重新改了一下,可以用鼠标控制,不过我看的很别扭.倒是很象积木样的.
请指教一下!!!

2006-06-23 18:59
lw8484654
Rank: 1
等 级:新手上路
帖 子:223
专家分:0
注 册:2005-12-1
得分:0 
不好意思,说错了.是用键盘控制.但是往上和往下的时候,处理的不好.......
请高手指教
2006-06-23 19:12
lw8484654
Rank: 1
等 级:新手上路
帖 子:223
专家分:0
注 册:2005-12-1
得分:0 

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);
}
};
重新改了一下.终于可以用键盘顺利地控制那条蛇了,不过我怎么越看越别扭啊!总感觉少了点什么!!!!
请各位老兄指教下哈!!!!!!

2006-06-24 19:18



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-74244-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.553008 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved