我应该用java中的队列创建一个蛇游戏。
我应该只使用下面的三个类,但是我不知道如何修改move()函数来移动蛇,因为它是一个队列,我无法访问蛇的尾部。
我得出了以下结论:
蛇.java
import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Snake extends JFrame implements Runnable, KeyListener {
int length=40, width = 30;
int time;
int factor=10;
Specialqueue q;
int direction = 6;
Thread t;
int[][] food;
private boolean started;
public Snake() {
super("Snake");
setSize(length*factor+30, width*factor+60);
setVisible(true);
addKeyListener(this);
started = false;
time = 0;
}
public void paint(Graphics g){
if (g == null) return;
g.clearRect(0,0,getWidth(),getHeight());
if (!gestartet){
g.drawString("start the game by pressing 5",10,50);
g.drawString("Controll the snake with W,A,S,D",10,80);
return;
}
g.setColor(Color.blue);
for (int k = 0; k < length; k++)
for (int j = 0; j < breite; j++)
if (food[k][j]==1)
g.fillOval(k*10,20+j*10,10,10);
}
public void run() {
time = 0;
while (started){
try {Thread.sleep(300);}
catch (Exception e){};
move();
time++;
Graphics g = this.getGraphics();
}
}
public void stop() {
started=false;
t = null;
}
public void initFood() {
food = new int[length][breite];
for (int k = 0; k < length; k++) {
for (int j = 0; j < breite; j++) {
if (Math.random() < 0.01) futter[k][j] = 1;
else food[k][j] = 0;
}
}
}
public void initSnake(){
q = new Specialqueue();
direction = 6;
q.append(new Point(20,20));
q.append(new Point(19,20));
q.append(new Point(18,20));
q.append(new Point(17,20));
Graphics g = this.getGraphics();
repaint();
g.setColor(Color.red);
g.fillOval(20*10,20+20*10,10,10);
g.fillOval(19*10,20+20*10,10,10);
g.fillOval(18*10,20+20*10,10,10);
g.fillOval(17*10,20+20*10,10,10);
g.setColor(Color.black);
}
public void move()
{
Graphics g = this.getGraphics();
Point p = (Point)q.tail();
Point newPoint = null;
int size = 4;
if (direction == 6) {
for(int i=0;i<3;i++) {
q.remove();
}
for(int q=0;q<3;q++) {
newPoint = new Point(p.x+1, p.y);
}
if (newPoint.x >= length) {stoppe(); return;}
}
else if (direction == 4) {
newPoint = new Point(p.x-1, p.y);
if (newPoint.x < 0) {stoppe(); return;}
}
else if (direction == 8)
{
newPoint = new Point(p.x, p.y-1);
if (newPoint.y < 0) {stoppe(); return;}
}
else if (direction == 2)
{
newPoint = new Point(p.x, p.y+1);
if (newPoint.y >= width) {stoppe(); return;}
}
q.append(newPoint);
p = (Point)q.tail();
g.setColor(Color.red);
g.fillOval(p.x*10,20+p.y*10,10,10);
if(food[newPoint.x][newPoint.y] == 0){
p = (Point)q.first();
g.setColor(Color.white);
g.fillOval(p.x*10,20+p.y*10,10,10);
q.remove();
}
else {
food[newPoint.x][newPoint.y]=0;
}
}
public void keyPressed(KeyEvent ke)
{
char c = ke.getKeyChar();
if (c=='5' && started) {started = false; t = null; repaint(); return; }
if (c=='5' && !started){
gestartet=true;
initSnake();
initFutter();
t = new Thread(this);
try {Thread.sleep(150);} catch (Exception e){};
t.start();
return;
}
if ((c =='6' || c=='d') && direction != 4) direction = 6;
else if ((c =='2' || c=='s') && direction != 8) direction = 2;
else if ((c =='4' || c=='a') && direction != 6) direction = 4;
else if ((c =='8' || c=='w') && direction != 2) direction = 8;
}
}
特殊队列.java
public class Specialqueue <ContentType> {
Queue<ContentType> s;
public Specialqueue()
{
s = new Queue<ContentType>();
}
public ContentType tail(){
if (s.isEmpty()) return null;
return s.front();
}
public boolean isEmpty(){
return s.isEmpty();
}
public void append(ContentType pContent)
{
s.enqueue(pContent);
}
public void remove()
{
if(!s.isEmpty()) {
s.dequeue();
}
}
public ContentType first(){
if (this.isEmpty())
{
return null;
}
else{
return s.front();
}
}
}
**Queue.java**
公共类队列{
protected class QueueNode {
protected ContentType content = null;
protected QueueNode nextNode = null;
public QueueNode(ContentType pContent) {
content = pContent;
nextNode = null;
}
public void setNext(QueueNode pNext) {
nextNode = pNext;
}
public QueueNode getNext() {
return nextNode;
}
public ContentType getContent() {
return content;
}
}
protected QueueNode head;
protected QueueNode tail;
public Queue() {
head = null;
tail = null;
}
public boolean isEmpty() {
return head == null;
}
public void enqueue(ContentType pContent) {
if (pContent != null) {
QueueNode newNode = new QueueNode(pContent);
if (this.isEmpty()) {
head = newNode;
tail = newNode;
} else {
tail.setNext(newNode);
tail = newNode;
}
}
}
public void dequeue() {
if (!this.isEmpty()) {
head = head.getNext();
if (this.isEmpty()) {
head = null;
tail = null;
}
}
}
public ContentType front() {
if (this.isEmpty()) {
return null;
} else {
return head.getContent();
}
}
}
暂无答案!
目前还没有任何答案,快来回答吧!