2014-05-07 47 views
2

我正在做一個蛇遊戲。我想讓蛇從一堵牆穿過另一堵牆。蛇遊戲java,讓蛇穿牆

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Point; 
import java.awt.Toolkit; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.awt.image.BufferStrategy; 
import java.util.LinkedList; 
import java.util.Random; 

import javax.swing.JFrame; 

public class Snake extends JFrame implements KeyListener { 

private static final long serialVersionUID = 1L; 

private int windowWidth = 800; 
private int windowHeight = 600; 
private LinkedList<Point> snake; 
private int dx; 
private int dy; 
private Random generator = new Random(); 
private Point food; 
private int points; 


public static void main(String[] args) { 
    new Snake(); 
} 

public Snake() { 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setSize(windowWidth, windowHeight); 
    this.setResizable(false); 
    this.setLocation(100, 100); 
    this.setVisible(true); 

    this.createBufferStrategy(2); 

    this.addKeyListener(this); 

    initGame(); 

    while(true) { 
     long start = System.currentTimeMillis(); 
     gameLoop(); 
     while(System.currentTimeMillis()-start < 40) { 
      //do nothing 
     } 
    } 
} 

private void initGame() { 
    // game variables initialized here 
    snake = new LinkedList<Point>(); 
    snake.addFirst(new Point(20,20)); 
    growSnake(5); 

    dx = 0; 
    dy = 0; 

    food = new Point(10,10); 

    points = 0; 
} 

private void gameLoop() { 
    // game logic 

    // move the snake 
    moveSnake(dx, dy); 

    // check if snake has eaten food 
    if(snake.getFirst().equals(food)) { 
     moveFood(); 
     growSnake(3); 
     points++; 
    } 

    // go through walls 
    if (snake.getFirst().x <= 0){ 
     snake.getFirst().x = windowWidth/10;//go left, wrap right 
    } 

    else if (snake.getFirst().x >= windowWidth/10){ 
     snake.getFirst().x = 0;//go right, wrap left 
    } 

    if (snake.getFirst().y <= 0){ 
     snake.getFirst().y = windowWidth/10;//go top, wrap bottom 
    } 

    else if (snake.getFirst().y >= windowHeight/10){ 
     snake.getFirst().y = 0;//go bottom, wrap top 
    }  


    // check if the snake has hit itself 
    for(int n = 1; n < snake.size(); n++) { 
     if(snake.getFirst().equals(snake.get(n))) { 
      initGame(); 
     } 
    } 

    drawFrame(); 
} 

private void drawFrame() { 
    // code for drawing 
    BufferStrategy bf = this.getBufferStrategy(); 
    Graphics g = null; 

    try { 
     g = bf.getDrawGraphics(); 

     // clear the back buffer (just draw a big black rectangle over it) 
     g.setColor(Color.BLACK); 
     g.fillRect(0, 0, windowWidth, windowHeight); 

     drawSnake(g); 
     drawFood(g); 
     drawPoints(g); 
    } finally { 

     g.dispose(); 
    } 

    // Shows the contents of the backbuffer on the screen. 
    bf.show(); 

    //Tell the System to do the Drawing now, otherwise it can take a few extra ms until 
    //Drawing is done which looks very jerky 
    Toolkit.getDefaultToolkit().sync(); 
} 

private void drawSnake (Graphics g) { 
    for(int n = 0; n < snake.size(); n++) { 
     g.setColor(Color.RED); 
     Point p = snake.get(n); 
     g.fillOval(p.x*10, p.y*10, 10, 10); 
    } 
} 

private void moveSnake(int dx, int dy) { 
    for(int n = snake.size()-1; n >= 1; n--) { 
     snake.get(n).setLocation(snake.get(n-1)); 
    } 
    snake.getFirst().x += dx; 
    snake.getFirst().y += dy; 
} 

private void growSnake (int n) { 
    while(n > 0) { 
     snake.add(new Point(snake.getLast())); 
     n--; 
    } 
} 

private void moveFood() { 
    food.x = generator.nextInt((windowWidth/10)-4)+2; 
    food.y = generator.nextInt((windowHeight/10)-5)+3; 
} 

private void drawFood(Graphics g) { 
    g.setColor(Color.BLUE); 
    g.fillOval(food.x*10, food.y*10, 10, 10); 
} 

private void drawPoints(Graphics g) { 
    g.setColor(Color.GRAY); 
    g.drawString("points: " + points, 10, 40); 
} 

@Override 
public void keyPressed(KeyEvent e) { 
    int key = e.getKeyCode(); 

    if(key == 37) { 
     dx = -1; 
     dy = 0; 
    } else if(key == 38) { 
     dx = 0; 
     dy = -1; 
    } else if(key == 39) { 
     dx = 1; 
     dy = 0; 
    } else if(key == 40) { 
     dx = 0; 
     dy = 1; 
    } 
} 

@Override 
public void keyReleased(KeyEvent e) { 

} 

@Override 
public void keyTyped(KeyEvent e) { 

} 
} 

我的問題就出在這一段代碼

// go through walls 
if (snake.getFirst().x <= 0){ 
     snake.getFirst().x = windowWidth/10;//go left, wrap right 
    } 

    else if (snake.getFirst().x >= windowWidth/10){ 
     snake.getFirst().x = 0;//go right, wrap left 
    } 

    if (snake.getFirst().y <= 0){ 
     snake.getFirst().y = windowWidth/10;//go top, wrap bottom 
    } 

    else if (snake.getFirst().y >= windowHeight/10){ 
     snake.getFirst().y = 0;//go bottom, wrap top 
    } 

如果蛇達到一個壁的邊界,然後將其變成另一個壁的價值。這工作除了頂部之外的所有牆壁。

我該如何解決這個問題?

+1

'else if(snake.getFirst()。y <= 2){'shouldnt this if'if'else if else if'? –

+0

snake.getFirst()。x = 600 - 1; ? – Tofandel

+0

另外,究竟發生了什麼問題?有錯誤嗎?有什麼特別的情況出現問題嗎? –

回答

0

你的邏輯是錯誤的,這是你必須做的

if(snake.getFirst().x <= 0){ 
    snake.getFirst().x = windowWidth/10; //Warp to right 
} 
else if (snake.getFirst().x >= windowWidth/10){ 
    snake.getFirst().x = 0; //Warp to left 
} 

if (snake.getFirst().y <= 0){ 
    snake.getFirst().y = windowHeight/10; //Warp to bottom 
} 
else if (snake.getFirst().y >= windowHeight/10) 
    snake.getFirst().y = 0; //Warp to top 
+0

蛇撞到牆上時就會消失。 –

+0

其實你的代碼是好的,除了在第三如果聲明是windowHeight而不是windowWidth – Tofandel

2

因爲這看起來像功課,我不會直接回答什麼。一次嘗試單步執行代碼1行。可以說snake.getFirst().x0。當你看到你的if聲明時,它會進入第一個區塊。

snake.getFirst().x = 600; 

現在snake.getFirst().x600。下一次它得到您的if語句就會陷入二次聲明

else if (snake.getFirst().x >= windowWidth/10) 

因爲windowWidth/1080snake.getFirst().x成爲800。希望這會幫助你看到你的邏輯可能出現的錯誤。

+1

+1爲自我教學方法 – Tofandel