Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (537)
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  
  Java game, what can I fix.  (Read 2085 times)
0 Members and 1 Guest are viewing this topic.
Offline Tylor

Junior Newbie





« Posted 2011-11-14 21:09:07 »

I have been working on this project mainly for the hell of It and I wanted to know what I could do better because I know just how bad I am at coding. Pay no attention to my imports. Cheesy

http://www.mediafire.com/?84fuk47hub9qqf4
Offline Tylor

Junior Newbie





« Reply #1 - Posted 2011-11-14 21:11:02 »

/*
 * Elude
 * By Tylor
 *
 *
 *
 */
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.io.PrintStream;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.awt.Toolkit;
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
import java.util.zip.*;

public final class JGame extends JFrame
{
    Grid grid;
    GameLoop gLoop;
    public static boolean p_up = false;
    public static boolean p_down = false;
    public static boolean p_left = false;
    public static boolean p_right = false;
    static Player player;
    static BufferedImage img;

    public JGame()
    {
        super("Eludë 0.1");
        this.grid = new Grid();
        add(this.grid);

        setDefaultCloseOperation(3);
        this.gLoop = new GameLoop(this);
        this.gLoop.start();

    }

    public void Menu()
    {

    }

    public static void main(String[] args)
    {

        int a = 2;
        int b = "DSU".hashCode() % 3000;
        int c = "WXC".hashCode() % 3000;
        for (int i = 0; i <= c; i++)
        {
            a = (a ^ i) % b;
        }
        System.out.println(a);
        JGame fm = new JGame();
        fm.setVisible(true);
        fm.setResizable(false);
        fm.setExtendedState(fm.MAXIMIZED_BOTH);
        fm.pack();
        fm.show();
    }
}
class Player
implements KeyListener
{
    int x;
    int buildings;
    Grid grid;

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    int y;
    int clevel = 1;
    String level = "Level: " + this.clevel;
    String version = "Eludë 0.1";
    String user = "Player.gif";

    boolean p_up = false;
    boolean p_down = false;
    boolean p_left = false;
    boolean p_right = false;
    int loop = 400;
    int startloop = 0;
    int backgroundx = 50;
    int backgroundy = 50;
    int backgroundx2 = 50;
    int backgroundy2 = 50;
    int backgroundx3 = 50;
    int backgroundy3 = 50;
    int backgroundx4 = 50;
    int backgroundy4 = 50;

    BufferedImage image;

    static int followx = 400;
    static int followy = 400;
    static boolean morex = false;
    static boolean morey = false;

    public Player(int i, int a)
    {
        this.x = i;
        this.y = a;
        try
        {
            image = ImageIO.read(Player.class.getResource(this.user));
        }
        catch (IOException e)
        {
            System.out.println("Image Not In Root");
        }
    }

    public void draw(Graphics g)
    {
        int gamespeed = 5;
        boolean boxagro = false;

        g.fillRect(followx, followy, 20, 20);

        g.drawString("Player", this.x - 2, this.y - 5);
        g.drawString(this.level, 0, screenSize.height - 25);
        g.drawString(this.version, screenSize.width - 90, screenSize.height - 25);
        //while ( startloop < loop)
        //{
        Random generator = new Random();
        backgroundx = generator.nextInt(screenSize.width);
        // System.out.println("backgroundX: " + backgroundx);
        backgroundy = generator.nextInt(screenSize.height);
        backgroundx2 = generator.nextInt(screenSize.width);
        //System.out.println("backgroundX2: " + backgroundx2);
        backgroundy2 = generator.nextInt(screenSize.height);
        backgroundx3 = generator.nextInt(screenSize.width);
        //System.out.println("backgroundX3: " + backgroundx3);
        backgroundy3 = generator.nextInt(screenSize.height);
        backgroundx4 = generator.nextInt(screenSize.width);
        backgroundy4 = generator.nextInt(screenSize.height);
        //}
        g.fillRect(backgroundx,backgroundy,5,5);
        g.fillRect(backgroundx2,backgroundy2,5,5);
        g.fillRect(backgroundx3,backgroundy3,5,5);
        g.fillRect(backgroundx4,backgroundy4,5,5);

        g.drawImage(image, this.x, this.y, null);
        int ydiff = this.y - followy;
        int xdiff = this.x - followx;
        //System.out.println("Xdiff" + xdiff);
        //System.out.println("Ydiff" + ydiff);
        if (ydiff < 300 || (xdiff < 300))
        {
            boxagro = true;
        }
        if (ydiff > 300 || (xdiff > 300) || (xdiff < -300) || (ydiff < -300))
        {
            boxagro = false;
            //System.out.println(boxagro);
        }
        if (boxagro == true)
        {
            if ( this.y < followy)
            {
                if (followy > 0 )
                {
                    followy--;followy--;
                }
            }
            if ( this.y > followy )
            {
                if (followy < screenSize.height - 30)
                {
                    followy++; followy++;
                }
            }
            if ( this.x > followx)
            {
                if (followx > 0 )
                {
                    followx++;followx++;
                }
            }
            if ( this.x < followx)
            {
                if (followy < screenSize.width )
                {
                    followx--;followx--;
                }
            }
        }

        if ((this.p_up == true) && (this.y > 25))
        {
            this.y -= gamespeed;

        }
        if ((this.p_down == true) && (this.y < screenSize.height - 55))
        {
            this.y += gamespeed;

        }
        if ((this.p_left == true) && (this.x > 10))
        {
            this.x -= gamespeed;

        }
        if ((this.p_right == true) && (this.x < screenSize.width - 35))
        {
            this.x += gamespeed;

        }
    }

    public void keyPressed(KeyEvent ke)
    {
        if ((ke.getKeyCode() == 38) &&
        (this.y > 15))
        {
            //System.out.println("Y: " + this.y);
            this.p_up = true;
            //System.out.println("FollowY" + followy);

        }
        if ((ke.getKeyCode() == 40) &&
        (this.y < screenSize.height - 45))
        {
            //System.out.println("Y: " + this.y);
            this.p_down = true;
            //System.out.println("FollowY" + followy);

        }
        if ((ke.getKeyCode() == 37) &&
        (this.x > 5))
        {

            //System.out.println("X: " + this.x);
            this.p_left = true;
            //System.out.println("FollowX" + followx);
        }
        if ((ke.getKeyCode() == 39) &&
        (this.x < screenSize.width - 35))
        {
            //System.out.println("X: " + this.x);
            this.p_right = true;
            //System.out.println("FollowX" + followx);

        }
        if (ke.getKeyCode() == 16)
        {
            this.clevel += 1;
            this.level = ("Level: " + this.clevel);
        }
    }

    public void keyReleased(KeyEvent ke)
    {
        if (ke.getKeyCode() == 38)
        {
            this.p_up = false;
        }
        if (ke.getKeyCode() == 40)
        {
            this.p_down = false;
        }
        if (ke.getKeyCode() == 37)
        {
            this.p_left = false;
        }
        if (ke.getKeyCode() == 39)
        {
            this.p_right = false;
        }
    }

    public void keyTyped(KeyEvent ke)
    {
    }
}
class Grid extends JPanel
{
    Player player;

    public Grid()
    {
        setFocusable(true);
        requestFocus();
        setBackground(Color.pink);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(0,0,screenSize.width, screenSize.height);
        setPreferredSize(new Dimension(screenSize.width, screenSize.height));

        this.player = new Player(16, 16);
        addKeyListener(this.player);
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        this.player.draw(g);
    }
}

class GameLoop extends Thread
{

    //int gamespeed = 1;
    JGame game;
    public GameLoop(JGame g)
    {
        this.game = g;
    }

    @Override
    public void run()
    {
        Player player;
        Grid grid;
        while (true)
        {
            this.game.repaint();
            try
            {
                Thread.sleep(15L);

            }
            catch (Exception e)
            {
                System.out.println("Error @ Thread.Sleep");
            }
        }
    }
}
Offline philfrei
« Reply #2 - Posted 2011-11-14 23:22:29 »

I was unable to compile/run the code from the link at the command line. I got these error messages:

javac JGame.java
>Note: JGame.java uses or overrides a deprecated API.
>Note: Recompile with -Xlint:deprecation for details.

java JGame
>1955
>Exception in thread "main" java.lang.IllegalArgumentException: input == null!
...
at Player.<init><JGame.java:122>

From within Eclipse:

in main, the line: fm.show() is marked as deprecated.

The line where things crash is on the resource load, which kind of makes sense since the gif didn't arrive with the download, afaik.

There's lots of unused methods as well. Maybe you are withholding parts of the code, and this is a simplification?

++++++++++++++++++++++++++

The main thing that jumps out at me is that it is unusual to see so much work done within a paint() method. Most folks separate the game logic from the drawing. Thus new positions and sizes would all be worked out prior to the draw call.

There are also what seem to be a few redundant calculations here and there. For example, in the calculation of new positions, I assume screen.height & .width don't vary during the course of the game, yet you are adding constants to them within a loop. Since you get the same result every time, why not just do it once?

There is a lot of discussion here about "the best" way to time game loops, and whether to use passive or active rendering. I will leave that to the more experienced!

Getting a working version up as an Applet will go a long way towards eliciting more feedback, as well.

Good luck!

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Tylor

Junior Newbie





« Reply #3 - Posted 2011-11-15 00:54:01 »

Im not really sure on your error. Mine compiles fine with no problem, Im using blueJ right now.
Offline Tylor

Junior Newbie





« Reply #4 - Posted 2011-11-15 00:56:24 »

http://www.mediafire.com/?5osfzjbpa264awc

The Jar.
Offline ra4king

JGO Kernel


Medals: 342
Projects: 2
Exp: 5 years


I'm the King!


« Reply #5 - Posted 2011-11-15 01:29:22 »

Could you please put your code in code blocks like so: [code] your code goes here [/code]

Also, we are not here/don't have the time to look through all your code and point out every error and bad coding you might have done. If you have specific questions, we will answer them to the best of our knowledge.

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.

CogWheelz (17 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (18 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (42 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

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

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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!