Yester
(Not Complete Yet)
Saturday, May 21, 2011
Sunday, May 8, 2011
Sunday, April 17, 2011
2D game in Java API - How to make a scroll game?
This is an extension of the previous post,
"2D game in Java API - Basic format."
A scroll game is a game that has a game map bigger than what is actually displayed.
The user can "scroll" through different parts of the map.
For example, Super Mario is a scroll game because its maps are much bigger than the screen.
As Mario moves right, the whole screen scrolls to the right in order to show hidden parts of the map.
In order to make a scroll game, you have to modify the MainPanel class.
Add the following variables to MainPanel's instance field ::
private int viewX; //x-coordinate of the view.
private int viewY; //y-coordinate of the view.
private final int VIEW_WIDTH; //Width of the view.
private final int VIEW_HEIGHT; //Height of the view.
private final int WORLD_WIDTH; //Width of the world.
private final int WORLD_HEIGHT; //Height of the world.
"WORLD" refers to the entire game map. All the interactions happen in this area.
"VIEW" refers to the area that is displayed on the screen.
If your game map is 1600 pixels long and 900 pixels high, set the WORLD_WIDTH to 1600 and
WORLD_HEIGHT to 900.
If your screen is 600 pixels long and 400 pixels high, set the VIEW_WIDTH to 600 and
VIEW_HEIGHT to 400.
If you want to display the upper-left part of the game map, set viewX and viewY to zero.
And so on.
Adding instance variables is not enough. You have to modify the paintComponent(Graphics g) method
in order to display the scroll effect.
Change the MainPanel class's paintComponent(Graphics g) method like this ::
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i = 0; i < actorList.size(); i++)
{
//If the game character is inside the view,
if((int)actorList.get(i).getX() > viewX-100
&& (int)actorList.get(i).getX() < viewX+VIEW_WIDTH+100
&& (int)actorList.get(i).getY() > viewY-100
&& (int)actorList.get(i).getY() < viewY+VIEW_HEIGHT+100)
{
//Draw the actor.
g.drawImage(actorList.get(i).getImage(),
(int)actorList.get(i).getX() - viewX,
(int)actorList.get(i).getY() - viewY, null);
}
}
}
When you subtract the upper-left position of the view (viewX,viewY) from the game character's
actual position (getX(),getY()), you get the position of the character to be displayed on the screen.
Try this on your own and see whether it works.
(Appendix)
Accessor / Modifier methods for this additional instance field ::
public int getViewX()
{
return viewX;
}
public void setViewX(int x)
{
viewX = x;
}
public int getViewY()
{
return viewY;
}
public void setViewY(int y)
{
viewY = y;
}
public int getViewWidth()
{
return VIEW_WIDTH;
}
public int getViewHeight()
{
return VIEW_HEIGHT;
}
public int getWorldWidth()
{
return WORLD_WIDTH;
}
public int getWorldHeight()
{
return WORLD_HEIGHT;
}
(Appendix)
Accessor / Modifier methods for this additional instance field ::
public int getViewX()
{
return viewX;
}
public void setViewX(int x)
{
viewX = x;
}
public int getViewY()
{
return viewY;
}
public void setViewY(int y)
{
viewY = y;
}
public int getViewWidth()
{
return VIEW_WIDTH;
}
public int getViewHeight()
{
return VIEW_HEIGHT;
}
public int getWorldWidth()
{
return WORLD_WIDTH;
}
public int getWorldHeight()
{
return WORLD_HEIGHT;
}
2D game in Java API - Basic format
Using Java to create a 2D game environment is quite complicated.
Yet, the following basic format outlines how to create a window and display game characters.
List of classes required
public class Runner // Where the program starts.
public class MainFrame extends JFrame // Where time and user inputs are managed.
public class MainPanel extends JPanel // Where game characters are displayed.
public abstract class Actor // A game character and its behaviors.
(And other concrete actor classes that extend Actor class)
Handler classes are inside the MainFrame class.
MainFrame class listens to events, and updates the MainPanel object.
MainPanel class updates its actors every time it is updated.
For example, TimeHandler in MainFrame tells MainPanel to update every 5 milliseconds,
and MainPanel tells its actors to update every 5 milliseconds.
Source Code (Based on Java 5.0)
Following lines are basic skeletons for the classes listed above:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i = 0; i < actorList.size(); i++)
{
g.drawImage(actorList.get(i).getImage(),
(int)actorList.get(i).getX(),
(int)actorList.get(i).getY(), null);
}
}
Yet, the following basic format outlines how to create a window and display game characters.
List of classes required
public class Runner // Where the program starts.
public class MainFrame extends JFrame // Where time and user inputs are managed.
public class MainPanel extends JPanel // Where game characters are displayed.
public abstract class Actor // A game character and its behaviors.
(And other concrete actor classes that extend Actor class)
Handler classes are inside the MainFrame class.
MainFrame class listens to events, and updates the MainPanel object.
MainPanel class updates its actors every time it is updated.
For example, TimeHandler in MainFrame tells MainPanel to update every 5 milliseconds,
and MainPanel tells its actors to update every 5 milliseconds.
Source Code (Based on Java 5.0)
Following lines are basic skeletons for the classes listed above:
public class Runner
{
public static void main(String[] args)
{
MainFrame mainFrame = new MainFrame();
}
}
public class MainFrame extends JFrame
{
private MainPanel mainPanel;
private TimeHandler timeHandler;
//Calls mainFrame's update() method every 5 milliseconds.
private Timer timer;
//Timer that calls timeHandler every 5 milliseconds.
public MainFrame()
{
mainPanel = new MainPanel();
add(mainPanel);
timeHandler = new TimeHandler();
timer = new Timer(5,null);
timer.addActionListener(timeHandler);
timer.start(); //Start the game.
setTitle("Title");
setSize(600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1,1));
setVisible(true);
}
//--------------------------------------------Inner classes
public class TimeHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
mainPanel.update();
}
}
/* Add some other Handler classes (ex: KeyHandler, MouseHandler) here.*/
}
public class MainPanel extends JPanel
{
private ArrayList<Actor> actorList; //List of all game characters.
public MainPanel()
{
actorList = new ArrayList<Actor>();
/*Add game characters to this list. */
}
//--------------------------------------------Core methods
public void update()
{
for(int i = 0; i < actorList.size(); i++)
{
actorList.get(i).update();
}
repaint();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
for(int i = 0; i < actorList.size(); i++)
{
g.drawImage(actorList.get(i).getImage(),
(int)actorList.get(i).getX(),
(int)actorList.get(i).getY(), null);
}
}
//--------------------------------------------Accessors/Modifiers
public ArrayList<Actor> getActorList()
{
return actorList;
}
public void addActor(Actor newActor)
{
actorList.add(newActor);
}
public void removeActor(Actor actorToRemove)
{
actorList.remove(actorToRemove);
}
public void toFront(Actor actor)
{
Actor tempActor = actor;
actorList.remove(actor);
actorList.add(tempActor);
}
public void toBack(Actor actor)
{
Actor tempActor = actor;
actorList.remove(actor);
actorList.add(0, tempActor);
}
}
public abstract class Actor
{
protected String name; //Name of the game character.
protected Image image; //Image of the game character.
protected double x; //x-coordinate of the character's position.
protected double y; //y-coordinate of the character's position.
protected double dx; //character's horizontal velocity.
protected double dy; //character's vertical velocity.
public Actor(String n, Image i, double ax, double wy)
{
name = n;
image = i;
x = ax;
y = wy;
dx = 0;
dy = 0;
}
//--------------------------------------------Core methods
public abstract void update();
public void controlMovements() //Include this in update() method.
{
x += dx;
y += dy;
}
//--------------------------------------------Accessors/Modifiers
public String getName()
{
return name;
}
public void setName(String n)
{
name = n;
}
public Image getImage()
{
return image;
}
public void setImage(Image i)
{
image = i;
}
public double getX()
{
return x;
}
public void setX(double ax)
{
x = ax;
}
public double getY()
{
return y;
}
public void setY(double why)
{
y = why;
}
public double getDx()
{
return dx;
}
public void setDx(double deltaX)
{
dx = deltaX;
}
public double getDy()
{
return dy;
}
public void setDy(double deltaY)
{
dy = deltaY;
}
}
(An example of Actor's subclass)
public class Monster extends Actor
{
public Monster(String n, Image i, double ax, double wy)
{
super(n,i,ax,wy);
}
//--------------------------------------------Core methods
public void update()
{
controlMovements();
}
}
Subscribe to:
Posts (Atom)












