Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

swing - Java snake game

Well starting off i must admit there are countless snake questions out there, though every Programmer even if newbie or expert writes his code differently so i decided to open another one case,

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;

public class Game1 extends JFrame {

float x;
float y;
int[][] tileKati = new int[30][30];
int keyCode;
int body;
boolean Right = true;
boolean Left = false;
boolean Up = false;
boolean Down = false;
boolean Running = false;


public class AL extends KeyAdapter{

    public void keyPressed(KeyEvent e){

         keyCode = e.getKeyCode();

        if((keyCode == e.VK_LEFT) && (!Right)){
            Left=true;
            Down=false;
            Up=false;
            Running=true;
        }
        if((keyCode == e.VK_RIGHT) && (!Left)){
            Right=true;
            Down=false;
            Up=false;
            Running=true;
        }
        if((keyCode == e.VK_UP) && (!Down)){
            Up=true;
            Left=false;
            Right=false;
            Running=true;
        }
        if((keyCode == e.VK_DOWN) && (!Up)){
            Down=true;
            Left=false;
            Right=false;
            Running=true;
        }
        }

    public void keyReleased(KeyEvent e){

}
}
public Game1(){
    addKeyListener(new AL());
    setTitle("Snake");
    setSize(960,960);
    setResizable(false);
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    x=0;
    y=0;
    for(int x=0;x<30;x++){
        for(int y=0;y<30;y++){
        tileKati[x][y] = 0;
     }  

    }

}


public void paint(Graphics g)
{
    state();

    if(Running==true)
    {

    if(x<0)
    {

        x=30;
    }

    if(x>30)
    {
        x=0;    
    }

    if(y<0)
    {
        y=30;
    }

    if(y>30)
    {
        y=0;
    }

    }
    tileKati[(int)x][(int)y] = 1;
    for(int x=0;x<30;x++)
    {

        for(int y=0;y<30;y++)
        {

        if(tileKati[x][y] == 0)
        {
            g.setColor(Color.black);
            g.drawRect(x*32, y*32, 32, 32);
        }   
        if(tileKati[x][y]== 1)
        {
            g.setColor(Color.cyan);
            g.fillRect(x*32, y*32, 32, 32); 
        }

        }  

    }



    repaint();
}

public void move(){
    if(Right){
        x+= 0.01;

    }
    if(Left){
        x-=0.01;
    }
    if(Up){
        y-=0.01;

    }
    if(Down){
        y+=0.01;
    }}

public void state(){
    if(Running){
        move();


    }
    repaint();
}

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

}
}

So my question is as follows, i want to start painting my tiles and clearing the rest of the "map" when my snake exceed 3 tiles (x or y), so i can make a 3 tile snake running through the map then i think i can figure out the rest, thanks in advance.

Edit:No more broken code..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use a Stack or LinkedList for this. Now with every move of the snakes head you add its position (x,y) to the start of the list and remove the last element if there are more elements in the list as the snake length.

And while painting you first paint the background and then the lists elements a the snake.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...