We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Newb who wants to make game
Pages: 1 2 3 
Newb who wants to make game (Read 7769 times)
Newb who wants to make game
Jan 14th, 2007, 2:40am
 
I have always been interested in making games and I was about to make one using the phrogram trial when I realised I could not export games with it. I found processing on the internet and was extremely intrigued.

I am a total newb to JAVA but I know somewhat of programming from using varied forms of KPL. I need to have someone walk through with me the creation of the game and tell me how to get the code right. I've learned a bit about java from trial and error. Can ANYONE tell me how to get a game up and running.

This first thing I need to know is how to load animated sprites and move them.

By the way, I am looking to make a super mario style shooter game with basic controls (forward backward jetpack shoot bomb run) and relatively simple graphics (amazing compared to mario.

I hope this experience will allow me to get a good understanding at java and programming.
Re: Newb who wants to make game
Reply #1 - Jan 14th, 2007, 4:40am
 
Your enthusiasm is awesome, but asking for someone to walk you through how to make a game is a bit broad for a single forum topic. I'm just going to briefly address your question about animated sprites.

As a prelude to that, I should mention that if you are not familiar with the concept of Object-Oriented programming, I'd suggest you read through these tutorials:

http://java.sun.com/docs/books/tutorial/java/index.html

Processing has a class called PImage that makes it easy to load an image you've placed in your sketches "data" folder and then display it on the screen by specifying where you want the left corner to be. You might do something like:

Code:

PImage myImage = loadImage("foo.jpg");
image(myImage, 0, 0);


If you want an animated sprite, you could create a class that references all the images needed for the animation and then keeps track of which one it should draw every time you ask it to draw. This class could also keep track of its position so that you didn't have to specify exactly where to draw the sprite every frame (if it's stationary, for example). You don't *need* to create a class to accomplish this, but collecting related data and behaviour into a single class (called encapsulating that data/behaviour) is what using an object-oriented language like Processing (ie Java) is all about. Here's a class that would create something like an animated gif (I haven't tested this, I'm just riffing it into the message box):

Code:

class AnimatedSprite
{
private PImage[] frames;
private int currFrame;
private float frameLength;
private int timer;
private float x, y;

AnimatedSprite(PImage[] f, float fps)
{
frames = f;
frameLength = 1000/fps; // set frameLength in milliseconds
currFrame = 0;
timer = millis();
x = 0;
y = 0;
}

void setPosition(float px, float py)
{
x = px;
y = py;
}

void draw()
{
if ( millis() - timer > frameLength )
{
currFrame += 1;
// this sets the current frame to 0
// if we've reached the end of the frame array
currFrame %= frames.length;
// this resets the timer
timer = millis();
}
image(frames[currFrame], x, y);
}
}


So in your main draw() loop you might have an instance of this class called spinner. When you want it to display you type:

spinner.draw();

If you need to reposition it, you type:

spinner.setPosition(x, y);

Where x and y are floats that have values stored in them.

Re: Newb who wants to make game
Reply #2 - Jan 14th, 2007, 6:02pm
 
thanks for that tutorial page, im sure it will help a lot. I dont really need someone guiding me through the entire process, just I need advice when it doesn't work.
Re: Newb who wants to make game
Reply #3 - Jan 14th, 2007, 7:16pm
 
And if your questions are specific enough you will find lots of people willing to help out on this forum. I look forward to seeing what you put together.
Re: Newb who wants to make game
Reply #4 - Jan 15th, 2007, 3:03am
 
I am working my way through the tutorials but when I tried using the hello world app

(how do I use a code box?)

class HelloWorldApp {
   public static void main(String[] args) {
       System.out.println("Hello World!"); // Display the string.
   }
}

I get an error saying: semantic error: the static method "main" is invalid, because it is enclosed in an inner class
Re: Newb who wants to make game
Reply #5 - Jan 15th, 2007, 5:27am
 
If you try to run any of the Java code in the tutorials from the Processing environment, it won't work. The reason I pointed you to those tutorials is because they explain Objects and Classes and so forth and nothing on the Processing website does.

Processing provides you with the main() funtion that a Java program requires, so you don't need to worry about it. It also provides you with a bunch of functions you can just call, just look at the reference.

To do Hello World in processing all you have to do is start a new sketch, type:

println("Hello World");

and press the play button. You should see your message printed out in the console.
Re: Newb who wants to make game
Reply #6 - Jan 15th, 2007, 12:43pm
 
I find this to be an excellent primer on objects. And it works in Processing. It's just a pity it's not on Shiffman's site because I always have trouble finding it:

http://itp.nyu.edu/icm/shiffman/week3/index.html
Re: Newb who wants to make game
Reply #7 - Jan 16th, 2007, 12:00am
 
Ok thanks, so to import a java code into processing all I have to do is take the main() etc.? Ok so I think I understand how this works enough to begin programming. I think I'll start with an animated moving sprite. Two things though, how do I make a transparent colour and how can I refreash the screen so the last image does not remain?
Re: Newb who wants to make game
Reply #8 - Jan 16th, 2007, 12:26am
 
refreshing the screen:

http://processing.org/reference/background_.html

colors:

http://processing.org/reference/color_.html
Re: Newb who wants to make game
Reply #9 - Jan 16th, 2007, 3:26am
 
Ok, I made a skeleton frame thing to test the basic idea of the class. Once I figure that out, I will make it able to use more kinds of sprites. The syntax is OK, but the delays don't work at all. maybe you can help me fix that? Also, is there any way to give an image a transparent background colour and what does the 'private' thing you said in your cade do?
Other than that I think I'm starting to get this, though the ;s are really annoying.
Re: Newb who wants to make game
Reply #10 - Jan 16th, 2007, 11:15am
 
I think I can probabbly guess what your issue with "delay" is, and it's a common one. Nothing is drawn to the screen until the end of draw(), no matter how much delay you put inside draw between two things, they will appear on screen at the same time, because they're drawn to an off-screen buffer, then at the end of draw, that buffre is put on screen.

As for transparent parts of images, you need to use http://processing.org/reference/PImage_mask_.html
Re: Newb who wants to make game
Reply #11 - Jan 16th, 2007, 5:57pm
 
'private' means that it's not possible to access the variable from outside the class. Typically, you want to make as many class variables private as you can because it enforces encapsulation. If you try to access the variable from outside the class, for example by saying:

mySprite.x = 5;

The compiler will throw an error telling you that's not allowed. In practice, when you are writing a small processing sketch, it's not a big deal to leave off 'private' (processing will add the 'public' modifier), but I tend to put it in out of respect for OOP principles.

You'll have to be more specific about the delays not working for me to be of any help there.
Re: Newb who wants to make game
Reply #12 - Jan 16th, 2007, 10:34pm
 
Ok, I never used the draw() funstion, but simce I used image() would the delays still not work? heres the code(yes I know its basic):


class animation
{
 void animate(int rnum1){
   for (int rnum2 = 0; rnum2 < rnum1; rnum2 = rnum2 + 1){
     PImage frame;
     frame = loadImage("testball1.GIF");
     image(frame, 50, 50);
     delay(1000);
     background(200);
     frame = loadImage("testball2.GIF");
     image(frame, 50, 50);
     delay(1000);
     background(200);
     frame = loadImage("testball3.GIF");
     image(frame, 50, 50);
     delay(1000);
     background(200);
     frame = loadImage("testball4.GIF");
     image(frame, 50, 50);
     delay(1000);
     background(200);          
   }
 }
}
size(600,600);
animation balls = new animation();
balls.animate(10);

The code completes faster than the computer can even show it

and when using mask(), I just have to make a black and white negative of the image I want to be masked but with a white spot where I dont want it to? The reference isn't very descriptive.
Re: Newb who wants to make game
Reply #13 - Jan 17th, 2007, 12:32am
 
Ah ha. The whole point of the draw() function is so that you can have a program loop to do things like animation and stuff. You shouldn't be using delay() for that, especially not in the middle of a for loop. You need something like this:

Code:

// an array to hold all your images
PImage[] frames;
// an int to keep track of which image to draw
int currFrame;

void setup()
{
size(600, 600);
// set the framerate
// (ie how many times draw will be called per second
frameRate(30);
// allocate a four element array of PImages to frames
frames = new PImage[4];
// load in the images (arrays are indexed starting from zero)
frames[0] = loadImage("testball1.GIF");
frames[1] = loadImage("testball2.GIF");
frames[2] = loadImage("testball3.GIF");
frames[3] = loadImage("testball4.GIF");
currFrame = 0;
}

void draw()
{
background(200);
image(frames[currFrame], 50, 50);
// every thirty frames, increment currFrame
// the % operator returns the remainder of the
// left side divided by the right side
// so what we are asking here is if frameCount is
// a multiple of 30
if ( frameCount % 30 == 0 )
{
currFrame++;
if ( currFrame > 3 ) currFrame = 0;
}
}


For starters, is there anything in that code you don't understand?
Re: Newb who wants to make game
Reply #14 - Jan 17th, 2007, 12:52am
 
I think our boy here needs to worry about the mechanics of the game before he starts animating.

If the graphics interfere with the game engine, you've wasted a lot of effort. Build the car around the engine, not the engine around the car.

Things like particle systems are a good place to start.
Code:

// Things we want to use
Bot bot0, bot1;
// Before the program is up and running
void setup(){
size(400, 400);
smooth();
bot0 = new Bot(random(width), random(height), 0.05);
bot1 = new Bot(random(width), random(height), 0.1);
}
// What happens in ONE frame of running the sketch
void draw(){
background(0);
bot0.move();
bot0.drawBot();
bot1.move();
bot1.drawBot();
}
// An object maker ->
class Bot{
// properties of our object
float x,y,speed;
// set the object's properties on creation
Bot(float x, float y, float speed){
this.x = x;
this.y = y;
this.speed = speed;
}
// methods
void move(){
x += (mouseX - x) * speed;
y += (mouseY - y) * speed;
}
void drawBot(){
ellipse(x, y, 50, 50);
}
}
Pages: 1 2 3