Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Galaxy Defender: 2D Space Shooter  (Read 3401 times)
0 Members and 1 Guest are viewing this topic.
Offline Shane75776
« Posted 2010-04-18 21:37:32 »

This is my first game.

This game is not entirely finished, Still have a few bugs here and there to fix and polish up the graphics a tad bit.

Would like feedback if at all possible, If you have suggestions please leave them in a comment and if anything is confusing please explain to me what it is and what I may be able to do to make it not so confusing.

Anyways hope you like what I have so far.

Download link
Galaxy Defender.msi

Screens:




Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2010-04-19 14:10:18 »

The backgrounds are gorgeous and the ships are pretty too (although if they were slightly more photorealistic I think that would look better overall). But you should never distribute Java as MSI, for several reasons - 1 people don't want to risk running an MSI, 2 you ruin the whole main purpose of Java (portability), 3 you are forcing a download and install, which is inherently a pain.

Please package this as an Applet (preferred) or Webstart.

See my work:
OTC Software
Offline Shane75776
« Reply #2 - Posted 2010-04-19 18:52:31 »

Thanks, The backgrounds I got from google.

I have absolutely no clue on how to do webstart, I have looked at tutorials and what not but there are none that make any sense to me. mainly the accessing of resources from within jar files. I am also not fammiliar with HTML which i read in one of the tuts that you needed to make an HTML file or somethin.

the IDE that I use is eclipse though.

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2010-04-19 18:56:53 »

mainly the accessing of resources from within jar files.


This is indeed non-obvious, so here we go:

1  
2  
3  
4  
5  
6  
public static InputStream getStream(String path)
{
   return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
}

InputStream stream = getStream("some/directory/beautiful.png");

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Shane75776
« Reply #4 - Posted 2010-04-19 19:48:05 »

1  
InputStream stream = getStream("some/directory/beautiful.png");


Do I need to put that line in wherever im getting an image at within my program?

this is how I am currently getting my background and using them.
1  
2  
Object  back1 = Toolkit.getDefaultToolkit().getImage("space.png");
    Object back2 = Toolkit.getDefaultToolkit().getImage("space2.png");


I guess what im asking is, how can I set them to an Object so that I can later use them within my program using your code?


Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2010-04-19 20:06:18 »

1  
InputStream stream = getStream("some/directory/beautiful.png");


Do I need to put that line in wherever im getting an image at within my program?

this is how I am currently getting my background and using them.
1  
2  
Object  back1 = Toolkit.getDefaultToolkit().getImage("space.png");
    Object back2 = Toolkit.getDefaultToolkit().getImage("space2.png");


I guess what im asking is, how can I set them to an Object so that I can later use them within my program using your code?



BufferedImage image = ImageIO.read(thatStream);

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Shane75776
« Reply #6 - Posted 2010-04-19 20:09:34 »

1  
2  
3  
4  
public static InputStream getStream(String path)
{
   return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
}


Lastly do i need to make a new thread for this? or do I just put this method in my class and call it?

Sorry for being such a noob to this =P

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2010-04-19 20:11:25 »

1  
2  
3  
4  
public static InputStream getStream(String path)
{
   return Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
}


Lastly do i need to make a new thread for this? or do I just put this method in my class and call it?

Sorry for being such a noob to this =P

It's up to you, but... as you call yourself a n00b: don't make a new thread

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Shane75776
« Reply #8 - Posted 2010-04-19 20:25:00 »

Ok then Ill make the Method and call it and see how that goes.
Gonna have to wait until im feeling better and can think straight. I believe I have the flu.

Thanks for all the help!

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #9 - Posted 2010-04-20 00:14:04 »

I'm surprised nobody said this yet, but it's a good idea to put all that stuff in a resource manager class. Here is a very simple example:

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  
import java.util.HashMap;
import java.awt.image.BufferedImage;
import java.awt.imageio.ImageIO;

public class ResourceManager
{
    private HashMap<String,BufferedImage> images;

    public void initialize()
    {
        images = new HashMap<String,BufferedImage>();
    }

    public BufferedImage getImage(String url)
    {
        BufferedImage image = images.get(url);

        if (image == null)
        {
            try
            {
                image = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream(url));
                images.put(url, image);
            }
            catch(Exception e)
            {
                System.err.println("Couldn't load resource: " + url);
                e.printStackTrace();
            }
        }

        return image;
    }
}


That way each image only gets loaded once. Probably you'd want some sort of preloading in there and all that, but the idea is that generally you don't want to directly load resources from within your code. Let some kind of manager deal with that. By the way, I make no guarantee that my syntax is right or anything else in that code, I have been coding in NotJava™ so much lately that my head isn't really in that space at the moment. Also you need to decide what you want your code to do when it gets a null resource (couldn't be loaded), which obviously depends greatly on where your resource is coming from (a local JAR, the web, a server, the filesystem, etc.), as well as personal taste. Typically I will preload a very small "ERROR" image in the initialize() function and then return that if no resource could be loaded.

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Shane75776
« Reply #10 - Posted 2010-04-20 14:38:45 »

That way each image only gets loaded once. Probably you'd want some sort of preloading in there and all that, but the idea is that generally you don't want to directly load resources from within your code. Let some kind of manager deal with that. By the way, I make no guarantee that my syntax is right or anything else in that code, I have been coding in NotJava™ so much lately that my head isn't really in that space at the moment. Also you need to decide what you want your code to do when it gets a null resource (couldn't be loaded), which obviously depends greatly on where your resource is coming from (a local JAR, the web, a server, the filesystem, etc.), as well as personal taste. Typically I will preload a very small "ERROR" image in the initialize() function and then return that if no resource could be loaded.

Ah thanks, I think Ill try this out if I run into any issues ill leave a post. hopefully I wont =)

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Shane75776
« Reply #11 - Posted 2010-04-20 14:50:48 »

Okay. so say I have an image called Craft.png. How would I load that image using your ResourceManager code? or does it load all of my images? im just sorta confused on how I am supposed to use it. Could you maybe post an example?

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #12 - Posted 2010-04-20 17:53:28 »

Okay. so say I have an image called Craft.png. How would I load that image using your ResourceManager code? or does it load all of my images? im just sorta confused on how I am supposed to use it. Could you maybe post an example?
Sure. This first snippet is a case when you are creating a new BufferedImage for every single object you make. Each object has their own image, and the image is loaded upon construction. This is the most obvious way to do things and is pretty much how you seem to be doing it.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public class Ship
{
    private int x, y, width, height;
    private BufferedImage image;

    public Ship(int xPos, int yPos, int wid, int hi, String imageLoc)
    {
        x = xPos;
        y = yPos;
        width = wi;
        height = hi;
        try {image = ImageIO.read(imageLoc); } catch (Exception e) {e.printStackTrace();}
    }

    public void draw(Graphics  g)
    {
        g.drawImage(image, x, y, width, height, null);
    }
}


The second snippet is almost exactly the same but uses the ResourceManager. Every object has a key that points to an image loaded within the ResourceManager, which will only be loaded the first time you call it. In addition, having only one instance of that image is much much easier on your memory.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public class Ship
{
    private int x, y, width, height;
    private String image;

    public Ship(int xPos, int yPos, int wid, int hi, String imageLoc)
    {
        x = xPos;
        y = yPos;
        width = wi;
        height = hi;
        image = imageLoc;
    }

    public void draw(Graphics  g)
    {
        g.drawImage(ResourceManager.getImage(image), x, y, width, height, null);
    }
}


See the difference? Typically you want to preload every image, by the way, I just didn't include it in the code because it's simple to do and would be up to you when you want to do it.

See my work:
OTC Software
Offline Shane75776
« Reply #13 - Posted 2010-04-20 19:50:10 »

Yes I think I understand now. Thanks for the help.

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Shane75776
« Reply #14 - Posted 2010-04-21 22:36:23 »

Ok so I tried to implement your resource manager code in and I seem to get an error.

this is how im using it from what I understand after reading your posts
1  
BufferedImage back1 = ResourceManager.getImage("space.png");


and then I get this error here.
1  
2  
3  
4  
5  
Exception in thread "main" java.lang.NullPointerException
   at ResourceManager.getImage(ResourceManager.java:17)
   at Board.<init>(Board.java:160)
   at ExecuteGame.<init>(ExecuteGame.java:22)
   at ExecuteGame.main(ExecuteGame.java:39)


That points to the code I got from you here
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  
import java.util.HashMap;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

public class ResourceManager
{
    private static HashMap<String,BufferedImage> images;

    public void initialize()
    {
        images = new HashMap<String,BufferedImage>();
    }

    public static BufferedImage getImage(String url)
    {
        BufferedImage image = images.get(url);

        if (image == null)
        {
            try
            {
                image = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream(url));
                images.put(url, image);
            }
            catch(Exception e)
            {
                System.err.println("Couldn't load resource: " + url);
                e.printStackTrace();
            }
        }

        return image;
    }
}


What I was trying to do was set my BufferedImage object to an Image from within my Jar file but I seem to be grasping this concept wrong yet again =(

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #15 - Posted 2010-04-21 22:39:35 »

Change
1  
2  
3  
4  
    public void initialize()
    {
        images = new HashMap<String,BufferedImage>();
    }


To:
1  
2  
3  
4  
    static
    {
        images = new HashMap<String,BufferedImage>();
    }

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Shane75776
« Reply #16 - Posted 2010-04-22 00:52:31 »

Ty so much it works now like a charm. May I ask though what changing that to just static does exactly?

Also how could I change this line of code so that it can get a sound file from my jar? my sound files are in .wav format if it matters.
1  
image = ImageIO.read(Thread.currentThread().getContextClassLoader().getResourceAsStream(url));

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #17 - Posted 2010-04-22 03:02:22 »

It should have been public static void intialize() for one thing. What Riven's code does is initialize the images map once the class ResourceManager loads, automatically. What I put into the initialize() function creates the images map when you tell the ResourceManager to initialize. So typically somewhere in your program (before you try to use the ResouceManager) and in some logical place (somewhere you want to be loading things) you would call ResourceManager.initialize();.

Basically Riven's is automatic but you have no control over when it happens, whereas doing it in initialize() happens when you want it to.

The reason you got a NullPointerException before is because you never created the images map in the first place. Any time you try to call an Object that hasn't been instantiated (i.e. "new" hasn't been called on it) you'll get that exception. Because the images map was never instantiated, (it gets instantiated in initialize) it's null. Therefore, NullPointerException.

To make it a sound file is a whole other can of worms. There are a lot of possible ways to load sounds, all with different advantages. I'd make a separate post in the sound forum asking for advice. Then once you know how you're going to store/load sounds, you can use the same basic idea (a HashMap) to have your sounds working.

See my work:
OTC Software
Offline Shane75776
« Reply #18 - Posted 2010-04-22 12:40:59 »

Ok, I'll post a new topic sometime in the future if when I get to that point if one isn't already posted over it.
Thanks again for clearing that up. Smiley

Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Shane75776
« Reply #19 - Posted 2010-04-23 02:13:57 »

Ok so I seemed to have run into a problem, When using the resource manager to pull the images out of the jar it just freezes the game up.  But when I put that same jar using the resource manager into a folder that has all the images in it. (So that the jar is next to all my image files) it works just fine.

Any hint to what could be wrong?

This is an example of how Im getting the images and storing them to buffered images in my code.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
   private void LoadResources()
   {
      // TODO Auto-generated method stub
     
       ResourceManager.initialize();
       //Create Objects of all my images.
      bubShield = ResourceManager.getImage("bubbledrop.gif");
       back1 = ResourceManager.getImage("space.png");
       back2 = ResourceManager.getImage("space2.png");
       back3 = ResourceManager.getImage("space3.png");
       back4 = ResourceManager.getImage("space4.jpg");
       back5 = ResourceManager.getImage("space5.jpg");
       back6 = ResourceManager.getImage("space6.jpg");
       back7 = ResourceManager.getImage("space7.png");
       back8 = ResourceManager.getImage("space8.png");
       back9 = ResourceManager.getImage("space9.png");
       back10 = ResourceManager.getImage("space10.png");
   }



Check out my Snipping Tool++ ! An advanced snippet/screenshot/text uploading tool! Meant to replace the windows snipping tool.

Check out Pixel Rain My most recent Swing based game!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #20 - Posted 2010-04-26 06:03:03 »

Well is there an error generated? Could have something to do with directories.

See my work:
OTC Software
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.

Pippogeek (35 views)
2014-09-24 16:13:29

Pippogeek (28 views)
2014-09-24 16:12:22

Pippogeek (17 views)
2014-09-24 16:12:06

Grunnt (40 views)
2014-09-23 14:38:19

radar3301 (23 views)
2014-09-21 23:33:17

BurntPizza (58 views)
2014-09-21 02:42:18

BurntPizza (29 views)
2014-09-21 01:30:30

moogie (34 views)
2014-09-21 00:26:15

UprightPath (47 views)
2014-09-20 20:14:06

BurntPizza (51 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!