How to make 3D game in java Applet
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(); }
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; } } } }
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(); } }
Ads