Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simple 2d games  (Read 3478 times)
0 Members and 1 Guest are viewing this topic.
Offline Call Me Jimmy!

Senior Newbie





« Posted 2005-10-17 19:40:32 »

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
Offline BlindMan

Senior Newbie





« Reply #1 - Posted 2005-10-18 14:26:41 »

Get a book on Java 2D,
and download an Integrated Development Environment,
eclipse is great, it will help newbies like you:
http://www.eclipse.org/downloads/index.php
Offline JAW

Senior Duke


Medals: 2



« Reply #2 - Posted 2005-10-22 16:18:48 »

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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #3 - Posted 2005-10-24 17:24:45 »

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.

See my work:
OTC Software
Offline Call Me Jimmy!

Senior Newbie





« Reply #4 - Posted 2005-10-24 19:17:38 »

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
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #5 - Posted 2005-10-24 22:41:51 »

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:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
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;
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
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;
   }
}


1  
2  
3  
4  
5  
6  
import java.awt.Graphics;

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


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
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();
         }
      }
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
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();
   }
}



See my work:
OTC Software
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #6 - Posted 2005-10-24 22:49:57 »

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.

See my work:
OTC Software
Offline kevglass

JGO Kernel


Medals: 186
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #7 - Posted 2005-10-24 23:37:48 »

The space invaders tutorial might help you out:

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

Kev

Offline JAW

Senior Duke


Medals: 2



« Reply #8 - Posted 2005-10-25 10:37:44 »

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
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (38 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (34 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!