Java Games Source Code

Sharing you the source code of this simple fighting game created in javaeclipse. You will learn character animation, basic interaction and some AI implementation. If you have some questions just spam me at markdotcabalaratyahoodotcom.

Projects

  • Advanced Wars Clone
    My attempt at an advanced wars like game.
  • Age of the Internet
    A Java game about a kid named Toby who's stuck inside the Internet.
  • ai agentwork
    Artificial Intelligence work on agents in game playing.
  • AndroidPunk
    , animation, input, and collision to name a few) is already coded for you and ready to go, giving you more time and energy to concentrate on the design and testing of your game.
  • angryships
    Prototype of a new game based on super flying thing, worms2 and angry birds ideas.
  • apollo
    An open source Java game server suite designed to be lightweight, fast and secure.
  • Arena
    Custom arena for any type of mini game.
  • atlantis
    A tile laying game that's fun for the whole family!.
  • basicblackjack
    Old school, console based blackjack game.
  • BlackJava
    A Blackjack game made with Java.
  • CaptureThePoints
    PVP capture game with wool.
  • casanova
    A charming general game player.
  • Chess
    A simple 2 player chess game with no AI.
  • cinnamon
    Dave and Brandon Clue game.
  • Civ6
    The ultimate civ game.
  • ClassiCubeLauncher
    Launcher/updated for the desktop version of the ClassiCube.net game client.
  • com.happyprog.pairhero
    Pair hero is a pair programming collaboration game.
  • com.team2.civ
    Civilization esque game project from Team2 (OOP course project).
  • Computer Games
    Fall '10, with Professor Perlin.
  • Continuity
    NaND Inc. software lab 4. homework repository, clone of the Continuity game.
  • Crapout
    A java based webapp for playing Craps, the popular dice game of chance.
  • CrossGate MLServer
    A Mail server for Online Game CrossGate.
  • Dominion
    Java version of the card game Dominion.
  • Doodle Games
    Ensemble de jeux de type Doodle.
  • DungeonCreeper
    A spout game , remake of the famous Dungeon Keeper.
  • epiris td example
    Public Domain fully featured tower defense game built on top of AndEngine.
  • Frogger
    Implementation of game Frogger in Java.
  • Game
    What for a game ? I dont know :P ;), surprise.
  • Game Grade 11
    Summative Project for Grade 11.
  • Game of Life in Java
    John Conways Game of Life in Java, either as a applet or as a stand alone program.
  • game theory poker
    A game theoretic poker player (written in 2005).
  • gamemasterdice
    Game Master Dice is (yet another) dice app, focused at the 3D20 system.
  • gamesim
    A simple social game workload simulator.
  • GamesRepo
    This is the initial repo for the games team.
  • Geronimoo s Dominion Simulator
    A tool to simulate games of Dominion (the card game ).
  • ggp base
    The General Game Playing Base Package.
  • Glitched
    Code repository for the game Glitched, by Glitched Studios.
  • Gmk Splitter
    GmkSplitter is a tool which can convert Game Maker.gmk and.gm81 files into a source control friendly directory, which contains the resources and scripts of the game as separate files. It can also reassemble this directory back into a new.gmk or.gm81 file. Binary downloads can be found on the project website, linked below.
  • gnubridge
    Open source java contract bridge game.
  • GraviTux
    cross platform open source game for our university project.
  • gte
    Game Theory Explorer: Build, explore and solve extensive form games.
  • GWLP R
    See Game Revision.com or the temporary project overview:.
  • Hackathon2010
    Flight PvP game for GTUG Stockholm Hackathon 2010.
  • Hexel
    A voxel based exploration game.
  • InkscapeToLevel
    Use Inkscape to create levels for your game projects.
  • INQSHTCT
    Guitar game with procedurally generated music.
  • ISI Social Game
    Aaron and Spencer's Prototype.
  • JCloisterZone
    Java implementation of Carcassonne board game.
  • JobScheduler
    Job Scheduling in Networked Manufacturing in Networked Enviornment using Game Theory.
  • Kaliber
    An application for easy access to using standard (HID) input devices, USB game controllers and PSMove motion controllers.
  • KillTeam
    KillTeam is a AI for a students competition @HAW Hamburg for the TankArena Game.
  • l2jserver2
    l2j game server rewrite interlude support added.
  • language game
    Java translators for various fun variants of English.
  • LateralGM
    A free Game Maker source file editor.
  • LunasDreamwalk
    Savegame editor for the popular mobile game 'My Little Pony'.
  • monopoly
    Projet Monopoly IA04 P2012 Simulation d'un jeu de monopoly avec plusieurs IA aux strat?gies diff?rentes // School project Simulation of a monopoly game involving bots player with artificial intelligence and different strategy to win the game.
  • netbang
    A networked computer version of the card game Bang!.
  • NovlServer
    The server powering Novl Game Social.
  • Othello
    the classic abstract strategy game.
  • PipeDream
    A copy of the old NES version of the game.
  • playn cookbook
    A collection of examples of common game techniques, written using PlayN.
  • PolandballRTS
    A Polandball RTS game coming out soon!.
  • Poolesville 2010 Games Project
    A computer game playing project for Poolesville High School's 2010 algorithms class.
  • Prelude of the Chambered
    Notch's Prelude of the Chambered 48 hour game.
  • prototypes
    A collection of tests and game prototypes.
  • QuantumDrive
    Open source RTS for the new handheld gaming system 'Pandora' at www.openpandora.org Please do not hesitate to donate. We need to get another pandora for testing.
  • R base
    R base is a game in which you play as a robot. You are either on the 'police' team, or the 'junkie' team. Your goal as the 'junkies' is to eliminate the other team within a certain amount of time, otherwise the 'police' win. Includes net play with unlimited players (eventually).
  • rand GAME
    Game made for Moosader 8 Competition Proceduraly Generated Elements.
  • ReadyMade
    An exploration into component based game development.
  • Refactoring Golf
    Refactoring Katas for the game 'Refactoring Golf'.
  • rock paper scissors in java
    The game rock paper scissors implemented using Event Sourcing in Java.
  • roguedream
    My failed 7DRL entry that used the web to autogenerate a game.
  • roguelike game development
    making a roguelike game like dnd.
  • ScoreBoard
    ScoreBoard is a web based system, to keep track of game results and player stats for small office games.
  • scribedroid
    The Mark Steere Abstract Game , Scribe.
  • SimplePowerStorage
    Just a simple way to store early game MJ.
  • social mongo spring data
    A simple project which demonstrates how MongoDB with Spring Data can be applied to create a backend of a social game.
  • sokoban client
    A client for the game sokoban, developed in a course on artificial intelligence.
  • Sorry Game
    Web browser based version of the board game Sorry?. Completed as part of a semester long project for CS205: Software Engineering at The University of Vermont, College of Engineering and Mathematical Science.
  • Squash
    Play game service sample 'Squash' with a Gradle build file.
  • sudo ku
    A Java based sudoku game.
  • Sudoku
    Sudoku game Technology Topic Class.
  • sudoku portlet
    Sudoku game implementation as portlet.
  • taken
    My game for Ludum Dare 20.
  • tdd tetris tutorial
    Tutorial for learning TDD. You make a Tetris game by writing code to pass the test cases. See README. Feedback is welcome!.
  • TeamKaiSuperHappyPlace
    Rock Paper Scissors game designed by select members of CS 151.
  • Telephone Game
    Let's play with speech recognition.
  • TerasologyLauncher
    Terasology Launcher is the official launcher for the open source game Terasology.
  • toohardforyou
    2H4U, which stands for Too Hard For You, is a mix between a Tetris like game and a wall breaker.
  • Tradesong
    A game about life (and money).
  • tyrant
    Tyrant Roguelike game in Java.
  • Ultimate Ascent
    FIRST Team 1923's official repository for the 2013 game , Ultimate Ascent.
  • Ursuppe Sample
    Example on how the Ursuppe game could be implemented (Programming2 exercise).
  • Wasteland MUD
    Multi User Dungeon command line game in Java.
  • zoogas
    Z00 G45. A simple entropy maximizing game on a Lotka Volterra cellular automaton.


Java Games Source Code
Home »
Java Free Code »
Game »
Game
Game 2D
Game 3D
Game adventure
Game API
Game engine
Game framework
Game GWT
Game HTML5
Game http
Game image
Game JavaFX
Game Library
Game multiplayer
Game network
Game plugin
Game Puzzle
Game RPG
Game shooter
Game Space
Minecraft
Minesweeper
TicTacToe
Tetris.java
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.Point;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjava.util.ArrayList;
importjava.util.Collections;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
publicclassTetrisextendsJPanel {
privatestaticfinallong serialVersionUID =-8715353373678321308L;
privatefinalPoint[][][] Tetraminos = {
// I-Piece
{
{ newPoint(0, 1), newPoint(1, 1), newPoint(2, 1), newPoint(3, 1) },
{ newPoint(1, 0), newPoint(1, 1), newPoint(1, 2), newPoint(1, 3) },
{ newPoint(0, 1), newPoint(1, 1), newPoint(2, 1), newPoint(3, 1) },
{ newPoint(1, 0), newPoint(1, 1), newPoint(1, 2), newPoint(1, 3) }
},
// J-Piece
{
{ newPoint(0, 1), newPoint(1, 1), newPoint(2, 1), newPoint(2, 0) },
{ newPoint(1, 0), newPoint(1, 1), newPoint(1, 2), newPoint(2, 2) },
{ newPoint(0, 1), newPoint(1, 1), newPoint(2, 1), newPoint(0, 2) },
{ newPoint(1, 0), newPoint(1, 1), newPoint(1, 2), newPoint(0, 0) }
},
// L-Piece
{
{ newPoint(0, 1), newPoint(1, 1), newPoint(2, 1), newPoint(2, 2) },
{ newPoint(1, 0), newPoint(1, 1), newPoint(1, 2), newPoint(0, 2) },
{ newPoint(0, 1), newPoint(1, 1), newPoint(2, 1), newPoint(0, 0) },
{ newPoint(1, 0), newPoint(1, 1), newPoint(1, 2), newPoint(2, 0) }
},
// O-Piece
{
{ newPoint(0, 0), newPoint(0, 1), newPoint(1, 0), newPoint(1, 1) },
{ newPoint(0, 0), newPoint(0, 1), newPoint(1, 0), newPoint(1, 1) },
{ newPoint(0, 0), newPoint(0, 1), newPoint(1, 0), newPoint(1, 1) },
{ newPoint(0, 0), newPoint(0, 1), newPoint(1, 0), newPoint(1, 1) }
},
// S-Piece
{
{ newPoint(1, 0), newPoint(2, 0), newPoint(0, 1), newPoint(1, 1) },
{ newPoint(0, 0), newPoint(0, 1), newPoint(1, 1), newPoint(1, 2) },
{ newPoint(1, 0), newPoint(2, 0), newPoint(0, 1), newPoint(1, 1) },
{ newPoint(0, 0), newPoint(0, 1), newPoint(1, 1), newPoint(1, 2) }
},
// T-Piece
{
{ newPoint(1, 0), newPoint(0, 1), newPoint(1, 1), newPoint(2, 1) },
{ newPoint(1, 0), newPoint(0, 1), newPoint(1, 1), newPoint(1, 2) },
{ newPoint(0, 1), newPoint(1, 1), newPoint(2, 1), newPoint(1, 2) },
{ newPoint(1, 0), newPoint(1, 1), newPoint(2, 1), newPoint(1, 2) }
},
// Z-Piece
{
{ newPoint(0, 0), newPoint(1, 0), newPoint(1, 1), newPoint(2, 1) },
{ newPoint(1, 0), newPoint(0, 1), newPoint(1, 1), newPoint(0, 2) },
{ newPoint(0, 0), newPoint(1, 0), newPoint(1, 1), newPoint(2, 1) },
{ newPoint(1, 0), newPoint(0, 1), newPoint(1, 1), newPoint(0, 2) }
}
};
privatefinalColor[] tetraminoColors = {
Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, Color.pink, Color.red
};
privatePoint pieceOrigin;
privateint currentPiece;
privateint rotation;
privateArrayList<Integer> nextPieces =newArrayList<Integer>();
privatelong score;
privateColor[][] well;
// Creates a border around the well and initializes the dropping piece
privatevoidinit() {
well =newColor[12][24];
for (int i =0; i <12; i++) {
for (int j =0; j <23; j++) {
if (i 0|| i 11|| j 22) {
well[i][j] =Color.GRAY;
} else {
well[i][j] =Color.BLACK;
}
}
}
newPiece();
}
// Put a new, random piece into the dropping position
publicvoidnewPiece() {
pieceOrigin =newPoint(5, 2);
rotation =0;
if (nextPieces.isEmpty()) {
Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6);
Collections.shuffle(nextPieces);
}
currentPiece = nextPieces.get(0);
nextPieces.remove(0);
}
// Collision test for the dropping piece
privatebooleancollidesAt(intx, inty, introtation) {
for (Point p :Tetraminos[currentPiece][rotation]) {
if (well[p.x + x][p.y + y] !=Color.BLACK) {
returntrue;
}
}
returnfalse;
}
// Rotate the piece clockwise or counterclockwise
publicvoidrotate(inti) {
int newRotation = (rotation + i) %4;
if (newRotation <0) {
newRotation =3;
}
if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) {
rotation = newRotation;
}
repaint();
}
// Move the piece left or right
publicvoidmove(inti) {
if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) {
pieceOrigin.x += i;
}
repaint();
}
// Drops the piece one line or fixes it to the well if it can't drop
publicvoiddropDown() {
if (!collidesAt(pieceOrigin.x, pieceOrigin.y +1, rotation)) {
pieceOrigin.y +=1;
} else {
fixToWell();
}
repaint();
}
// Make the dropping piece part of the well, so it is available for
// collision detection.
publicvoidfixToWell() {
for (Point p :Tetraminos[currentPiece][rotation]) {
well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece];
}
clearRows();
newPiece();
}
publicvoiddeleteRow(introw) {
for (int j = row-1; j >0; j--) {
for (int i =1; i <11; i++) {
well[i][j+1] = well[i][j];
}
}
}
// Clear completed rows from the field and award score according to
// the number of simultaneously cleared rows.
publicvoidclearRows() {
boolean gap;
int numClears =0;
for (int j =21; j >0; j--) {
gap =false;
for (int i =1; i <11; i++) {
if (well[i][j] Color.BLACK) {
gap =true;
break;
}
}
if (!gap) {
deleteRow(j);
j +=1;
numClears +=1;
}
}
switch (numClears) {
case1:
score +=100;
break;
case2:
score +=300;
break;
case3:
score +=500;
break;
case4:
score +=800;
break;
}
}
// Draw the falling piece
privatevoiddrawPiece(Graphicsg) {
g.setColor(tetraminoColors[currentPiece]);
for (Point p :Tetraminos[currentPiece][rotation]) {
g.fillRect((p.x + pieceOrigin.x) *26,
(p.y + pieceOrigin.y) *26,
25, 25);
}
}
@Override
publicvoidpaintComponent(Graphicsg)
{
// Paint the well
g.fillRect(0, 0, 26*12, 26*23);
for (int i =0; i <12; i++) {
for (int j =0; j <23; j++) {
g.setColor(well[i][j]);
g.fillRect(26*i, 26*j, 25, 25);
}
}
// Display the score
g.setColor(Color.WHITE);
g.drawString(''+ score, 19*12, 25);
// Draw the currently falling piece
drawPiece(g);
}
publicstaticvoidmain(String[] args) {
JFrame f =newJFrame('Tetris');
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(12*26+10, 26*23+25);
f.setVisible(true);
finalTetris game =newTetris();
game.init();
f.add(game);
// Keyboard controls
f.addKeyListener(newKeyListener() {
publicvoidkeyTyped(KeyEvente) {
}
publicvoidkeyPressed(KeyEvente) {
switch (e.getKeyCode()) {
caseKeyEvent.VK_UP:
game.rotate(-1);
break;
caseKeyEvent.VK_DOWN:
game.rotate(+1);
break;
caseKeyEvent.VK_LEFT:
game.move(-1);
break;
caseKeyEvent.VK_RIGHT:
game.move(+1);
break;
caseKeyEvent.VK_SPACE:
game.dropDown();
game.score +=1;
break;
}
}
publicvoidkeyReleased(KeyEvente) {
}
});
// Make the falling piece drop every second
newThread() {
@Overridepublicvoidrun() {
while (true) {
try {
Thread.sleep(1000);
game.dropDown();
} catch ( InterruptedException e ) {}
}
}
}.start();
}
}

commented Aug 26, 2016

no game over?

commented Feb 24, 2017

Hello DataWraith,

As a Software Engineering student at McMaster University, I had chosen to work with your project (Tetris) for one of my assignments.
The main task is to document the source code according to given guidelines. I was hoping that you would be able to revise my proposed comments and documentation and potentially merge those changes with your current project. I am sending you this as a comment as it is not possible to create pull requests on GitHub Gist, surprisingly. The changes I propose can be found on my public fork under this project.

Thank you for your time, and hope that you accept to merge.

Regards,
Kareem Abdel Mesih

commented Jul 16, 2017

Good day. The fork that I created adds additional functionality to your Tetris game, and with it you'll be able to see the theoretical position of the moving block. You'll probably learn something from the code I've written, for I'm also making my own Tetris game.

I could see improvements that could be added like adding a 'Game Over' feature, but your coding cleanliness is exceptional, and I even tried to implement the 'well array' idea to my own app.

Thanks, and I would love it if you could merge this.

commented May 15, 2018

Hi, I can't seem to run this on bluej

commented Jul 14, 2018

The tetr-a-mino is a typo. It's called tetr-o-mino, though we use the word tetra for 4-something.

commented Mar 1, 2019

Using a Thread is not good for java swing, especially if you are messing around with the user interface. I suggest you use a java.swing.timer for this.

Java Games Open Source Code

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment