初学java 尝试写一个贪吃蛇小游戏,怎么让“蛇”不停的动起来 现在是按一个键动一下 30
importjava.awt.*;importjava.awt.event.KeyEvent;importjavax.swing.JOptionPane;publiccl...
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
public class Snake {
static int x,y;
static int X_SPEED=10,Y_SPEED=10,width=10,height=10;
enum Direction {UP,LEFT,DOWN,RIGHT,STOP};
Direction dir=Direction.DOWN;
boolean isEatSelf,isLive=true,L,U,D,R;
GameClient gc;
Snake(int x,int y,GameClient gc){
this.x=x;
this.y=y;
this.gc=gc;
}
void move(){
switch(dir){
case UP:
y-=Y_SPEED;
break;
case DOWN:
y+=Y_SPEED;
System.out.print("mark");
break;
case LEFT:
x-=X_SPEED;
break;
case RIGHT:
x+=X_SPEED;
break;
}
if(x<=0 || y<=0 || x>gc.FRAME_WIDTH || y>=gc.FRAME_HEIGHT ){
isLive=false;
JOptionPane.showMessageDialog(null, "GAME OVER!");
}
}
boolean eatFood(Food food){
if(this.getRec().intersects(food.getRec())) return true;
else return false;
}
Rectangle getRec() {
return new Rectangle(x,y,width,height);
}
void direction(){
if(L&&!U&&!R&&!D) dir=Direction.LEFT;
else if(U && !L && !R && ! D) dir=Direction.UP;
else if(R &&!U && !L && !D) dir=Direction.RIGHT;
else if(D &&!L && !U && !R) dir=Direction.DOWN;
move();
}
void keyPressed(KeyEvent e){
int key=e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT:
L=true;
D=false;
U=false;
R=false;
break;
case KeyEvent.VK_DOWN:
L=false;
D=true;
U=false;
R=false;
break;
case KeyEvent.VK_UP:
L=false;
D=false;
U=true;
R=false;
System.out.print("mark2");
break;
case KeyEvent.VK_RIGHT:
L=false;
D=false;
U=false;
R=true;
break;
}
direction();
}
void drawMe(Graphics g){
Color c=g.getColor();
g.setColor(Color.BLACK);
g.fillRect(x, y, width, height);
g.setColor(c);
//move();
}
class moveThread implements Runnable{
@Override
public void run() {
move();
System.out.print(D+"ttt");
try{
Thread.sleep(200);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
void start(){
System.out.print("tett");
new Thread(new moveThread()).start();
}
} 展开
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
public class Snake {
static int x,y;
static int X_SPEED=10,Y_SPEED=10,width=10,height=10;
enum Direction {UP,LEFT,DOWN,RIGHT,STOP};
Direction dir=Direction.DOWN;
boolean isEatSelf,isLive=true,L,U,D,R;
GameClient gc;
Snake(int x,int y,GameClient gc){
this.x=x;
this.y=y;
this.gc=gc;
}
void move(){
switch(dir){
case UP:
y-=Y_SPEED;
break;
case DOWN:
y+=Y_SPEED;
System.out.print("mark");
break;
case LEFT:
x-=X_SPEED;
break;
case RIGHT:
x+=X_SPEED;
break;
}
if(x<=0 || y<=0 || x>gc.FRAME_WIDTH || y>=gc.FRAME_HEIGHT ){
isLive=false;
JOptionPane.showMessageDialog(null, "GAME OVER!");
}
}
boolean eatFood(Food food){
if(this.getRec().intersects(food.getRec())) return true;
else return false;
}
Rectangle getRec() {
return new Rectangle(x,y,width,height);
}
void direction(){
if(L&&!U&&!R&&!D) dir=Direction.LEFT;
else if(U && !L && !R && ! D) dir=Direction.UP;
else if(R &&!U && !L && !D) dir=Direction.RIGHT;
else if(D &&!L && !U && !R) dir=Direction.DOWN;
move();
}
void keyPressed(KeyEvent e){
int key=e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT:
L=true;
D=false;
U=false;
R=false;
break;
case KeyEvent.VK_DOWN:
L=false;
D=true;
U=false;
R=false;
break;
case KeyEvent.VK_UP:
L=false;
D=false;
U=true;
R=false;
System.out.print("mark2");
break;
case KeyEvent.VK_RIGHT:
L=false;
D=false;
U=false;
R=true;
break;
}
direction();
}
void drawMe(Graphics g){
Color c=g.getColor();
g.setColor(Color.BLACK);
g.fillRect(x, y, width, height);
g.setColor(c);
//move();
}
class moveThread implements Runnable{
@Override
public void run() {
move();
System.out.print(D+"ttt");
try{
Thread.sleep(200);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
void start(){
System.out.print("tett");
new Thread(new moveThread()).start();
}
} 展开
展开全部
添加循环,跳出循环的条件设置为按了其他方向的按键或撞墙
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码没粘全啊,开头应该给出一个默认运动方向,线程里应该调用direction()函数。你看看这个吧:http://wenku.baidu.com/view/2093560c90c69ec3d5bb753d.html#39124-qzone-1-25161-f00cdc7f4857236c67481f200187bab5
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
while(true){
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询