- Saved searches
- Use saved searches to filter your results more quickly
- java-game
- Here are 325 public repositories matching this topic.
- FreeCol / freecol
- PixelGuys / Cubyz
- mingli1 / Unlucky
- mykola-dev / Tetris
- hdescottes / GdxGame
- TheDudeFromCI / WraithEngine
- arminkz / PlantsVsZombies
- Rohitjoshi9023 / KBC—Kaun-Banega-Crorepati
- 2003scape / rsc-client
- OSSpk / Minesweeper-Desktop-Game
- ashish2199 / Aidos
- Sesu8642 / FeudalTactics
- mtala3t / Super-Mario-Java-2D-Game
- kishanrajput23 / Java-Tutorials
- rafiulgits / SFC-The-Game
- derickfelix / GreatSpace
- mynttt / gnuman
- raydac / battleships-resurrection
- onurozuduru / java-shooter-game-project
- tonikolaba / BatBat-Game
- Improve this page
- Add this topic to your repo
- Java Snake
- Snake
- Development of Java Sname game
- Simple 2D arcade game in java with source code
- Source Code
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
java-game
Here are 325 public repositories matching this topic.
FreeCol / freecol
FreeCol: FreeCol is a turn-based strategy game based on the old game Colonization, and similar to Civilization. The objective of the game is to create an independent nation.
PixelGuys / Cubyz
Voxel game with extensive modding API and procedural generated content.
mingli1 / Unlucky
Java LibGDX Android 2D RPG Game
mykola-dev / Tetris
The well known brick game written with Kotlin Multiplatform and Jetpack Compose Multiplatform.
hdescottes / GdxGame
Turn-based RPG game using Java and libGDX
TheDudeFromCI / WraithEngine
A free, open source, Java game engine library built on top of LWJGL. Designed to act as a use-exactly-what-you-need, no-assumptions framework, WraithEngine is versatile enough to act as a library within other tools or projects, or a full fledged standalone game engine.
arminkz / PlantsVsZombies
My own clone of Plants Vs. Zombies game using Java/Swing
Rohitjoshi9023 / KBC—Kaun-Banega-Crorepati
It is Core Java based Game based on Indian television game show having best animation as possible in Core java 5000+ lines
2003scape / rsc-client
🎮 runescape classic web client
OSSpk / Minesweeper-Desktop-Game
💣 An object-oriented clone of the famous Windows game Minesweeper made in Java-Swing Framework following the Model View Controller (MVC) Architecture. Its a stand-alone desktop game which also provides save and load game functionalities.
ashish2199 / Aidos
A beginner friendly project with aim of creating our own version of Bomberman.
Sesu8642 / FeudalTactics
Strategy game with countless unique and challenging levels.
mtala3t / Super-Mario-Java-2D-Game
It is a simple Java 2D game like super mario.
kishanrajput23 / Java-Tutorials
Compilation of all study materials related to code with harry java course with assignment solutions
rafiulgits / SFC-The-Game
A game, developing for create awareness about cancer
derickfelix / GreatSpace
👾 Space Invaders Game Style
mynttt / gnuman
Satirical Pacman Clone with Map Editor using JavaFX
raydac / battleships-resurrection
Attempt to restore game-play of old demo-game Battleships written for E3 2001
onurozuduru / java-shooter-game-project
A simple Java game project for programming class.
tonikolaba / BatBat-Game
BatBat 🔋 ⚡ is an easy and free Maven Java game run in Spring Boot.
Improve this page
Add a description, image, and links to the java-game topic page so that developers can more easily learn about it.
Add this topic to your repo
To associate your repository with the java-game topic, visit your repo’s landing page and select «manage topics.»
Java Snake
In this part of the Java 2D games tutorial, we create a Java Snake game clone. Source code and images can be found at the author’s Github Java-Snake-Game repository.
Snake
Snake is an older classic video game. It was first created in late 70s. Later it was brought to PCs. In this game the player controls a snake. The objective is to eat as many apples as possible. Each time the snake eats an apple its body grows. The snake must avoid the walls and its own body. This game is sometimes called Nibbles.
Development of Java Sname game
The size of each of the joints of a snake is 10 px. The snake is controlled with the cursor keys. Initially, the snake has three joints. If the game is finished, the «Game Over» message is displayed in the middle of the board.
package com.zetcode; import java.awt.Color; import java.awt.Dimension; 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 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() < initBoard(); >private void initBoard() < addKeyListener(new TAdapter()); setBackground(Color.black); setFocusable(true); setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT)); loadImages(); initGame(); >private void loadImages() < ImageIcon iid = new ImageIcon("src/resources/dot.png"); ball = iid.getImage(); ImageIcon iia = new ImageIcon("src/resources/apple.png"); apple = iia.getImage(); ImageIcon iih = new ImageIcon("src/resources/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])) < 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; >> > >
First we will define the constants used in our game.
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;
The B_WIDTH and B_HEIGHT constants determine the size of the board. The DOT_SIZE is the size of the apple and the dot of the snake. The ALL_DOTS constant defines the maximum number of possible dots on the board (900 = (300*300)/(10*10)). The RAND_POS constant is used to calculate a random position for an apple. The DELAY constant determines the speed of the game.
private final int x[] = new int[ALL_DOTS]; private final int y[] = new int[ALL_DOTS];
These two arrays store the x and y coordinates of all joints of a snake.
In the loadImages() method we get the images for the game. The ImageIcon class is used for displaying PNG images.
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(); >
In the initGame() method we create the snake, randomly locate an apple on the board, and start the timer.
If the apple collides with the head, we increase the number of joints of the snake. We call the locateApple() method which randomly positions a new apple object.
In the move() method we have the key algorithm of the game. To understand it, look at how the snake is moving. We control the head of the snake. We can change its direction with the cursor keys. The rest of the joints move one position up the chain. The second joint moves where the first was, the third joint where the second was etc.
This code moves the joints up the chain.
This line moves the head to the left.
In the checkCollision() method, we determine if the snake has hit itself or one of the walls.
for (int z = dots; z > 0; z--) < if ((z >4) && (x[0] == x[z]) && (y[0] == y[z])) < inGame = false; >>
If the snake hits one of its joints with its head the game is over.
The game is finished if the snake hits the bottom of the board.
package com.zetcode; import java.awt.EventQueue; import javax.swing.JFrame; public class Snake extends JFrame < public Snake() < initUI(); >private void initUI() < 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 Snake(); ex.setVisible(true); >); > >
The setResizable() method affects the insets of the JFrame container on some platforms. Therefore, it is important to call it before the pack() method. Otherwise, the collision of the snake’s head with the right and bottom borders might not work correctly. Figure: Snake
This was the Snake game in Java.
Simple 2D arcade game in java with source code
Today we are going to see how to create a sample classic arcade video game using only a sample code of java. The object of the game (mini project) is to prevent the ball from falling while moving the cursor left or right. the score increase by +1 point in every touch and best score is displayed when the red ball falls. i have also made a challenge by activating another black ball, the user should avoid it or the game is over. in every +10 point the speed increase x2 to make the game difficult.
Source Code
import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionListener; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.Timer; import javax.swing.WindowConstants; public class RedBall extends JComponent implements ActionListener, MouseMotionListener, KeyListener private int ballx = 150; private int bally = 30; private int ballx1 = 100; private int bally1 = 10; private int paddlex = 0; private int ballySpeed = 7; private int ballxSpeed = 5; private int bally1Speed = 14; private int ballx1Speed = 10; public int score = 0; public int score1 = 0; private int scorefinal; public int bestscore; public int bestscore1; public boolean gameOver, started; public static void main(String[] args) JFrame wind = new JFrame("RedBall/GamePinfo"); RedBall g = new RedBall(); wind.add(g); wind.pack(); wind.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); wind.setLocationRelativeTo(null); wind.setVisible(true); wind.addMouseMotionListener(g); Timer tt = new Timer(17, g); tt.start(); > public void newball(int ballx, int bally, int ballxspeed, int ballyspeed) ballx = 150; bally = 30; ballxspeed = 5; ballyspeed = 7; JOptionPane.showMessageDialog(null, "new ball !"); return; > @Override public Dimension getPreferredSize() return new Dimension(800, 600); > @Override protected void paintComponent(Graphics g) //draw the sky g.setColor(Color.cyan); g.fillRect(0, 0, 800, 600); g.setColor(Color.GREEN); g.fillRect(0, 550, 800, 100); //draw the paddel g.setColor(Color.black); g.fillRect(paddlex, 500, 100, 20); //draw the ball g.setColor(Color.RED); g.fillOval(ballx, bally, 30, 30); //draw the ball_1 if (score >= 5) g.setColor(Color.BLACK); g.fillOval(ballx1, bally1, 30, 30); > //score if (score >= 5) g.setColor(Color.red); g.setFont(new Font("Arial", 8, 50)); g.drawString(String.valueOf(score + score1), 30 / 1 - 15, 80); > else g.setColor(Color.white); g.setFont(new Font("Arial", 8, 50)); g.drawString(String.valueOf(score), 30 / 1 - 15, 80); > // start && gameOver g.setColor(Color.white); g.setFont(new Font("Arial", 8, 50)); if (gameOver) g.drawString(String.valueOf(" Best Score :" + scorefinal), 50 / 1 - 15, 200); > > @Override public void actionPerformed(ActionEvent e) ballx = ballx + ballxSpeed; bally = bally + ballySpeed; // Window Down if (ballx >= paddlex && ballx paddlex + 100 && bally >= 475) ballySpeed = -7; score++; > if (bally >= 700 ) score = 0; bally = 30; gameOver = true; > // Window up if (bally 0) ballySpeed = 7; > // Window right if (ballx >= 775) ballxSpeed = -5; > // Window left if (ballx 0) ballxSpeed = 5; > //********************************************************************** ballx1 = ballx1 + ballx1Speed; bally1 = bally1 + bally1Speed; // Window down if (ballx1 >= paddlex && ballx1 paddlex + 100 && bally1 >= 475) bally1Speed = -14; score1++; > if (bally1 >= 700) score1 = 0; bally1 = 10; > // Window up if (bally1 0) bally1Speed = 14; > // Window right if (ballx1 >= 775) ballx1Speed = -10; > // Window left if (ballx1 0) ballx1Speed = 10; > //********************************************************************** bestscore = score; bestscore1 = score1; if (scorefinal > bestscore) scorefinal = scorefinal; > else scorefinal = bestscore1; scorefinal = score + score1; > if (scorefinal > bestscore) scorefinal = scorefinal; > else scorefinal = bestscore; scorefinal = score + score1; > repaint(); > @Override public void mouseMoved(MouseEvent e) paddlex = e.getX() - 50; repaint(); > @Override public void mouseDragged(MouseEvent e) > @Override public void keyTyped(KeyEvent e) > @Override public void keyPressed(KeyEvent e) > @Override public void keyReleased(KeyEvent e) > >