New to Java Game Programming and my Dreams

Hello, and I’m not here to say “Oh, I want to make a huge MMORPG with 1000s upon 10000000000000s of players.”

No, no, not at all, I plan to start more humble… but still not really :P.

Ok, I have a little console game in C Languge prior and you can see it here: http://forums.devshed.com/game-development-141/first-major-program-in-c-360987.html
The reason I moved to java, is that I was looking for a language with more instant and usable interface abilities with graphics and swing. So far, I konw very little about java especially. I have a lot to learn about its packages ect. I have trained my logic in my console app in C language, yet my new ideas are a whole new story, and require understanding of threads and graphics, and vast knowledge of the use of packets, to my extents.

My goal… well firstly is learn sufficent enough about java in my plan, yet while making my big game project, it will be a huge learning experience as I do it, just like I did in C, that whole project was a learning experience. Yet there are very complicated things my game must do to my knowledge. What my game will be, is a turn-based strategy game, where you customize your empire to your liking againt up to many AI players if desired. My game will be 2d, and top-down view directly. It will include tiles, and sprite that I will make myself, and music and sound effects that I will make in “Reason 3.0” music creation software. I plan to use nearly all orginal stuffs that I make, execpt some of the sound effects, and possibly picture, but most will be enhanced or sized by me. My game will be similar in play to a Civilization game, where there are border, and a line of sight. What makes my idea so special from any other strategy game is that there are MORE THAN ONE world in the SAME game. What I mean is that, since my game will allow era from ancient to far future, you can oclonize on other planets in the galaxy. I want my planets to be made using a random generator algoritym that understand my tile gfx and generates the worlds properly and dispalces the resouces and such. So, usually several AI players will share the same planet, and other will be other the other side of the galaxy. I will allow the player to select options for how the words and galaxies are generated. My battles will be in all sorta of ways, and they will be more “interactable” then most, and I want it so you can see troop sprites move about the screen and shoot. I’m more concerned about generating my worlds and learning how grfx and swing work more than anything. Also the use of animated sprites and rotating them. I have a book that tells me how luckilly and I’ve dabbed in it already. I am also concerned about my art skills in MAKING the sprites and ground terrain tilesets pixel by pixel.

Problems that look very difficult to me:
-The overall world, making it so there is a bottom map, and it paints rectangle in accuracy to the actually world and updates with player.
-Scrolling upon mouse movement to edge of screen… which leads to more of the tilesets of the world exposed.
-Clicking on the world map, and it sets your view on that tile.
-PAINT graphics g, how do i make it so the game ONLY refreshed the screen of the part of the world you are looking at.
-Generating the worlds randomly, but intelligently… array? having game remeber status of terrains, prolly just array assingments, and calling the terrain by array number reference.
-CONTROLS, luckilly java has nice way of handeling the mouse, by knowing when its released ect. but its gonna be taking a while to make control proper for my game. each unit in my game will have its own abiltiies such as a trooper will have different box options than a worker who can build roads ect.
-also with swing, I will be having it so the bottom part is an interface for the buttons for the units and the UI map, but if u click on certain units the cuttons will change for the unit… like in an rts… can i remove swing buttons and replace them with different buttons in java swing?
-animation, scarry
-everything :confused:

There is zillions of room for bugs and mistakes in my game. Are there any soruces you recommend to help me? And also, I am contantly learning soemthing new about java. ty for help.

PAINT graphics g, how do i make it so the game ONLY refreshed the screen of the part of the world you are looking at.

Well, you can easily tell whats on the screen and what not. You know the upper right corner coords of the viewport and you also know its width height. And you also know the width/height of the tiles… so, you only need 2 for loops for drawing em.

Use a smaller viewport in the centre of the screen for checking if you got everything right.

can i remove swing buttons and replace them with different buttons in java swing?

Yes, you can remove components. However, the cleaner and faster (no need to recreate anything) way is to use the CardLayout.

http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html

There you have different “cards”, which contain different components and you just say that this one should be shown or that other one etc.

Oh and welcome to the board :slight_smile:

So should I like… keep track of my tiles with arrays… but lets say the user moves the camera “up” to see more tiles, my game will move up by one tile. So for the game to draw the tiles that are above, and forget about the tiles just one below… how would i do this?

im sorry but this is a very important thing to me, as it can affect performance greatly.

EDIT:
Also can anyone recommend a good sprite create that is pixel by pixel, thanx. Id like to make custom 2d sprites formy game.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;
import java.awt.geom.*;
import java.io.*;
import javax.imageio.*;

public class Tiles extends Canvas{
	static int [] controls = new int[0xFF];
	public Tiles()throws Exception{
		JFrame f;
		BufferStrategy strat;
		GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
		BufferedImage set=ImageIO.read(new BufferedInputStream(getClass().getResourceAsStream("/set2.png")));

		int frames=0;
		long lastSecond=0;

		int MAP_W=1024;
		int MAP_H=1024;
		int SCREEN_W=640;
		int SCREEN_H=480;
		int TILE_W=32;
		int TILE_H=32;
		int TILES_X=SCREEN_W/TILE_W;
		int TILES_Y=SCREEN_H/TILE_H;
		int MAX_WX=MAP_W*TILE_W-TILES_X*TILE_W-1;
		int MAX_WY=MAP_H*TILE_H-TILES_Y*TILE_H-1;

		int [][]map=new int[MAP_W][MAP_H];
		int wx=0;
		int wy=0;

		f=new JFrame();
		setSize(SCREEN_W,SCREEN_H);
		f.getContentPane().add(this);
		f.pack();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setResizable(false);
		f.setVisible(true);
		createBufferStrategy(2);
		strat=getBufferStrategy();

		enableEvents(AWTEvent.KEY_EVENT_MASK);
		requestFocus();

		Random gen=new Random();
		for(int x=0;x<MAP_W;x++)
			for(int y=0;y<MAP_H;y++)
				map[x][y]=gen.nextInt(6);

		BufferedImage[]tiles=new BufferedImage[6];
		for(int i=0;i<6;i++){
			tiles[i]=gc.createCompatibleImage(TILE_W,TILE_H,BufferedImage.OPAQUE);
			Graphics g=tiles[i].getGraphics();
			g.drawImage(set,
				0,0,
				TILE_W,TILE_H,
				i*TILE_W,0,
				i*TILE_W+TILE_W,TILE_H,
				null);
			g.dispose();
		}

		while(true){
			wx+=controls[KeyEvent.VK_RIGHT]-controls[KeyEvent.VK_LEFT];
			wy+=controls[KeyEvent.VK_DOWN]-controls[KeyEvent.VK_UP];

			Graphics2D g = (Graphics2D) strat.getDrawGraphics();

			if(wx<0)wx=0;
			if(wy<0)wy=0;
			if(wx>MAX_WX)wx=MAX_WX;
			if(wy>MAX_WY)wy=MAX_WY;
			int ox=wx/TILE_W;
			int oy=wy/TILE_H;

			for(int x=0;x<TILES_X+1;x++)
				for(int y=0;y<TILES_Y+1;y++)
					g.drawImage(tiles[map[ox+x][oy+y]],x*TILE_W-wx%TILE_W,y*TILE_H-wy%TILE_H,null);

			//-
			frames++;
			long timeNow = System.currentTimeMillis();

			if(timeNow-lastSecond>=1000){
				f.setTitle("fps:"+frames);
				frames=0;
				lastSecond=timeNow;
			}
			//-
			g.dispose();
			strat.show();
		}
	}
	public void processKeyEvent(KeyEvent ke){
		controls[ke.getKeyCode()&0xFF] = (ke.getID()==KeyEvent.KEY_RELEASED)?0:1;
	}
	public static void main(String[]$)throws Exception{
		//System.setProperty("sun.java2d.opengl","True");
		//System.setProperty("sun.java2d.noddraw","");
		System.setProperty("sun.java2d.accthreshold", "0");
		System.setProperty("sun.java2d.ddforcevram", "true");
		new Tiles();
	}
}

set2.png

http://kaioa.com/k/set2.png

holy crap! this is amazing.

yet i cant seem to find a variable to control the speed i scroll the map at, because it is too slow for me.

whats this mean: the number at least
BufferedImage[]tiles=new BufferedImage[6];

what is the purpose of this:
static int [] controls = new int[0xFF];

What is “Canvas” that you extended.

and whats this stuff: u mention opengl commented out, and i get less fps if i try it, and what do these mean?
//System.setProperty(“sun.java2d.opengl”,“True”);
//System.setProperty(“sun.java2d.noddraw”,"");
System.setProperty(“sun.java2d.accthreshold”, “0”);
System.setProperty(“sun.java2d.ddforcevram”, “true”);

is the fps counter accurate as well?

TY for help.

yet i cant seem to find a variable to control the speed i scroll the map at, because it is too slow for me.

wx+=controls[KeyEvent.VK_RIGHT]-controls[KeyEvent.VK_LEFT];
wy+=controls[KeyEvent.VK_DOWN]-controls[KeyEvent.VK_UP];

1pixel per tick. One would need proper timing (or frame capping) for constant scroll speed.

What is “Canvas” that you extended.

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Canvas.html
(Its an emtpy heavy weight component.)

and whats this stuff: u mention opengl commented out, and i get less fps if i try it, and what do these mean?

Those are flags, which influence/select the drawing pipeline. The opengl pipeline doesnt work that great with 1.5, but it should be faster and nicer with 1.6.

is the fps counter accurate as well?

Not really. Rule of tree stuff is missing and the used timer itself isnt all that accurate. But its good enough as rough frame indicator.

Check this:
http://www.cokeandcode.com/tutorials

id like to understand the different renders better for optimal stuff… like the opengl and the nodraw… what do they mean?

well i know what directx and opengl is “duh”, but i just wanan know

In addition to the specific questions and feedback you’re getting here, don’t forget to give some time to existing tutorials. Common suggestions found here are to first go thru the ones offered by Sun on their Java site first. Alternatively taking a Java class or worked thru a beginner’s Java book ( nothing game-specific ), etc. Get the foundations down. It sounds like you already have some.

After that, cokeandcode.com has some really good tutorials on basic Java game programming. He even iterrates the same game thru multiple display options.

Good luck to you! Turn-based strategy is my fav game style as well and it’s always good to see another one of us around. 8) I still have Civ2 loaded up on my computer and return to it a couple times per year.

Yes civ rules, and turn based it awsome. i have civ3 and i love it. i am not fond of object oreintated languages, but will learn it. and the code and coke tuts are cool, i saw the tile tut, i just think it doesnt tell me how to scroll a map tho.

I did C ( and bad C++ that was really just C ) for a litte while and some of my early attempts and games were procedural. OO design is really just a change of mindset that may be more difficult than the technology behind it. Lucklly for game programmers, it’s less abastract of a concept than it is for many other programmers. In a Civ like game, you have cities, each of which has attributes such as size, production capacity, etc and has various upgrades ( another type of object ), you have Wonders, you have unites, you have resources, etc. All of those are oportunities for modeling the game. Then there is the game world itself, which will include the map.

Before you bother trying to figure out how to write the code, just sit down with a piece of paper and think about what makes up a Civ game. Include the obvious ones above but also info regarding the interface, AI leaders, etc. You will probably have a good list of your objects. Look for common groupings. Settlers, Chariots, Steam Ships and Stealth Bombers are all Units and share common traits and actions ( including drawing, etc ). Write those commonalities down. Maybe you want to subdivied into Land Units, Air Units and Sea Units based on movement ( or maybe not…consider the options of both ). Now write down the common actions and traits of each of those subgroups that are not already included in the parent Unit object. You probably get the idea. Don’t go deeper than you need to.

Others with more experience may suggest differntly or expand on that. The key is not to let the differences of OO vs. procedural programming seem as a bother or a limitation, but rather as an oportunity for easy modeling and understanding.

“Free your mind”
Morpheus

Good post.

And what are some good book to get?

Well, I’m not making a civ CLONE, but there will be similarities. For one, my game will handel the type of units very differently, and like I said there is a galaxy with randomly generated planets, and each has their own world to explore. My rules on combat will be very different. Each unit, has a set of stats, and the game is not done in “units” its more like you send “this many troops here” ect. Right now im trying to think of how I should go about doing this troop management. If i should allow you to split the group up and move to more tiles. Also my combat somehow I let you create armies that are on a tile, and they attack when used. The game will goto a different mode all together in combat mode. you will see your top-down sprites up close and see a simulated fight, where depending on your government… how much control and command you have over the fight. Each unit has their level, accuracy, dodge rates, movement, speed, equipment, weapons, ect. This is how battles work… you see the exact number of troops represented by the unit on the world map… and a huge fight… should be fun if done properly. Plus my “default” cvilizations are ones I created. The city management will be very different as welll… I have no touched at all on the tech trees or the finalcial mangement lol.

I have started a ms word doc about my game. I am making the table of contents first, and there is much more to be implemented, but this is it so far.

[quote]Table of Contents
Cosmos Galactic Powers

Design History
Version 1.01

Game Overview
Philosophy

Common Questions
What is the game?
Why create this game?
Where does the game take place?
What do I control?
What is the main focus?
What’s different?

Feature Set
General Features
Game Play

The World
Overview


Planet Mode
Overview
Terrain

Resources

Scale
Objects
Weather

Day and Night
Time
Cycles

Disasters!
Earthquake
Fire
Flood
Hurricane

Space Mode
	Overview
	Galaxy
		Type
			…
		Size
			…
		Density
			…
	Suns
		Types
			White Dwarf
			…
		Age
		Color
	Planets
		Inner Planets
			Earthlike

Oceanic
Tropical
Rocky
Barren
Arid
Volcanic
Outer Planets
Gas Giant
Icy
Moons

Age
Younger Planets
Older Planets
Climate
Temperate
Arid
Cool
Ratios
Land/Water
Sizes
Minuscule
Minute
Small
Standard
Large
Massive
Giant
Giant2 and Beyond

Rendering System
Overview
2D Rendered

View
Overview

Game Engine
Overview

Water
Collision Detection

Lighting
Overview

Layout
Overview

Civilizations
Overview
Creating a Civilization
Government

User Interface
Overview

Sound and Music
Overview
Soundtrack
Main Menu
Space Mode
Eras
Ancient Music

Civilization Themes
Peace
War
Sound
Interface

[/quote]