Simple 2d games

Hello, I have started learning Java in college and it got me thinking about simple 2d computer games. I’m interested to know how one would go about learning to make them, what tools are needed (if any) for simple 2d games, and if it is possible to have a java game can be programmed to allow 4 controllers (4 people controlling four different objects).

Basically just a point in the right direction, some insight into the subject maybe, thanks.
Chris

Get a book on Java 2D,
and download an Integrated Development Environment,
eclipse is great, it will help newbies like you:

There are a few good books about java game programming and also a few tutorials in the web. I cant give any URL, because i didnt save them, but I have seen some java game prog tutors. Try some google and some research on gamedev sites, you might not find something instantly so dig deeper.

Java allows simple handling of mouse and keyboard input, I have never used a joystick so I cannot tell. But if you get 4 people on a keyboard, it will work. Or you try to develop a network multiplayer mode, for easy games it is not that hard.

Start off with simple arcade games. I wrote a Pong and a Breakoid first. It covers some basics, gives you a managable task and covers some basic gamedev stuff.

-JAW

I’ll upload my Tetris clone that I made back in my first Java class for you to look at. I made it four player compatible and had it all work okay. I wouldn’t buy a book, as that is unnecessary and I waste of money. Aside from BufferedImage, Java2D is a cakewalk. Unfortunately I am in class right now (and slacking :p) so I can’t upload at the moment. I’ll do so later today and provide a link.

Let me warn you that this was perhaops the first Java game I ever made, therefore it is incredibly inefficient, so I would only use it for understand only. Don’t copy the code unless you want something to run poorly.

A few notes:
Calling the repaint() method tells the game to match the monitor’s refresh rate and erase old stuff on the screen while drawing new stuff. When it is called, the paintComponent(Graphics g) method is used. This is where you do your actual drawing, using the Graphics context g. However, be sure to call the superclass paintComponent also, or nothing will be refreshed. Try looking at http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html Graphics to see drawing methods.

Hey thanks for all the replies so far, appreciate it. Eh that eclipse thing, what exactly does it do or what do i need it for? Demon, do i need to download anything to get ur tetris to run (I already have the java SDK)

Also, i know demon says i dont need a book but i’de like to get one. Do you guys know what would be the best java book for 2d gaming (atm i am only interested in 2d.) thanks

Eclipse is a compiler, go ahead and get it from http://www.eclipse.org/ I use it, it’s definitely the best free compiler out there.

Here’s my source code:


import java.awt.Graphics;
import java.awt.Color;
import java.util.ArrayList;

public class Block extends Collidable implements Drawable
{
// Variables
	Color color;
	Formation parent;
	
// Constructor
    public Block(int x, int y, int w, int h, Color c, Formation p)
    {
        super(x,y,w,h);
		color = c;
		parent = p;
    }
    
// Mutator Methods
    public void draw(Graphics g)
    {
		// Base Block
        g.setColor(color);
        g.fillRect(x,y,width,height);
		
		// Surrounding black lines
		g.setColor(Color.BLACK);
		g.drawLine(x,y,x+width,y);
		g.drawLine(x,y,x,y+height);
		g.drawLine(x+width,y,x+width,y+height);
		g.drawLine(x+width,y+height,x,y+height);
		
		// Darkest shading on the bottom right
		g.setColor(color.darker().darker().darker());
		g.drawLine(x+width-1,y+1,x+width-1,y+height-1);
		g.drawLine(x+width-1,y+height-1,x+1,y+height-1);
		
		// Lightest shading on the top left
		g.setColor(color.brighter().brighter().brighter());
		g.drawLine(x+1,y+1,x+width-1,y+1);
		g.drawLine(x+1,y+1,x+1,y+height-1);
		
		// Darker shading on the bottom right
		g.setColor(color.darker().darker());
		g.drawLine(x+width-2,y+3,x+width-2,y+height-2);
		g.drawLine(x+width-2,y+height-2,x+3,y+height-2);
		
		// Lighter shading on the top left
		g.setColor(color.brighter().brighter());
		g.drawLine(x+2,y+2,x+width-3,y+2);
		g.drawLine(x+2,y+2,x+2,y+height-3);
		
		// Dark shading on the bottom right
		g.setColor(color.darker());
		g.drawLine(x+width-3,y+5,x+width-3,y+height-3);
		g.drawLine(x+width-3,y+height-3,x+5,y+height-3);
		
		// Light shading on the top left
		g.setColor(color.brighter());
		g.drawLine(x+3,y+3,x+width-5,y+3);
		g.drawLine(x+3,y+3,x+3,y+height-5);
    }
	
	public void remove()
	{
		parent.removeBlock(this);
	}
	
// Accessor Methods
	public Formation parent()
	{
		return parent;
	}
	
	public Color color()
	{
		return color;
	}
}


import java.util.ArrayList;

public abstract class Collidable
{
    protected int x;
    protected int y;
    protected int width;
    protected int height;

    public Collidable()
    {
        x = y = width = height = 0;
    }
    
    public Collidable(int newX, int newY, int newWidth, int newHeight)
    {
        x = newX;
        y = newY;
        width = newWidth;
        height = newHeight;
    }

    public int getX()
    {
        return x;
    }
    
    public int getY()
    {
        return y;
    }
    
    public int width()
    {
        return width;
    }
    
    public int height()
    {
        return height;
    }

    public void move(int newX, int newY)
    {
        x = newX;
        y = newY;
    }
	
	public ArrayList getContents()
	{
		ArrayList returnValue = new ArrayList();
		returnValue.add(this);
		return returnValue;
	}
}


import java.awt.Graphics;

public interface Drawable
{
    public void draw(Graphics g);
}


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;

public class Driver extends JFrame
{
	private JMenuBar menuBar;
	private Timer timer = new Timer(15,null);
    private ArrayList panels;
	private ArrayList scoreboards;
	private ArrayList buttonsPressed;
    private GridBagLayout gbl;
    private GridBagConstraints gbc;
	private JRadioButtonMenuItem p1,p2,p3,p4;
	private SoundPlayer soundPlayer;
    
    public static void main(String[] args)
    {
        Driver tpo = new Driver();
        tpo.setSize(600,800);
        tpo.makeMenu();
		tpo.setVisible(true);
		tpo.runWaitLoop();
        tpo.start();
    }
    
	private void runWaitLoop()
	{
		while (!p1.isSelected() && !p2.isSelected() && !p3.isSelected() && !p4.isSelected())
		{
			//keep doing nothing until they select a number of players
		}
		makePanels();
	}
	
    private void makeMenu()
    {
		menuBar = new JMenuBar();
		JMenu menu = new JMenu("Choose the number of players");
		menuBar.add(menu);
		
		ButtonGroup group = new ButtonGroup();
		p1 = new JRadioButtonMenuItem("One Player");
		group.add(p1);
		p2 = new JRadioButtonMenuItem("Two Players");
		group.add(p1);
		p3 = new JRadioButtonMenuItem("Three Players");
		group.add(p1);
		p4 = new JRadioButtonMenuItem("Four Players");
		group.add(p1);
		menu.add(p1);
		menu.add(p2);
		menu.add(p3);
		menu.add(p4);
		
		setJMenuBar(menuBar);
		menuBar.addKeyListener(new PanelKeyListener());
		setTitle("Party Tetris - by Eli Delventhal, Edwin Chen, and Joseph Park");
		setIconImage(Toolkit.getDefaultToolkit().getImage("tetris.gif"));
    }
	
	private void makePanels()
	{
		gbl = new GridBagLayout();
        gbc = new GridBagConstraints();
        getContentPane().setLayout(gbl);
        gbc.fill = GridBagConstraints.BOTH;
		int numPlayers = 0;
		buttonsPressed = new ArrayList();
		panels = new ArrayList();
		scoreboards = new ArrayList();
		
		if (p1.isSelected())
			numPlayers = 1;
		if (p2.isSelected())
			numPlayers = 2;
		if (p3.isSelected())
			numPlayers = 3;
		if (p4.isSelected())
			numPlayers = 4;
		
		for (int i = 0; i < numPlayers; i++)
		{
			int p = i; if (p > 1) p -= 2;
			
			Panel panel = new Panel(this, i+1);
			panels.add(panel);
			addComponent(panel,p*4+1,i/2+1,2,1,10.0,10.0);
			
			Scoreboard scoreboard = new Scoreboard(panel, timer, panel.getPlayer());
			scoreboards.add(scoreboard);
			addComponent(scoreboard,p*4+3,i/2+1,1,1,5.0 ,5.0);
			panel.setScoreboard(scoreboard);
			new Thread(panel).start();
		}
		if (numPlayers == 3)
		{
			DummyPanel filler = new DummyPanel();
			addComponent(filler, 5, 2, 3, 1, 10.0,10.0);
			filler.draw();
		}
		
		setSize(480 * ((numPlayers+2)/2), 670 * ((numPlayers+1)/2));
		addKeyListener(new PanelKeyListener());
		timer.addActionListener(new TetrisListener());
		soundPlayer = new SoundPlayer();
		soundPlayer.play(SoundPlayer.BKGD_SND, true);
		setResizable(false);
	}
    
	private void addComponent(Component c, int x, int y, int w, int h, double wide, double tall)
	{
		gbc.gridx = x;
        gbc.gridy = y;
        gbc.gridwidth = w;
        gbc.gridheight = h;
		gbc.weightx = wide;
        gbc.weighty = tall;
        getContentPane().add(c);
        gbl.setConstraints(c, gbc);
	}
	
    private class MyWindowAdapter extends WindowAdapter
    {
        public void windowClosing (WindowEvent e)
        {
            System.exit(0);
        }
    }
    
    public void start()
    {
		for (int i = 0; i < panels.size(); i++)
		{
			((Panel)panels.get(i)).startGrid();
			((Scoreboard)scoreboards.get(i)).newNextItem();
		}
		((Panel)panels.get(0)).startGrid();
        timer.start();
    }

    public void stop()
    {
        timer.stop ();
    }
	
	public Player[] getPlayers()
	{
		Player[] players = new Player[panels.size()];
		for (int i = 0; i < panels.size(); i++)
			players[i] = ((Panel)panels.get(i)).getPlayer();
		return players;
	}
	
	public ArrayList getPanels()
	{
		return panels;
	}
	
	public ArrayList buttonsPressed()
	{
		return buttonsPressed;
	}
	
	private class PanelKeyListener extends KeyAdapter
    {
        public void keyPressed(KeyEvent k)
        {
            if (!buttonsPressed.contains(new Integer(k.getKeyCode())))
                buttonsPressed.add(new Integer(k.getKeyCode()));
        }
        public void keyReleased(KeyEvent k)
        {
            if (buttonsPressed.contains(new Integer(k.getKeyCode())))
                buttonsPressed.remove(new Integer(k.getKeyCode()));
			for (int i = 0; i < panels.size(); i++)
				if (k.getKeyCode() == ((Panel)panels.get(i)).getPlayer().rotateButton())
					((Panel)panels.get(i)).setDidRotate(false);
        }
    }
	
	public boolean isFocusable()
	{
		return true;
	}
	
	public class TetrisListener implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			boolean allLost = true;
			for (int i = 0; i < panels.size(); i++)
			{
				if (!((Panel)panels.get(i)).getPlayer().lostGame())
					allLost = false;
			}
			
			if (allLost)
			{
				soundPlayer.stop(SoundPlayer.BKGD_SND);
				timer.stop();
			}
		}
	}
}


import java.awt.Graphics;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JPanel;

public class DummyPanel extends JPanel
{
	public DummyPanel()
	{
		setBackground(new Color(34867 * 12));
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		drawLogo(g);
		drawBorders(g);
	}
	
	private void drawLogo(Graphics g)
	{
		Color color = Color.RED;
		for (int i = 0; i < 8; i++)
			color = color.darker();
		
		for (int i = 0; i < 10; i++)
		{
			Font font = new Font("Arial", Font.BOLD, 80);
			g.setFont(font);
			g.setColor(color);
			g.drawString("PARTY", 10 + i, 100 + i);
			g.drawString("TETRIS", 100 + i, 200 + i);
		
			color = color.brighter();
		}
		
		Font font = new Font("Arial", Font.BOLD, 30);
		g.setFont(font);
		g.setColor(Color.WHITE);
		g.drawString("by Eli Delventhal", 10, 350);
		g.drawString("Edwin Chen", 60, 400);
		g.drawString("Joseph Park", 110, 450);
	}
	
	private void drawBorders(Graphics g)
	{
		Color color = Color.BLACK;
		for (int i = 0; i < 5; i++)
		{
			color = color.brighter().brighter();
			g.setColor(color);
			g.drawRect(i,i,getWidth()-i,getHeight()-i);
		}
	}
	
	public void draw()
	{
		repaint();
	}
}

Heh, one of the classes only is longer than 10,000 characters, so I did the sensible thing and put the rest in a text file.

I hope that helps. Looking through it all, I can see a lot of bad practices and stupid mistakes, so be careful about basing your coding style on this. Anyway, hopefully it will be more benefit than hurt.

The space invaders tutorial might help you out:

http://www.cokeandcode.com/info/tut2d.html

Kev

I own two books

Java 2 Game Programming.
-Thomas Petchel

Gives a good start and really begins with basics.

Developing Games in Java
-David Brackeen

Is a little more advanced and might be too hard without any knowledge, but might be good enough if you are good at Java.

Best would be if you could get them from a library and first have a look.

Try
http://java.about.com/od/gamegraphicsprogramming/
http://www.java-tutorial.net/
http://www.freewarejava.com/tutorials/index.shtml

I just entered “java game programming tutorials” in Google, I didnt check the sites.

-JAW