Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
I added a ton of stuff including collision(right now lasers kill stuff
and continue on), movement, shooting, enemies spawning in randomly and
moving to the left with random speeds, background, I created some
interfaces. Right now the game is an actual "game" but you can't lose.
There is not yet a menu
  • Loading branch information
jdm13003 committed Apr 17, 2016
1 parent 542c0f9 commit 4e25ca4
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 47 deletions.
Binary file added resources/background.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/entity/EntityTypeA.java
@@ -0,0 +1,13 @@
package entity;

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

public interface EntityTypeA {

public void tick();
public void render(Graphics g);
public Rectangle getBounds();
public double getX();
public double getY();
}
14 changes: 14 additions & 0 deletions src/entity/EntityTypeB.java
@@ -0,0 +1,14 @@
package entity;

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

public interface EntityTypeB {

public void tick();
public void render(Graphics g);
public Rectangle getBounds();

public double getX();
public double getY();
}
71 changes: 54 additions & 17 deletions src/main/Controller.java
Expand Up @@ -2,37 +2,74 @@ package main;


import java.awt.Graphics; import java.awt.Graphics;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Random;

import entity.EntityTypeA;
import entity.EntityTypeB;


public class Controller { public class Controller {
private LinkedList<Laser> l = new LinkedList<Laser>(); private LinkedList<EntityTypeA> ea = new LinkedList<EntityTypeA>();

private LinkedList<EntityTypeB> eb = new LinkedList<EntityTypeB>();
Laser tempLaser;

EntityTypeA tempea;
Game game; EntityTypeB tempeb;
Random r = new Random();
private Skins skin;
private Game game;


public Controller(Game game){ public Controller(Skins skin, Game game){
this.skin = skin;
this.game = game; this.game = game;
addLaser(new Laser(100, 300, game)); }
public void createEnemy(int enemy_count){
for(int i = 0; i < enemy_count; i++){
addEntity(new EnemyFighter(810, r.nextInt(536), skin, this, game));

}
} }


public void tick(){ public void tick(){
for(int i = 0; i < l.size(); i++){ for(int n = 0; n < ea.size(); n++){
tempLaser = l.get(i); tempea = ea.get(n);

tempea.tick();
}
for(int n = 0; n < eb.size(); n++){
tempeb = eb.get(n);


tempLaser.tick(); tempeb.tick();
} }
} }
public void render(Graphics g){ public void render(Graphics g){
for(int i = 0; i < l.size(); i++){ for(int n = 0; n < ea.size(); n++){
tempLaser = l.get(i); tempea = ea.get(n);


tempLaser.render(g); tempea.render(g);
} }
for(int n = 0; n < eb.size(); n++){
tempeb = eb.get(n);

tempeb.render(g);
}
}
public void addEntity(EntityTypeA en){
ea.add(en);
}
public void removeEntity(EntityTypeA en){
ea.remove(en);
} }
public void addLaser(Laser laser){ public void addEntity(EntityTypeB en){
l.add(laser); eb.add(en);
} }
public void removeLaser(Laser laser){ public void removeEntity(EntityTypeB en){
l.remove(laser); eb.remove(en);
} }
public LinkedList<EntityTypeA> getEntityA(){
return ea;
}
public LinkedList<EntityTypeB> getEntityB(){
return eb;
}

} }

44 changes: 44 additions & 0 deletions src/main/EnemyFighter.java
@@ -0,0 +1,44 @@
package main;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

import entity.EntityTypeB;

public class EnemyFighter extends GameObject implements EntityTypeB{
private Skins skin;

Random r = new Random();
private Game game;
private Controller controller;
private int speed = r.nextInt(3)+1;

public EnemyFighter(double x, double y, Skins skin, Controller c, Game game){
super(x,y);
this.game = game;
this.controller = c;
this.skin = skin;
}
public double getX(){
return x;
}

public void tick(){
x -= speed;
if(GamePhysics.Collision(this, game.ea)){
controller.removeEntity(this);

game.setKills(game.getKills()+1);
}
}
public void render(Graphics g){
g.drawImage(skin.enemy1, (int)x, (int)y, null);
}
public double getY() {
return y;
}
public Rectangle getBounds(){
return new Rectangle((int)x, (int) y, 64, 64);
}
}
60 changes: 55 additions & 5 deletions src/main/Game.java
Expand Up @@ -2,33 +2,51 @@ package main;




import java.awt.Canvas; import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy; import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedList;
import java.util.Random;


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


import entity.EntityTypeA;
import entity.EntityTypeB;

public class Game extends Canvas implements Runnable{ public class Game extends Canvas implements Runnable{


private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public static final int Width = 800; public static final int Width = 800;
public static final int Height = 600; public static final int Height = 600;
public final String Title = "Padriaca"; public final String Title = "Star Wars";


private boolean gameon = false; private boolean gameon = false;
private Thread thread; private Thread thread;




private BufferedImage image = new BufferedImage(Width, Height, BufferedImage.TYPE_INT_RGB); private BufferedImage image = new BufferedImage(Width, Height, BufferedImage.TYPE_INT_RGB);
private BufferedImage spriteSheet = null; private BufferedImage spriteSheet = null;
private BufferedImage background = null;

private int is_shooting = 0;

private int enemy_count = 1;
private int kills = 0;
public LinkedList<EntityTypeA> ea;
public LinkedList<EntityTypeB> eb;

Random r = new Random();





//private BufferedImage player; //private BufferedImage player;
private Player p; private Player p;
private Controller c; private Controller c;
private Skins tp;


public void init() public void init()
{ {
Expand All @@ -37,14 +55,18 @@ public class Game extends Canvas implements Runnable{
try{ try{


spriteSheet = loader.loadimage("/sprite_sheet.png"); spriteSheet = loader.loadimage("/sprite_sheet.png");
background = loader.loadimage("/background.png");


}catch(IOException e){ }catch(IOException e){
e.printStackTrace(); e.printStackTrace();
} }
addKeyListener(new KeyboardInput(this)); addKeyListener(new KeyboardInput(this));

tp = new Skins(this);
p = new Player(200, 200, this); p = new Player(200, 200, tp, this);
c = new Controller(this); c = new Controller(tp, this);
ea = c.getEntityA();
eb = c.getEntityB();
c.createEnemy(enemy_count);
} }




Expand Down Expand Up @@ -123,6 +145,12 @@ public class Game extends Canvas implements Runnable{
private void tick() { private void tick() {
p.tick(); p.tick();
c.tick(); c.tick();

if(kills >= enemy_count){
enemy_count +=2;
kills = 0;
c.createEnemy(enemy_count);
}
} }
private void render(){ private void render(){
BufferStrategy bufferstrat = this.getBufferStrategy(); BufferStrategy bufferstrat = this.getBufferStrategy();
Expand All @@ -133,7 +161,7 @@ public class Game extends Canvas implements Runnable{
Graphics g = bufferstrat.getDrawGraphics(); Graphics g = bufferstrat.getDrawGraphics();
///////////////// /////////////////
g.drawImage(image, 0, 0, getWidth(), getHeight(), this); g.drawImage(image, 0, 0, getWidth(), getHeight(), this);

g.drawImage(background, 0,0, null);
//g.drawImage(player, 100, 100, this); //g.drawImage(player, 100, 100, this);
p.render(g); p.render(g);
c.render(g); c.render(g);
Expand All @@ -154,6 +182,13 @@ public class Game extends Canvas implements Runnable{
p.setVelY(5); p.setVelY(5);
} else if(key == KeyEvent.VK_UP){ } else if(key == KeyEvent.VK_UP){
p.setVelY(-5); p.setVelY(-5);
} else if(key == KeyEvent.VK_SPACE ){
if(is_shooting == 3){
is_shooting = 0;
}
if(is_shooting == 0){
c.addEntity(new Laser(p.getX(),p.getY(), tp, this));}
is_shooting++;
} }


} }
Expand All @@ -168,11 +203,26 @@ public class Game extends Canvas implements Runnable{
p.setVelY(0); p.setVelY(0);
} else if(key == KeyEvent.VK_UP){ } else if(key == KeyEvent.VK_UP){
p.setVelY(0); p.setVelY(0);
} else if(key == KeyEvent.VK_SPACE ){
is_shooting =0;
} }


} }


public BufferedImage getSpriteSheet(){ public BufferedImage getSpriteSheet(){
return spriteSheet; return spriteSheet;
} }

public int getEnemy_count() {
return enemy_count;
}
public void setEnemy_count(int enemy_count) {
this.enemy_count = enemy_count;
}
public int getKills() {
return kills;
}
public void setKills(int kills) {
this.kills = kills;
}
} }
17 changes: 17 additions & 0 deletions src/main/GameObject.java
@@ -0,0 +1,17 @@
package main;

import java.awt.Rectangle;

public class GameObject {
public double x;
public double y;
public GameObject(double x, double y){
this.x = x;
this.y = y;

}

public Rectangle getBounds(int width, int height){
return new Rectangle((int)x, (int) y, width, height);
}
}
25 changes: 25 additions & 0 deletions src/main/GamePhysics.java
@@ -0,0 +1,25 @@
package main;

import java.util.LinkedList;

import entity.EntityTypeA;
import entity.EntityTypeB;

public class GamePhysics {
public static boolean Collision(EntityTypeA enta, LinkedList<EntityTypeB> bb){
for(int n = 0; n <bb.size(); n++ ){
if(enta.getBounds().intersects(bb.get(n).getBounds())){
return true;
}
}
return false;
}
public static boolean Collision(EntityTypeB entb, LinkedList<EntityTypeA> aa){
for(int n = 0; n <aa.size(); n++ ){
if(entb.getBounds().intersects(aa.get(n).getBounds())){
return true;
}
}
return false;
}
}

0 comments on commit 4e25ca4

Please sign in to comment.