Skip to content
Permalink
Browse files

Added Laser, Controller, KeyboardInput. This allows the player to move

around, the laser should also be showing up and moving towards the
bottom right but it does not even show up for some reason.  My battery
is dying so I am uploading this but I will fix the laser later
  • Loading branch information
jdm13003 committed Apr 15, 2016
1 parent 7dcd106 commit 8ba4e500aa1fb1cb0aa5425b3cf0da1e7c2d281a
@@ -1,11 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.3
org.eclipse.jdt.core.compiler.compliance=1.5
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=ignore
org.eclipse.jdt.core.compiler.problem.enumIdentifier=ignore
org.eclipse.jdt.core.compiler.source=1.3
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.5
BIN +52.5 KB (2900%) resources/sprite_sheet.png
Binary file not shown.
@@ -0,0 +1,38 @@
package main;

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

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

Laser tempLaser;

Game game;

public Controller(Game game){
this.game = game;
addLaser(new Laser(100, 300, game));
}

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

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

tempLaser.render(g);
}
}
public void addLaser(Laser laser){
l.add(laser);
}
public void removeLaser(Laser laser){
l.remove(laser);
}
}
@@ -4,6 +4,7 @@
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;
@@ -27,9 +28,11 @@

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

public void init()
{
requestFocus();
BufferedImageLoader loader = new BufferedImageLoader();
try{

@@ -38,9 +41,10 @@ public void init()
}catch(IOException e){
e.printStackTrace();
}
SpriteSheet ss = new SpriteSheet(spriteSheet);
//player = ss.grabimage(1, 1, 16, 16);
addKeyListener(new KeyboardInput(this));

p = new Player(200, 200, this);
c = new Controller(this);
}


@@ -118,6 +122,7 @@ public void run() {

private void tick() {
p.tick();
c.tick();
}
private void render(){
BufferStrategy bufferstrat = this.getBufferStrategy();
@@ -131,11 +136,40 @@ private void render(){

//g.drawImage(player, 100, 100, this);
p.render(g);
c.render(g);

/////////////////
g.dispose();
bufferstrat.show();

}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_RIGHT){
p.setVelX(5);

} else if(key == KeyEvent.VK_LEFT){
p.setVelX(-5);
} else if(key == KeyEvent.VK_DOWN){
p.setVelY(5);
} else if(key == KeyEvent.VK_UP){
p.setVelY(-5);
}

}

public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_RIGHT){
p.setVelX(0);
} else if(key == KeyEvent.VK_LEFT){
p.setVelX(0);
} else if(key == KeyEvent.VK_DOWN){
p.setVelY(0);
} else if(key == KeyEvent.VK_UP){
p.setVelY(0);
}

}

public BufferedImage getSpriteSheet(){
@@ -0,0 +1,25 @@
package main;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

//whenever a key is pressed this runs
public class KeyboardInput extends KeyAdapter{


Game game;

public KeyboardInput(Game game){
this.game = game;
}
@Override
public void keyPressed(KeyEvent e){
game.keyPressed(e);

}
@Override
public void keyReleased(KeyEvent e){
game.keyReleased(e);
}

}
@@ -0,0 +1,30 @@
package main;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

public class Laser {
private double x;
private double y;

BufferedImage laser;



public Laser(double x, double y, Game game){
this.x = x;
this.y = y;

SpriteSheet ss = new SpriteSheet(game.getSpriteSheet());

laser = ss.grabimage(1, 1, 64, 64);
}

public void tick(){
x+=1;
y+=1;
}
public void render(Graphics g){
g.drawImage(laser, (int)x, (int)y, null);
}
}
@@ -7,6 +7,9 @@
private double x;
private double y;

private double velX = 0;
private double velY = 0;

private BufferedImage player;

public Player(double x, double y, Game game){
@@ -15,15 +18,48 @@ public Player(double x, double y, Game game){

SpriteSheet ss = new SpriteSheet(game.getSpriteSheet());

player = ss.grabimage(1 ,1, 32, 32);
player = ss.grabimage(1 ,1, 64, 64);
}

public void tick(){
x++;
x += velX;
y += velY;

if (x <= 0 ){
x = 0;
}
if (x >= 736){
x = 736;
}
if (y <= 0){
y = 0;
}
if (y >= 536){
y = 536;
}
}

public void render(Graphics g){
g.drawImage(player, (int)x, (int)y, null);
}
public double getX(){
return x;
}
public double getY(){
return y;
}
public void setX(double x){
this.x = x;
}
public void setY(double y){
this.y = y;

}
public void setVelX(double velX){
this.velX = velX;
}
public void setVelY(double velY){
this.velY = velY;
}

}

0 comments on commit 8ba4e50

Please sign in to comment.
You can’t perform that action at this time.