Results 1 to 5 of 5
  1. #1
    majeed14 is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Making java code simpler.

    I have been working on some computer science homework for about half an hour, and i got this code. My teacher is kind of hard on the grading of the actual program, so i would appreciate it if anyone has any suggestions to make this code simpler and more professional.

    Java Code:
    package com.SuperDot;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    public class SuperSquare extends JFrame
    {
        public static SuperSquare game;
        
        public static void main(String[]args)
        {
            game = new SuperSquare();
        }
        
        private Font fontOne = new Font("Times New Roman", Font.BOLD, 40);
        
        public boolean running = false;
        public static boolean ableToMove = true;
        
        Toolkit tk = Toolkit.getDefaultToolkit();  
        final int WIDTH = ((int) tk.getScreenSize().getWidth());
        final int HEIGHT = ((int) tk.getScreenSize().getHeight());
        
        public static boolean playerDead;
        
        //////////////////////////xCoord,yCoord,xSpeed,ySpeed/////////////////////
        public int[] playerOneStats = {200, 300, 10, 10};
        public int[] mobStats = {600, 300, 5, 5};
        
        public int[] originalCoords = {200, 300, 600, 300};
        
        public int[] MapBorder = {10, WIDTH-40, 10, HEIGHT-60};
        
        public SuperSquare()
        {    
            setTitle("Super Square");
            setSize(WIDTH, HEIGHT);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setVisible(true);
            setResizable(false);
            setLayout(new BorderLayout());
            setFocusTraversalKeysEnabled(false);
            
            this.addKeyListener(new KeysListener());
            
            playerDead = false;
            running = true;
            
            Draw d = new Draw();
            add(d, BorderLayout.CENTER);
            
            tick();
        }
        
        public void tick()
        {
            while(running)
            {
                try
                {
                    Thread.sleep(30);
                }
                catch(Exception e)
                {
                    stop();
                }
                
                playerOneUpdate();
                mobUpdate();
                collisionCheck(playerOneStats[0], playerOneStats[1], mobStats[0], mobStats[1]);
            }
        }
        
        public void playerOneUpdate()
        {
            if(ableToMove)
            {
                if(KeysListener.goingUpOne && playerOneStats[1] >= MapBorder[0])
                {
                    playerOneStats[1]-=playerOneStats[3];
                }
                
                if(KeysListener.goingDownOne && playerOneStats[1] <= MapBorder[3])
                {
                    playerOneStats[1]+=playerOneStats[3];
                }
                
                if(KeysListener.goingLeftOne && playerOneStats[0] >= MapBorder[2])
                {
                    playerOneStats[0]-=playerOneStats[2];
                }
                
                if(KeysListener.goingRightOne && playerOneStats[0] <= MapBorder[1])
                {
                    playerOneStats[0]+=playerOneStats[2];
                }
            }
        }
        
        public void mobUpdate()
        {
            if(ableToMove)
            {
                if(playerOneStats[1] < mobStats[1] && mobStats[1] >= MapBorder[0])
                {
                    mobStats[1]-=mobStats[3];
                }
                
                if(playerOneStats[1] > mobStats[1] && mobStats[1] <= MapBorder[3])
                {
                    mobStats[1]+=mobStats[3];
                }
                
                if(playerOneStats[0] < mobStats[0] && mobStats[0] >= MapBorder[2])
                {
                    mobStats[0]-=mobStats[2];
                }
                
                if(playerOneStats[0] > mobStats[0] && mobStats[0] <= MapBorder[1])
                {
                    mobStats[0]+=mobStats[2];
                }
            }
        }
        
        public void collisionCheck(int p1x, int p1y, int p2x, int p2y)
        {
            if(p1x==p2x||p1x-10==p2x||p1x+10==p2x||p1x==p2x-10||p1x==p2x+10)
            {
                if(p1y==p2y||p1y-10==p2y||p1y+10==p2y||p1y==p2y-10||p1y==p2y+10)
                {
                    kill();
                }
            }
        }
        
        public void kill()
        {
            playerDead = true;
            ableToMove = false;
            
            playerOneStats[0] = originalCoords[0];
            playerOneStats[1] = originalCoords[1];
            
            mobStats[0] = originalCoords[2];
            mobStats[1] = originalCoords[3];
        }
        
        public void stop()
        {
            System.out.println("An error has occurred");
            System.exit(0);
        }
        
        private class Draw extends JPanel
        {
            public void paintComponent(Graphics g)
            {
                g.setColor(Color.BLACK);
                g.fillRect(0, 0, this.getWidth(), this.getHeight());
                
                g.setColor(Color.GREEN);
                g.fillRect(mobStats[0], mobStats[1], 20, 20);
                
                g.setColor(Color.RED);
                if(!playerDead)
                {
                    //if player is not dead, draw it out.
                    g.fillRect(playerOneStats[0], playerOneStats[1], 20, 20);
                }
                else
                {
                    //drawing the death screen.
                    g.fillRect(0, 0, this.getWidth(), this.getHeight());
                    g.setColor(Color.BLACK);
                    g.setFont(fontOne);
                    g.drawString("You were hit by \"the mob\". Press Enter to try again.", 300, this.getHeight()/2);
                }
                
                repaint();
            }
        }
        
        private static class KeysListener implements KeyListener
        {
            public static boolean goingUpOne;
            public static boolean goingDownOne;
            public static boolean goingLeftOne;
            public static boolean goingRightOne;
            
            public KeysListener()
            {
                goingUpOne = false;
                goingDownOne = false;
                goingLeftOne = false;
                goingRightOne = false;
            }
            
            public void keyPressed(KeyEvent e)
            {
                int keyCode = e.getKeyCode();
                
                if(keyCode == KeyEvent.VK_W)
                {
                    goingUpOne = true;
                }
                
                if(keyCode == KeyEvent.VK_A)
                {
                    goingLeftOne = true;
                }
                
                if(keyCode == KeyEvent.VK_S)
                {
                    goingDownOne = true;
                }
                
                if(keyCode == KeyEvent.VK_D)
                {
                    goingRightOne = true;
                }
                
                if(playerDead && keyCode == KeyEvent.VK_ENTER)
                {
                    playerDead = false;
                    ableToMove = true;
                }
            }
    
            public void keyReleased(KeyEvent e)
            {    
                int keyCode = e.getKeyCode();
                
                if(keyCode == KeyEvent.VK_W)
                {
                    goingUpOne = false;
                }
                
                if(keyCode == KeyEvent.VK_A)
                {
                    goingLeftOne = false;
                }
                
                if(keyCode == KeyEvent.VK_S)
                {
                    goingDownOne = false;
                }
                
                if(keyCode == KeyEvent.VK_D)
                {
                    goingRightOne = false;
                }
            }
    
            public void keyTyped(KeyEvent e)
            {    
            }
        }
    }
    P.S we weren't supposed to make an actual game goal, just a little gui that has some function to it.
    Last edited by majeed14; 01-29-2013 at 03:14 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Making java code simpler.

    Your code is kind of hard to read. Can you edit your post above and add [code] [/code] tags around your posted code? See the link in my signature links below to see more on this. Also, rather than just posting a code dump, could you tell us what part of your program bugs you the most? What are you trying to simplify? And what does it do?

  3. #3
    majeed14 is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Making java code simpler.

    Hello Fubarable. The part of the program that bugs me the most is the collision check method. may i ask how i can convert the series of "if" statements into two or less "for" loops to see if a square collided with the "mob". Any ideas would be greatly appreciated. lol my teacher also says that my code is really hard to read, but i have no trouble reading it for some reason. The actual program is a full screen GUI that has a mob with some AI that makes it follow the user until it collides with it.Thanks for the help!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Making java code simpler.

    Suggestsions:
    • Never call repaint() from inside of a paintComponent() method. That's a poor way to try to do a game loop as you will have absolutely no control of the loop, and you risk side effects. Use a Swing Timer instead.
    • Don't use a while (true) loop in your Swing application as you risk freezing the Swing event thread and thus freezing your application. Again use a Swing Timer for your game loop.
    • For your collision, you might want to use <= rather than ==. Movement isn't always guaranteed to be a unit step and so you could miss some collisions if you use ==.
    • Also for the collision, consider using Math.abs(...) to allow you to simplify the code.

  5. #5
    majeed14 is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Making java code simpler.

    Thanks man! I will sure look into using Swing Timer in the future, but as of now the teacher requested us using it. I actually have not thought of the math.abs method whilst doing this, that simplified some code a bunch!

Similar Threads

  1. Needs to be compressed in to simpler code.
    By thomachan21 in forum New To Java
    Replies: 2
    Last Post: 12-05-2012, 11:11 AM
  2. Help I need to make this code simpler ASAP!!
    By Quizzle23 in forum New To Java
    Replies: 5
    Last Post: 03-10-2011, 03:10 AM
  3. needs help making battleship in java
    By aznkid1221 in forum Java 2D
    Replies: 6
    Last Post: 11-06-2009, 06:05 PM
  4. Replies: 1
    Last Post: 10-02-2008, 04:06 AM
  5. code for making a java swing program a demo verson
    By fakhruddin in forum AWT / Swing
    Replies: 1
    Last Post: 11-27-2007, 09:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •