(Link to AcmlmWiki) Offline: thank ||bass
Register | Login
Views: 13,040,846
Main | Memberlist | Active users | Calendar | Chat | Online users
Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album
05-18-24 04:48 AM
0 users currently in General Gaming.
Acmlm's Board - I3 Archive - General Gaming - How to make a game [beginner New poll | |
Add to favorites | Next newer thread | Next older thread
User Post
Squash Monster

Bouncy


 





Since: 11-18-05
From: Right next to myself.

Last post: 6306 days
Last view: 6299 days
Posted on 11-19-05 04:43 PM Link | Quote
Too many beginning game programmers either start with a tool like RPG Maker and never learn how to program for real or start with the crazy notion that they're going to make Doom 4 on their first try.

So I'm going to give you all a piece of advice and all the hard code (in Java).

Start with Pong, then make Gradius, then make Super Mario Brothers, then go ahead and make whatever it was you were planning on in the first place.

And here's some code:

This will show how to get yourself a window, a timer, and keyboard input. Also known as all you should really need to start programming.

First, the main class. You may want to rename this from EmptyClass to the name of your game.

Excuse the fact that everything's doublespaced, Acmlmboard's treatment of the pre tag is icky.

import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


class EmptyClass extends JPanel implements ActionListener
{
//Timing variables:
private Timer gameTimer = new Timer(15, this); //One frame per 15ms = 60fps
private boolean midFrame = false; //Explained in actionPerformed
//Game-related variables:
private Player player = new Player(); //Every game needs a player.

public EmptyClass()
{
//Set up the game.
setPreferredSize(new Dimension(512,512)); //(512,512) are the dimensions
//You may want to change it to something else.
//It's in pixels.
setFocusable(true); //Say that it's possible to receive keypresses
addKeyListener(player); //Give the player object access to keypresses.
//Put any initialization code you'll need here:


//And tell the game it's time to start:
gameTimer.start();
}

public void actionPerformed(ActionEvent eventWeDontCareAbout)
{
//This method is run every frame.
//Naturally, bad things will happen if the last frame hasn't finished.
//So check that before doing anything.
if(!midFrame)
{
midFrame = true; //Stop letting new frames be processed.
//Actual game code goes here:
player.doStep(); //For sake of example, the player will do something

//Tell the game it's time to draw again
repaint(); //This indirectly calls paintComponent
midFrame = false; //We're done, so make sure we know that.
}
}

public void paintComponent(Graphics g1)
{
//This method is run every time the game needs to be drawn.
super.paintComponent(g1); //ALWAYS do this or weird things may happen.
Graphics2D g = (Graphics2D) g1; //Graphics2D is more advanced,
//So get an instance of that.
//Do any drawing code, or calling of things that need to be drawn here:
player.draw(g);
}

public static void main(String[] args)
{
//Sets up a window for the game and all that nonsense.
JFrame appFrame = new JFrame(); //Make a window to put everything in
EmptyClass game = new EmptyClass(); //Make an instance of your game
appFrame.setContentPane(game); //Make the window show your game
appFrame.pack(); //Make the window sort itself out
appFrame.show(); //Show the window
}
}


And now, a class for the "player", or whatever it is that recieves input from the player. This one is a bit more examply.

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

class Player implements KeyListener
{
//Class variables:
private int x = 20;
private int y = 20;
private int xs = 0;
private int ys = 0;

public Player()
{
//Initialization code would go here if you needed any

}

public void doStep()
{
//A method to call once per frame

//Just example code here:
x += xs;
y += ys;
}

public void draw(Graphics2D g)
{
//Drawing code goes here:

//Just an ugly example:
g.fillRect(x,y,10,10);
}

public void keyPressed(KeyEvent e)
{
//Called whenever a key is pressed
switch(e.getKeyCode()) //Check what key was pressed
{
//Note that what we do with each of these keys is just an example
case KeyEvent.VK_RIGHT:
xs = 4;
break;
case KeyEvent.VK_UP:
ys = -4;
break;
case KeyEvent.VK_LEFT:
xs = -4;
break;
case KeyEvent.VK_DOWN:
ys = 4;
break;
}
}

public void keyReleased(KeyEvent e)
{
//Called whenever a key is released
switch(e.getKeyCode()) //Check what key was released
{
//Note that what we do with each of these keys is just an example
case KeyEvent.VK_RIGHT:
if(xs > 0)
xs = 0;
break;
case KeyEvent.VK_UP:
if(ys < 0)
ys = 0;
break;
case KeyEvent.VK_LEFT:
if(xs < 0)
xs = 0;
break;
case KeyEvent.VK_DOWN:
if(ys > 0)
ys = 0;
break;
}
}

public void keyTyped(KeyEvent e)
{
//Called when a key is pressed then released.
//Nobody cares about this method.
//But you need it because you implemented KeyListener
}
}



(edited by Squash Monster on 11-19-05 03:44 PM)
(edited by Squash Monster on 11-19-05 04:42 PM)
(edited by Squash Monster on 11-19-05 04:55 PM)
Teundusia

Red Cheep-cheep


 





Since: 11-18-05

Last post: 6300 days
Last view: 6306 days
Posted on 11-20-05 04:34 AM Link | Quote
I think the point you are trying to get across explains DarkBASIC's creation.

Also, how would someone who is a beginner gamer programmer use that? You've given us what we need for a window, a timer and player input, but we now need to find a place that can teach us more... Is it Java? So we need to find a book or website that can teach us Java.

And what if they don't wanna learn Java? Your example doesn't help those people.
Squash Monster

Bouncy


 





Since: 11-18-05
From: Right next to myself.

Last post: 6306 days
Last view: 6299 days
Posted on 11-20-05 11:01 AM Link | Quote
Ah, see, I assumed people would see me say it's in Java in that post, and go look into it a bit.

Java is rediculously easy to program in once you learn how to do the stuff that I just showed how to do.

If you want to make a game, you're going to learn a programming language. There's no way around it. Even most game making tools have their own stripped-down programming language.

Why use a gimped language that only has booleans and if statements when you can take a tiny bit more effort and use one that works?

To learn Java, go here:
Sun's Java tutorial -- basics.
Then here:
Sun's Java tutorial -- objects.
Then use this for reference:
The Java API
spel werdz rite









Since: 11-19-05

Last post: 6299 days
Last view: 6298 days
Posted on 11-21-05 04:57 AM Link | Quote
Any help for C++?
I downloaded source code for a game, apparantly it's a C++ game and I want to play with it a little.
Why do I find it more fun to work on games than to play them?
Eponick

Rat








Since: 11-17-05

Last post: 6533 days
Last view: 6332 days
Posted on 11-21-05 05:16 AM Link | Quote
How do I make games? A Path to Game Development
GameDev.net Tutorials

Originally posted by spel werdz rite
Any help for C++?

The above links and a quick google search turned up this site.

Originally posted by spel werdz rite
Why do I find it more fun to work on games than to play them?

Because it is


(edited by Eponick on 11-21-05 04:17 AM)
Deleted User
Banned


 





Since: 05-08-06

Last post: None
Last view: 6299 days
Posted on 11-29-05 08:28 AM Link | Quote
Forgive my ignorance but isn't it true that once you have Java or C++ down, all other programming languages aren't that hard?
Eponick

Rat








Since: 11-17-05

Last post: 6533 days
Last view: 6332 days
Posted on 11-29-05 09:41 AM Link | Quote
Since I learned C++ I havnt wanted to learn anything else.
It handles anything I want to make so I dont have a reason to
Squash Monster

Bouncy


 





Since: 11-18-05
From: Right next to myself.

Last post: 6306 days
Last view: 6299 days
Posted on 11-29-05 06:37 PM Link | Quote
Originally posted by Thayer
Forgive my ignorance but isn't it true that once you have Java or C++ down, all other programming languages aren't that hard?

Well, no, not quite. Once you have Java or C++ down, about a third of all languages will take roughly ten minutes to learn. A huge number of languages exist that just try to copy one of the two whenever possible. Java and C++ themselves only have around five major differences between them.

It's the same with learning an assembly language, or a version of basic. Once you've learned one or two, a lot of languages of the same type are easy.

It applies more to the ones like Java and C++, though. I just wouldn't say that it makes all programming languages not hard. I can think of a few programming languages that will always be hard no matter how you slice it.


(edited by Squash Monster on 11-29-05 05:39 PM)
Deleted User
Banned


 





Since: 05-08-06

Last post: None
Last view: 6299 days
Posted on 11-29-05 07:21 PM Link | Quote
Isn't that fundamentally what I said? I was asking if after you knew those two, if other languages weren't that hard to learn...
Squash Monster

Bouncy


 





Since: 11-18-05
From: Right next to myself.

Last post: 6306 days
Last view: 6299 days
Posted on 11-29-05 08:55 PM Link | Quote
You said all languages. I said about a third.

I think that's a reasonable enough gap to make it a good idea to point out the distinction while answering your question.
Add to favorites | Next newer thread | Next older thread
Acmlm's Board - I3 Archive - General Gaming - How to make a game [beginner |


ABII

Acmlmboard 1.92.999, 9/17/2006
©2000-2006 Acmlm, Emuz, Blades, Xkeeper

Page rendered in 0.013 seconds; used 400.90 kB (max 497.34 kB)