-4

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..

  • 2
    That's not a question, that's a desire. – Paul Samsotha Jun 20 '14 at 10:10
  • Well yeah i wasn't clear sorry, i just want a hint on how to do this even if it's on pseudocode, i am stuck. – user3712275 Jun 20 '14 at 10:12
  • What did you do? Where is your try? – ikh Jun 20 '14 at 10:13
  • What are you stuck on specifically? – JonK Jun 20 '14 at 10:13
  • There's too much code here that isn't relevant to the issue. What specific part are you stuck on. What algorithm / block did you try for this and how is it not performing correctly? I'm lost why you would dump all the code, broken, without specifying what part is actually relevant to your question. – RossC Jun 20 '14 at 10:14
  • This is a printscreen if this can help http://prntscr.com/3uob6e. The Draw block is the false one. – user3712275 Jun 20 '14 at 10:14
  • Why would you post broken code that you haven't finished yet? Take out anything that doesn't work and/or isn't relevant to your problem. Also, you should not be calling `new Game1();` outside of the [Event Dispatch Thread](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html). – David Yee Jun 20 '14 at 10:15
  • Well sorry once again i will try to clarify here, i am running through a 2 dimensional array to run through each tile, i have user input to select a tile to paint, i don't know how to clear the tilemap EXCEPT the snake which in my case i want it to be 3 tiles. – user3712275 Jun 20 '14 at 10:20
  • `if newbie or expert writes his code differently` - yes, but most people don't make up programming conventions. Variable names should NOT start with an upper case character. Most of your variable names are correct but some are not. Be consistent!!! – camickr Jun 20 '14 at 14:02

1 Answers1

1

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.

Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41