Develop a Snake Game in java - part 2 (Interface)

preview_player
Показать описание
In this second part of "Developing snake game in java", we will write code for making the user interface for our game. We will use JFrame and JPanel to do that.

Facebook:
Рекомендации по теме
Комментарии
Автор

Nice explanation.👩‍💻
But how can we know the exact values of the parameters to be given in drawRect(24, 10, 851, 55);

NewLife-ozoc
Автор

sir using notepad its not workin...nd here no option for set all those(project mame....) setups which we do in netbeans...

dnainawa
Автор

Hey man, trying to run the program on netbeans. Have the images in the same src folder as the classes. When I call the file and run the program, nothing comes up, not even the borders. I'm using a MacBook, maybe not inputting the path correctly? Not sure, but I've tried a lot of different things. Would really appreciate some tips. Thank you!

shivilsachdeva
Автор

Where i can downloads the png images for the game?

vascoponciano
Автор

There are some problem with this code please resolve this (snake had drown but the movement of snake is not working) please help

muditsrivastava
Автор

hello I do not understand why the error

import java.awt.Color;
import javax.swing.JFrame;

public class Main {

public static void main(String[] args) {
JFrame obj = new JFrame();
Gameplay gameplay = new Gameplay();

obj.setBounds(10, 10, 905, 700);

obj.setResizable(false);
obj.setVisible(true);

obj.add(gameplay);
}

}



import java.awt.Color;
import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Gameplay extends JFrame{
private ImageIcon titleImage;

public Gameplay() {

}

public void paint(Graphics g)
{
// draw title image border
g.setColor(Color.white);
g.drawRect(24, 10, 851, 55);

// draw the title image
titleImage = new ImageIcon("snaketitle.jpg");
titleImage.paintIcon(this, g, 25, 11);

// draw border for gameplay
g.setColor(Color.WHITE);
g.drawRect(24, 74, 851, 577);

// draw backqround for the gameplay
g.setColor(Color.black);
g.fillRect(25, 75, 850, 575);

}
}


Exception in thread "main" adding a window to a container
at Source)
at Source)
at Source)
at Source)
at Source)
at

kvachweb
Автор

ive tried all the help in the comments for how to get images to show but nothing is working. can someone help me out ?

here is my code:

import java.awt.Color;

import javax.swing.JFrame;
public class Main {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame obj = new JFrame();
Gameplay gameplay = new Gameplay();

obj.setBounds(10, 10, 905, 700);

obj.setResizable(false);
obj.setVisible(true);
);
obj.add(gameplay);


}

}

Gameplay frame:

import java.awt.Color;


import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;


public class Gameplay extends JPanel implements KeyListener, ActionListener{



private int [] snakexlength = new int[750];
private int [] snakeylength = new int[750];

private boolean left = false;
private boolean right = false;
private boolean up = false;
private boolean down = false;

private ImageIcon rightmouth;
private ImageIcon downmouth;
private ImageIcon upmouth;
private ImageIcon leftmouth;

private int lengthofsnake = 3;



private Timer timer;
private int delay = 100;
private ImageIcon snakeimage;

private int moves = 0;





private ImageIcon titleImage;




public Gameplay()
{
addKeyListener(this);
setFocusable(true);

timer = new Timer(delay, this);
timer.start();
}

public void paint(Graphics g)
{

if(moves == 0)
{
snakexlength[2] = 50;
snakexlength[1] = 75;
snakexlength[0] = 100;

snakeylength[2] = 100;
snakeylength[1] = 100;
snakeylength[0] = 100;
}
// draw title image border
g.setColor(Color.white);
g.drawRect(24, 10, 851, 55);

// draw the title image
titleImage = new ImageIcon("snaketitle.jpg");
titleImage.paintIcon(this, g, 25, 11);

// draw border for gameplay
g.setColor(Color.WHITE);
g.drawRect(24, 74, 851, 577);

// draw background for gameplay
g.setColor(Color.black);
g.fillRect(25, 75, 850, 575);

rightmouth = new ImageIcon("rightmouth.png");
rightmouth.paintIcon(this, g, snakexlength[0], snakeylength[0]);

for(int a = 0; a < lengthofsnake; a++)
{
if(a == 0 && right)
{
rightmouth = new ImageIcon("rightmouth.png");
rightmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]);
}
if(a == 0 && left)
{
leftmouth = new ImageIcon("lefttmouth.png");
leftmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]);
}
if(a == 0 && down)
{
downmouth = new ImageIcon("downmouth.png");
downmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]);
}
if(a == 0 && up)
{
upmouth = new ImageIcon("upmouth.png");
upmouth.paintIcon(this, g, snakexlength[a], snakeylength[a]);
}
if (a!=0)
{
snakeimage = new ImageIcon("snakeimage.png");
snakeimage.paintIcon(this, g, snakexlength[a], snakeylength[a]);
}

}

g.dispose();
}

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub

}

@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub

}
}

derekjohnson
Автор

after run the project there is nothing in output??

prashantshukla
Автор

bro I am trying to build this app in net-beans. I try to import the images but it wont showing while running the project. I need know where should i have to import the images in my project and is there any separate code for net beans?

madhurshanan
welcome to shbcf.ru