repaint only happens at the end of my loop - java - java

basically I'm making TRON and my first step is to make a basic snake game and my first step is to make a square move down the screen and then implement key listeners. i want to see the square moving down at every stage stage but i just seem to get the final paint image, so i think it does move, i just don't see the frame being updated, can anyone help?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.*;
public class Multiplayer extends JPanel { //implements ActionListener {
public static Multiplayer multiplayer;
public static ArrayList<Point> bikeParts = new ArrayList<>();
public static final int UP = 0, DOWN = 1, LEFT = 2, RIGHT = 3, SCALE = 10;
public static int ticks = 0, direction = DOWN;
public static Point bike;
public boolean over = false;
public JFrame frame;
public renderPanel rp;
public Multiplayer() {
frame = new JFrame();
//JPanel panel = new JPanel();
rp = new renderPanel();
//add(panel, BorderLayout.CENTER);
frame.getContentPane().add(rp, BorderLayout.CENTER);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(900, 700);
frame.setVisible(true);
frame.setResizable(false);
bike = new Point(0, 0);
for (int i = 0; i < 1000; i++) {
animate(null);
rp.repaint();
}
}
//#Override
public void animate(ActionEvent ef) {
rp.repaint();
ticks++;
System.out.println("yw");
if (ticks % 10 == 0 && bike != null && over != true) {
System.out.println("did");
rp.repaint();
bikeParts.add(new Point(bike.x, bike.y));
if (direction == UP) {
if (bike.y - 1 > 0) {
bike = new Point(bike.x, bike.y - 1);
} else {
over = true;
}
}
if (direction == DOWN) {
if (bike.y + 1 < 700) {
bike = new Point(bike.x, bike.y + 1);
} else {
over = true;
}
}
if (direction == LEFT) {
if (bike.x - 1 > 0) {
bike = new Point(bike.x - 1, bike.y);
} else {
over = true;
}
bike = bikeParts.get(bikeParts.size() - 1);
bikeParts.remove(0);
}
if (direction == RIGHT) {
if (bike.x + 1 < 900) {
bike = new Point(bike.x + 1, bike.y);
} else {
over = true;
}
}
}
}
}
and then:
import javax.swing.*;
import java.awt.*;
public class renderPanel extends JComponent {
#Override
public void paintComponent(Graphics g) {
// super.paintComponent(g);
System.out.println("painting");
Multiplayer mp = Multiplayer.multiplayer;
g.setColor(Color.black);
g.drawRect(0, 0, 900, 700);
g.fillRect(0, 0, 900, 700); //arena
g.setColor(Color.WHITE);
for (Point point : mp.bikeParts) {
g.fillRect(point.x * Multiplayer.SCALE, point.y * Multiplayer.SCALE, Multiplayer.SCALE, Multiplayer.SCALE);
}
g.fillRect(mp.bike.x * Multiplayer.SCALE, mp.bike.y * Multiplayer.SCALE, Multiplayer.SCALE, Multiplayer.SCALE);
}
}

Related

Making a character land on a platform

Im trying to code a simple 2D game. I have a Square that you can move with a/w/d. I got jumping working with a sort of gravity but now I cant get myself to land on a platform. I know how to detect collision between two things but even still idk what to do. My gravity always pulls me down until I reach groundLevel which is part of the problem. Here is my code so far. There's a lot of experimenting happing so its pretty messy.
import javax.swing.Timer;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
/**
* Custom Graphics Example: Using key/button to move a line left or right.
*/
public class PlatformingGame extends JFrame implements ActionListener, KeyListener{
// Name-constants for the various dimensions
public static final int CANVAS_WIDTH = 800;
public static final int CANVAS_HEIGHT = 400;
public static final Color LINE_COLOR = Color.BLACK;
public static final Color CANVAS_BACKGROUND = Color.WHITE;
public int GRAVITY = 10;
public int TERMINAL_VELOCITY = 300;
public int vertical_speed = 0;
public int jumpSpeed;
public int groundLevel = CANVAS_HEIGHT;
int Shapes;
private DrawCanvas canvas; // the custom drawing canvas (extends JPanel)
public enum STATE {
PLAYING,
PAUSED,
ONGROUND,
INAIR
};
JButton btnStartRestat, btnExit;
Timer timer;
Rectangle2D.Double guy, platform, platform2;
Shape[] shapeArr = new Shape[10];
int dx, dy;
/** Constructor to set up the GUI */
ArrayList myKeys = new ArrayList<Character>();
public static STATE gameState = STATE.PAUSED;
//public static STATE playerState = STATE.ONGROUND;
public PlatformingGame() {
dx = 3;
dy = 3;
guy = new Rectangle2D.Double( CANVAS_WIDTH/2 - 20, CANVAS_HEIGHT, 30, 20);
platform2 = new Rectangle2D.Double( CANVAS_WIDTH/4, CANVAS_HEIGHT/2+130, 50, 10);
platform = new Rectangle2D.Double( CANVAS_WIDTH/3, CANVAS_HEIGHT/2+50, 50, 10);
timer = new Timer(10, this);
// Set up a panel for the buttons
JPanel btnPanel = new JPanel();
// btnPanel.setPreferredSize(new Dimension(CANVAS_WIDTH/2, CANVAS_HEIGHT));
btnPanel.setLayout(new FlowLayout());
btnStartRestat = new JButton("Start/Restart");
btnExit = new JButton("Exit");
btnPanel.add(btnStartRestat);
btnPanel.add(btnExit);
btnStartRestat.addActionListener(this);
btnExit.addActionListener(this);
// Set up a custom drawing JPanel
canvas = new DrawCanvas();
canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
// Add both panels to this JFrame
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(canvas, BorderLayout.CENTER);
cp.add(btnPanel, BorderLayout.SOUTH);
// "this" JFrame fires KeyEvent
addKeyListener(this);
requestFocus(); // set the focus to JFrame to receive KeyEvent
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Handle the CLOSE button
pack(); // pack all the components in the JFrame
setVisible(true); // show it
}
public void generateSpikes(){
Rectangle2D.Double spikes = null;
for (int i = 0; i < 10; i++) {
spikes = new Rectangle2D.Double (CANVAS_WIDTH - 300 + i*20 , CANVAS_HEIGHT - 30 , 20, 30);
shapeArr[i] = spikes;
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==btnStartRestat)
{
generateSpikes();
dx = 3;
dy = 3;
guy = new Rectangle2D.Double( 100, CANVAS_HEIGHT - guy.height, 15, 30);
platform = new Rectangle2D.Double( CANVAS_WIDTH/3, CANVAS_HEIGHT/2+50, 50, 10);
platform2 = new Rectangle2D.Double( CANVAS_WIDTH/4, CANVAS_HEIGHT/2+130, 50, 10);
gameState = STATE.PLAYING;
}
else if (e.getSource()==btnExit)
{
// requestFocus(); // change the focus to JFrame to receive KeyEvent
}
else if (e.getSource()== timer){
if (isHitDetected(platform2, guy)){
// playerState = STATE.ONGROUND;
guy.y = platform2.y;
}
if (myKeys.contains('a')
&& (guy.x > 0)){
guy.x = guy.x - 5; }
if (myKeys.contains('d')
&& (guy.x < CANVAS_WIDTH - guy.width)){
guy.x = guy.x + 5; }
{
updateGuyPosition();
}
requestFocus();
canvas.repaint();
}
}
public void updateGuyPosition(){
double guyHeight = guy.height;
if (guy.x >= CANVAS_WIDTH - guy.width){
}
if(gameState == STATE.PLAYING) {
guy.y += jumpSpeed;
if (guy.y < groundLevel - guyHeight) {
// if(playerState == STATE.INAIR) {
jumpSpeed += 1;
}
// }
else {
// if(playerState == STATE.INAIR) {
//playerState = STATE.ONGROUND;
jumpSpeed = 0;
guy.y = groundLevel - guyHeight;
}
// }
if (myKeys.contains('w') == true && guy.y == groundLevel - guyHeight) {
jumpSpeed = -15;
// playerState = STATE.INAIR;
}
}
}
public static boolean isHitDetected(Shape shapeA, Shape shapeB) {
Area areaA = new Area(shapeA);
areaA.intersect(new Area(shapeB));
return !areaA.isEmpty();
}
public void keyPressed(KeyEvent evt) {
if (!myKeys.contains(evt.getKeyChar())){
myKeys.add(evt.getKeyChar());
}
}
public void keyReleased(KeyEvent evt) {
myKeys.remove(myKeys.indexOf(evt.getKeyChar()));
}
public void keyTyped(KeyEvent evt) {
}
/** The entry main() method */
public static void main(String[] args) {
PlatformingGame myProg = new PlatformingGame(); // Let the constructor do the job
myProg.timer.start();
}
/**
* DrawCanvas (inner class) is a JPanel used for custom drawing
*/
class DrawCanvas extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(CANVAS_BACKGROUND);
g.setColor(LINE_COLOR);
Graphics2D g2d = (Graphics2D)g;
if(gameState == STATE.PLAYING) {
g2d.fill(guy);
g.setColor(Color.lightGray);
g2d.fill(platform);
g2d.fill(platform2);
for (int i = 0; i < 10; i++) {
g2d.fill(shapeArr[i]);
}
}
if(gameState == STATE.PAUSED) {
g.drawString("Game Paused", CANVAS_WIDTH/2, CANVAS_HEIGHT/2);
}
}
}
}

How do you make, that your Player in an Java2d Top Down Game alwayls looks towards the mouse

So I'm trying to awnser this question for like 2-3 hours now, but I can't quite find an fix or resolution for my problem. Like there are no Video tutorials. And because I am new to programming, especially with Java, I just don't know, how to rewrite code, that it matches my code and perfectly works. Here is what I have right now:
Also my project is all based on 2D and you only see your player for right above. So I dont really need Animation for the Player and Entity models, just in case you wondered.
Class: GamePanel
package main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import entity.Player;
public class GamePanel extends JPanel implements Runnable{
// SCREEN SETTINGS
final int originalTitleSize = 16; // 16x16 title
final int scale = 3; //16x3(scale) = 48
public final int tileSize = originalTitleSize * scale; //48x48 title
final int maxScreenCol = 16;
final int maxScreenRow = 12;
final int screenWidth = tileSize * maxScreenCol; // 768 pixels
final int screenHeight = tileSize * maxScreenRow; // 576 pixels
//FPS
int FPS = 60;
KeyHandler keyH = new KeyHandler();
Thread gameThread;
Player player = new Player(this,keyH);
// Set player's default position
int playerX = 100;
int playerY = 100;
int playerSpeed = 4;
public GamePanel() {
this.setPreferredSize(new Dimension(screenWidth, screenHeight));
this.setBackground(Color.BLACK);
this.setDoubleBuffered(true);
this.addKeyListener(keyH);
this.setFocusable(true);
}
public void startGameThread() {
gameThread = new Thread(this);
gameThread.start();
}
#Override
// public void run() {
//
// double drawInterval = 1000000000/FPS; // 0.0166666... seconds
// double nextDrawTime = System.nanoTime() + drawInterval;
//
//
//
// while(gameThread != null) {
//
// // System.out.println("The game loop is running");
//
// // 1 UPDATE: update information such as character positions
// update();
//
//
//
// // 2 DRAW: draw the screen with the updated information
// repaint();
//
// try {
// double remainingTime = nextDrawTime - System.nanoTime();
// remainingTime = remainingTime/1000000;
//
// if(remainingTime < 0) {
// remainingTime = 0;
// }
//
// Thread.sleep((long) remainingTime);
//
// nextDrawTime += drawInterval;
//
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// }
public void run() {
double drawInterval = 1000000000/FPS;
double delta = 0;
long lastTime = System.nanoTime();
long currentTime;
long timer = 0;
int drawCount = 0;
while(gameThread != null) {
currentTime =System.nanoTime();
delta += (currentTime - lastTime) / drawInterval;
timer += (currentTime -lastTime);
lastTime = currentTime;
if(delta >=1) {
update();
repaint();
delta--;
drawCount++;
}
if(timer >= 1000000000) {
System.out.println("FPS:" + drawCount);
drawCount = 0;
timer = 0;
}
}
}
public void update() {
player.update();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
player.draw(g2);
g2.dispose();
}
}
Class: KeyHandler
package main;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyHandler implements KeyListener{
public boolean upPressed, downPressed, leftPressed, rightPressed;
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if(code == KeyEvent.VK_W) {
upPressed = true;
}
if(code == KeyEvent.VK_S) {
downPressed = true;
}
if(code == KeyEvent.VK_A) {
leftPressed = true;
}
if(code == KeyEvent.VK_D) {
rightPressed = true;
}
}
#Override
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
if(code == KeyEvent.VK_W) {
upPressed = false;
}
if(code == KeyEvent.VK_S) {
downPressed = false;
}
if(code == KeyEvent.VK_A) {
leftPressed = false;
}
if(code == KeyEvent.VK_D) {
rightPressed = false;
}
}
}
Class: Main
package main;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.setTitle("Zombio 0.0.0.01");
GamePanel gamePanel = new GamePanel();
window.add(gamePanel);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
gamePanel.startGameThread();
}
}
I also have an Player:
package entity;
import java.awt.Color;
import java.awt.Graphics2D;
import main.GamePanel;
import main.KeyHandler;
public class Player extends Entity{
GamePanel gp;
KeyHandler keyH;
public Player(GamePanel gp, KeyHandler keyH) {
this.gp = gp;
this.keyH = keyH;
setDefaultValues();
}
public void setDefaultValues() {
x = 100;
y = 100;
speed = 4;
}
public void update() {
if(keyH.upPressed == true) {
y -= speed;
}
else if(keyH.downPressed == true) {
y += speed;
}
else if(keyH.leftPressed == true) {
x -= speed;
}
else if(keyH.rightPressed == true) {
x += speed;
}
}
public void draw(Graphics2D g2) {
g2.setColor(Color.yellow);
g2.fillRect(x, y, gp.tileSize, gp.tileSize); //(x, y, width, height)
}
}
And this Player is based on the normal-entity:
package entity;
public class Entity {
public int x, y;
public int speed;
}
I know, this is a lot you need to look through, but I really don't know, what exaclty you need. I would like to implement the rotation in the Player Class, if this is possible for you. Also please not only write Code and set it as awnser, I really have no expirience, so take my by the hand :)
Thanks for your help, appreciate it!
You will want to have a look at:
Key bindings - these will solve all the focus related issues with KeyListener
Concurrency in Swing and probably switch over to using a Swing Timer. Swing is NOT thread safe, it's not a good idea to use Thread as your "main loop".
So your basic problem is a "simple" trigonometry problem (I as say simple, but I'm an idiot). You have two points in space and need to calculate the angle between them, for example...
// Radians
-Math.atan2(startY - endY, startX - endX)
Runnable example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashSet;
import java.util.Set;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
GamePanel gamePanel = new GamePanel();
JFrame frame = new JFrame();
frame.add(gamePanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
gamePanel.startGameThread();
}
});
}
public class GamePanel extends JPanel { //implements Runnable {
// SCREEN SETTINGS
final int originalTitleSize = 16; // 16x16 title
final int scale = 3; //16x3(scale) = 48
public final int tileSize = originalTitleSize * scale; //48x48 title
final int maxScreenCol = 16;
final int maxScreenRow = 12;
final int screenWidth = tileSize * maxScreenCol; // 768 pixels
final int screenHeight = tileSize * maxScreenRow; // 576 pixels
//FPS
int FPS = 60;
Player player = new Player(this);
private Timer timer;
private Set<KeyAction.Direction> movementState = new HashSet<>();
private Point lastKnownMousePoint;
public GamePanel() {
this.setBackground(Color.BLACK);
addMouseMotionListener(new MouseAdapter() {
#Override
public void mouseMoved(MouseEvent e) {
lastKnownMousePoint = e.getPoint();
}
});
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, false), "Pressed.up");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, true), "Released.up");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, false), "Pressed.down");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, true), "Released.down");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "Pressed.left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), "Released.left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "Pressed.right");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "Released.right");
ActionMap am = getActionMap();
am.put("Pressed.up", new KeyAction(KeyAction.Direction.UP, true, movementState));
am.put("Released.up", new KeyAction(KeyAction.Direction.UP, false, movementState));
am.put("Pressed.down", new KeyAction(KeyAction.Direction.DOWN, true, movementState));
am.put("Released.down", new KeyAction(KeyAction.Direction.DOWN, false, movementState));
am.put("Pressed.left", new KeyAction(KeyAction.Direction.LEFT, true, movementState));
am.put("Released.left", new KeyAction(KeyAction.Direction.LEFT, false, movementState));
am.put("Pressed.right", new KeyAction(KeyAction.Direction.RIGHT, true, movementState));
am.put("Released.right", new KeyAction(KeyAction.Direction.RIGHT, false, movementState));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(screenWidth, screenHeight);
}
public void startGameThread() {
if (timer == null) {
timer = new Timer((int) Math.floor(1000f / FPS), new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
update();
repaint();
}
});
}
timer.start();
}
public void update() {
if (movementState.contains(KeyAction.Direction.UP)) {
player.y -= player.speed;
} else if (movementState.contains(KeyAction.Direction.DOWN)) {
player.y += player.speed;
} else if (movementState.contains(KeyAction.Direction.LEFT)) {
player.x -= player.speed;
} else if (movementState.contains(KeyAction.Direction.RIGHT)) {
player.x += player.speed;
}
if (lastKnownMousePoint != null) {
// This assumes that character is facing "UP" by default
// That is, 0 has the character entity facing towards to the
// top of the sceen. If the character is facing in a different
// direction, then you will need to offset this calculation
// to compenstate, but that might be better done in the player
// entity
double angle = -Math.toDegrees(Math.atan2(player.x - lastKnownMousePoint.x, player.y - lastKnownMousePoint.y));
player.angleInDegrees = angle;
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
player.draw(g2);
g2.dispose();
}
}
public class KeyAction extends AbstractAction {
enum Direction {
UP, DOWN, LEFT, RIGHT
}
private Direction direction;
private boolean activate;
private Set<Direction> inputState;
public KeyAction(Direction direction, boolean activate, Set<Direction> inputState) {
this.direction = direction;
this.activate = activate;
this.inputState = inputState;
}
#Override
public void actionPerformed(ActionEvent e) {
if (activate) {
inputState.add(direction);
} else {
inputState.remove(direction);
}
}
}
public class Entity {
public int x;
public int y;
public int speed;
}
public class Player extends Entity {
GamePanel gp;
double angleInDegrees = 0;
public Player(GamePanel gp) {
this.gp = gp;
setDefaultValues();
}
public void setDefaultValues() {
x = 100;
y = 100;
speed = 4;
}
public void draw(Graphics2D g2) {
g2 = (Graphics2D) g2.create();
g2.translate(x, y);
g2.rotate(Math.toRadians(angleInDegrees), (gp.tileSize / 2), (gp.tileSize / 2));
g2.setColor(Color.yellow);
g2.fillRect(0, 0, gp.tileSize, gp.tileSize);
g2.setColor(Color.RED);
g2.drawLine((gp.tileSize / 2), (gp.tileSize / 2), (gp.tileSize / 2), 0);
g2.dispose();
}
}
}
Oh, and also the rotation point is not right in the middle of the player (rectangle). Some dont need accurancy, I really do
Just beware, you're probably never going to find the "exact" solutions to your problems and you're going to need to take the time to experiment ;)
Okay, admittedly, that seemed to work for me, until I started debugging it. The problem was, the original code was calculating the angle from the mouse point and the players current x/y point. It should be using the players "rotation" point, which, in this example, is the players mid point.
So, I added...
public Point midPoint() {
return new Point(x + (gp.tileSize / 2), y + (gp.tileSize / 2));
}
to Player, so we can easily get the player's mid point (and not have to retype that a lot)
I then updated the angle calculation to make use of it, for example..
Point playerMidPoint = player.midPoint();
double angle = Math.toDegrees(Math.atan2(lastKnownMousePoint.y - playerMidPoint.y, lastKnownMousePoint.x - playerMidPoint.x)) + 90d;
player.angleInDegrees = angle;
Runnable example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.util.HashSet;
import java.util.Set;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
public class Main {
public static void main(String[] args) {
new Main();
}
public Main() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
GamePanel gamePanel = new GamePanel();
JFrame frame = new JFrame();
frame.add(gamePanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
gamePanel.startGameThread();
}
});
}
public class GamePanel extends JPanel { //implements Runnable {
// SCREEN SETTINGS
final int originalTitleSize = 16; // 16x16 title
final int scale = 3; //16x3(scale) = 48
public final int tileSize = originalTitleSize * scale; //48x48 title
final int maxScreenCol = 16;
final int maxScreenRow = 12;
final int screenWidth = tileSize * maxScreenCol; // 768 pixels
final int screenHeight = tileSize * maxScreenRow; // 576 pixels
//FPS
int FPS = 60;
Player player = new Player(this);
private Timer timer;
private Set<KeyAction.Direction> movementState = new HashSet<>();
private Point lastKnownMousePoint;
public GamePanel() {
this.setBackground(Color.BLACK);
addMouseMotionListener(new MouseAdapter() {
#Override
public void mouseMoved(MouseEvent e) {
lastKnownMousePoint = new Point(e.getPoint());
}
});
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, false), "Pressed.up");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, true), "Released.up");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, false), "Pressed.down");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, true), "Released.down");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "Pressed.left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), "Released.left");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "Pressed.right");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "Released.right");
ActionMap am = getActionMap();
am.put("Pressed.up", new KeyAction(KeyAction.Direction.UP, true, movementState));
am.put("Released.up", new KeyAction(KeyAction.Direction.UP, false, movementState));
am.put("Pressed.down", new KeyAction(KeyAction.Direction.DOWN, true, movementState));
am.put("Released.down", new KeyAction(KeyAction.Direction.DOWN, false, movementState));
am.put("Pressed.left", new KeyAction(KeyAction.Direction.LEFT, true, movementState));
am.put("Released.left", new KeyAction(KeyAction.Direction.LEFT, false, movementState));
am.put("Pressed.right", new KeyAction(KeyAction.Direction.RIGHT, true, movementState));
am.put("Released.right", new KeyAction(KeyAction.Direction.RIGHT, false, movementState));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(screenWidth, screenHeight);
}
public void startGameThread() {
if (timer == null) {
timer = new Timer((int) Math.floor(1000f / FPS), new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
update();
repaint();
}
});
}
timer.start();
}
public void update() {
if (movementState.contains(KeyAction.Direction.UP)) {
player.y -= player.speed;
} else if (movementState.contains(KeyAction.Direction.DOWN)) {
player.y += player.speed;
} else if (movementState.contains(KeyAction.Direction.LEFT)) {
player.x -= player.speed;
} else if (movementState.contains(KeyAction.Direction.RIGHT)) {
player.x += player.speed;
}
if (lastKnownMousePoint != null) {
// This assumes that character is facing "UP" by default
// That is, 0 has the character entity facing towards to the
// top of the sceen. If the character is facing in a different
// direction, then you will need to offset this calculation
// to compenstate, but that might be better done in the player
// entity
Point playerMidPoint = player.midPoint();
double angle = Math.toDegrees(Math.atan2(lastKnownMousePoint.y - playerMidPoint.y, lastKnownMousePoint.x - playerMidPoint.x)) + 90d;
player.angleInDegrees = angle;
repaint();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
player.draw(g2);
g2.dispose();
if (lastKnownMousePoint != null) {
g2 = (Graphics2D) g;
g2.setColor(Color.GREEN);
int midX = player.x + (tileSize / 2);
int midY = player.y + (tileSize / 2);
g2.drawLine(midX, midY, lastKnownMousePoint.x, lastKnownMousePoint.y);
g2.dispose();
}
}
}
public class KeyAction extends AbstractAction {
enum Direction {
UP, DOWN, LEFT, RIGHT
}
private Direction direction;
private boolean activate;
private Set<Direction> inputState;
public KeyAction(Direction direction, boolean activate, Set<Direction> inputState) {
this.direction = direction;
this.activate = activate;
this.inputState = inputState;
}
#Override
public void actionPerformed(ActionEvent e) {
if (activate) {
inputState.add(direction);
} else {
inputState.remove(direction);
}
}
}
public class Entity {
public int x;
public int y;
public int speed;
}
public class Player extends Entity {
GamePanel gp;
double angleInDegrees = 0;
public Player(GamePanel gp) {
this.gp = gp;
setDefaultValues();
}
public void setDefaultValues() {
x = 100;
y = 100;
speed = 4;
}
public Point midPoint() {
return new Point(x + (gp.tileSize / 2), y + (gp.tileSize / 2));
}
public void draw(Graphics2D g2) {
g2 = (Graphics2D) g2.create();
AffineTransform at = AffineTransform.getTranslateInstance(x, y);
at.rotate(Math.toRadians(angleInDegrees), (gp.tileSize / 2d), (gp.tileSize / 2d));
g2.transform(at);
g2.setColor(Color.yellow);
g2.fillRect(0, 0, gp.tileSize, gp.tileSize);
g2.setColor(Color.RED);
g2.drawLine((gp.tileSize / 2), (gp.tileSize / 2), (gp.tileSize / 2), 0);
g2.dispose();
}
}
}

I want to put the game on a JPane instead of it being on a JFrame so that I can access it though the use of the GUI however it wont work

Cannot put my game into the GUI without there being issues. When starting the GUI, it shows the game. It glitches out and only fixes after pressing on of the buttons. However the buttons are hidden unless you put your mouse over it. Here is the code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.*;
public class BBSays extends JFrame implements ActionListener, MouseListener
{
private BufferedImage image;
public static BBSays bbsays;
public Renderer renderer;
public static final int WIDTH = 800, HEIGHT = 800;
public int flashed = 0, glowTime, dark, ticks, indexPattern;
public boolean creatingPattern = true;
public ArrayList<Integer> pattern;
public Random random;
private boolean gameOver;
private JPanel game;
JFrame frame = new JFrame("BB8 Says");
private JPanel menu;
private JPanel credits;
ImageIcon bbegif = new ImageIcon("tumblr_o0c57n9gfv1tha1vgo1_r3_250.gif");
public BBSays()
{
Timer timer = new Timer(20, this);
renderer = new Renderer();
frame.setSize(WIDTH +7, HEIGHT +30);
frame.setVisible(true);
frame.addMouseListener(this);
frame.add(renderer);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
start();
timer.start();
menu = new JPanel();
credits = new JPanel();
game = new JPanel();
menu.setBackground(Color.yellow);
credits.setBackground(Color.yellow);
game.setBackground(Color.yellow);
JButton button = new JButton("Start");
JButton button2 = new JButton("Exit");
JButton button4 = new JButton("Start");
JLabel greet = new JLabel(" Welcome to BB8 Says");
JLabel jif = new JLabel(bbegif);
JLabel jif2 = new JLabel(bbegif);
JLabel saus = new JLabel("BB8 Image: https://49.media.tumblr.com/7ba3be87bff2efc009e9cfa889d46b4e/tumblr_o0c57n9gfv1tha1vgo1_r3_250.gif");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
frame.setContentPane(game);
frame.invalidate();
frame.validate();
};
});
button2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
};
});
menu.setLayout(new GridLayout(2,2));
menu.add(jif2);
menu.add(greet);
menu.add(jif);
menu.add(button);
menu.add(button6);
menu.add(button2);
frame.setVisible(true);
}
private class MenuAction implements ActionListener {
private JPanel panel;
private MenuAction(JPanel pnl) {
this.panel = pnl;
}
#Override
public void actionPerformed(ActionEvent e) {
changePanel(panel);
}
}
private void changePanel(JPanel panel) {
getContentPane().removeAll();
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().doLayout();
update(getGraphics());
}
public void start()
{
random = new Random();
pattern = new ArrayList<Integer>();
indexPattern = 0;
dark = 2;
flashed = 0;
ticks = 0;
}
public static void main(String[] args)
{
bbsays = new BBSays();
}
#Override
public void actionPerformed(ActionEvent e)
{
ticks++;
if (ticks % 20 == 0)
{
flashed = 0;
if (dark >= 0)
{
dark--;
}
}
if (creatingPattern)
{
if (dark <= 0)
{
if (indexPattern >= pattern.size())
{
flashed = random.nextInt(40) % 4 + 1;
pattern.add(flashed);
indexPattern = 0;
creatingPattern = false;
}
else
{
flashed = pattern.get(indexPattern);
indexPattern++;
}
dark = 2;
}
}
else if (indexPattern == pattern.size())
{
creatingPattern = true;
indexPattern = 0;
dark = 2;
}
renderer.repaint();
}
public void paint(Graphics2D g)
{
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.yellow);
g.fillRect(0, 0, WIDTH, HEIGHT);
if (flashed == 1)
{
g.setColor(Color.blue);
}
else
{
g.setColor(Color.blue.darker());
}
g.fillRect(0, 0, WIDTH/2, HEIGHT/2);
if (flashed == 2)
{
g.setColor(Color.green);
}
else
{
g.setColor(Color.green.darker());
}
g.fillRect(WIDTH/2, 0, WIDTH/2, HEIGHT/2);
if (flashed == 3)
{
g.setColor(Color.orange);
}
else
{
g.setColor(Color.orange.darker());
}
g.fillRect(0, HEIGHT/2, WIDTH/2, HEIGHT/2);
if (flashed == 4)
{
g.setColor(Color.gray);
}
else
{
g.setColor(Color.gray.darker());
}
g.fillRect(WIDTH/2, HEIGHT/2, WIDTH/2, HEIGHT/2);
g.setColor(Color.BLACK);
g.fillRoundRect(220, 220, 350, 350, 300, 300);
g.fillRect(WIDTH/2 - WIDTH/14, 0, WIDTH/7, HEIGHT);
g.fillRect(0, WIDTH/2 - WIDTH/12, WIDTH, HEIGHT/7);
g.setColor(Color.yellow);
g.setStroke(new BasicStroke(200));
g.drawOval(-100, -100, WIDTH+200, HEIGHT+200);
g.setColor(Color.black);
g.setStroke(new BasicStroke(5));
g.drawOval(0, 0, WIDTH, HEIGHT);
if (gameOver)
{
g.setColor(Color.WHITE);
g.setFont(new Font("Comic Sans", 1, 80));
g.drawString("You let", WIDTH / 2 - 140, HEIGHT / 2 - 70);
g.drawString("down BB8 :(", WIDTH / 2 - 220, HEIGHT / 2 );
g.drawString("Try again!", WIDTH / 2 - 195, HEIGHT / 2 + 80);
}
else
{
g.setColor(Color.WHITE);
g.setFont(new Font("Ariel", 1, 144));
g.drawString(indexPattern + "/" + pattern.size(), WIDTH / 2 - 100, HEIGHT / 2 + 42);
}
}
#Override
public void mousePressed(MouseEvent e)
{
int x = e.getX(), y = e.getY();
if (!creatingPattern && !gameOver)
{
if (x>0 && x<WIDTH/2 && y>0 && y<HEIGHT/2)
{
flashed = 1;
ticks = 1;
}
else if (x>WIDTH/2 && x<WIDTH && y>0 && y<HEIGHT/2)
{
flashed = 2;
ticks = 1;
}
else if (x>0 && x<WIDTH/2 && y>HEIGHT/2 && y<HEIGHT)
{
flashed = 3;
ticks = 1;
}
else if (x>WIDTH/2 && x<WIDTH && y>HEIGHT/2 && y<HEIGHT)
{
flashed = 4;
ticks = 1;
}
if (flashed != 0)
{
if (pattern.get(indexPattern)==flashed)
{
indexPattern++;
}
else
{
gameOver = true;
}
}
else
{
start();
gameOver = true;
}
}
else if (gameOver)
{
start();
gameOver = false;
}
}
Here is the code for the renderer class:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Renderer extends JPanel
{
#Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (BBSays.bbsays != null)
{
BBSays.bbsays.paint((Graphics2D) g);
}
}
}
I think that the issue is here as I use the super. method and want to put the graphics on the game JPanel in the main code. I have tried many ways of doing this but I am not able to put the game on a JPanel.
If you can help it is greatly appreciated.

Unexpected extra animation in Java

http://pastebin.com/PXLFJjNG
I'm trying to figure out threading and animations so I made this really basic program where you can press buttons to move a rectangle up and down. The problem is that if you keep pressing the UP button till the rectangle reaches the top of the screen there's suddenly an extra blue rectangle.
This problem is kinda hard to explain just run the program and keep pressing UP and when you get to the top there's suddenly 2 rectangles being drawn. Is it a problem with my paint component?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RectangleMovement implements Runnable {
JFrame frame;
MyPanel panel;
private int x = 100;
private int y = 100;
private int playerX = 400;
private int playerY = 350;
private int horizontalGoal = 0;
private int verticalGoal = 0;
public static void main(String[] args) {
new RectangleMovement().go();
}
private void go() {
frame = new JFrame("Worst Game Ever");
panel = new MyPanel();
MyPanel buttonPanel = new MyPanel();
JButton left = new JButton("Left");
JButton right = new JButton("Right");
JButton down = new JButton("Down");
JButton up = new JButton("Up");
left.addActionListener(new LeftButton());
right.addActionListener(new RightButton());
down.addActionListener(new DownButton());
up.addActionListener(new UpButton());
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(left);
buttonPanel.add(down);
buttonPanel.add(up);
buttonPanel.add(right);
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(500, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
Thread player = new Thread(this);
player.start();
animate();
}
private class MyPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g) {
g.setColor(Color.RED);
g.fillRect(x, y, 10, 10);
g.setColor(Color.BLUE);
g.fillRect(playerX, playerY, 10, 10);
}
}
class LeftButton implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
horizontalGoal = -5;
}
}
class RightButton implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
horizontalGoal = 5;
}
}
class UpButton implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
verticalGoal = -5;
}
}
class DownButton implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
verticalGoal = 5;
}
}
/*
* Animates the player
*/
public void run() {
while (true) {
if (horizontalGoal < 0 && playerX > 0) {
playerX--;
horizontalGoal++;
} else if (horizontalGoal > 0 && playerX + 20 < frame.getWidth()) {
playerX++;
horizontalGoal--;
}
if (verticalGoal < 0 && playerY > 0) {
playerY--;
verticalGoal++;
} else if (verticalGoal > 0 && playerY + 10 < 400) {
playerY++;
verticalGoal--;
}
try {
Thread.sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (intersects()){
playerX = 400;
playerY = 350;
}
frame.repaint();
}
}
private void animate() {
boolean direction = true;
while (true) {
if (y <= 100) {
direction = true;
} else if (y >= 400) {
direction = false;
}
if (direction) {
y++;
} else {
y--;
}
try {
Thread.sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
frame.repaint();
}
}
private boolean intersects() {
if (x <= playerX && playerX <= x + 10 && y <= playerY
&& playerY <= y + 10) {
return true;
}
return false;
}
}
Your buttonPanel is the Problem. It is also a MyPanel which uses your own paintComponent method. So if your player should be drawn playerY = 10. It is drawn on your game panel and your buttonPanel.
Change your ButtonPanel from
MyPanel buttonPanel = new MyPanel();
to
Panel buttonPanel = new Panel();
and your probelm is gone :-)

SnakeGame how to make the tail follow the head?

I am making a snake game, and I am stuck at where making the tails follow the head. And I heard using an add and remove on the head and tails could make that happen, but I have no idea where to start with that.
Here's my code so far:
Screen.java
public class Screen extends JPanel implements ActionListener, KeyListener {
public static final JLabel statusbar = new JLabel("Default");
public static final int WIDTH = 800, HEIGHT = 800;
Timer t = new Timer(100, this);
int x = 400;
int y = 400;
int size = 5; //increase size if eat
private boolean right = false, left = false, up = false, down = false;
int head = 0;
private LinkedList<BodyPart> snake = new LinkedList<BodyPart>();
private BodyPart b;
public Screen(){
initSnake();
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
public void update(){
}
public void direction(){
if(right) x+=10;
if(left) x-=10;
if(up) y-=10;
if(down) y+=10;
}
public void trackOutBound(){
if(x < 0 || x > 800 || y < 0 || y > 800) {
x = 400;
y = 400;
}
}
public void initSnake(){
if(snake.size() == 0){
b = new BodyPart(x, y);
for(int i = 0; i < size; i++) {
snake.add(b);
}
System.out.println(snake);
}
}
public static void main(String[] args) {
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(new Color(10, 50, 0));
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
for(int i = 0; i < WIDTH / 10; i++) {
g.drawLine(i * 10, 0, i * 10, HEIGHT);
}
for(int i = 0; i < HEIGHT / 10; i++) {
g.drawLine(0, i * 10, WIDTH, i * 10);
}
int tempx = 0, tempy = 0;
int temp = 0;
for(int i = 0; i < size; i++){
if(i == head) {
snake.get(i).x = x;
snake.get(i).y = y;
snake.get(i).draw(g);
g.setColor(Color.blue);
g.fillRect(x, y, 10, 10);
g.setColor(Color.white);
g.drawRect(x, y, 10, 10);
} else if(i > 0 && up) {
snake.get(i).x = x;
snake.get(i).y = y + temp;
snake.get(i).draw(g);
} else if(i > 0 && down) {
snake.get(i).x = x;
snake.get(i).y = y - temp;
snake.get(i).draw(g);
} else if(i > 0 && left) {
snake.get(i).x = x + temp;
snake.get(i).y = y;
snake.get(i).draw(g);
} else if(i > 0 && right) {
snake.get(i).x = x - temp;
snake.get(i).y = y;
snake.get(i).draw(g);
}
temp += 10;
}
/*
if(snake.size() == 5){
snake.add(b);
size += 1;
}
*/
}
#Override
public void actionPerformed(ActionEvent e) {
direction();
trackOutBound();
repaint();
// System.out.println(snake);
statusbar.setText("(" + x + " , " + y + ")");
}
#Override
public void keyTyped(KeyEvent e) {}
#Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if(key == KeyEvent.VK_RIGHT && !left) {
up = false;
down = false;
right = true;
}
if(key == KeyEvent.VK_LEFT && !right) {
up = false;
down = false;
left = true;
}
if(key == KeyEvent.VK_UP && !down) {
left = false;
right = false;
up = true;
}
if(key == KeyEvent.VK_DOWN && !up) {
left = false;
right = false;
down = true;
}
}
#Override
public void keyReleased(KeyEvent e) {}
}
BodyPart.java
public class BodyPart {
int x;
int y;
public BodyPart(int x, int y) {
this.x = x;
this.y = y;
}
public void draw(Graphics g) {
this.x = x;
this.y = y;
g.setColor(Color.red);
g.fillRect(x, y, 10, 10);
g.setColor(Color.white);
g.drawRect(x, y, 10, 10);
}
}
Frame.java
public class Frame extends JPanel {
private static JLabel statusbar = new JLabel("Default");
public void statusbar(){
statusbar = Screen.statusbar;
}
public static void main(String[] args) {
JFrame f = new JFrame();
Screen s = new Screen();
f.add(s);
f.add(statusbar, BorderLayout.SOUTH);
f.setSize(800, 800);
f.setVisible(true);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Now this code would only make the tails flip to the horizontal or vertical, is it possible to make the tails follow the head by using this code? or I need to change my code?
Thank you
The basic idea is, you need some kind of List which contains ALL the points of the snake. Conceptually, the List would contain virtual coordinates, that is 1x1 would represent a coordinate in virtual space, which presented a place on a virtual board (which would have some wide and height).
You could then translate that to the screen, so this would allow each part of the snake to be larger then a single pixel. So, if each part was 5x5 pixels, then 1x1 would actually be 5x5 in the screen.
Each time the snake moves, you add a new value to the head and remove the last value from tail (assuming it's not growing). When you needed to paint the snake, you would simply iterate over the List, painting each point of the snake.
The following is a simple example, which uses a LinkedList, which pushes a new Point onto the List, making a new head, and removing the last element (the tail) on each cycle.
Which basically boils down to...
snakeBody.removeLast();
snakeBody.push(new Point(xPos, yPos));
As a runnable concept
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.LinkedList;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Snake {
public static void main(String[] args) {
new Snake();
}
public Snake() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public static class TestPane extends JPanel {
public enum Direction {
UP, DOWN, LEFT, RIGHT
}
private int xPos, yPos;
private Direction direction = Direction.UP;
private LinkedList<Point> snakeBody = new LinkedList<>();
public TestPane() {
xPos = 100;
yPos = 100;
for (int index = 0; index < 50; index++) {
snakeBody.add(new Point(xPos, yPos));
}
bindKeyStrokeTo("up.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0, false), new MoveAction(Direction.UP));
bindKeyStrokeTo("down.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, false), new MoveAction(Direction.DOWN));
bindKeyStrokeTo("left.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, false), new MoveAction(Direction.LEFT));
bindKeyStrokeTo("right.pressed", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), new MoveAction(Direction.RIGHT));
Timer timer = new Timer(40, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
switch (direction) {
case UP:
yPos--;
break;
case DOWN:
yPos++;
break;
case LEFT:
xPos--;
break;
case RIGHT:
xPos++;
break;
}
if (yPos < 0) {
yPos--;
} else if (yPos > getHeight() - 1) {
yPos = getHeight() - 1;
}
if (xPos < 0) {
xPos--;
} else if (xPos > getWidth() - 1) {
xPos = getWidth() - 1;
}
snakeBody.removeLast();
snakeBody.push(new Point(xPos, yPos));
repaint();
}
});
timer.start();
}
public void bindKeyStrokeTo(String name, KeyStroke keyStroke, Action action) {
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(keyStroke, name);
am.put(name, action);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
for (Point p : snakeBody) {
g2d.drawLine(p.x, p.y, p.x, p.y);
}
g2d.dispose();
}
public class MoveAction extends AbstractAction {
private Direction moveIn;
public MoveAction(Direction direction) {
this.moveIn = direction;
}
#Override
public void actionPerformed(ActionEvent e) {
direction = this.moveIn;
}
}
}
}
Now, this has no collision detection or other functionality, but you can move the snake around and it will follow itself
For snake style movement, you can, from the tail to the head, move each BodyPart position to the position of the BodyPart ahead of it. For the head there is no part ahead so you have to write decision code whether to simply move the same direction as the part before it or a new direction based on input. Then update the screen.

Categories

Resources