Java 蛇游戏问题,蛇不动
我试图为我的编程课重新创建一个简单的蛇游戏。我正在Eclipse程序上使用java。如果你不知道怎么玩这个游戏,这是一个游戏,当蛇吃了圆点,它就会长大,当蛇撞到自己时,游戏就结束了。任何帮助都将不胜感激Java 蛇游戏问题,蛇不动,java,Java,我试图为我的编程课重新创建一个简单的蛇游戏。我正在Eclipse程序上使用java。如果你不知道怎么玩这个游戏,这是一个游戏,当蛇吃了圆点,它就会长大,当蛇撞到自己时,游戏就结束了。任何帮助都将不胜感激 import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Point; import java.awt.event.KeyEvent; im
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.JFrame;
public class Source extends JFrame implements Runnable, KeyListener {
private final int boxHeight = 15; //each individual box height
private final int boxWidth = 15; //each individual box width
private final int gridWidth = 25; //Total width of all boxes in Grid
private final int gridHeight = 25; //Total height of all boxes in Grid
JFrame frame = new JFrame();
private LinkedList<Point> snake;
public Point fruit;
public int direction = Direction.noDirection;
private Thread runThread;
private Graphics globalGraphics;
private int score = 0;
public void paint (Graphics g)
{
setBounds(0,0,500,500);
snake = new LinkedList<Point>();
GenerateDefaultSnake();
PlaceFruit();
globalGraphics = g.create();
this.addKeyListener(this);
if (runThread == null){
runThread = new Thread(this);
runThread.start();
}
}
public void GenerateDefaultSnake(){
score = 0;
snake.clear();
snake.add(new Point (0,2));
snake.add(new Point (0,1));
snake.add(new Point (0,0));
direction = Direction.noDirection;
}
public void Draw (Graphics g){ //main method of what will be drawn
g.clearRect(0, 0, boxWidth * gridWidth + 10, boxHeight * gridHeight +20);
//create a new image
BufferedImage buffer = new BufferedImage(boxWidth * gridWidth + 10, boxHeight * gridHeight +20, BufferedImage.TYPE_INT_ARGB);
Graphics bufferGraphics = buffer.getGraphics();
DrawFruit(bufferGraphics);
DrawGrid(bufferGraphics);
DrawSnake(bufferGraphics);
DrawScore(bufferGraphics);
//flip
g.drawImage(buffer, 0,0, boxWidth * gridWidth +10, boxHeight * gridHeight +20, this);
}
public void Move(){ //directions
Point head = snake.peekFirst(); //head of snake, allows us to have body follow in chronological order
Point newPoint = head;
snake.remove(snake.peekLast()); //removes end of tail
if(newPoint.equals(fruit))
{
score += 10;
Point addPoint = (Point) newPoint.clone();
//the snake has hit the fruit
switch(direction){
case Direction.North:
newPoint = new Point (head.x, head.y -1);
break;
case Direction.South:
newPoint = new Point(head.x,head.y +1);
break;
case Direction.West:
newPoint = new Point(head.x -1,head.y);
break;
case Direction.East:
newPoint = new Point(head.x + 1,head.y);
break;
}
snake.push(addPoint);
PlaceFruit();
}
else if (newPoint.x < 0 || newPoint.x > (gridWidth - 1)){
//we went out of bounds, reset game
GenerateDefaultSnake();
return;
}
else if (newPoint.y < 0 || newPoint.y > (gridHeight - 1 )){
//we went out of bounds, reset game
GenerateDefaultSnake();
return;
}
else if (snake.contains(newPoint)){
//we ran into ourselves, reset game
GenerateDefaultSnake();
return;
}
//if we reach this point of the game, we are still good
snake.push(newPoint); //pushes all points one point ahead when you eat fruit and adds fruit that you ate at the end
}
public void DrawScore(Graphics g){
g.drawString("Score: " + score,0, boxHeight * gridHeight +10);
}
public void DrawGrid (Graphics g){
//drawing outer rectangle
g.drawRect(0,0, gridWidth * boxWidth, gridHeight * boxHeight); //creates the outer rectangle
//drawing vertical lines of grid
for (int x = boxWidth; x < gridWidth * boxWidth; x += boxWidth){
g.drawLine(x, 0, x, boxHeight * gridHeight);
}
//drawing horizontal lines of grid
for(int y = boxHeight; y < gridHeight * boxHeight; y += boxHeight){
g.drawLine(0, y, gridWidth * boxWidth, y);
}
}
public void DrawSnake(Graphics g){
g.setColor(Color.GREEN);
for ( Point p : snake){
g.fillRect(p.x * boxWidth, p.y * boxHeight, boxWidth, boxHeight);
}
g.setColor(Color.BLACK);
}
public void DrawFruit(Graphics g){
g.setColor(Color.RED);
g.fillOval(fruit.x * boxWidth, fruit.y * boxHeight, boxWidth, boxHeight);
g.setColor(Color.BLACK);
}
public void PlaceFruit()
{
Random rand = new Random();
int randomX = rand.nextInt(gridWidth);
int randomY = rand.nextInt(gridHeight);
Point randomPoint = new Point(randomX, randomY);
while (snake.contains(randomPoint)){
randomX = rand.nextInt(gridWidth);
randomY = rand.nextInt(gridHeight);
randomPoint = new Point(randomX, randomY);
}
fruit = randomPoint;
}
public void run() {
while(true){
//runs indefinitely, every second the objects in this loop will move
Move();
Draw(globalGraphics);
try{
Thread.currentThread();
Thread.sleep(100); //game will be updating itself every tenth of a second (.1 of a second)
}
catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode())
{
case KeyEvent.VK_UP:
if(direction != Direction.South)
direction = Direction.North;
break;
case KeyEvent.VK_DOWN:
if(direction != Direction.North)
direction = Direction.South;
break;
case KeyEvent.VK_RIGHT:
if(direction != Direction.West)
direction = Direction.East;
break;
case KeyEvent.VK_LEFT:
if(direction != Direction.East)
direction = Direction.West;
break;
}
}
public class Direction {
public static final int noDirection = 0;
public static final int North = 1;
public static final int South = 2;
public static final int West = 3;
public static final int East = 4;
}
public class Snake extends JFrame{
c = new Source();
c.setPreferredSize(new Dimension (640,480));
c.setVisible(true);
c.setFocusable(true);
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}*
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Point;
导入java.awt.event.KeyEvent;
导入java.awt.event.KeyListener;
导入java.awt.image.buffereImage;
导入java.util.LinkedList;
导入java.util.Random;
导入javax.swing.JFrame;
公共类源扩展JFrame实现可运行的KeyListener{
私有最终int-boxHeight=15;//每个单独的框高度
私有最终int-boxWidth=15;//每个单独的框宽度
private final int gridWidth=25;//网格中所有框的总宽度
private final int gridHeight=25;//网格中所有框的总高度
JFrame=新JFrame();
私人链接列表蛇;
公共点水果;
公共int方向=direction.noDirection;
私有线程runThread;
私有图形;全球图形;
私人智力得分=0;
公共空间涂料(图g)
{
立根(0,0500);;
snake=newlinkedlist();
GenerateDefaultSnake();
PlaceFruit();
globalGraphics=g.create();
this.addKeyListener(this);
if(runThread==null){
runThread=新线程(此线程);
runThread.start();
}
}
public void GenerateDefaultSnake(){
得分=0;
snake.clear();
添加(新点(0,2));
添加(新点(0,1));
添加(新点(0,0));
方向=direction.noDirection;
}
公共空白绘制(图g){//绘制内容的主要方法
g、 clearRect(0,0,boxWidth*gridWidth+10,boxHeight*gridHeight+20);
//创建一个新图像
BuffereImage buffer=新的BuffereImage(boxWidth*gridWidth+10,boxHeight*gridHeight+20,BuffereImage.TYPE_INT_ARGB);
Graphics bufferGraphics=buffer.getGraphics();
DrawFruit(图形);
绘图网格(图形);
DrawSnake(图形);
绘图分数(图形);
//翻转
g、 drawImage(缓冲区,0,0,boxWidth*gridWidth+10,boxHeight*gridHeight+20,此值);
}
public void Move(){//directions
Point head=snake.peekFirst();//蛇头,允许我们按时间顺序跟随身体
点newPoint=头部;
snake.remove(snake.peekLast());//删除尾部末端
if(newPoint.equals(fruit))
{
分数+=10分;
Point addPoint=(Point)newPoint.clone();
//蛇咬到了水果
开关(方向){
案件方向:北:
新点=新点(head.x,head.y-1);
打破
案例方向:南部:
新点=新点(头部x,头部y+1);
打破
案例方向:西:
新点=新点(head.x-1,head.y);
打破
案例方向:东:
新点=新点(头部x+1,头部y);
打破
}
snake.push(addPoint);
PlaceFruit();
}
else if(newPoint.x<0 | | newPoint.x>(gridWidth-1)){
//我们出界了,重新开始比赛
GenerateDefaultSnake();
返回;
}
else if(newPoint.y<0 | | newPoint.y>(gridHeight-1)){
//我们出界了,重新开始比赛
GenerateDefaultSnake();
返回;
}
else if(snake.contains(newPoint)){
//我们撞到了自己,重新设定了游戏
GenerateDefaultSnake();
返回;
}
//如果我们达到了比赛的这一点,我们仍然很好
snake.push(newPoint);//当你吃水果时,将所有点向前推一点,并在最后添加你吃的水果
}
公众虚空抽签分数(图g){
g、 抽绳(“分数:+分数,0,箱高*网格高+10);
}
公共空心绘图网格(图形g){
//绘制外矩形
g、 drawRect(0,0,gridWidth*boxWidth,gridHeight*boxHeight);//创建外部矩形
//绘制网格的垂直线
用于(int x=boxWidth;xif(newPoint.equals(fruit))
{
score += 10;
Point addPoint = (Point) newPoint.clone();
//the snake has hit the fruit
switch(direction){
case Direction.North:
newPoint = new Point (head.x, head.y -1);
break;
case Direction.South:
newPoint = new Point(head.x,head.y +1);
break;
case Direction.West:
newPoint = new Point(head.x -1,head.y);
break;
case Direction.East:
newPoint = new Point(head.x + 1,head.y);
break;
}
snake.push(addPoint);
PlaceFruit();
}
public void Move(){ //directions
Point head = snake.peekFirst(); //head of snake, allows us to have body follow in chronological order
Point newPoint = head;
snake.remove(snake.peekLast()); //removes end of tail
Point addPoint = (Point) newPoint.clone();
switch(direction) {
case Direction.North:
newPoint = new Point (head.x, head.y -1);
break;
case Direction.South:
newPoint = new Point(head.x,head.y +1);
break;
case Direction.West:
newPoint = new Point(head.x -1,head.y);
break;
case Direction.East:
newPoint = new Point(head.x + 1,head.y);
break;
}
//the snake has hit the fruit
if(newPoint.equals(fruit))
{
score += 10;
fruit = null;
snake.push(addPoint);
}
else if (newPoint.x < 0 || newPoint.x > (gridWidth - 1)){
//we went out of bounds, reset game
GenerateDefaultSnake();
return;
}
else if (newPoint.y < 0 || newPoint.y > (gridHeight - 1 )){
//we went out of bounds, reset game
GenerateDefaultSnake();
return;
}
else if (snake.contains(newPoint)){
//we ran into ourselves, reset game
GenerateDefaultSnake();
return;
}
//if we reach this point of the game, we are still good
PlaceFruit();
snake.push(newPoint); //pushes all points one point ahead when you eat fruit and adds fruit that you ate at the end
}
public void Move(){ //directions
Point head = snake.peekFirst(); //head of snake, allows us to have body follow in chronological order
Point newPoint = head;
snake.remove(snake.peekLast()); //removes end of tail
Point addPoint = (Point) newPoint.clone();
switch(direction) {
case Direction.North:
newPoint = new Point (head.x, head.y -1);
break;
case Direction.South:
newPoint = new Point(head.x,head.y +1);
break;
case Direction.West:
newPoint = new Point(head.x -1,head.y);
break;
case Direction.East:
newPoint = new Point(head.x + 1,head.y);
break;
}
//the snake has hit the fruit
if(newPoint.equals(fruit))
{
score += 10;
snake.push(addPoint);
PlaceFruit();
}
else if (newPoint.x < 0 || newPoint.x > (gridWidth - 1)){
//we went out of bounds, reset game
GenerateDefaultSnake();
PlaceFruit();
return;
}
else if (newPoint.y < 0 || newPoint.y > (gridHeight - 1 )){
//we went out of bounds, reset game
GenerateDefaultSnake();
PlaceFruit();
return;
}
else if (snake.contains(newPoint)){
//we ran into ourselves, reset game
GenerateDefaultSnake();
PlaceFruit();
return;
}
//if we reach this point of the game, we are still good
snake.push(newPoint); //pushes all points one point ahead when you eat fruit and adds fruit that you ate at the end
}