Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
easyhunn authored Nov 5, 2019
1 parent b7fc4d3 commit e9dacbe
Show file tree
Hide file tree
Showing 16 changed files with 848 additions and 0 deletions.
91 changes: 91 additions & 0 deletions Assets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import java.awt.*;
import java.awt.image.BufferedImage;

public class Assets {
public static final int width = 70, height = 130, offset = 26, fOffset = 22;
public static final int offsetf = 28, fOffsetf = 39, widthf = 70, offsetW = 12;
public static BufferedImage p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, bg, resized;
public static BufferedImage fp1, fp2, fp3, fp4, fp5, fp6, fp7, fp8, fp9, fp10, fp11, fp12,
t1, bullet, blood;

public static BufferedImage resize(BufferedImage img, int height, int width) {
Image tmp = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = resized.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return resized;
}

public static void init(){
SpriteSheet sheet = new SpriteSheet((imageLoader.loadImage("image/bone1.png")));
SpriteSheet fsheet = new SpriteSheet((imageLoader.loadImage("image/bonef.png")));
SpriteSheet tower = new SpriteSheet(imageLoader.loadImage("image/tower/towers.png"));
SpriteSheet Bullet = new SpriteSheet(imageLoader.loadImage("image/bullet/bullet.png"));
SpriteSheet bloodBar = new SpriteSheet(imageLoader.loadImage("image/blood/bloodBar.png"));

blood = bloodBar.crop(20,0,1250,90);
blood = resize(blood,6,70);
t1 = tower.crop(50,80,460,970);
bullet = Bullet.crop(0,0,65,65);
t1 = resize(t1,100,60);
bullet = resize(bullet,12,12);

p1 = sheet.crop(fOffset,565,width,height);
p2 = sheet.crop(width + fOffset + offset,565,width,height);
p3 = sheet.crop(2*width +fOffset+2*offset,565,width,height);
p4 = sheet.crop(3*width +fOffset+ 3*offset,565,width,height);
p5 = sheet.crop(4*width+fOffset+4*offset,565,width,height);
p6 = sheet.crop(5*width+fOffset +5*offset,565,width,height);
p7 = sheet.crop(6*width+fOffset + 6*offset,565,width,height);
p8 = sheet.crop(7*width+fOffset + 7*offset,565,width,height);
p9 = sheet.crop(8*width+fOffset + 8*offset,565,width,height);
p10 = sheet.crop(9*width+fOffset + 9*offset,565,width,height);
p11 = sheet.crop(10*width+fOffset + 10*offset,565,width,height);
p12 = sheet.crop(11*width+fOffset + 11*offset,565,width,height);
p1 = resize(p1,height/3, width/3);
p2 = resize(p2,height/3, width/3);
p3 = resize(p3,height/3, width/3);
p4 = resize(p4,height/3, width/3);
p5 = resize(p5,height/3, width/3);
p6 = resize(p6,height/3, width/3);
p7 = resize(p7,height/3, width/3);
p8 = resize(p8,height/3, width/3);
p9 = resize(p9,height/3, width/3);
p10 = resize(p10,height/3, width/3);
p11 = resize(p11,height/3, width/3);
p12 = resize(p12,height/3, width/3);

//flip

fp1 = fsheet.crop(fOffset,565,width,height);
fp2 = fsheet.crop(widthf + fOffsetf + offsetf,565,width+offsetW,height);
fp3 = fsheet.crop(2*widthf +fOffsetf+2*offsetf,565,width+offsetW,height);
fp4 = fsheet.crop(3*widthf +fOffsetf+ 3*offsetf,565,width+offsetW,height);
fp5 = fsheet.crop(4*widthf+fOffsetf+4*offsetf,565,width+offsetW,height);
fp6 = fsheet.crop(5*widthf+fOffsetf +5*offsetf,565,width+offsetW,height);
fp7 = fsheet.crop(6*widthf+fOffsetf + 6*offsetf,565,width+offsetW,height);
fp8 = fsheet.crop(7*widthf+fOffsetf + 7*offsetf,565,width+offsetW,height);
fp9 = fsheet.crop(8*widthf+fOffsetf + 8*offsetf,565,width+offsetW,height);
fp10 = fsheet.crop(9*widthf+fOffsetf + 9*offsetf,565,width+offsetW,height);
fp11 = fsheet.crop(10*widthf+fOffsetf + 10*offsetf,565,width+offsetW,height);
fp12 = fsheet.crop(11*widthf+fOffsetf + 11*offsetf,565,width+offsetW,height);
fp1 = resize(fp1,height/3, (width+offsetW)/3);
fp2 = resize(fp2,height/3, width/3+offsetW/3);
fp3 = resize(fp3,height/3, width/3+offsetW/3);
fp4 = resize(fp4,height/3, width/3+offsetW/3);
fp5 = resize(fp5,height/3, width/3+offsetW/3);
fp6 = resize(fp6,height/3, width/3+offsetW/3);
fp7 = resize(fp7,height/3, width/3+offsetW/3);
fp8 = resize(fp8,height/3, width/3+offsetW/3);
fp9 = resize(fp9,height/3, width/3+offsetW/3);
fp10 = resize(fp10,height/3, width/3+offsetW/3);
fp11 = resize(fp11,height/3, width/3+offsetW/3);
fp12 = resize(fp12,height/3, width/3+offsetW/3);


bg = imageLoader.loadImage("image/back_ground/bg2.jpg");

}

}
22 changes: 22 additions & 0 deletions Bullet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import java.awt.*;

public class Bullet {
private int x, y, dame;

public Bullet(int x, int y, int dame) {
this.x = x;
this.y = y;
this.dame = dame;

}

public void tick(){

}

public void rend (Graphics g){
g.drawImage(Assets.bullet, x, y, null);
}


}
19 changes: 19 additions & 0 deletions Creature.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public abstract class Creature extends Entity {


protected int health;
protected double speed;

public Creature(double x, double y){
super(x, y);
health = 100;
speed = 2;
}

public double getSpeed() {
return speed;
}
public int getHealth() {
return health;
}
}
47 changes: 47 additions & 0 deletions Display.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import javax.swing.*;
import java.awt.*;

public class Display {

private JFrame frame;
private Canvas canvas;

private String title;
private int width;
private int height;

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

createDisplay();
}

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

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

frame.add(canvas);
frame.pack();

}

public Canvas getCanvas(){
return canvas;
}
public JFrame getFrame(){
return frame;
}
}
13 changes: 13 additions & 0 deletions Entity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.awt.*;

public abstract class Entity {

protected double x, y;
public Entity(double x, double y){
this.x = x;
this.y = y;
}

public abstract void tick();
public abstract void render(Graphics g);
}
141 changes: 141 additions & 0 deletions Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.List;

public class Game implements Runnable{
private Display display;
private int width, height;
private String title;

private boolean running = false;
private Thread thread;

private BufferStrategy bs, bg;
private Graphics g,g1;
private List<Graphics> g2;

//State
private State gameState;

//Score
protected int Score;

public Game(String title, int width, int height){
g2 = new ArrayList<>();
this.title = title;
this.width = width;
this.height = height;

Score = 6;

}

private void init(){

display = new Display(title, width, height);
Assets.init();

gameState = new gameState(this);

gameState.setState(gameState);
}

private void tick(){

if(gameState.getState() != null)
gameState.getState().tick();
}

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

g = bs.getDrawGraphics();
//CLear Screen

g.clearRect(0,0,width,height);
g1 = bs.getDrawGraphics();

for(int i = 0; i< State.numberEnermy; ++i){
g2.add(bs.getDrawGraphics());
}

//DRAW HERE

g.drawImage(Assets.bg,0,0,null);

for(int i=0; i<State.numberEnermy;++i){
if(gameState.getState() != null){
gameState.getState().render(g);
}
}

if(gameState.getState() != null)
gameState.getState().render(g);

//END DRAWING
bs.show();
g.dispose();

}

public void run(){

init();
int fps = 60;
double timePerTick = 100000000/fps;
double delta = 0;
long now;
long lastTime = System.nanoTime();

while(running){

now = System.nanoTime();
delta+= (now - lastTime)/timePerTick;
lastTime = now;

if(delta >=16) {

tick();
render();
delta -=16;
}
}

stop();

}


public synchronized void start(){
if(running)
return;
running = true;
thread = new Thread(this::run);
thread.start();
}

public synchronized void stop(){
if(!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}


//Score
public void setScore(int Score){
this.Score = Score;
}
public int getScore(){
return Score;
}
}
8 changes: 8 additions & 0 deletions Launcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public class Launcher {

public static void main(String[] args){
Game game = new Game("denfense tower",1280,700);
game.start();

}
}
Loading

0 comments on commit e9dacbe

Please sign in to comment.