So i’ve gotten the very basics of my game working, but am having a hard time moving my character. I have my Inputmanager class, but I can’t seem to use addKeyListner. Also is their anything else that seems problematic with my code?
Main Class
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import javax.swing.ImageIcon;
public class Game {
public Renderer s;
public PlayerShip player= new PlayerShip();
private Image bg;
private Image txt1;
private Image txt2;
private Image txt3;
private Image txt4;
private Ani a;
private int moveSpeed = 5;
public boolean waitingForKeyPress = false;
public boolean upPressed = false;
public boolean downPressed = false;
public boolean rightPressed = false;
public boolean leftPressed = false;
public boolean islooping = true;
private static final DisplayMode modes1[] = {
new DisplayMode(800,600,32,0),
new DisplayMode(800,600,24,0),
new DisplayMode(800,600,16,0),
new DisplayMode(640,480,32,0),
new DisplayMode(640,480,24,0),
new DisplayMode(640,480,16,0),
};;
public void run(DisplayMode dm2){
s = new Renderer();
DisplayMode dm = s.find1stcompatmode(modes1);
s.setFullScreen(dm);
loadmenu();
player.loadplayer();
movieLoop();
while(islooping){
movieLoop();
player.setHorizontalMovement(0);
if ((leftPressed) && (!rightPressed)) {
player.setHorizontalMovement(-moveSpeed);
} else if ((rightPressed) && (!leftPressed)) {
player.setHorizontalMovement(moveSpeed);
}
}
s.restoreScreen();
}
public void movieLoop(){
long startTime = System.currentTimeMillis();
long cumTime = startTime;
while(cumTime - startTime<2000){
long timePassed = System.currentTimeMillis() - cumTime;
cumTime = System.currentTimeMillis();
player.f.update(timePassed);
a.update(timePassed);
Graphics2D g = s.getGraphics();
draw(g);
g.dispose();
s.update();
try{
Thread.sleep(10);
}catch(Exception ex){}
}
}
public void loadmenu(){
bg = new ImageIcon("J:\\New folder (2)\\Untitled-2.jpg").getImage();
txt1 = new ImageIcon("C:\\Users\\Nima\\workspace\\ulimate gunner.png").getImage();
txt2 = new ImageIcon("C:\\Users\\Nima\\workspace\\ulimate gunner2.png").getImage();
txt3 = new ImageIcon("C:\\Users\\Nima\\workspace\\ulimate gunner3.png").getImage();
txt4 = new ImageIcon("C:\\Users\\Nima\\workspace\\ulimate gunner4.png").getImage();
a = new Ani();
a.addScene(txt1, 80);
a.addScene(txt2, 80);
a.addScene(txt3, 80);
a.addScene(txt4, 80);
}
public void draw(Graphics2D g){
g.drawImage(bg,0,0, null);
g.drawImage(player.f.getImage(), player.getX() ,player.getY(), null);
g.drawImage(a.getImage(), 400, 150, null);
}
class KeyInputHandler extends KeyAdapter {
/**
* Notification from AWT that a key has been pressed. Note that
* a key being pressed is equal to being pushed down but *NOT*
* released. Thats where keyTyped() comes in.
*
* @param e The details of the key that was pressed
*/
public void keyPressed(KeyEvent e) {
// if we're waiting for an "any key" typed then we don't
// want to do anything with just a "press"
if (waitingForKeyPress) {
return;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
upPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = true;
}
}
/**
* Notification from AWT that a key has been released.
*
* @param e The details of the key that was released
*/
public void keyReleased(KeyEvent e) {
// if we're waiting for an "any key" typed then we don't
// want to do anything with just a "released"
if (waitingForKeyPress) {
return;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
upPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
downPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
leftPressed = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
rightPressed = false;
}
}
}
public static void main(String argv[]) {
DisplayMode dm = new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
Game game = new Game();
game.run(dm);
}
}
Player Class
import java.awt.Image;
import javax.swing.ImageIcon;
public class PlayerShip{
public Ani f;
Image sf1;
Image sf2;
Image sf3;
int x=400;
int y=150;
int dy;
int dx;
public PlayerShip() {
}
public void setHorizontalMovement(int dx) {
this.dx= dx;
}
public void setVerticalMovement(int dy) {
this.dy= dy;
}
public double getHorizontalMovement() {
return dx;
}
public double getVerticalMovement() {
return dy;
}
public void move(long delta) {
if ((dx < 0) && (x < 10)) {
return;
}
if ((dx > 0) && (x > 780)) {
return;
}
x += dx;
y += dy;
}
public int getX() {
return (int) x;
}
public int getY() {
return (int) y;
}
public void loadplayer(){
sf1 = new ImageIcon("C:\\Users\\Nima\\workspace\\Compsci Game\\Sprites\\Playership\\stable fight\\f1playerstable.png").getImage();
sf2 = new ImageIcon("C:\\Users\\Nima\\workspace\\Compsci Game\\Sprites\\Playership\\stable fight\\f2playerstable.png").getImage();
sf3 = new ImageIcon("C:\\Users\\Nima\\workspace\\Compsci Game\\Sprites\\Playership\\stable fight\\f3playerstable.png").getImage();
f = new Ani();
f.addScene(sf1, 25);
f.addScene(sf2, 25);
f.addScene(sf3, 25);
}
}
Screen Manager
import java.awt.*;
import javax.swing.JFrame;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
public class Renderer{
private GraphicsDevice vc;
public Renderer(){
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = e.getDefaultScreenDevice();
}
public DisplayMode[] getCompatibleDisplayModes(){
return vc.getDisplayModes();
}
public DisplayMode find1stcompatmode(DisplayMode modes[]){
DisplayMode goodModes[] = vc.getDisplayModes();
for(int x=0; x<modes.length; x++){
for(int y =0; y<goodModes.length;y++){
if(displayModeMatch(modes[x], goodModes[y])){
return modes[x];
}
}
}
return null;
}
public DisplayMode getCurrentDisplayMode(){
return vc.getDisplayMode();
}
public boolean displayModeMatch(DisplayMode m1, DisplayMode m2){
if(m1.getWidth() != m2.getWidth() || m1.getHeight() != m2.getHeight()){
return false;
}
if(m1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && m1.getBitDepth() != m2.getBitDepth()){
return false;
}
if(m1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && m1.getRefreshRate() != m2.getRefreshRate()){
return false;
}
return true;
}
public void setFullScreen(DisplayMode dm){
JFrame f = new JFrame();
f.setUndecorated(true);
f.setIgnoreRepaint(true);
f.setResizable(false);
vc.setFullScreenWindow(f);
if(dm != null && vc.isDisplayChangeSupported()){
try{
vc.setDisplayMode(dm);
}catch(Exception ex){}
}
f.createBufferStrategy(2);
}
public Graphics2D getGraphics(){
Window w = vc.getFullScreenWindow();
if(w != null){
BufferStrategy s = w.getBufferStrategy();
return(Graphics2D)s.getDrawGraphics();
}else{
return null;
}
}
public void update(){
Window w = vc.getFullScreenWindow();
if(w != null){
BufferStrategy s = w.getBufferStrategy();
if(!s.contentsLost()){
s.show();
}
}
}
public Window getFullScreenWindow(){
return vc.getFullScreenWindow();
}
public int getWidth(){
Window w = vc.getFullScreenWindow();
if(w != null){
return w.getWidth();
}else{
return 0;
}
}
public int getHeight(){
Window w = vc.getFullScreenWindow();
if(w != null){
return w.getHeight();
}else{
return 0;
}
}
public void restoreScreen(){
Window w = vc.getFullScreenWindow();
if(w != null){
w.dispose();
}
vc.setFullScreenWindow(null);
}
public BufferedImage createCompatibleImage(int w, int h, int t){
Window win = vc.getFullScreenWindow();
if(win != null){
GraphicsConfiguration gc = win.getGraphicsConfiguration();
return gc.createCompatibleImage(w,h,t);
}
return null;
}
}
Animation Class
import java.awt.Image;
import java.util.ArrayList;
public class Ani {
private ArrayList<OneScene> scenes;
private int sceneIndex;
private long movieTime;
private long totalTime;
public Ani(){
scenes = new ArrayList<OneScene>();
totalTime = 0;
start();
}
public synchronized void addScene(Image i, long t){
totalTime+= t;
scenes.add(new OneScene(i, totalTime));
}
public synchronized void start(){
movieTime = 0;
sceneIndex = 0;
}
public synchronized void update(long timePassed){
if(scenes.size()>1){
movieTime += timePassed;
if (movieTime >= totalTime){
movieTime = 0;
sceneIndex = 0;
}
while(movieTime > getScene(sceneIndex).endTime){
sceneIndex++;
}
}
}
public synchronized Image getImage(){
if(scenes.size()==0){
return null;
}else{
return getScene(sceneIndex).pic;
}
}
protected OneScene getScene(int x){
return (OneScene)scenes.get(x);
}
}
class OneScene{
Image pic;
long endTime;
public OneScene(Image pic, long endTime){
this.pic = pic;
this.endTime = endTime;
}
}