贪吃蛇小游戏里面的蛇怎么能自动跑起来 刚学java,写贪吃蛇小游戏,只有输入方向键,才能走动,怎么能初始化的时候,蛇就能跑起来?求思路?游戏Java贪吃蛇 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 游戏不是fps的嘛,1、首先确定蛇头。2、每X fps,让蛇朝着蛇头的方向移动 (即不停的改变 x或 y坐标的值,重新画蛇)3、X值,随难度提高而越来越快,这样蛇也就跑的越来越快了。4、反正键盘控制的是蛇头。 用一个while循环,里面要有判断向左向右还是向下向上的条件语句,除非你触发换方向,否则蛇就会一直向某个方向移动。当然这个移动你应该知道怎么编吧(就是你按一下动一下的那个)。你还可以给个判断是否继续的参数给while循环,以便蛇出界时就结束游戏。(如果设置不给,设置为true,那也可以,游戏永不结束而已) 以前练习写的一个蛇的例子,没写完,当时是想用线程来画来着。import javax.swing.*;import java.awt.*;import java.util.*;class GameThread implements Runnable{ private GamePanle gamePanel; private final int fps = 40; public GameThread(final GamePanle gamePanel) { this.gamePanel = gamePanel; } public void run() { while (true) { try { gamePanel.logic(); gamePanel.draw(); Thread.sleep(1000/fps); } catch (Exception e) { e.printStackTrace(); } } }}class Snake{ //蛇头的开始位置 public int x = 200; public int y = 100; private int speed = 5; //1,2,3,4 表示4个方向 private byte direct = 1; //简单起见就3节 private ArrayList<Body> bodys = new ArrayList<Body>(); class Body { int x,y; int w = 20; int h = 20; //0 往下走 1往上走 2 往右 3 往左 int dir = 3; public Body(int x,int y) { this.x = x; this.y = y; } public void move() { //测试距离 int dleft = x; int dright = GamePanle.WIDTH - x; int dtop = y; int dbottom = GamePanle.HEIGHT - y; System.out.println("{"+dleft+","+dright+","+dtop+","+dbottom+"}"); //靠左边的时候 if (dleft <= w) { //到了左上角 if (dtop < h) { dir = 1 ; } //到了左下角 else if (dbottom < h) { dir = 2 ; } else //中间 { dir = 0; } } //靠右边的时候 if (dright <= w) { //到了右上角 if (dtop <= h) { dir = 3 ; } //到了右下角 else if (dbottom < h) { dir = 1 ; } else //中间 { dir = 1; } } System.out.println(dir); switch (dir) { case 0: if (y<=GamePanle.HEIGHT) { y+=speed; } break; case 1: if (y>=h) { y-=speed; } break; case 2: if (x<=GamePanle.WIDTH) { x+=speed; } break; case 3: if (x>=w) { x-=speed; } break; } } } Snake() { bodys.add(new Body(x,y)); bodys.add(new Body(x+25,y)); bodys.add(new Body(x+50,y)); } public void move() { for (int i = 0; i< bodys.size() ;i++ ) { Body body = bodys.get(i); body.move(); } } public void draw(Graphics g) { //画蛇,这里简单就画几个方块 //g.drawrect(snake.x,snake.y,50,50); //g.drawrect(snake.x+60,snake.y,50,50); //g.drawrect(snake.x+120,snake.y,50,50); for (int i = 0; i< bodys.size() ;i++ ) { Body body = bodys.get(i); g.drawRect(body.x,body.y,body.w,body.h); } }}class GamePanle extends JPanel{ Snake snake; static final int WIDTH = 750; static final int HEIGHT= 550; GamePanle() { snake = new Snake(); GameThread gthread = new GameThread(this); Thread thread = new Thread(gthread); thread.start(); } public void logic() { snake.move(); //其他逻辑 } public void draw() { //画蛇 repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); //画其他的东西 snake.draw(g); }}public class SnakeGame extends JFrame{ JPanel gamePanle; public SnakeGame() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(0,0,800,600); gamePanle = new GamePanle(); add(gamePanle); setVisible(true); } public static void main(String[] args) { new SnakeGame(); System.out.println("Hello World!"); }} 原来写过一个javascript版的坦克大战,那个游戏里敌人坦克自动行走的思路是是写一个行走函数,然后调用一个系统的一个计时器函数,就可以每隔固定的时间调用行走函数,重绘面板,实现自动行走 编译出错,不知道什么问题 初学者,见笑了。 基于B/S的实时曲线 java中怎样判断复选框被选中?? 怎么样将一个编译好的java类打成包使其能直接运行,给点源代码: java简体中文的问题 JTree如何将新添加的叶子节点变成目录而非文件的形式? 为什么会报UnsupportedClassVersionError这个错? 高分求教一个程序问题 急需Tomcat-4.1.27 $$$$$$$$$$$$ 如何在窗口上显示一棵树(tree) $$$$$$$$$$$$$$$$ 一个数据组合算法的问题,求教论坛里各位高人 jquery自动完成怎么实现动态
1、首先确定蛇头。
2、每X fps,让蛇朝着蛇头的方向移动 (即不停的改变 x或 y坐标的值,重新画蛇)
3、X值,随难度提高而越来越快,这样蛇也就跑的越来越快了。
4、反正键盘控制的是蛇头。
当然这个移动你应该知道怎么编吧(就是你按一下动一下的那个)。
你还可以给个判断是否继续的参数给while循环,以便蛇出界时就结束游戏。(如果设置不给,设置为true,那也可以,游戏永不结束而已)
import java.awt.*;
import java.util.*;
class GameThread implements Runnable
{
private GamePanle gamePanel;
private final int fps = 40; public GameThread(final GamePanle gamePanel)
{
this.gamePanel = gamePanel;
}
public void run()
{
while (true)
{
try
{
gamePanel.logic();
gamePanel.draw();
Thread.sleep(1000/fps);
}
catch (Exception e)
{
e.printStackTrace();
} }
}
}class Snake
{
//蛇头的开始位置
public int x = 200;
public int y = 100;
private int speed = 5;
//1,2,3,4 表示4个方向
private byte direct = 1;
//简单起见就3节
private ArrayList<Body> bodys = new ArrayList<Body>(); class Body
{
int x,y;
int w = 20;
int h = 20;
//0 往下走 1往上走 2 往右 3 往左
int dir = 3; public Body(int x,int y)
{
this.x = x;
this.y = y;
} public void move()
{ //测试距离
int dleft = x;
int dright = GamePanle.WIDTH - x;
int dtop = y;
int dbottom = GamePanle.HEIGHT - y;
System.out.println("{"+dleft+","+dright+","+dtop+","+dbottom+"}");
//靠左边的时候
if (dleft <= w)
{
//到了左上角
if (dtop < h)
{
dir = 1 ;
}
//到了左下角
else if (dbottom < h)
{
dir = 2 ;
}
else
//中间
{
dir = 0;
}
} //靠右边的时候
if (dright <= w)
{
//到了右上角
if (dtop <= h)
{
dir = 3 ;
}
//到了右下角
else if (dbottom < h)
{
dir = 1 ;
}
else
//中间
{
dir = 1;
}
} System.out.println(dir); switch (dir)
{
case 0:
if (y<=GamePanle.HEIGHT)
{
y+=speed;
}
break;
case 1:
if (y>=h)
{
y-=speed;
}
break;
case 2:
if (x<=GamePanle.WIDTH)
{
x+=speed;
}
break;
case 3:
if (x>=w)
{
x-=speed;
}
break;
}
}
} Snake()
{
bodys.add(new Body(x,y));
bodys.add(new Body(x+25,y));
bodys.add(new Body(x+50,y));
} public void move()
{
for (int i = 0; i< bodys.size() ;i++ )
{
Body body = bodys.get(i);
body.move();
}
} public void draw(Graphics g)
{
//画蛇,这里简单就画几个方块
//g.drawrect(snake.x,snake.y,50,50);
//g.drawrect(snake.x+60,snake.y,50,50);
//g.drawrect(snake.x+120,snake.y,50,50);
for (int i = 0; i< bodys.size() ;i++ )
{
Body body = bodys.get(i);
g.drawRect(body.x,body.y,body.w,body.h);
}
}
}class GamePanle extends JPanel
{
Snake snake;
static final int WIDTH = 750;
static final int HEIGHT= 550; GamePanle()
{
snake = new Snake();
GameThread gthread = new GameThread(this);
Thread thread = new Thread(gthread);
thread.start();
}
public void logic()
{
snake.move();
//其他逻辑
} public void draw()
{
//画蛇
repaint();
} public void paintComponent(Graphics g)
{
super.paintComponent(g);
//画其他的东西
snake.draw(g); }
}
public class SnakeGame extends JFrame
{
JPanel gamePanle;
public SnakeGame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0,0,800,600);
gamePanle = new GamePanle();
add(gamePanle);
setVisible(true);
}
public static void main(String[] args)
{
new SnakeGame();
System.out.println("Hello World!");
}
}