贪吃蛇的代码java,贪吃蛇的代码html

java 贪吃蛇代码。移动方面的问题。

你不是有个temp的标识吗?可以用这个判断撒,记录上一状态。

公司主营业务:成都网站制作、网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出静宁免费做网站回馈大家。

修改如下:

public void keyPressed(KeyEvent e) {

if (start){

switch (e.getKeyCode()){

case KeyEvent.VK_UP:

if(temp==2) break;

move(0,-1);

temp =1;

break;

case KeyEvent.VK_DOWN:

if(temp==1) break;

move(0,1);

temp =2;

break;

case KeyEvent.VK_LEFT:

if(temp==4) break;

move(-1,0);

temp =3;

break;

case KeyEvent.VK_RIGHT:

if(temp==3) break;

move(1,0);

temp =4;

break;

default:

break;

}

}

}

高手帮忙解释一下JAVA 贪食蛇游戏中蛇的代码

public

class

Snake

implements

Data//蛇类实现了data接口

{

public

Snake()

{

array.add(new

Point(10,

10));//

array.add(new

Point(10,

11));//

array.add(new

Point(10,

12));//

array.add(new

Point(10,

13));//

array.add(new

Point(10,

14));//这五句话是构造一个蛇,这条蛇由五个坐标点连成

currentFlag

=UPFLAG

;//当前的运动方向设为向上

lifeFlag

=

true;//当前蛇的生命设为活着

}

public

void

move()//蛇的运动函数

{

tair

=

(Point)array.get(array.size()

-

1);//得到蛇的尾巴

int

len

=

array.size()

-

2;

for(int

i

=

len;

i

=

0;

i--)//这个for循环把蛇的每一个点都坐标偏移即运动了

{

Point

leftPoint

=

(Point)array.get(i);

Point

rightPoint

=

(Point)array.get(i

+

1);

rightPoint.x

=

leftPoint.x;

rightPoint.y

=

leftPoint.y;//每一个右边的点等于其左边的点,像蛇那样的一缩一张的运动

}

}

public

void

moveHeadLeft()//把蛇的头部转向左边

{

if(currentFlag

==

RIGHTFLAG

||

currentFlag

==

LEFTFLAG)//如果运动方向设为向左或向右则不执行

{

return;

}

move();

Point

point

=

(Point)(array.get(0));

//得到蛇头部

point.x

=

point.x

-

1;//蛇头部横坐标减一,纵坐标不变

point.y

=

point.y;

if(point.x

LEFT)//如果蛇头部不能再左,就不执行

{

lifeFlag

=

false;

}

currentFlag

=

LEFTFLAG;//将蛇运动方向改为左

}

java贪吃蛇代码注释求解

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.awt.image.BufferedImage;

import java.util.ArrayList;

import java.util.List;

import javax.swing.JFrame;

public class InterFace extends JFrame {

/**

* WIDTH:宽

* HEIGHT:高

* SLEEPTIME:可以看作蛇运动的速度

* L = 1,R = 2, U = 3, D = 4 左右上下代码

*/

public static final int WIDTH = 800, HEIGHT = 600, SLEEPTIME = 200, L = 1,R = 2, U = 3, D = 4;

BufferedImage offersetImage= new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_3BYTE_BGR);;

Rectangle rect = new Rectangle(20, 40, 15 * 50, 15 * 35);

Snake snake;

Node node;

public InterFace() {

//创建"蛇"对象

snake = new Snake(this);

//创建"食物"对象

createNode();

this.setBounds(100, 100, WIDTH, HEIGHT);

//添加键盘监听器

this.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent arg0) {

System.out.println(arg0.getKeyCode());

switch (arg0.getKeyCode()) {

//映射上下左右4个键位

case KeyEvent.VK_LEFT:

snake.dir = L;

break;

case KeyEvent.VK_RIGHT:

snake.dir = R;

break;

case KeyEvent.VK_UP:

snake.dir = U;

break;

case KeyEvent.VK_DOWN:

snake.dir = D;

}

}

});

this.setTitle("贪吃蛇 0.1 By : Easy");

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

this.setVisible(true);

//启动线程,开始执行

new Thread(new ThreadUpadte()).start();

}

public void paint(Graphics g) {

Graphics2D g2d = (Graphics2D) offersetImage.getGraphics();

g2d.setColor(Color.white);

g2d.fillRect(0, 0, WIDTH, HEIGHT);

g2d.setColor(Color.black);

g2d.drawRect(rect.x, rect.y, rect.width, rect.height);

//如果蛇碰撞(吃)到食物,则创建新食物

if (snake.hit(node)) {

createNode();

}

snake.draw(g2d);

node.draw(g2d);

g.drawImage(offersetImage, 0, 0, null);

}

class ThreadUpadte implements Runnable {

public void run() {

//无限重绘画面

while (true) {

try {

Thread.sleep(SLEEPTIME);

repaint(); //

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

/**

* 创建食物

*/

public void createNode() {

//随机食物的出现位置

int x = (int) (Math.random() * 650) + 50,y = (int) (Math.random() * 500) + 50;

Color color = Color.blue;

node = new Node(x, y, color);

}

public static void main(String args[]) {

new InterFace();

}

}

/**

* 节点类(包括食物和蛇的身躯组成节点)

*/

class Node {

int x, y, width = 15, height = 15;

Color color;

public Node(int x, int y, Color color) {

this(x, y);

this.color = color;

}

public Node(int x, int y) {

this.x = x;

this.y = y;

this.color = color.black;

}

public void draw(Graphics2D g2d) {

g2d.setColor(color);

g2d.drawRect(x, y, width, height);

}

public Rectangle getRect() {

return new Rectangle(x, y, width, height);

}

}

/**

* 蛇

*/

class Snake {

public ListNode nodes = new ArrayListNode();

InterFace interFace;

int dir=InterFace.R;

public Snake(InterFace interFace) {

this.interFace = interFace;

nodes.add(new Node(20 + 150, 40 + 150));

addNode();

}

/**

* 是否碰撞到食物

* @return true 是 false 否

*/

public boolean hit(Node node) {

//遍历整个蛇体是否与食物碰撞

for (int i = 0; i nodes.size(); i++) {

if (nodes.get(i).getRect().intersects(node.getRect())) {

addNode();

return true;

}

}

return false;

}

public void draw(Graphics2D g2d) {

for (int i = 0; i nodes.size(); i++) {

nodes.get(i).draw(g2d);

}

move();

}

public void move() {

nodes.remove((nodes.size() - 1));

addNode();

}

public synchronized void addNode() {

Node nodeTempNode = nodes.get(0);

//如果方向

switch (dir) {

case InterFace.L:

//判断是否会撞墙

if (nodeTempNode.x = 20) {

nodeTempNode = new Node(20 + 15 * 50, nodeTempNode.y);

}

nodes.add(0, new Node(nodeTempNode.x - nodeTempNode.width,

nodeTempNode.y));

break;

case InterFace.R:

//判断是否会撞墙

if (nodeTempNode.x = 20 + 15 * 50 - nodeTempNode.width) {

nodeTempNode = new Node(20 - nodeTempNode.width, nodeTempNode.y);

}

nodes.add(0, new Node(nodeTempNode.x + nodeTempNode.width,

nodeTempNode.y));

break;

case InterFace.U:

//判断是否会撞墙

if (nodeTempNode.y = 40) {

nodeTempNode = new Node(nodeTempNode.x, 40 + 15 * 35);

}

nodes.add(0, new Node(nodeTempNode.x, nodeTempNode.y - nodeTempNode.height));

break;

case InterFace.D:

//判断是否会撞墙

if (nodeTempNode.y = 40 + 15 * 35 - nodeTempNode.height) {

nodeTempNode = new Node(nodeTempNode.x,40 - nodeTempNode.height);

}

nodes.add(0, new Node(nodeTempNode.x, nodeTempNode.y + nodeTempNode.height));

break;

}

}

}

Java贪吃蛇的界面背景代码怎么写

这是一个最基本的界面,下面又不会的地方去查API

import java.awt.*;

import javax.swing.*;

public class View {

JFrame frame;

Container c;

JLabel backgound;

JButton start;

JLabel help;

public void first(){

frame=new JFrame("我的贪吃蛇");

c=frame.getContentPane();

c.setLayout(null);

Font f1=new Font("楷体",Font.BOLD,50);

backgound=new JLabel();

backgound.setBounds(0,0,800,600);

c.add(backgound);

backgound.setLayout(null);

start=new JButton();

start.setBounds(300,70,200,58);

start.setCursor(new Cursor(Cursor.HAND_CURSOR));

start.setBorder(null);

start.setContentAreaFilled(false);

backgound.add(start);

help = new JLabel();

help.setText("游戏帮助");

help.setBounds(180,230,500,50);

help.setForeground(Color.black);

help.setFont(f1);

backgound.add(help);

frame.setSize(800,600);

frame.setResizable(false);

frame.setVisible(true);

}


本文名称:贪吃蛇的代码java,贪吃蛇的代码html
转载来于:http://ybzwz.com/article/dschoih.html