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.
Related
I have been sitting here for 5 hours trying to make my ball bounce off the paddle and the walls when it hits it but it just goes straight through the walls and paddle then re enters through the top and the same cycle continues. I don't know where I am going wrong
My MainWindow class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
public class MainWindow extends JPanel implements KeyListener, ActionListener {
private boolean gameRunning = true;
private int BOARD_WIDTH = 500;
private int BOARD_HEIGHT = 500;
private Thread animator;
private BufferedImage img;
private Dimension dimension;
private int ballX = 20;
private int ballY = 200;
private int ballSpeedX = 4;
private int ballSpeedY = 5;
private int ballWidth = 20;
private Brick[] bricks = new Brick[24];
private boolean[] showBrick = new boolean[24];
private int paddleX = BOARD_WIDTH/2;
private int paddleY = BOARD_HEIGHT-50;
private int paddleW = 100;
private int paddleH = 10;
boolean paddleLeft = false;
boolean paddleRight = true;
private Timer timer;
private int delay = 50;
public MainWindow(){
addKeyListener(this);
//addMouseListener(this);
setFocusable(true);
dimension = new Dimension(BOARD_WIDTH, BOARD_HEIGHT);
setBackground(Color.BLACK);
int x = 20;
int y = 20;
int count = 1;
for(int i = 0; i < bricks.length; i++){
bricks[i] = new Brick(x, y, 50, 10);
showBrick[i] = true;
x += 55;
if (count%8 == 0){
x = 20;
y += 20;
}
count++;
}
timer = new Timer(delay, this);
timer.start();
//if (animator == null || !gameRunning) {
// animator = new Thread(this);
//animator.start();
//}
setDoubleBuffered(true);
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.pink);
g.fillRect(0,0, dimension.width, dimension.height);
g.setColor(Color.black);
// for the bricks
for(int i = 0; i<bricks.length; i++) {
if(showBrick[i])
g.fillRect(bricks[i].getBrickX(), bricks[i].getBrickY(),bricks[i].getBrickW(),bricks[i].getBrickH());
}
// for the paddle
g.fillRect(paddleX, paddleY, paddleW, paddleH);
// for the ball
g.fillOval(ballX, ballY, ballSpeedX,ballSpeedY);
//if (gameRunning){
//ballX+=ballSpeedX;
// movingBall();
//repaint();
//}
g.dispose();
}
private void movingBall(){
}
#Override
public void keyTyped(KeyEvent e) {
//if (e.getKeyCode() == KeyEvent.VK_RIGHT){
// paddleX+=5;
//}
}
#Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
if(paddleX >= 385){
paddleRight = false;
}
else {
//ballX+=ballSpeedX;
paddleRight = true;
paddleX+=5;
repaint();
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT){
if(paddleX <= 10){
paddleLeft = false;
}
else {
paddleLeft = false;
paddleX-=5;
repaint();
}
}
}
#Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
paddleRight = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT){
paddleLeft = false;
}
}
#Override
public void actionPerformed(ActionEvent e) {
timer.start();
if(ballX < 0 || ballX > BOARD_WIDTH){
ballSpeedX = ballSpeedX * -1;
}
if(ballY < 0){
ballSpeedY = ballSpeedY * -1;
}
if(ballY > BOARD_HEIGHT){
ballX = 20;
ballY = 200;
ballSpeedY *= -1;
}
if(ballX == paddleX && ballY == paddleY){
ballSpeedY *= -1;
}
if(ballX + ballWidth > paddleX && ballX < paddleX + paddleW && ballY + ballWidth > paddleY && ballY < paddleY + paddleH) {
ballSpeedY *= -1;
}
ballX += ballSpeedX;
ballY += ballSpeedY;
repaint();
//System.out.println("its working");
}
}
My Window class:
import javax.swing.*;
public class Window {
public Window() {
JFrame frame = new JFrame("Brick Builder Game");
frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE);
frame.setSize(500,500);
frame.add(new MainWindow());
frame.setVisible(true);
frame.setResizable(false);
}
public static void main(String[] args){
new Window();
}
}
My brick class:
public class Brick {
// X-position of brick
private int brickX;
// Y-position of brick
private int brickY;
// width of brick
private int brickW;
// height of brick
private int brickH;
public Brick(int brickX, int brickY, int brickW, int brickH) {
this.brickX = brickX;
this.brickY = brickY;
this.brickW = brickW;
this.brickH = brickH;
}
public int getBrickX(){
return brickX;
}
public int getBrickY(){
return brickY;
}
public int getBrickW(){
return brickW;
}
public int getBrickH(){
return brickH;
}
}
the signature for fillOval is as follows: (x_position, y_position, width, height). And in your signature, you try to use ballSpeedX and ballSpeedY as the width and height.
So when you do ballSpeedX or ballSpeedY *= -1. Swing tries to draw an oval, with negative measures, which is not possible, and thus fails to show the ball
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;
}
}
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.
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);
}
});
}
}
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