Snake in Java is not building or growing properly - java

I'm trying to make snake in Java, I have gotten everything with it to work, but when I try to run the program, the Snake isn't in one long string, but in many different spaced out parts. I've tried searching through every like of code and couldn't identify where anything went wrong.
Here is what I have
Board.Java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.Timer;
import javax.swing.JPanel;
public class Board extends JPanel implements ActionListener {
private final int B_WIDTH = 300;
private final int B_HEIGHT= 300;
private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
private final int RANDOM_POS = 29;
private final int DELAY = 140;
private final int x[] = new int[ALL_DOTS];
private final int y[] = new int[ALL_DOTS];
private int dots;
private int apple_x;
private int apple_y;
private boolean left = false;
private boolean right = true;
private boolean up = false;
private boolean down = false;
private boolean ingame = true;
private Timer timer;
public Board() {
CreateBoard();
}
private void CreateBoard() {
addKeyListener(new TAdapter());
setBackground(Color.black);
setFocusable(true);
setPreferredSize(new Dimension(B_WIDTH,B_HEIGHT));
StartGame();
}
private void StartGame() {
dots = 3;
for (int z = 0; z < dots; z++) {
x[z] = 50 - z * 10;
y[z] = 50;
}
LocateApple();
timer = new Timer(DELAY, this);
timer.start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
private void doDrawing(Graphics g) {
if (ingame) {
g.setColor(Color.red);
g.fillRect(apple_x, apple_y, DOT_SIZE, DOT_SIZE);
for (int z = 0; z < dots; z++) {
g.setColor(Color.green);
g.fillRect(x[z], y[z], DOT_SIZE, DOT_SIZE);
}
} else {
gameOver(g);
}
}
private void gameOver(Graphics g) {
String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
}
private void CheckApple() {
if ((x[0] == apple_x) && (y[0] == apple_y)) {
dots++;
LocateApple();
}
}
private void move() {
for (int z = dots; z > 0; z--) {
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
if(left) {
x[0] -= DOT_SIZE;
}
if(right) {
x[0] += DOT_SIZE;
}
if(up) {
y[0] -= DOT_SIZE;
}
if(down) {
y[0] += DOT_SIZE;
}
}
}
private void CheckCollision() {
for (int z = dots; z > 0; z--) {
if((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
ingame = false;
}
}
if (x[0] >= B_WIDTH) {
ingame = false;
}
if (x[0] < 0) {
ingame = false;
}
if (y[0] >= B_HEIGHT) {
ingame = false;
}
if (y[0] < 0) {
ingame = false;
}
if (!ingame) {
timer.stop();
}
}
private void LocateApple() {
int r = (int) (Math.random() * RANDOM_POS);
apple_x = ((r * DOT_SIZE));
r = (int) (Math.random() * RANDOM_POS);
apple_y = ((r * DOT_SIZE));
}
#Override
public void actionPerformed(ActionEvent e) {
if (ingame) {
CheckApple();
CheckCollision();
move();
}
repaint();
}
private class TAdapter extends KeyAdapter {
#Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (right == false)) {
left = true;
up = false;
down = false;
}
if ((key == KeyEvent.VK_RIGHT) && (left == false)) {
right = true;
up = false;
down = false;
}
if ((key == KeyEvent.VK_UP) && (down == false)) {
up = true;
left = false;
right = false;
}
if ((key == KeyEvent.VK_DOWN) && (up == false)) {
down = true;
left = false;
right = false;
}
}
}
}
Game.Java (main class)
import javax.swing.JFrame;
import java.awt.EventQueue;
public class Game extends JFrame {
public Game() {
StartUI();
}
private void StartUI() {
add(new Board());
setResizable(false);
pack();
setTitle("Snake");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
JFrame ex = new Game();
ex.setVisible(true);
});
}
}
When I try to run the code it behaves very strangely and I can't identify the problem.
Can anyone find what is causing it and what to do to fix it?

It's easier to see what's going wrong with a correct indentation of the move method:
private void move() {
for (int z = dots; z > 0; z--) {
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
if(left) {
x[0] -= DOT_SIZE;
}
if(right) {
x[0] += DOT_SIZE;
}
if(up) {
y[0] -= DOT_SIZE;
}
if(down) {
y[0] += DOT_SIZE;
}
}
}
The head move (x[0], y[0]) is inside the for loop, making it done several times per move.
You should better move all the body parts first, then move the head.
private void move() {
// 1) Move the body
for (int z = dots; z > 0; z--) {
x[z] = x[z - 1];
y[z] = y[z - 1];
}
// 2) Move the head
if (left) {
x[0] -= DOT_SIZE;
}
if (right) {
x[0] += DOT_SIZE;
}
if (up) {
y[0] -= DOT_SIZE;
}
if (down) {
y[0] += DOT_SIZE;
}
}

Related

Where do I put public static void main(String[] args)?

I am new to java, and after attempting to create a small snake type game, and error occurred
Error: Main method not found in class Game.GamePanel, please define the main method as: public static void main(String[] args)"
Could someone please help me as to fixing the error? It would be a big help as a beginner to java. Im using Eclipse latest version
package Game;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.JPanel;
public class GamePanel extends JPanel implements Runnable, KeyListener {
public static final int WIDTH = 1000;
public static final int HEIGHT = 1000;
//Render
private Graphics2D g2d;
private BufferedImage image;
//GameLoop
private Thread thread;
private boolean running;
private long targetTime;
//Game Stuff
private final int SIZE = 10;
Entity head;
ArrayList<Entity> snake;
//movement
private int dx, dy;
//key input
private boolean up,down,right,left,start;
public GamePanel() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
requestFocus();
addKeyListener(this);
}
public void addNotify() {
super.addNotify();
thread = new Thread(this);
thread.start();
}
private void setFPS(int fps) {
targetTime = 1000 / fps;
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();
if(k == KeyEvent.VK_UP) up = true;
if(k == KeyEvent.VK_DOWN) down = true;
if(k == KeyEvent.VK_LEFT) left = true;
if(k == KeyEvent.VK_RIGHT) right = true;
if(k == KeyEvent.VK_ENTER) start = true;
}
#Override
public void keyReleased(KeyEvent e) {
int k = e.getKeyCode();
if(k == KeyEvent.VK_UP) up = false;
if(k == KeyEvent.VK_DOWN) down = false;
if(k == KeyEvent.VK_LEFT) left = false;
if(k == KeyEvent.VK_RIGHT) right = false;
if(k == KeyEvent.VK_ENTER) start = false;
}
#Override
public void run() {
if(running) return;
init();
long startTime;
long elapsed;
long wait;
while(running){
startTime = System.nanoTime();
update();
requestRender();
elapsed = System.nanoTime() - startTime;
wait = targetTime - elapsed / 1000000;
if(wait > 0) {
try {
Thread.sleep(wait);
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
private void init() {
image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_ARGB);
g2d = image.createGraphics();
running = true;
setUplevel();
setFPS (10);
}
private void setUplevel() {
snake = new ArrayList<Entity>();
head = new Entity(SIZE);
head.setPosition(WIDTH / 2, HEIGHT / 2);
snake.add(head);
for(int i = 1;i < 10;i++) {
Entity e = new Entity(SIZE);
e.setPosition(head.getX() + (i * SIZE), head.getY());
snake.add(e);
}
}
private void requestRender() {
render(g2d);
Graphics g = getGraphics();
g.drawImage(image, 0,0,null);
g.dispose();
}
private void update() {
if(up && dy == 0) {
dy = -SIZE;
dx = 0;
}
if(down && dy == 0) {
dy = SIZE;
dx = 0;
}
if(left && dx == 0) {
dy = 0;
dx = -SIZE;
}
if(right && dx == 0) {
dy = 0;
dx = SIZE;
}
if(dx != 0 || dy != 0) {
for(int i = snake.size() - 1;i > 0;i--) {
snake.get(i).setPosition(
snake.get(i - 1).getX(),
snake.get(i - 1).getY()
);
}
head.move(dx, dy);
}
if(head.getX() < 0 ) head.getX(WIDTH);
if(head.getY() < 0 ) head.getY(HEIGHT);
if(head.getX() > WIDTH ) head.getX(0);
if(head.getY() > HEIGHT ) head.getY (0);
}
public void render(Graphics2D g2d) {
g2d.clearRect(0, 0, WIDTH, HEIGHT);
g2d.setColor(Color.GREEN);
for(Entity e : snake) {
e.render(g2d);
}
}
}
You can add it to GamePanel, but you need to create a JFrame and you want to add an instance of your GamePanel to it. Something like,
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.add(new GamePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}

Image files won't load in 2D Java game

So I am fairly new to Java and I built this Board for a snake game using a tutorial. It seems to work fine except for the fact that the image files won't load. I haven't worked with images in Eclipse before so I don't really know what I'm doing. I tried putting the .png image files in the package but that did not help at all. I would appreciate any ideas.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private final int B_WIDTH = 300;
private final int B_HEIGHT = 300;
private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
private final int RAND_POS = 29;
private final int DELAY = 140;
private final int x[] = new int[ALL_DOTS];
private final int y[] = new int[ALL_DOTS];
private int dots;
private int apple_x;
private int apple_y;
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
private Timer timer;
private Image ball;
private Image apple;
private Image head;
public Board() {
addKeyListener(new TAdapter());
setBackground(Color.black);
setFocusable(true);
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
loadImages();
initGame();
}
private void loadImages() {
ImageIcon iid = new ImageIcon("dot.png");
ball = iid.getImage();
ImageIcon iia = new ImageIcon("apple.png");
apple = iia.getImage();
ImageIcon iih = new ImageIcon("head.png");
head = iih.getImage();
}
private void initGame() {
dots = 3;
for (int z = 0; z < dots; z++) {
x[z] = 50 - z * 10;
y[z] = 50;
}
locateApple();
timer = new Timer(DELAY, this);
timer.start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
private void doDrawing(Graphics g) {
if (inGame) {
g.drawImage(apple, apple_x, apple_y, this);
for (int z = 0; z < dots; z++) {
if (z == 0) {
g.drawImage(head, x[z], y[z], this);
} else {
g.drawImage(ball, x[z], y[z], this);
}
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
private void gameOver (Graphics g) {
String msg = "Game Over";
Font small = new Font ("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
}
private void checkApple(){
if ((x[0] == apple_x) && (y[0] == apple_y)) {
dots++;
locateApple();
}
}
private void move() {
for (int z = dots; z > 0; z--) {
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
}
if (leftDirection) {
x[0] -= DOT_SIZE;
}
if (rightDirection) {
x[0] += DOT_SIZE;
}
if (upDirection) {
y[0] -= DOT_SIZE;
}
if (downDirection) {
y[0] += DOT_SIZE;
}
}
private void checkCollision(){
for (int z = dots; z > 0; z--) {
if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
}
}
if (y[0] >= B_HEIGHT) {
inGame = false;
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] >= B_WIDTH) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
if(!inGame) {
timer.stop();
}
}
private void locateApple() {
int r = (int) (Math.random() * RAND_POS);
apple_x = ((r * DOT_SIZE));
r = (int) (Math.random() * RAND_POS);
apple_y = ((r * DOT_SIZE));
}
#Override
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
private class TAdapter extends KeyAdapter {
#Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {
leftDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {
rightDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_UP) && (!downDirection)) {
upDirection = true;
rightDirection = false;
leftDirection = false;
}
if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {
downDirection = true;
rightDirection = false;
leftDirection = false;
}
}
}
}
I tried putting the .png image files in the package
I would advise against putting the image files in the same package as the source files. In fact, a common hierarchy for source folders looks like this:
src/main/java
src/main/resources
src/test/java
src/test/resources
Within the src/main/java source folder, you can have your different packages for source files. Same for src/test/java, except they are test files.
In src/main/resources, you can place folders that contain all of the resource files (i18n files, images, etc.). These folders can be excluded from the build path and don't need to be source folders.
For example:
Then, you should be able to do:
public static void main(String... args) {
final ImageIcon imageIcon = new ImageIcon("src/main/resources/Desert.jpg");
}
Use ImageIO instead.
If you need an ImageIcon, create one from an image loaded through ImageIO. In this case the game isn't even using the Icon so don't bother, just use ImageIO to load a BufferedImage.
ImageIO.read() is overloaded to accept a File, InputStream or URL. So where your files need to be located depends on how you use it. Try putting your images in a specific location and use an absolute path to test:
private void loadImages() {
ball = ImageIO.read(new File("c:/img/dot.png"));
}
Once you have that working you can refer to the general documentation around File or ClassLoader.getResource() on where files will be loaded from.
I don't use Eclipse however I suspect in general you probably want to:
Put the image files into the same package as the Board class.
Use getClass().getResourceAsStream(...) to get an InputStream.
Convert the stream into an image.
Something like:
public void loadImages() {
ball = ImageIO.read(getClass().getResourceAsStream("dot.png"));
}
ImageIO also has the benefit of giving you more useful exceptions when loading fails too.

My "Snake" app. is not displaying fully even though there are no compilation errors

I'm trying to create a version of the snake game but it is not working correctly. What it is doing now is displaying a black screen without any of the components in the paintComponent section. Occasionally it displays my "Game Over" message after a while so something is working but the snake and food are not displaying.
I've tried for hours to look for any logic error but have not been able to find any. If anyone would be able to guide me on what I am doing wrong, I would be very grateful.
Below is my code that sets up the board and underneath that is my main class that runs the program. I put the entire code just in case you wanted to run it on your own devices but the important pieces of code have 2 stars surrounding them. Any advice or suggestions would be greatly appreciated.
public class Board2 extends JPanel implements ActionListener{
private int bWidth = 500;
private int bHeight = 500;
private int segmentSize = 10;
private int bodyMax = 2500;
private int randomPos = 29;
private int delay = 140;
private int x[] = new int[bodyMax];
private int y[] = new int[bodyMax];
private int bodySize, foodX, foodY;
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
private Timer timer;
private Image head;
private Image food;
private Image body;
Board2(){
addKeyListener(new changeDirection());
setBackground(Color.BLACK);
setPreferredSize(new Dimension(bWidth, bHeight));
loadImages();
init();
}
**private void loadImages(){
ImageIcon headImage = new ImageIcon("Square-Freemason-Symbols-300x300.png");
head = headImage.getImage();
ImageIcon foodImage = new ImageIcon("Square-Freemason-Symbols-300x300.png");
food = foodImage.getImage();
ImageIcon bodyImage = new ImageIcon("Flat1.jpg");
body = bodyImage.getImage();
}**
private void init(){
bodySize = 3;
for(int z = 0; z < bodySize; z++){
x[z] = 50 - z * 10;
y[z] = 50;
}
findFood();
timer = new Timer(delay, this);
timer.start();
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
doDrawing(g);
}
**private void doDrawing(Graphics g){
if(inGame){
g.drawImage(food, foodX, foodY, this);
for(int z = 0; z < bodySize; z++){
if(z == 0){
g.drawImage(head, x[z], y[z], this);
} else {
g.drawImage(body, x[z], y[z], this);
}
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}**
private void gameOver(Graphics g){
String msg = "Game Over";
Font small = new Font("Helvetiva", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.WHITE);
g.setFont(small);
g.drawString(msg, (bWidth - metr.stringWidth(msg)/2), bHeight/2);
}
private void findFood(){
int r = (int) Math.random() * randomPos;
foodX = r * segmentSize;
int s = (int) Math.random() * randomPos;
foodY = s * segmentSize;
}
private void checkFood(){
if(x[0] == foodX && y[0] == foodY){
bodySize += 3;
findFood();
}
}
private void checkCollision(){
for(int z = bodySize; z > 0; z--){
if(z > 4 && x[0] == x[z] && y[0] == y[z]){
inGame = false;
}
}
if(x[0] > bWidth){
inGame = false;
}
if(x[0] < 0){
inGame = false;
}
if(y[0] >bHeight){
inGame = false;
}
if(y[0] < 0){
inGame = false;
}
}
private void Move(){
for(int z = bodySize; z > 0; z--){
x[z] = x[z - 1];
y[z] = y[z - 1];
}
if(leftDirection){
x[0] -= segmentSize;
}
if(rightDirection){
x[0] += segmentSize;
}
if(upDirection){
y[0] -= segmentSize;
}
if(downDirection){
y[0] += segmentSize;
}
}
public void actionPerformed(ActionEvent e){
if(inGame){
checkFood();
checkCollision();
Move();
}
}
public class changeDirection extends KeyAdapter{
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT && !rightDirection){
leftDirection = true;
upDirection = false;
downDirection = false;
}
if(key == KeyEvent.VK_RIGHT && !leftDirection){
rightDirection = true;
upDirection = false;
downDirection = false;
}
if(key == KeyEvent.VK_UP && !downDirection){
upDirection = true;
leftDirection = false;
rightDirection = false;
}
if(key == KeyEvent.VK_DOWN && !upDirection){
downDirection = true;
leftDirection = false;
rightDirection = false;
}
}
}
}
Here is the main class:
public class SnakeV2 extends JFrame{
SnakeV2(){
add(new Board2());
setResizable(false);
pack();
setTitle("SnakeV2");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
#Override
public void run(){
JFrame ex = new SnakeV2();
ex.setVisible(true);
}
});
}
}

How to add a Main Menu interface in a java game program? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to make a main menu in my java game program and I tried many different ways but all of them failed. Can someone help doing it? I want the simplest. Thank you btw. Here's my source code
Snake.java
public class Snake extends JFrame {
public Snake() {
add(new Board());
setResizable(false);
pack();
setTitle("Snake");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame ex = new Snake();
ex.setVisible(true);
}
});
}
}
Board.java
public class Board extends JPanel implements ActionListener {
private final int B_WIDTH = 300;
private final int B_HEIGHT = 300;
private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
private final int RAND_POS = 29;
private final int DELAY = 140;
private final int x[] = new int[ALL_DOTS];
private final int y[] = new int[ALL_DOTS];
private int dots;
private int apple_x;
private int apple_y;
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
private Timer timer;
private Image ball;
private Image apple;
private Image head;
public Board() {
addKeyListener(new TAdapter());
setBackground(Color.black);
setFocusable(true);
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
loadImages();
initGame();
}
private void loadImages() {
ImageIcon iid = new ImageIcon("dot.png");
ball = iid.getImage();
ImageIcon iia = new ImageIcon("apple.png");
apple = iia.getImage();
ImageIcon iih = new ImageIcon("head.png");
head = iih.getImage();
}
private void initGame() {
dots = 3;
for (int z = 0; z < dots; z++) {
x[z] = 50 - z * 10;
y[z] = 50;
}
locateApple();
timer = new Timer(DELAY, this);
timer.start();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
private void doDrawing(Graphics g) {
if (inGame) {
g.drawImage(apple, apple_x, apple_y, this);
for (int z = 0; z < dots; z++) {
if (z == 0) {
g.drawImage(head, x[z], y[z], this);
} else {
g.drawImage(ball, x[z], y[z], this);
}
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
private void gameOver(Graphics g) {
String msg = "Game Over. Thank you for playing ";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
}
private void checkApple() {
if ((x[0] == apple_x) && (y[0] == apple_y)) {
dots++;
locateApple();
}
}
private void move() {
for (int z = dots; z > 0; z--) {
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
}
if (leftDirection) {
x[0] -= DOT_SIZE;
}
if (rightDirection) {
x[0] += DOT_SIZE;
}
if (upDirection) {
y[0] -= DOT_SIZE;
}
if (downDirection) {
y[0] += DOT_SIZE;
}
}
private void checkCollision() {
for (int z = dots; z > 0; z--) {
if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
inGame = false;
}
}
if (y[0] >= B_HEIGHT) {
inGame = false;
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] >= B_WIDTH) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
if(!inGame) {
timer.stop();
}
}
private void locateApple() {
int r = (int) (Math.random() * RAND_POS);
apple_x = ((r * DOT_SIZE));
r = (int) (Math.random() * RAND_POS);
apple_y = ((r * DOT_SIZE));
}
#Override
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
private class TAdapter extends KeyAdapter {
#Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (!rightDirection)) {
leftDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_RIGHT) && (!leftDirection)) {
rightDirection = true;
upDirection = false;
downDirection = false;
}
if ((key == KeyEvent.VK_UP) && (!downDirection)) {
upDirection = true;
rightDirection = false;
leftDirection = false;
}
if ((key == KeyEvent.VK_DOWN) && (!upDirection)) {
downDirection = true;
rightDirection = false;
leftDirection = false;
}
}
}
}
The simplest and more common way is adding a top Menu. As you are using AWT, I strongly recommend you to use MenuBar - java.awt.MenuBar
public class MenuBar
extends MenuComponent
implements MenuContainer, Accessible
In this site you have a complete example

snake game score

This snake game code. I want to add the system score in this game. So that each time the snake eating his score would be increased. But if the snake does not get food score will not increase.
How do I go about displaying the current score?
this is :
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener {
private final int WIDTH = 300;
private final int HEIGHT = 300;
private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
private final int RAND_POS = 29;
private final int DELAY = 140;
private int x[] = new int[ALL_DOTS];
private int y[] = new int[ALL_DOTS];
private int dots;
private int apple_x;
private int apple_y;
private boolean left = false;
private boolean right = true;
private boolean up = false;
private boolean down = false;
private boolean inGame = true;
private Timer timer;
private Image ball;
private Image apple;
private Image head;
public Board() {
addKeyListener(new TAdapter());
setBackground(Color.black);
ImageIcon iid = new ImageIcon(this.getClass().getResource("dot.png"));
ball = iid.getImage();
ImageIcon iia = new ImageIcon(this.getClass().getResource("apple.png"));
apple = iia.getImage();
ImageIcon iih = new ImageIcon(this.getClass().getResource("head.png"));
head = iih.getImage();
setFocusable(true);
initGame();
}
public void initGame() {
dots = 3;
for (int z = 0; z < dots; z++) {
x[z] = 50 - z*10;
y[z] = 50;
}
locateApple();
timer = new Timer(DELAY, this);
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
if (inGame) {
g.drawImage(apple, apple_x, apple_y, this);
for (int z = 0; z < dots; z++) {
if (z == 0)
g.drawImage(head, x[z], y[z], this);
else g.drawImage(ball, x[z], y[z], this);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
} else {
gameOver(g);
}
}
public void gameOver(Graphics g) {
String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = this.getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2,
HEIGHT / 2);
}
public void checkApple() {
if ((x[0] == apple_x) && (y[0] == apple_y)) {
dots++;
locateApple();
}
}
public void move() {
for (int z = dots; z > 0; z--) {
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
}
if (left) {
x[0] -= DOT_SIZE;
}
if (right) {
x[0] += DOT_SIZE;
}
if (up) {
y[0] -= DOT_SIZE;
}
if (down) {
y[0] += DOT_SIZE;
}
}
public void checkCollision() {
for (int z = dots; z > 0; z--) {
if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
inGame = false;
}
}
if (y[0] > HEIGHT) {
inGame = false;
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] > WIDTH) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
}
public void locateApple() {
int r = (int) (Math.random() * RAND_POS);
apple_x = ((r * DOT_SIZE));
r = (int) (Math.random() * RAND_POS);
apple_y = ((r * DOT_SIZE));
}
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
private class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (!right)) {
left = true;
up = false;
down = false;
}
if ((key == KeyEvent.VK_RIGHT) && (!left)) {
right = true;
up = false;
down = false;
}
if ((key == KeyEvent.VK_UP) && (!down)) {
up = true;
right = false;
left = false;
}
if ((key == KeyEvent.VK_DOWN) && (!up)) {
down = true;
right = false;
left = false;
}
}
}
You need two things:
1) Code to add the score - you should check after the move to see whether the snake's head is at the same coordinates as the apple, if so add to the score.
2) Create a JLabel to store the value of the player's score. On each timer invoked ActionPerformed, update the text of this JLabel. You won't need to worry about Multi-threading, the action event handlers invoked by the Timer are handled in a separate thread.

Categories

Resources