Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Java/MiniTennis/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions Java/MiniTennis/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>MiniTennis</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
15 changes: 15 additions & 0 deletions Java/MiniTennis/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
8 changes: 8 additions & 0 deletions Java/MiniTennis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Mini-tennis
It is an interactive game developed for entertainment purpose with basic graphical interface in Java. It is One-Man Game i.e. for indivual players.

Skills used: Java, Object-Oriented programming, Game Development Basics (Spikes, Collision Detection), Threads

It containts 2 entities: Ball and Raquet. The player uses raquet to bounce back the ball and once ball crosses raquet the game ends. To make the game interesting, the speed of the ball increases by some percent each time it bounces off raquet.


65 changes: 65 additions & 0 deletions Java/MiniTennis/src/entities/Ball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package entities;

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

import game.Game;

public class Ball extends Entity{
private Game game;
private int score;

public Ball(Game game) {
this.game = game;
width = 30;
height = 30;
x = 0;
y = 30;
xMove = speed;
yMove = speed;
score = 0;
}

@Override
public void tick() {
getInput();
move();
}

private void getInput() {
if(x + xMove < 0)
xMove = speed;
if(x + xMove > game.getWidth() - width)
xMove = -speed;
if(y + yMove < 0)
yMove = speed;
if(y + yMove > game.getHeight() - game.raquet.getHeight())
game.gameOver();
if(game.raquet.getBounds().intersects(getBounds())) {
yMove = -speed;
y = game.raquet.getY() - height;
speed += 0.2;
score++;
}

}

@Override
public void render(Graphics g) {
g.drawOval(x, y, width, height);
g.fillOval(x, y, width, height);
g.setColor(Color.gray);
g.setFont(new Font("Verdana", Font.BOLD, 15));
g.drawString("Score: " + String.valueOf(getScore()), 10, 30);
}

public int getScore() {
return score;
}





}
38 changes: 38 additions & 0 deletions Java/MiniTennis/src/entities/Entity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package entities;

import java.awt.Graphics;
import java.awt.Rectangle;

public abstract class Entity {
protected int width;
protected int height;
protected int x,y;
protected float xMove,yMove;
protected static float speed = 1f;

public abstract void tick();
public abstract void render(Graphics g);

public void move() {
x += xMove;
y += yMove;
}

public Rectangle getBounds() {
return new Rectangle(x,y,width,height);
}

public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getX() {
return x;
}
public int getY() {
return y;
}

}
50 changes: 50 additions & 0 deletions Java/MiniTennis/src/entities/Raquet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package entities;

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

import game.Game;

public class Raquet extends Entity{

private Game game;

public Raquet(Game game) {
this.game = game;
width = 60;
height = 10;
x = 0;
y = 330;
xMove = 0;
yMove = 0;
}

@Override
public void tick() {
xMove = 0;
getInput();
if(x + xMove > 0 && x + xMove < game.getWidth() - 60) {
move();
}

}

private void getInput() {
if(game.getKeyManager().left) {
xMove = -speed;
}
if(game.getKeyManager().right) {
xMove = speed;
}
}

@Override
public void render(Graphics g) {
g.setColor(Color.black);
g.drawRect(x, y, width, height);
g.fillRect(x, y, width, height);
}



}
51 changes: 51 additions & 0 deletions Java/MiniTennis/src/game/Display.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package game;

import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Display {

private JFrame frame;
private Canvas canvas;

private String title;
private int width,height;
Game game;

public Display(Game game,String title,int width,int height) {
this.game = game;
this.title = title;
this.width = width;
this.height = height;
createDisplay();

}

private void createDisplay() {
frame = new JFrame(title);
frame.setSize(width+4, height);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);

canvas = new Canvas();
canvas.setPreferredSize(new Dimension(width,height));
canvas.setMaximumSize(new Dimension(width,height));
canvas.setMinimumSize(new Dimension(width,height));
canvas.setFocusable(false);
frame.add(canvas);
}

public Canvas getCanvas() {
return canvas;
}

public JFrame getFrame() {
return frame;
}

}
100 changes: 100 additions & 0 deletions Java/MiniTennis/src/game/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package game;

import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JOptionPane;

import entities.Ball;
import entities.Raquet;

public class Game implements Runnable{

private int height, width;
private String title;

private Display display;
private Graphics g;
private BufferStrategy bs;

private KeyManager keyManager;

private Ball ball;
public Raquet raquet;

public Game(String title,int width,int height) {
this.title = title;
this.height = height;
this.width = width;
keyManager = new KeyManager();
}

private void init() {
display = new Display(this,title,width,height);
display.getFrame().addKeyListener(keyManager);
ball = new Ball(this);
raquet = new Raquet(this);
}

public int getHeight() {
return height;
}

public int getWidth() {
return width;
}

public KeyManager getKeyManager() {
return keyManager;
}

public void run() {
init();
int fps = 60;
double timePerTick = 1000000000 / fps;
double delta = 0;
long now;
long lastTime = System.nanoTime();

while(true) {
now = System.nanoTime();
delta += (now - lastTime) / timePerTick;
lastTime = now;
if(delta >= 1) {
tick();
render();
delta--;
}
}

}

private void render() {
bs = display.getCanvas().getBufferStrategy();
if(bs == null) {
display.getCanvas().createBufferStrategy(2);
return;
}

g = bs.getDrawGraphics();
g.clearRect(0, 0, width, height);
ball.render(g);
raquet.render(g);
bs.show();
g.dispose();
}

private void tick() {
keyManager.tick();
ball.tick();
raquet.tick();
}

public void gameOver() {
JOptionPane.showMessageDialog(display.getFrame(), "Your Score is: " + ball.getScore(),
"Game Over", JOptionPane.YES_NO_OPTION);
System.exit(1);
}


}
Loading