Snake Game — Can't steer snake



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















Actual question on the bottom of the post!


At first, I want to explain my problem.



I'm writing a basic Snake game and I got the snake to move automatically. It moves automatically to the right of the window when you execute the code, just like intended. However, I can't steer my snake the way I want, it doesn't change its direction at all.




To avoid confusion, player is an instance of the class Snake.




To explain the movement of the snake:



The Snake object has a coordinates property which is an ArrayList holding SnakePart objects. Each SnakePart object has the property x and y. Using this ArrayList, the snake is moving by drawing little rectangles on a canvas by using the x and y properties on the y- and x-axis of the canvas.



The Snake object also has a dx and a dy property that gets added (or subtracted -- depending on the direction of the snake) to the x and y property of the SnakePart object to move the snake in a direction.




To update the ArrayList in Snake.java:



public void move() 
SnakePart head = new SnakePart(this.coordinates.get(0).x + this.dx, this.coordinates.get(0).y + this.dy);

this.coordinates.add(0, head);
this.coordinates.remove(this.coordinates.size() - 1);




To draw the snake on the canvas in Board.java (partly, rest of the method is not necessary for now):



@Override
public void paintComponent(Graphics g)
this.player.coordinates.forEach(snakePart ->
g.setColor(Color.BLUE);
g.fillRect(snakePart.x, snakePart.y, 10, 10);
);




To steer the snake, I want to use the arrow keys. Depending on which arrow key is pressed, the snake's x and y coordinates/properties get modified (Board.java):



@Override
public void keyPressed(KeyEvent event)
int keyCode = event.getKeyCode();

if (keyCode == 37)
this.player.dx = -10;
this.player.dy = 0;
else if (keyCode == 38)
this.player.dx = 0;
this.player.dy = -10;
else if (keyCode == 39)
this.player.dx = 10;
this.player.dy = 0;
else if (keyCode == 40)
this.player.dx = 0;
this.player.dy = 10;





Whole code:



Snake.java:



package com.codef0x.snake;
import java.util.ArrayList;

public class Snake
ArrayList < SnakePart > coordinates;
int dx = 10;
int dy = 0;

public Snake(ArrayList < SnakePart > coords)
this.coordinates = coords;


public void move()
SnakePart head = new SnakePart(this.coordinates.get(0).x + this.dx, this.coordinates.get(0).y + this.dy);

this.coordinates.add(0, head);
this.coordinates.remove(this.coordinates.size() - 1);


public void grow()
SnakePart newPart = new SnakePart(0, 0);
newPart.x = this.coordinates.get(this.coordinates.size() - 1).x - 10;
newPart.y = this.coordinates.get(this.coordinates.size() - 1).y;

this.coordinates.add(this.coordinates.size() - 1, newPart);




Board.java (showing only relevant parts, otherwise it would be too much code)



package com.codef0x.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;


public class Board extends JPanel implements KeyListener
Snake player;
ArrayList<SnakePart> snakeCoordinates;

public Board()

this.snakeCoordinates = new ArrayList<>();

snakeCoordinates.add(new SnakePart(150, 150));
snakeCoordinates.add(new SnakePart(140, 150));
snakeCoordinates.add(new SnakePart(130, 150));
snakeCoordinates.add(new SnakePart(120, 150));

this.player = new Snake(snakeCoordinates);

this.food = new Food();


@Override
public void paintComponent(Graphics g)
clear(g);

this.player.coordinates.forEach(snakePart - >
g.setColor(Color.BLUE);
g.fillRect(snakePart.x, snakePart.y, 10, 10);
);


public void clear(Graphics g)
g.clearRect(0, 0, getHeight(), getWidth());


@Override
public void update(Graphics g)
paintComponent(g);


@Override
public void keyPressed(KeyEvent event)
int keyCode = event.getKeyCode();

if (keyCode == 37)
this.player.dx = -10;
this.player.dy = 0;
else if (keyCode == 38)
this.player.dx = 0;
this.player.dy = -10;
else if (keyCode == 39)
this.player.dx = 10;
this.player.dy = 0;
else if (keyCode == 40)
this.player.dx = 0;
this.player.dy = 10;



@Override
public void keyTyped(KeyEvent event)
return;


@Override
public void keyReleased(KeyEvent event)
return;


public void run(Board board)
Timer game = new Timer();
game.schedule(new TimerTask()
boolean initiallySpawned = false;
@Override
public void run()
Graphics g = board.getGraphics();

if (hitSomething()) // removed method hitSomething, not relevant
game.cancel();
return;


player.move();
update(g);

, 0, 500);




SnakePart.java:



package com.codef0x.snake;

public class SnakePart
int x;
int y;

public SnakePart(int x, int y)
this.x = x;
this.y = y;





What am I doing wrong and what do I need to change to steer the snake properly?




In case you still want / need to see all files as a whole, you can have a look at them here:



Snake.java

Board.java

SnakePart.java

Food.java <- Not related, but may prevent confusion about the Food object










share|improve this question
























  • What behavior are you seeing? Does the snake respond at all? Have you checked to make sure that the keyPressed event is firing like you think it is? I would put System.out.println() statements in your movement code and validate that as you press keys it's flowing through the codepaths that you expect.

    – Jazzepi
    Nov 15 '18 at 14:56











  • I would do key-typed or released rather than just key-pressed, but have you tried just printing or otherwise debugging the grow method to see why dx is always moving "to the right"?

    – cricket_007
    Nov 15 '18 at 14:57












  • @Jazzepi I've used System.out.println(this.player.dx + " " + this.player.dx) to debug, but only in the keyPressed method where everything looked fine. Now I did it again, but this time I also added a System.out.println(this.dx + " " + this.dy) to the move() method in Snake.java. In keyPressed the outputs look like expected (after pressing key up dx went from 10 to 0 and dy from 0 to -10). But in move() dx is still 10 and dy still 0...

    – CodeF0x
    Nov 15 '18 at 15:13











  • @cricket_007 I'm sure the grow() method is not related to the problem because dx is sopposed to be moving to the right all the time, until changed via the arrow keys what doesn't work.

    – CodeF0x
    Nov 15 '18 at 15:15






  • 1





    Don't use a KeyListener. See Motion Using the Keyboard

    – camickr
    Nov 15 '18 at 16:00

















2















Actual question on the bottom of the post!


At first, I want to explain my problem.



I'm writing a basic Snake game and I got the snake to move automatically. It moves automatically to the right of the window when you execute the code, just like intended. However, I can't steer my snake the way I want, it doesn't change its direction at all.




To avoid confusion, player is an instance of the class Snake.




To explain the movement of the snake:



The Snake object has a coordinates property which is an ArrayList holding SnakePart objects. Each SnakePart object has the property x and y. Using this ArrayList, the snake is moving by drawing little rectangles on a canvas by using the x and y properties on the y- and x-axis of the canvas.



The Snake object also has a dx and a dy property that gets added (or subtracted -- depending on the direction of the snake) to the x and y property of the SnakePart object to move the snake in a direction.




To update the ArrayList in Snake.java:



public void move() 
SnakePart head = new SnakePart(this.coordinates.get(0).x + this.dx, this.coordinates.get(0).y + this.dy);

this.coordinates.add(0, head);
this.coordinates.remove(this.coordinates.size() - 1);




To draw the snake on the canvas in Board.java (partly, rest of the method is not necessary for now):



@Override
public void paintComponent(Graphics g)
this.player.coordinates.forEach(snakePart ->
g.setColor(Color.BLUE);
g.fillRect(snakePart.x, snakePart.y, 10, 10);
);




To steer the snake, I want to use the arrow keys. Depending on which arrow key is pressed, the snake's x and y coordinates/properties get modified (Board.java):



@Override
public void keyPressed(KeyEvent event)
int keyCode = event.getKeyCode();

if (keyCode == 37)
this.player.dx = -10;
this.player.dy = 0;
else if (keyCode == 38)
this.player.dx = 0;
this.player.dy = -10;
else if (keyCode == 39)
this.player.dx = 10;
this.player.dy = 0;
else if (keyCode == 40)
this.player.dx = 0;
this.player.dy = 10;





Whole code:



Snake.java:



package com.codef0x.snake;
import java.util.ArrayList;

public class Snake
ArrayList < SnakePart > coordinates;
int dx = 10;
int dy = 0;

public Snake(ArrayList < SnakePart > coords)
this.coordinates = coords;


public void move()
SnakePart head = new SnakePart(this.coordinates.get(0).x + this.dx, this.coordinates.get(0).y + this.dy);

this.coordinates.add(0, head);
this.coordinates.remove(this.coordinates.size() - 1);


public void grow()
SnakePart newPart = new SnakePart(0, 0);
newPart.x = this.coordinates.get(this.coordinates.size() - 1).x - 10;
newPart.y = this.coordinates.get(this.coordinates.size() - 1).y;

this.coordinates.add(this.coordinates.size() - 1, newPart);




Board.java (showing only relevant parts, otherwise it would be too much code)



package com.codef0x.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;


public class Board extends JPanel implements KeyListener
Snake player;
ArrayList<SnakePart> snakeCoordinates;

public Board()

this.snakeCoordinates = new ArrayList<>();

snakeCoordinates.add(new SnakePart(150, 150));
snakeCoordinates.add(new SnakePart(140, 150));
snakeCoordinates.add(new SnakePart(130, 150));
snakeCoordinates.add(new SnakePart(120, 150));

this.player = new Snake(snakeCoordinates);

this.food = new Food();


@Override
public void paintComponent(Graphics g)
clear(g);

this.player.coordinates.forEach(snakePart - >
g.setColor(Color.BLUE);
g.fillRect(snakePart.x, snakePart.y, 10, 10);
);


public void clear(Graphics g)
g.clearRect(0, 0, getHeight(), getWidth());


@Override
public void update(Graphics g)
paintComponent(g);


@Override
public void keyPressed(KeyEvent event)
int keyCode = event.getKeyCode();

if (keyCode == 37)
this.player.dx = -10;
this.player.dy = 0;
else if (keyCode == 38)
this.player.dx = 0;
this.player.dy = -10;
else if (keyCode == 39)
this.player.dx = 10;
this.player.dy = 0;
else if (keyCode == 40)
this.player.dx = 0;
this.player.dy = 10;



@Override
public void keyTyped(KeyEvent event)
return;


@Override
public void keyReleased(KeyEvent event)
return;


public void run(Board board)
Timer game = new Timer();
game.schedule(new TimerTask()
boolean initiallySpawned = false;
@Override
public void run()
Graphics g = board.getGraphics();

if (hitSomething()) // removed method hitSomething, not relevant
game.cancel();
return;


player.move();
update(g);

, 0, 500);




SnakePart.java:



package com.codef0x.snake;

public class SnakePart
int x;
int y;

public SnakePart(int x, int y)
this.x = x;
this.y = y;





What am I doing wrong and what do I need to change to steer the snake properly?




In case you still want / need to see all files as a whole, you can have a look at them here:



Snake.java

Board.java

SnakePart.java

Food.java <- Not related, but may prevent confusion about the Food object










share|improve this question
























  • What behavior are you seeing? Does the snake respond at all? Have you checked to make sure that the keyPressed event is firing like you think it is? I would put System.out.println() statements in your movement code and validate that as you press keys it's flowing through the codepaths that you expect.

    – Jazzepi
    Nov 15 '18 at 14:56











  • I would do key-typed or released rather than just key-pressed, but have you tried just printing or otherwise debugging the grow method to see why dx is always moving "to the right"?

    – cricket_007
    Nov 15 '18 at 14:57












  • @Jazzepi I've used System.out.println(this.player.dx + " " + this.player.dx) to debug, but only in the keyPressed method where everything looked fine. Now I did it again, but this time I also added a System.out.println(this.dx + " " + this.dy) to the move() method in Snake.java. In keyPressed the outputs look like expected (after pressing key up dx went from 10 to 0 and dy from 0 to -10). But in move() dx is still 10 and dy still 0...

    – CodeF0x
    Nov 15 '18 at 15:13











  • @cricket_007 I'm sure the grow() method is not related to the problem because dx is sopposed to be moving to the right all the time, until changed via the arrow keys what doesn't work.

    – CodeF0x
    Nov 15 '18 at 15:15






  • 1





    Don't use a KeyListener. See Motion Using the Keyboard

    – camickr
    Nov 15 '18 at 16:00













2












2








2








Actual question on the bottom of the post!


At first, I want to explain my problem.



I'm writing a basic Snake game and I got the snake to move automatically. It moves automatically to the right of the window when you execute the code, just like intended. However, I can't steer my snake the way I want, it doesn't change its direction at all.




To avoid confusion, player is an instance of the class Snake.




To explain the movement of the snake:



The Snake object has a coordinates property which is an ArrayList holding SnakePart objects. Each SnakePart object has the property x and y. Using this ArrayList, the snake is moving by drawing little rectangles on a canvas by using the x and y properties on the y- and x-axis of the canvas.



The Snake object also has a dx and a dy property that gets added (or subtracted -- depending on the direction of the snake) to the x and y property of the SnakePart object to move the snake in a direction.




To update the ArrayList in Snake.java:



public void move() 
SnakePart head = new SnakePart(this.coordinates.get(0).x + this.dx, this.coordinates.get(0).y + this.dy);

this.coordinates.add(0, head);
this.coordinates.remove(this.coordinates.size() - 1);




To draw the snake on the canvas in Board.java (partly, rest of the method is not necessary for now):



@Override
public void paintComponent(Graphics g)
this.player.coordinates.forEach(snakePart ->
g.setColor(Color.BLUE);
g.fillRect(snakePart.x, snakePart.y, 10, 10);
);




To steer the snake, I want to use the arrow keys. Depending on which arrow key is pressed, the snake's x and y coordinates/properties get modified (Board.java):



@Override
public void keyPressed(KeyEvent event)
int keyCode = event.getKeyCode();

if (keyCode == 37)
this.player.dx = -10;
this.player.dy = 0;
else if (keyCode == 38)
this.player.dx = 0;
this.player.dy = -10;
else if (keyCode == 39)
this.player.dx = 10;
this.player.dy = 0;
else if (keyCode == 40)
this.player.dx = 0;
this.player.dy = 10;





Whole code:



Snake.java:



package com.codef0x.snake;
import java.util.ArrayList;

public class Snake
ArrayList < SnakePart > coordinates;
int dx = 10;
int dy = 0;

public Snake(ArrayList < SnakePart > coords)
this.coordinates = coords;


public void move()
SnakePart head = new SnakePart(this.coordinates.get(0).x + this.dx, this.coordinates.get(0).y + this.dy);

this.coordinates.add(0, head);
this.coordinates.remove(this.coordinates.size() - 1);


public void grow()
SnakePart newPart = new SnakePart(0, 0);
newPart.x = this.coordinates.get(this.coordinates.size() - 1).x - 10;
newPart.y = this.coordinates.get(this.coordinates.size() - 1).y;

this.coordinates.add(this.coordinates.size() - 1, newPart);




Board.java (showing only relevant parts, otherwise it would be too much code)



package com.codef0x.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;


public class Board extends JPanel implements KeyListener
Snake player;
ArrayList<SnakePart> snakeCoordinates;

public Board()

this.snakeCoordinates = new ArrayList<>();

snakeCoordinates.add(new SnakePart(150, 150));
snakeCoordinates.add(new SnakePart(140, 150));
snakeCoordinates.add(new SnakePart(130, 150));
snakeCoordinates.add(new SnakePart(120, 150));

this.player = new Snake(snakeCoordinates);

this.food = new Food();


@Override
public void paintComponent(Graphics g)
clear(g);

this.player.coordinates.forEach(snakePart - >
g.setColor(Color.BLUE);
g.fillRect(snakePart.x, snakePart.y, 10, 10);
);


public void clear(Graphics g)
g.clearRect(0, 0, getHeight(), getWidth());


@Override
public void update(Graphics g)
paintComponent(g);


@Override
public void keyPressed(KeyEvent event)
int keyCode = event.getKeyCode();

if (keyCode == 37)
this.player.dx = -10;
this.player.dy = 0;
else if (keyCode == 38)
this.player.dx = 0;
this.player.dy = -10;
else if (keyCode == 39)
this.player.dx = 10;
this.player.dy = 0;
else if (keyCode == 40)
this.player.dx = 0;
this.player.dy = 10;



@Override
public void keyTyped(KeyEvent event)
return;


@Override
public void keyReleased(KeyEvent event)
return;


public void run(Board board)
Timer game = new Timer();
game.schedule(new TimerTask()
boolean initiallySpawned = false;
@Override
public void run()
Graphics g = board.getGraphics();

if (hitSomething()) // removed method hitSomething, not relevant
game.cancel();
return;


player.move();
update(g);

, 0, 500);




SnakePart.java:



package com.codef0x.snake;

public class SnakePart
int x;
int y;

public SnakePart(int x, int y)
this.x = x;
this.y = y;





What am I doing wrong and what do I need to change to steer the snake properly?




In case you still want / need to see all files as a whole, you can have a look at them here:



Snake.java

Board.java

SnakePart.java

Food.java <- Not related, but may prevent confusion about the Food object










share|improve this question
















Actual question on the bottom of the post!


At first, I want to explain my problem.



I'm writing a basic Snake game and I got the snake to move automatically. It moves automatically to the right of the window when you execute the code, just like intended. However, I can't steer my snake the way I want, it doesn't change its direction at all.




To avoid confusion, player is an instance of the class Snake.




To explain the movement of the snake:



The Snake object has a coordinates property which is an ArrayList holding SnakePart objects. Each SnakePart object has the property x and y. Using this ArrayList, the snake is moving by drawing little rectangles on a canvas by using the x and y properties on the y- and x-axis of the canvas.



The Snake object also has a dx and a dy property that gets added (or subtracted -- depending on the direction of the snake) to the x and y property of the SnakePart object to move the snake in a direction.




To update the ArrayList in Snake.java:



public void move() 
SnakePart head = new SnakePart(this.coordinates.get(0).x + this.dx, this.coordinates.get(0).y + this.dy);

this.coordinates.add(0, head);
this.coordinates.remove(this.coordinates.size() - 1);




To draw the snake on the canvas in Board.java (partly, rest of the method is not necessary for now):



@Override
public void paintComponent(Graphics g)
this.player.coordinates.forEach(snakePart ->
g.setColor(Color.BLUE);
g.fillRect(snakePart.x, snakePart.y, 10, 10);
);




To steer the snake, I want to use the arrow keys. Depending on which arrow key is pressed, the snake's x and y coordinates/properties get modified (Board.java):



@Override
public void keyPressed(KeyEvent event)
int keyCode = event.getKeyCode();

if (keyCode == 37)
this.player.dx = -10;
this.player.dy = 0;
else if (keyCode == 38)
this.player.dx = 0;
this.player.dy = -10;
else if (keyCode == 39)
this.player.dx = 10;
this.player.dy = 0;
else if (keyCode == 40)
this.player.dx = 0;
this.player.dy = 10;





Whole code:



Snake.java:



package com.codef0x.snake;
import java.util.ArrayList;

public class Snake
ArrayList < SnakePart > coordinates;
int dx = 10;
int dy = 0;

public Snake(ArrayList < SnakePart > coords)
this.coordinates = coords;


public void move()
SnakePart head = new SnakePart(this.coordinates.get(0).x + this.dx, this.coordinates.get(0).y + this.dy);

this.coordinates.add(0, head);
this.coordinates.remove(this.coordinates.size() - 1);


public void grow()
SnakePart newPart = new SnakePart(0, 0);
newPart.x = this.coordinates.get(this.coordinates.size() - 1).x - 10;
newPart.y = this.coordinates.get(this.coordinates.size() - 1).y;

this.coordinates.add(this.coordinates.size() - 1, newPart);




Board.java (showing only relevant parts, otherwise it would be too much code)



package com.codef0x.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;


public class Board extends JPanel implements KeyListener
Snake player;
ArrayList<SnakePart> snakeCoordinates;

public Board()

this.snakeCoordinates = new ArrayList<>();

snakeCoordinates.add(new SnakePart(150, 150));
snakeCoordinates.add(new SnakePart(140, 150));
snakeCoordinates.add(new SnakePart(130, 150));
snakeCoordinates.add(new SnakePart(120, 150));

this.player = new Snake(snakeCoordinates);

this.food = new Food();


@Override
public void paintComponent(Graphics g)
clear(g);

this.player.coordinates.forEach(snakePart - >
g.setColor(Color.BLUE);
g.fillRect(snakePart.x, snakePart.y, 10, 10);
);


public void clear(Graphics g)
g.clearRect(0, 0, getHeight(), getWidth());


@Override
public void update(Graphics g)
paintComponent(g);


@Override
public void keyPressed(KeyEvent event)
int keyCode = event.getKeyCode();

if (keyCode == 37)
this.player.dx = -10;
this.player.dy = 0;
else if (keyCode == 38)
this.player.dx = 0;
this.player.dy = -10;
else if (keyCode == 39)
this.player.dx = 10;
this.player.dy = 0;
else if (keyCode == 40)
this.player.dx = 0;
this.player.dy = 10;



@Override
public void keyTyped(KeyEvent event)
return;


@Override
public void keyReleased(KeyEvent event)
return;


public void run(Board board)
Timer game = new Timer();
game.schedule(new TimerTask()
boolean initiallySpawned = false;
@Override
public void run()
Graphics g = board.getGraphics();

if (hitSomething()) // removed method hitSomething, not relevant
game.cancel();
return;


player.move();
update(g);

, 0, 500);




SnakePart.java:



package com.codef0x.snake;

public class SnakePart
int x;
int y;

public SnakePart(int x, int y)
this.x = x;
this.y = y;





What am I doing wrong and what do I need to change to steer the snake properly?




In case you still want / need to see all files as a whole, you can have a look at them here:



Snake.java

Board.java

SnakePart.java

Food.java <- Not related, but may prevent confusion about the Food object







java swing awt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:40









Sedrick

6,72832340




6,72832340










asked Nov 15 '18 at 14:50









CodeF0xCodeF0x

2,03951021




2,03951021












  • What behavior are you seeing? Does the snake respond at all? Have you checked to make sure that the keyPressed event is firing like you think it is? I would put System.out.println() statements in your movement code and validate that as you press keys it's flowing through the codepaths that you expect.

    – Jazzepi
    Nov 15 '18 at 14:56











  • I would do key-typed or released rather than just key-pressed, but have you tried just printing or otherwise debugging the grow method to see why dx is always moving "to the right"?

    – cricket_007
    Nov 15 '18 at 14:57












  • @Jazzepi I've used System.out.println(this.player.dx + " " + this.player.dx) to debug, but only in the keyPressed method where everything looked fine. Now I did it again, but this time I also added a System.out.println(this.dx + " " + this.dy) to the move() method in Snake.java. In keyPressed the outputs look like expected (after pressing key up dx went from 10 to 0 and dy from 0 to -10). But in move() dx is still 10 and dy still 0...

    – CodeF0x
    Nov 15 '18 at 15:13











  • @cricket_007 I'm sure the grow() method is not related to the problem because dx is sopposed to be moving to the right all the time, until changed via the arrow keys what doesn't work.

    – CodeF0x
    Nov 15 '18 at 15:15






  • 1





    Don't use a KeyListener. See Motion Using the Keyboard

    – camickr
    Nov 15 '18 at 16:00

















  • What behavior are you seeing? Does the snake respond at all? Have you checked to make sure that the keyPressed event is firing like you think it is? I would put System.out.println() statements in your movement code and validate that as you press keys it's flowing through the codepaths that you expect.

    – Jazzepi
    Nov 15 '18 at 14:56











  • I would do key-typed or released rather than just key-pressed, but have you tried just printing or otherwise debugging the grow method to see why dx is always moving "to the right"?

    – cricket_007
    Nov 15 '18 at 14:57












  • @Jazzepi I've used System.out.println(this.player.dx + " " + this.player.dx) to debug, but only in the keyPressed method where everything looked fine. Now I did it again, but this time I also added a System.out.println(this.dx + " " + this.dy) to the move() method in Snake.java. In keyPressed the outputs look like expected (after pressing key up dx went from 10 to 0 and dy from 0 to -10). But in move() dx is still 10 and dy still 0...

    – CodeF0x
    Nov 15 '18 at 15:13











  • @cricket_007 I'm sure the grow() method is not related to the problem because dx is sopposed to be moving to the right all the time, until changed via the arrow keys what doesn't work.

    – CodeF0x
    Nov 15 '18 at 15:15






  • 1





    Don't use a KeyListener. See Motion Using the Keyboard

    – camickr
    Nov 15 '18 at 16:00
















What behavior are you seeing? Does the snake respond at all? Have you checked to make sure that the keyPressed event is firing like you think it is? I would put System.out.println() statements in your movement code and validate that as you press keys it's flowing through the codepaths that you expect.

– Jazzepi
Nov 15 '18 at 14:56





What behavior are you seeing? Does the snake respond at all? Have you checked to make sure that the keyPressed event is firing like you think it is? I would put System.out.println() statements in your movement code and validate that as you press keys it's flowing through the codepaths that you expect.

– Jazzepi
Nov 15 '18 at 14:56













I would do key-typed or released rather than just key-pressed, but have you tried just printing or otherwise debugging the grow method to see why dx is always moving "to the right"?

– cricket_007
Nov 15 '18 at 14:57






I would do key-typed or released rather than just key-pressed, but have you tried just printing or otherwise debugging the grow method to see why dx is always moving "to the right"?

– cricket_007
Nov 15 '18 at 14:57














@Jazzepi I've used System.out.println(this.player.dx + " " + this.player.dx) to debug, but only in the keyPressed method where everything looked fine. Now I did it again, but this time I also added a System.out.println(this.dx + " " + this.dy) to the move() method in Snake.java. In keyPressed the outputs look like expected (after pressing key up dx went from 10 to 0 and dy from 0 to -10). But in move() dx is still 10 and dy still 0...

– CodeF0x
Nov 15 '18 at 15:13





@Jazzepi I've used System.out.println(this.player.dx + " " + this.player.dx) to debug, but only in the keyPressed method where everything looked fine. Now I did it again, but this time I also added a System.out.println(this.dx + " " + this.dy) to the move() method in Snake.java. In keyPressed the outputs look like expected (after pressing key up dx went from 10 to 0 and dy from 0 to -10). But in move() dx is still 10 and dy still 0...

– CodeF0x
Nov 15 '18 at 15:13













@cricket_007 I'm sure the grow() method is not related to the problem because dx is sopposed to be moving to the right all the time, until changed via the arrow keys what doesn't work.

– CodeF0x
Nov 15 '18 at 15:15





@cricket_007 I'm sure the grow() method is not related to the problem because dx is sopposed to be moving to the right all the time, until changed via the arrow keys what doesn't work.

– CodeF0x
Nov 15 '18 at 15:15




1




1





Don't use a KeyListener. See Motion Using the Keyboard

– camickr
Nov 15 '18 at 16:00





Don't use a KeyListener. See Motion Using the Keyboard

– camickr
Nov 15 '18 at 16:00












1 Answer
1






active

oldest

votes


















2














The problem is in the main.
You create a board to host your game status, and create different one to listen to the keyboard.



public static void main(String args) 

JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(3);

Board board = new Board();
frame.add(board);
frame.setSize(500, 500);
frame.addKeyListener(new Board());
frame.setVisible(true);

board.run(board);



it should be:



public static void main(String args) 

JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(3);

Board board = new Board();
frame.add(board);
frame.setSize(500, 500);
frame.addKeyListener(board);
frame.setVisible(true);


board.run(board);



Also board.run(board) has little sense, in the scope of run method, board can be swapped to this (and so omitted) ...






share|improve this answer

























  • That's it, thanks!

    – CodeF0x
    Nov 16 '18 at 7:58











  • @minus Nice catch!

    – Jazzepi
    Nov 21 '18 at 19:41











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322069%2fsnake-game-cant-steer-snake%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














The problem is in the main.
You create a board to host your game status, and create different one to listen to the keyboard.



public static void main(String args) 

JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(3);

Board board = new Board();
frame.add(board);
frame.setSize(500, 500);
frame.addKeyListener(new Board());
frame.setVisible(true);

board.run(board);



it should be:



public static void main(String args) 

JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(3);

Board board = new Board();
frame.add(board);
frame.setSize(500, 500);
frame.addKeyListener(board);
frame.setVisible(true);


board.run(board);



Also board.run(board) has little sense, in the scope of run method, board can be swapped to this (and so omitted) ...






share|improve this answer

























  • That's it, thanks!

    – CodeF0x
    Nov 16 '18 at 7:58











  • @minus Nice catch!

    – Jazzepi
    Nov 21 '18 at 19:41















2














The problem is in the main.
You create a board to host your game status, and create different one to listen to the keyboard.



public static void main(String args) 

JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(3);

Board board = new Board();
frame.add(board);
frame.setSize(500, 500);
frame.addKeyListener(new Board());
frame.setVisible(true);

board.run(board);



it should be:



public static void main(String args) 

JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(3);

Board board = new Board();
frame.add(board);
frame.setSize(500, 500);
frame.addKeyListener(board);
frame.setVisible(true);


board.run(board);



Also board.run(board) has little sense, in the scope of run method, board can be swapped to this (and so omitted) ...






share|improve this answer

























  • That's it, thanks!

    – CodeF0x
    Nov 16 '18 at 7:58











  • @minus Nice catch!

    – Jazzepi
    Nov 21 '18 at 19:41













2












2








2







The problem is in the main.
You create a board to host your game status, and create different one to listen to the keyboard.



public static void main(String args) 

JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(3);

Board board = new Board();
frame.add(board);
frame.setSize(500, 500);
frame.addKeyListener(new Board());
frame.setVisible(true);

board.run(board);



it should be:



public static void main(String args) 

JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(3);

Board board = new Board();
frame.add(board);
frame.setSize(500, 500);
frame.addKeyListener(board);
frame.setVisible(true);


board.run(board);



Also board.run(board) has little sense, in the scope of run method, board can be swapped to this (and so omitted) ...






share|improve this answer















The problem is in the main.
You create a board to host your game status, and create different one to listen to the keyboard.



public static void main(String args) 

JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(3);

Board board = new Board();
frame.add(board);
frame.setSize(500, 500);
frame.addKeyListener(new Board());
frame.setVisible(true);

board.run(board);



it should be:



public static void main(String args) 

JFrame frame = new JFrame("Snake");
frame.setDefaultCloseOperation(3);

Board board = new Board();
frame.add(board);
frame.setSize(500, 500);
frame.addKeyListener(board);
frame.setVisible(true);


board.run(board);



Also board.run(board) has little sense, in the scope of run method, board can be swapped to this (and so omitted) ...







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 15:47

























answered Nov 15 '18 at 15:34









minusminus

1,8511113




1,8511113












  • That's it, thanks!

    – CodeF0x
    Nov 16 '18 at 7:58











  • @minus Nice catch!

    – Jazzepi
    Nov 21 '18 at 19:41

















  • That's it, thanks!

    – CodeF0x
    Nov 16 '18 at 7:58











  • @minus Nice catch!

    – Jazzepi
    Nov 21 '18 at 19:41
















That's it, thanks!

– CodeF0x
Nov 16 '18 at 7:58





That's it, thanks!

– CodeF0x
Nov 16 '18 at 7:58













@minus Nice catch!

– Jazzepi
Nov 21 '18 at 19:41





@minus Nice catch!

– Jazzepi
Nov 21 '18 at 19:41



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322069%2fsnake-game-cant-steer-snake%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20