Skip to content

Commit

Permalink
Added music player, automatic frame initialization, and more!
Browse files Browse the repository at this point in the history
  • Loading branch information
suncloudsmoon committed Aug 9, 2020
1 parent 236ed69 commit 2816ad1
Show file tree
Hide file tree
Showing 14 changed files with 628 additions and 97 deletions.
Binary file removed tilegame2d/bin/tilegame2d/MainCharacter.class
Binary file not shown.
Binary file removed tilegame2d/bin/tilegame2d/SimpleGame$1.class
Binary file not shown.
Binary file removed tilegame2d/bin/tilegame2d/SimpleGame.class
Binary file not shown.
8 changes: 0 additions & 8 deletions tilegame2d/src/tilegame2d/MainCharacter.java

This file was deleted.

55 changes: 55 additions & 0 deletions tilegame2d/src/tilegame2d/SimpleFrame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package tilegame2d;

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

/**
* Contains methods that automatically creates and sets values for JFrame.
*
* @author Ganesha Ajjampura
*
*/
public class SimpleFrame {

public JFrame frame;

/**
* Initializes the frame with default values.
*
* @param title A String value specifying the title of the JFrame object.
* @param icon A ImageIcon object containing the image for the JFrame icon.
* @param width An integer value specifying the width of the JFrame object.
* @param height An integer value specifying the height of the JFrame object.
*/
public void initializeFrame(String title, ImageIcon icon, int width, int height) {
frame = new JFrame();
frame.setTitle(title);
frame.setSize(width, height);
frame.setIconImage(icon.getImage());
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

/**
* Initializes the frame with default values and the animation class added.
*
* @param sg A object containing the child class of SimpleGraphics.
* @param title A String value specifying the title of the JFrame object.
* @param icon A ImageIcon object containing the image for the JFrame icon.
* @param width An integer value specifying the width of the JFrame object.
* @param height An integer value specifying the height of the JFrame object.
*/
public void initializeFrameForAnimation(SimpleGraphics sg, String title, ImageIcon icon, int width, int height) {
frame = new JFrame();
sg.screen();
frame.add(sg);
frame.setTitle(title);
frame.setSize(width, height);
frame.setLocationRelativeTo(null);
frame.setIconImage(icon.getImage());
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
89 changes: 0 additions & 89 deletions tilegame2d/src/tilegame2d/SimpleGame.java

This file was deleted.

110 changes: 110 additions & 0 deletions tilegame2d/src/tilegame2d/SimpleGameKeys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package tilegame2d;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

/**
* Contains methods that simply the process of key bindings.
*
* @author Ganesha Ajjampura
*
*/
public abstract class SimpleGameKeys {

private Action goUp;
private Action goDown;
private Action goLeft;
private Action goRight;

public abstract void moveUp();

public abstract void moveDown();

public abstract void moveLeft();

public abstract void moveRight();

/**
* Configures W A S D keys to an input and action map with key bindings.
*
* @param frame A JFrame object specifying the main frame used by the game.
*/
public void configureWasdKeyBindings(JFrame frame) {
goUp = new goUp();
goDown = new goDown();
goLeft = new goLeft();
goRight = new goRight();

JRootPane rootOfAll = frame.getRootPane();

// Move Up
rootOfAll.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "goUp");
rootOfAll.getActionMap().put("goUp", goUp);

// Move Down
rootOfAll.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("S"), "goDown");
rootOfAll.getActionMap().put("goDown", goDown);

// Move Left
rootOfAll.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("A"), "goLeft");
rootOfAll.getActionMap().put("goLeft", goLeft);

// Move Right
rootOfAll.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("D"), "goRight");
rootOfAll.getActionMap().put("goRight", goRight);
}

private class goUp extends AbstractAction {

private static final long serialVersionUID = -5327395479500958279L;

@Override
public void actionPerformed(ActionEvent e) {
moveUp();

}

}

private class goDown extends AbstractAction {

private static final long serialVersionUID = -2867139883452440832L;

@Override
public void actionPerformed(ActionEvent e) {
moveDown();

}

}

private class goLeft extends AbstractAction {

private static final long serialVersionUID = -3638859233582503618L;

@Override
public void actionPerformed(ActionEvent e) {
moveLeft();

}

}

private class goRight extends AbstractAction {

private static final long serialVersionUID = 7041504566770698300L;

@Override
public void actionPerformed(ActionEvent e) {
moveRight();

}

}
}
Loading

0 comments on commit 2816ad1

Please sign in to comment.