![]() |
#2
幻紫灵心2018-10-07 20:03
|

代码有点多,感觉还行凑活着看吧。

发完继续刷剧咯


package com.huawei.game.snake;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.stream.Collectors;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Test2 extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 400;
private static final int UP = 0;
private static final int LEFT = 1;
private static final int DOWN = 2;
private static final int RIGHT = 3;
private static JFrame jframe = new JFrame("Snake");
private static int dir;
private static LinkedList<NodeObj> snake = new LinkedList<>();
private static NodeObj apple;
private Random rand = new Random();
static class NodeObj {
int x;
int y;
int width = 20;
int height = 20;
public NodeObj(int x, int y) {
this.x = x;
this.y = y;
}
public void moveUp() {
y -= height;
if(y < 0) {
y = DEFAULT_HEIGHT - height;
}
}
public void moveDown() {
y += height;
if(y + height > DEFAULT_HEIGHT) {
y = 0;
}
}
public void moveLeft() {
x -= width;
if(x < 0) {
x = DEFAULT_WIDTH-width;
}
}
public void moveRight() {
x += width;
if(x + width > DEFAULT_WIDTH) {
x = 0;
}
}
}
public Test2() {
NodeObj head = new NodeObj(DEFAULT_WIDTH/2, DEFAULT_HEIGHT/2);
snake.add(head);
apple = new NodeObj(DEFAULT_WIDTH/4, DEFAULT_HEIGHT/2);
jframe.setAlwaysOnTop(true);
jframe.setSize(DEFAULT_WIDTH + 20, DEFAULT_HEIGHT + 40);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
jframe.setResizable(false);
jframe.add(this);
jframe.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if(dir != RIGHT)
dir = LEFT;
break;
case KeyEvent.VK_RIGHT:
if(dir != LEFT)
dir = RIGHT;
break;
case KeyEvent.VK_DOWN:
if(dir != UP)
dir = DOWN;
break;
case KeyEvent.VK_UP:
if(dir != DOWN)
dir = UP;
break;
}
}
});
new Timer().schedule(new TimerTask() {
@Override
public void run() {
moveAction();
checkEatAction();
checkOver();
repaint();
}
}, 10, 10);
}
protected void checkOver() {
Iterator<NodeObj> iter = snake.iterator();
NodeObj head = iter.next();
while(iter.hasNext()) {
NodeObj s = iter.next();
if(s.x == head.x && s.y == head.y) {
JOptionPane.showMessageDialog(null,"Game Over");
System.exit(0);
}
}
}
protected void checkEatAction() {
NodeObj head = snake.peek();
if(apple.x == head.x && apple.y == head.y) {
int x = head.x;
int y = head.y;
switch (dir) {
case UP:
y -= head.height;
break;
case DOWN:
y += head.height;
break;
case LEFT:
x -= head.width;
break;
case RIGHT:
x += head.width;
break;
}
NodeObj newNode = new NodeObj(x, y);
snake.push(newNode);
x = rand.nextInt(DEFAULT_WIDTH);
apple.x = x - x % apple.width;
y = rand.nextInt(DEFAULT_HEIGHT);
apple.y = y - y % apple.height;
}
}
long moveIndex = 0;
protected void moveAction() {
moveIndex++;
if(moveIndex % 20 == 0) {
// move snake
moveBody(snake);
moveHead(snake.peek());
}
}
private void moveHead(NodeObj head) {
switch (dir) {
case UP:
if(dir != DOWN)
head.moveUp();
break;
case DOWN:
if(dir != UP)
head.moveDown();
break;
case LEFT:
if(dir != RIGHT)
head.moveLeft();
break;
case RIGHT:
if(dir != LEFT)
head.moveRight();
break;
}
}
private void moveBody(LinkedList<NodeObj> nodes) {
List<NodeObj> list = nodes.stream()
.sorted((a,b)->-1).collect(Collectors.toList());
Iterator<NodeObj> iter = list.listIterator();
int i = nodes.size() - 1;
for(iter.next(); iter.hasNext(); i--) {
NodeObj cur = iter.next();
nodes.get(i).x = cur.x;
nodes.get(i).y = cur.y;
}
}
@Override
public void paint(Graphics g) {
paintArea(g);
paintObjs(g);
}
private void paintObjs(Graphics g) {
g.setColor(Color.BLACK);
boolean isFirst = true;
for(NodeObj s : snake) {
if(isFirst) {
g.fillArc(s.x, s.y, s.width, s.height, dir*90 + 135, 270);
} else
g.fillRect(s.x, s.y, s.width - 1, s.height - 1);
isFirst = false;
}
g.setColor(Color.GREEN);
g.fillOval(apple.x, apple.y, apple.width, apple.height);
}
private void paintArea(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public static void main(String[] args) {
new Test2();
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.stream.Collectors;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Test2 extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private static final int DEFAULT_WIDTH = 400;
private static final int DEFAULT_HEIGHT = 400;
private static final int UP = 0;
private static final int LEFT = 1;
private static final int DOWN = 2;
private static final int RIGHT = 3;
private static JFrame jframe = new JFrame("Snake");
private static int dir;
private static LinkedList<NodeObj> snake = new LinkedList<>();
private static NodeObj apple;
private Random rand = new Random();
static class NodeObj {
int x;
int y;
int width = 20;
int height = 20;
public NodeObj(int x, int y) {
this.x = x;
this.y = y;
}
public void moveUp() {
y -= height;
if(y < 0) {
y = DEFAULT_HEIGHT - height;
}
}
public void moveDown() {
y += height;
if(y + height > DEFAULT_HEIGHT) {
y = 0;
}
}
public void moveLeft() {
x -= width;
if(x < 0) {
x = DEFAULT_WIDTH-width;
}
}
public void moveRight() {
x += width;
if(x + width > DEFAULT_WIDTH) {
x = 0;
}
}
}
public Test2() {
NodeObj head = new NodeObj(DEFAULT_WIDTH/2, DEFAULT_HEIGHT/2);
snake.add(head);
apple = new NodeObj(DEFAULT_WIDTH/4, DEFAULT_HEIGHT/2);
jframe.setAlwaysOnTop(true);
jframe.setSize(DEFAULT_WIDTH + 20, DEFAULT_HEIGHT + 40);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
jframe.setResizable(false);
jframe.add(this);
jframe.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if(dir != RIGHT)
dir = LEFT;
break;
case KeyEvent.VK_RIGHT:
if(dir != LEFT)
dir = RIGHT;
break;
case KeyEvent.VK_DOWN:
if(dir != UP)
dir = DOWN;
break;
case KeyEvent.VK_UP:
if(dir != DOWN)
dir = UP;
break;
}
}
});
new Timer().schedule(new TimerTask() {
@Override
public void run() {
moveAction();
checkEatAction();
checkOver();
repaint();
}
}, 10, 10);
}
protected void checkOver() {
Iterator<NodeObj> iter = snake.iterator();
NodeObj head = iter.next();
while(iter.hasNext()) {
NodeObj s = iter.next();
if(s.x == head.x && s.y == head.y) {
JOptionPane.showMessageDialog(null,"Game Over");
System.exit(0);
}
}
}
protected void checkEatAction() {
NodeObj head = snake.peek();
if(apple.x == head.x && apple.y == head.y) {
int x = head.x;
int y = head.y;
switch (dir) {
case UP:
y -= head.height;
break;
case DOWN:
y += head.height;
break;
case LEFT:
x -= head.width;
break;
case RIGHT:
x += head.width;
break;
}
NodeObj newNode = new NodeObj(x, y);
snake.push(newNode);
x = rand.nextInt(DEFAULT_WIDTH);
apple.x = x - x % apple.width;
y = rand.nextInt(DEFAULT_HEIGHT);
apple.y = y - y % apple.height;
}
}
long moveIndex = 0;
protected void moveAction() {
moveIndex++;
if(moveIndex % 20 == 0) {
// move snake
moveBody(snake);
moveHead(snake.peek());
}
}
private void moveHead(NodeObj head) {
switch (dir) {
case UP:
if(dir != DOWN)
head.moveUp();
break;
case DOWN:
if(dir != UP)
head.moveDown();
break;
case LEFT:
if(dir != RIGHT)
head.moveLeft();
break;
case RIGHT:
if(dir != LEFT)
head.moveRight();
break;
}
}
private void moveBody(LinkedList<NodeObj> nodes) {
List<NodeObj> list = nodes.stream()
.sorted((a,b)->-1).collect(Collectors.toList());
Iterator<NodeObj> iter = list.listIterator();
int i = nodes.size() - 1;
for(iter.next(); iter.hasNext(); i--) {
NodeObj cur = iter.next();
nodes.get(i).x = cur.x;
nodes.get(i).y = cur.y;
}
}
@Override
public void paint(Graphics g) {
paintArea(g);
paintObjs(g);
}
private void paintObjs(Graphics g) {
g.setColor(Color.BLACK);
boolean isFirst = true;
for(NodeObj s : snake) {
if(isFirst) {
g.fillArc(s.x, s.y, s.width, s.height, dir*90 + 135, 270);
} else
g.fillRect(s.x, s.y, s.width - 1, s.height - 1);
isFirst = false;
}
g.setColor(Color.GREEN);
g.fillOval(apple.x, apple.y, apple.width, apple.height);
}
private void paintArea(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, DEFAULT_WIDTH, DEFAULT_HEIGHT);
}
public static void main(String[] args) {
new Test2();
}
}
只有本站会员才能查看附件,请 登录