貪吃蛇java代碼報(bào)告 貪吃蛇java代碼報(bào)告怎么看

高手幫忙解釋一下JAVA 貪食蛇游戲中蛇的代碼

public

站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到愛(ài)民網(wǎng)站設(shè)計(jì)與愛(ài)民網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋愛(ài)民地區(qū)。

class

Snake

implements

Data//蛇類實(shí)現(xiàn)了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));//這五句話是構(gòu)造一個(gè)蛇,這條蛇由五個(gè)坐標(biāo)點(diǎn)連成

currentFlag

=UPFLAG

;//當(dāng)前的運(yùn)動(dòng)方向設(shè)為向上

lifeFlag

=

true;//當(dāng)前蛇的生命設(shè)為活著

}

public

void

move()//蛇的運(yùn)動(dòng)函數(shù)

{

tair

=

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

-

1);//得到蛇的尾巴

int

len

=

array.size()

-

2;

for(int

i

=

len;

i

=

0;

i--)//這個(gè)for循環(huán)把蛇的每一個(gè)點(diǎn)都坐標(biāo)偏移即運(yùn)動(dòng)了

{

Point

leftPoint

=

(Point)array.get(i);

Point

rightPoint

=

(Point)array.get(i

+

1);

rightPoint.x

=

leftPoint.x;

rightPoint.y

=

leftPoint.y;//每一個(gè)右邊的點(diǎn)等于其左邊的點(diǎn),像蛇那樣的一縮一張的運(yùn)動(dòng)

}

}

public

void

moveHeadLeft()//把蛇的頭部轉(zhuǎn)向左邊

{

if(currentFlag

==

RIGHTFLAG

||

currentFlag

==

LEFTFLAG)//如果運(yùn)動(dòng)方向設(shè)為向左或向右則不執(zhí)行

{

return;

}

move();

Point

point

=

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

//得到蛇頭部

point.x

=

point.x

-

1;//蛇頭部橫坐標(biāo)減一,縱坐標(biāo)不變

point.y

=

point.y;

if(point.x

LEFT)//如果蛇頭部不能再左,就不執(zhí)行

{

lifeFlag

=

false;

}

currentFlag

=

LEFTFLAG;//將蛇運(yùn)動(dòng)方向改為左

}

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:可以看作蛇運(yùn)動(dòng)的速度

* 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() {

//創(chuàng)建"蛇"對(duì)象

snake = new Snake(this);

//創(chuàng)建"食物"對(duì)象

createNode();

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

//添加鍵盤(pán)監(jiān)聽(tīng)器

this.addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent arg0) {

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

switch (arg0.getKeyCode()) {

//映射上下左右4個(gè)鍵位

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);

//啟動(dòng)線程,開(kāi)始執(zhí)行

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);

//如果蛇碰撞(吃)到食物,則創(chuàng)建新食物

if (snake.hit(node)) {

createNode();

}

snake.draw(g2d);

node.draw(g2d);

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

}

class ThreadUpadte implements Runnable {

public void run() {

//無(wú)限重繪畫(huà)面

while (true) {

try {

Thread.sleep(SLEEPTIME);

repaint(); //

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

/**

* 創(chuàng)建食物

*/

public void createNode() {

//隨機(jī)食物的出現(xiàn)位置

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();

}

}

/**

* 節(jié)點(diǎn)類(包括食物和蛇的身軀組成節(jié)點(diǎn))

*/

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) {

//遍歷整個(gè)蛇體是否與食物碰撞

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:

//判斷是否會(huì)撞墻

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:

//判斷是否會(huì)撞墻

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:

//判斷是否會(huì)撞墻

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:

//判斷是否會(huì)撞墻

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代碼

自己寫(xiě)著玩的,很簡(jiǎn)單,你試一試哦...

主要用了javax.swing.Timer這個(gè)類:

import java.awt.*;

import javax.swing.*;

@SuppressWarnings("serial")

public class MainClass extends JFrame {

ControlSnake control;

Toolkit kit;

Dimension dimen;

public static void main(String[] args) {

new MainClass("my snake");

}

public MainClass(String s) {

super(s);

control = new ControlSnake();

control.setFocusable(true);

kit = Toolkit.getDefaultToolkit();

dimen = kit.getScreenSize();

add(control);

setLayout(new BorderLayout());

setLocation(dimen.width / 3, dimen.height / 3);// dimen.width/3,dimen.height/3

setSize(FWIDTH, FHEIGHT);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setResizable(false);

setVisible(true);

}

public static final int FWIDTH = 315;

public static final int FHEIGHT = 380;

}

import java.util.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.Timer;

import java.util.Random;

@SuppressWarnings("serial")

public class ControlSnake extends JPanel implements ActionListener {

Random rand;

ArrayListPoint list, listBody;

String str, str1;

static boolean key;

int x, y, dx, dy, fx, fy, flag;

int snakeBody;

int speed;

public ControlSnake() {

snakeBody = 1;

str = "上下左右方向鍵控制 P鍵暫停...";

str1 = "現(xiàn)在的長(zhǎng)度為:" + snakeBody;

key = true;

flag = 1;

speed = 700;

rand = new Random();

list = new ArrayListPoint();

listBody = new ArrayListPoint();

x = 5;

y = 5;

list.add(new Point(x, y));

listBody.add(list.get(0));

dx = 10;

dy = 0;

fx = rand.nextInt(30) * 10 + 5;// 2

fy = rand.nextInt(30) * 10 + 5;// 2

setBackground(Color.WHITE);

setSize(new Dimension(318, 380));

final Timer time = new Timer(speed, this);

time.start();

addKeyListener(new KeyAdapter() {

public void keyPressed(KeyEvent e) {

if (e.getKeyCode() == 37) {

dx = -10;

dy = 0;

} else if (e.getKeyCode() == 38) {

dx = 0;

dy = -10;

} else if (e.getKeyCode() == 39) {

dx = 10;

dy = 0;

} else if (e.getKeyCode() == 40) {

dx = 0;

dy = 10;

} else if (e.getKeyCode() == 80) {

if (flag % 2 == 1) {

time.stop();

}

if (flag % 2 == 0) {

time.start();

}

flag++;

}

}

});

}

public void paint(Graphics g) {

g.setColor(Color.WHITE);

g.fillRect(0, 0, 400, 400);

g.setColor(Color.DARK_GRAY);

g.drawLine(3, 3, 305, 3);

g.drawLine(3, 3, 3, 305);

g.drawLine(305, 3, 305, 305);

g.drawLine(3, 305, 305, 305);

g.setColor(Color.PINK);

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

g.fillRect(listBody.get(i).x, listBody.get(i).y, 9, 9);

}

g.fillRect(x, y, 9, 9);

g.setColor(Color.ORANGE);

g.fillRect(fx, fy, 9, 9);

g.setColor(Color.DARK_GRAY);

str1 = "現(xiàn)在的長(zhǎng)度為:" + snakeBody;

g.drawString(str, 10, 320);

g.drawString(str1, 10, 335);

}

public void actionPerformed(ActionEvent e) {

x += dx;

y += dy;

if (makeOut() == false) {

JOptionPane.showMessageDialog(null, "重新開(kāi)始......");

speed = 700;

snakeBody = 1;

x = 5;

y = 5;

list.clear();

list.add(new Point(x, y));

listBody.clear();

listBody.add(list.get(0));

dx = 10;

dy = 0;

}

addPoint(x, y);

if (x == fx y == fy) {

speed = (int) (speed * 0.8);//速度增加參數(shù)

if (speed 200) {

speed = 100;

}

fx = rand.nextInt(30) * 10 + 5;// 2

fy = rand.nextInt(30) * 10 + 5;// 2

snakeBody++;// 2

} // 2

repaint();

}

public void addPoint(int xx, int yy) {

// 動(dòng)態(tài)的記錄最新發(fā)生的50步以內(nèi)的移動(dòng)過(guò)的坐標(biāo)

// 并畫(huà)出最新的snakeBody

if (list.size() 100) {//蛇身長(zhǎng)度最長(zhǎng)為100

list.add(new Point(xx, yy));

} else {

list.remove(0);

list.add(new Point(xx, yy));

}

if (snakeBody == 1) {

listBody.remove(0);

listBody.add(0, list.get(list.size() - 1));

} else {

listBody.clear();

if (list.size() snakeBody) {

for (int i = list.size() - 1; i 0; i--) {

listBody.add(list.get(i));

}

} else {

for (int i = list.size() - 1; listBody.size() snakeBody; i--) {

listBody.add(list.get(i));

}

}

}

}

public boolean makeOut() {

if ((x 3 || y 3) || (x 305 || y 305)) {

return false;

}

for (int i = 0; i listBody.size() - 1; i++) {

for (int j = i + 1; j listBody.size(); j++) {

if (listBody.get(i).equals(listBody.get(j))) {

return false;

}

}

}

return true;

}

}

分享文章:貪吃蛇java代碼報(bào)告 貪吃蛇java代碼報(bào)告怎么看
本文鏈接:http://muchs.cn/article6/dojhcog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)定制網(wǎng)站、響應(yīng)式網(wǎng)站ChatGPT、移動(dòng)網(wǎng)站建設(shè)軟件開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都app開(kāi)發(fā)公司