This is a read-only archive of the Opticraft Community forums (2010–2022), preserved in October 2026. Posting, logins and registration are closed.

Started by xNuzzy, December 26, 2012, 04:05:35 PM

Last Person To Reply Wins

NotABronie



Chief149

#47
Anyone interested in making their own game in java?

Code Select Expand
public class CraftMine
{
    public static void main(String [] args)
    {
         Game g = new Game(); //make the game object
         g.run(); //run the game
         System.exit(0); //exit and close all threads when the game is ready to close (isRunning is set to false)
    }
}


(had to use quote tag since code tag didnt display the entire thing)
Quote
//this class does nothing but provide you with a blank window to fill in with your game objects

import javax.swing.*;
import java.awt.*;

public class Game extends JFrame
{
    private boolean isPlaying = true;

    //fps settings
    private final int DEFAULT_FPS = 60;
    private int fps = DEFAULT_FPS;

    //window dimensions settings
    private int windowWidth = 640;
    private int windowHeight = 480;

    ///place any other global objects you may need below



    public static void main(String [] args) //ignore this
    {
    }

    public run()
    {
         boolean isPlaying = true;

         initialize(); //call the initialization logic
         
         while(isPlaying)
         {
              long startTime = System.currentTimeMillis();

              update();
              draw();

              long endTime = System.currentTimeMillis();

              //keep the framerate from going above the specified fps (default fps is 60)
              if((endTime - startTime) > (1000/fps))
              {
                   try {Thread.sleep((endTime - startTime) - (1000/fps));}
                   catch (Exception e) {}
              }
         }
    }

    public void initialize()
    {
         setSize(windowWidth, windowHeight); //sets the screen dimensions

         //TODO:  place initialization logic here! Set other JFrame settings as well
    }

    public void update()
    {
         //TODO:  place update logic here! Include user input. To make the program
         //stop, simply set isRunning to false to make the while loop exit and consequently, the whole program
    }
   
    public void draw()
    {
         BufferedImage bb = new BufferedImage(windowWidth, windowHeight, BufferedImage.TYPE_INT_RGB);
         
         Graphics buffer = getGraphics(); //get the graphics object (from the JFrame)
         Graphics backbuffer = bb.getGraphics(); //get the graphics object (from the backbuffer buffered image "bb")
         
         //TODO: place drawing logic here! (draw all of your shapes to the backbuffer object. NOT the buffer object)
         



        //END DRAWING LOGIC

        buffer.drawImage(backbuffer, 0, 0, this); //draw the backbuffer to the screen
        //this is known as double buffering, and this prevents the screen from flickering while performing the draw method
    }
}

Might as well post this little snippet while we're talking about games.
I'm sure cschurz will point out how it's not as efficient or neat as it could be. Well, I'm about to go to bed, so yeah. It's just the basic code. Of course you work your way up by making game objects which do all the lower level work so it makes the developement process easier.

kagarium

My English teacher from last year had a book called 'All my Friends are Dead'

RuthlessTomato

My dad likes weird songs. Title of 1: death to my hometown.
Chorus of the other: god is great, beer is good, nd ppl r crazy!

Morrison1996


Pew1998

Quote from: That Goalie Guy. on December 29, 2012, 07:34:22 PM
My dad likes weird songs. Title of 1: death to my hometown.
Chorus of the other: god is great, beer is good, nd ppl r crazy!
That song is not weird. I grew up listening to country and the songs have good meanings. Sure, most of them are about love, but that one is not. I know most of you are think that I am crazy for listening to country music, but "God is great, beer is good and people are crazy" :p

Pensfan121


NotABronie


RuthlessTomato

Quote from: pew1998 on December 30, 2012, 05:16:44 AM
Quote from: That Goalie Guy. on December 29, 2012, 07:34:22 PM
My dad likes weird songs. Title of 1: death to my hometown.
Chorus of the other: god is great, beer is good, nd ppl r crazy!
That song is not weird. I grew up listening to country and the songs have good meanings. Sure, most of them are about love, but that one is not. I know most of you are think that I am crazy for listening to country music, but "God is great, beer is good and people are crazy" :p
Dont get me wrong i love country. I just think 'death to my hometown' is an odd name. And the other is well, kinda stupid.

SonicHogSpeed

Quote from: Morrison1996 on December 30, 2012, 04:30:36 AM
Is it the 31st yet. :o
I just noticed it's the 30th XD. Depends on his time zone for the 31st

Morrison1996

Lol I posted that on the 29th I just needed something to say.

Pensfan121

Nothing for you to say other than Pensfan121 will win

CookieMaster

oh no,
MY BUTT is POOPING Turtles That POOP MEEEEE

NotABronie

so in essence you are pooping your creator?