game

game

How to make 3D game in java Applet

View Answers

July 24, 2012 at 12:19 PM

Here is a code of snake game in java swing.

1)SnakeGame.java:

import javax.swing.JFrame;

public class SnakeGame extends JFrame {

public SnakeGame() {

add(new Snake());

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(320, 340);
setLocationRelativeTo(null);
setTitle("Snake");

setResizable(false);
setVisible(true);
}

public static void main(String[] args) {
new SnakeGame();
}
}

2)Snake.java:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Snake 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 Snake() {
addKeyListener(new TAdapter());
setBackground(Color.black);
ImageIcon iid = new ImageIcon(this.getClass().getResource("image3.gif"));
ball = iid.getImage();
ImageIcon iia = new ImageIcon(this.getClass().getResource("image2.png"));
apple = iia.getImage();
ImageIcon iih = new ImageIcon(this.getClass().getResource("image1.gif"));
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;
}
locateStar();
timer = new Timer(DELAY, this);
timer.start();
}

July 24, 2012 at 12:20 PM

continue....

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++;
locateStar();
}
}
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 locateStar() {
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;
}
}
}
}

July 24, 2012 at 12:21 PM

Here is a code of TicTacToe game. We usually say cross and zero to it.

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TicTacToe implements ActionListener {
private int[][] winCombinations = new int[][] {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8},
{0, 3, 6}, {1, 4, 7}, {2, 5, 8},
{0, 4, 8}, {2, 4, 6}};
private JFrame window = new JFrame("Tic-Tac-Toe");
private JButton buttons[] = new JButton[9];
private int count, xWins, oWins = 0;
private String letter = "";
private boolean win = false;
public TicTacToe(){
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
window.setLocationRelativeTo(null);
for(int i=0; i<=8; i++){
buttons[i] = new JButton();
window.add(buttons[i]);
buttons[i].addActionListener(this);
}
window.setVisible(true);
}
public void actionPerformed(ActionEvent a) {
count++;
if(count % 2 == 0){
letter = "O";
} else {
letter = "X";
}
JButton pressedButton = (JButton)a.getSource();
pressedButton.setText(letter);
pressedButton.setEnabled(false);
for(int i=0; i<=7; i++){
if( buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText()) &&
buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText()) &&
buttons[winCombinations[i][0]].getText() != ""){
win = true;
}
}
if(win == true){
if (count % 2 == 0){
JOptionPane.showMessageDialog(null, "Player O WINS the game!");
saveName();
playAgainDialog();
}else
JOptionPane.showMessageDialog(null, "Player X WINS the game!");
saveName();
playAgainDialog();
} else if(count == 9 && win == false){
JOptionPane.showMessageDialog(null, "The game was tie!");
playAgainDialog();
}
}
public void playAgainDialog() {
if(letter.equals("X")) xWins++;
else oWins++;
int response = JOptionPane.showConfirmDialog(null, "Do you want to play again?","Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if(response == JOptionPane.YES_OPTION) reset();
else window.hide();
}
public void reset() {
for (int i=0; i<=8; i++){
buttons[i].setText("");
buttons[i].setEnabled(true);
}
win = false;
count = 0;
}
public void saveName(){
int response = JOptionPane.showConfirmDialog(null, "Do you want to save your name?","Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if(response == JOptionPane.YES_OPTION){
String name= JOptionPane.showInputDialog(null,"Enter Name (Player X or Player O)");
try{
File file = new File("TicTacToe.txt");
FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(name);
out.newLine();
out.close();
}
catch(Exception e){}
}
}
public static void main(String[] args){
new TicTacToe();
}
}









Related Tutorials/Questions & Answers:
Game
Game  How to develop a game using java? give me a total code for any simple game
game
game  How to make 3D game in java Applet
Advertisements
Game
Game  how can create simple game in java . thank u so match ,please help
game
game  Hi thank you for answers.I am devoloping a java game caled Duck Hunt Game in netbeans6.7.1 software and I need help in some codes...,if you have this game's code please post me as soon as possible
java game
java game  Can anyone please give me a game source code
game programming
game programming  Hi. i have witten the script and have almost completed developing a social game like mafia wars. can any one plz help me by answering me some of my doubts.. after uploading the game on facebook how tokeep data
JAVA Game
JAVA Game  I want to make a JAVA game...in dat game...d player has to input a word in 10 secnds...4 dat m running a delay loop... but the problem is not...i am not getting any way...to make that input field(eg: String str
game Nim - Java Beginners
game Nim  how is the search tree for a game Nim looks like
tetris game code - Swing AWT
tetris game code  To develop a JAVA puzzle game which is a "variation" of the Tetris game
Artifacts of cn.q-game
List of Artifacts of cn.q-game maven depenency
Blackjack game - IDE Questions
Blackjack game  Create a java code for a simple blackjack game for just one hand of Blackjack, it does not need to include money and betting
Create a Swamp Game in Java
Create a Swamp Game in Java  You are to create a game called â??Get Out of My Swampâ??. In this game an ogre, called â??Hekâ??, wanders around his... two ogre enemies in the same place they kill the ogre and the game ends. Full
Java Game Pontoon
Java Game Pontoon  Could someone help me create a programme for the game pontoon //play a game of pontoon import java.util.*; public class Pontoon { public static void main (String args[]) { //deal initial hands
Version of cn.q-game>akka-log4j2-logger_2.11 dependency
Version of cn.q-game>easyconfig_2.11 dependency
Version of cn.q-game>easyfiles_2.11 dependency
Version of cn.q-game>engine_2.11 dependency
ModuleNotFoundError: No module named 'Apples_Game'
ModuleNotFoundError: No module named 'Apples_Game'  Hi, My Python... 'Apples_Game' How to remove the ModuleNotFoundError: No module named 'Apples_Game' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'Apples_Game'
ModuleNotFoundError: No module named 'Apples_Game'  Hi, My Python... 'Apples_Game' How to remove the ModuleNotFoundError: No module named 'Apples_Game' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'life-game'
ModuleNotFoundError: No module named 'life-game'  Hi, My Python... 'life-game' How to remove the ModuleNotFoundError: No module named 'life-game' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'market-game'
ModuleNotFoundError: No module named 'market-game'  Hi, My Python... 'market-game' How to remove the ModuleNotFoundError: No module named 'market-game' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'numbereddoor-game'
ModuleNotFoundError: No module named 'numbereddoor-game'  Hi, My... named 'numbereddoor-game' How to remove the ModuleNotFoundError: No module named 'numbereddoor-game' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'pacman-game'
ModuleNotFoundError: No module named 'pacman-game'  Hi, My Python... 'pacman-game' How to remove the ModuleNotFoundError: No module named 'pacman-game' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'pacman-game'
ModuleNotFoundError: No module named 'pacman-game'  Hi, My Python... 'pacman-game' How to remove the ModuleNotFoundError: No module named 'pacman-game' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'platformer-game'
ModuleNotFoundError: No module named 'platformer-game'  Hi, My... named 'platformer-game' How to remove the ModuleNotFoundError: No module named 'platformer-game' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'platformer-game'
ModuleNotFoundError: No module named 'platformer-game'  Hi, My... named 'platformer-game' How to remove the ModuleNotFoundError: No module named 'platformer-game' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'pong-game'
ModuleNotFoundError: No module named 'pong-game'  Hi, My Python... 'pong-game' How to remove the ModuleNotFoundError: No module named 'pong-game' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'ponzischeme-game'
ModuleNotFoundError: No module named 'ponzischeme-game'  Hi, My... named 'ponzischeme-game' How to remove the ModuleNotFoundError: No module named 'ponzischeme-game' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'PyOpenGL_game'
ModuleNotFoundError: No module named 'PyOpenGL_game'  Hi, My... 'PyOpenGL_game' How to remove the ModuleNotFoundError: No module named 'PyOpenGL_game' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'scythe-game'
ModuleNotFoundError: No module named 'scythe-game'  Hi, My Python... 'scythe-game' How to remove the ModuleNotFoundError: No module named 'scythe-game' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'simon-game'
ModuleNotFoundError: No module named 'simon-game'  Hi, My Python... 'simon-game' How to remove the ModuleNotFoundError: No module named 'simon-game' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'slacker-game'
ModuleNotFoundError: No module named 'slacker-game'  Hi, My Python... 'slacker-game' How to remove the ModuleNotFoundError: No module named 'slacker-game' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'sneak-game'
ModuleNotFoundError: No module named 'sneak-game'  Hi, My Python... 'sneak-game' How to remove the ModuleNotFoundError: No module named 'sneak-game' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'telegram-game'
ModuleNotFoundError: No module named 'telegram-game'  Hi, My... 'telegram-game' How to remove the ModuleNotFoundError: No module named 'telegram-game' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'telegram-game'
ModuleNotFoundError: No module named 'telegram-game'  Hi, My... 'telegram-game' How to remove the ModuleNotFoundError: No module named 'telegram-game' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'tes-game'
ModuleNotFoundError: No module named 'tes-game'  Hi, My Python...-game' How to remove the ModuleNotFoundError: No module named 'tes-game... to install padas library. You can install tes-game python with following
ModuleNotFoundError: No module named 'tes-game'
ModuleNotFoundError: No module named 'tes-game'  Hi, My Python...-game' How to remove the ModuleNotFoundError: No module named 'tes-game... to install padas library. You can install tes-game python with following
ModuleNotFoundError: No module named 'test-game'
ModuleNotFoundError: No module named 'test-game'  Hi, My Python... 'test-game' How to remove the ModuleNotFoundError: No module named 'test-game' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'tetris-game'
ModuleNotFoundError: No module named 'tetris-game'  Hi, My Python... 'tetris-game' How to remove the ModuleNotFoundError: No module named 'tetris-game' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'twister-game'
ModuleNotFoundError: No module named 'twister-game'  Hi, My Python... 'twister-game' How to remove the ModuleNotFoundError: No module named 'twister-game' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'type-this-game'
ModuleNotFoundError: No module named 'type-this-game'  Hi, My... named 'type-this-game' How to remove the ModuleNotFoundError: No module named 'type-this-game' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'Uno-Game'
ModuleNotFoundError: No module named 'Uno-Game'  Hi, My Python...-Game' How to remove the ModuleNotFoundError: No module named 'Uno-Game... to install padas library. You can install Uno-Game python with following
ModuleNotFoundError: No module named 'Word-game'
ModuleNotFoundError: No module named 'Word-game'  Hi, My Python... 'Word-game' How to remove the ModuleNotFoundError: No module named 'Word-game' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'adventure-game'
ModuleNotFoundError: No module named 'adventure-game'  Hi, My... named 'adventure-game' How to remove the ModuleNotFoundError: No module named 'adventure-game' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'Apples_Game'
ModuleNotFoundError: No module named 'Apples_Game'  Hi, My Python... 'Apples_Game' How to remove the ModuleNotFoundError: No module named 'Apples_Game' error? Thanks   Hi, In your python environment
ModuleNotFoundError: No module named 'ascii-game'
ModuleNotFoundError: No module named 'ascii-game'  Hi, My Python... 'ascii-game' How to remove the ModuleNotFoundError: No module named 'ascii-game' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'battleship_game'
ModuleNotFoundError: No module named 'battleship_game'  Hi, My... named 'battleship_game' How to remove the ModuleNotFoundError: No module named 'battleship_game' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'Card-Game'
ModuleNotFoundError: No module named 'Card-Game'  Hi, My Python... 'Card-Game' How to remove the ModuleNotFoundError: No module named 'Card-Game' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'cryptic-game'
ModuleNotFoundError: No module named 'cryptic-game'  Hi, My Python... 'cryptic-game' How to remove the ModuleNotFoundError: No module named 'cryptic-game' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'django-game'
ModuleNotFoundError: No module named 'django-game'  Hi, My Python... 'django-game' How to remove the ModuleNotFoundError: No module named 'django-game' error? Thanks   Hi, In your python environment

Ads