Results 1 to 7 of 7
  1. #1
    Gourav2122's Avatar
    Join Date
    Sep 2006
    Gender
    male
    Location
    Pittsburgh, Pennsylvania, United States
    Posts
    12,531
    Reputation
    212
    Thanks
    2,026
    My Mood
    Dead

    pacman java -class code

    Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     /*
     * keyPressedClassUI.java
     *
     * Created on Sep 29, 2010, 8:05:53 AM
     */
    package pacManPlan;
     import java.awt.BasicStroke;
    import java.aw*****lor;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Polygon;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.geom.Ellipse2D;
     /**
     *
     * @author Administrator
     */
    public class pacManPlanUI extends javax.swing.JFrame
            implements KeyListener, Runnable {
         Graphics draw;
        int x = 50, y = 110;
        int start = 40, angle = 280;
        boolean okMove = false;//thread control flag
        long delay = 10; //fps
        int xMove = 0;
        int yMove = 0;
        Thread move = null;
        long time;// = System.currentTimeMillis();
        Graphics2D draw2d;
        Polygon borderLeft = new Polygon();
        Polygon borderRight= new Polygon();
       Polygon borderbot = new Polygon();
        Polygon triangle = new Polygon();
        // int xOffset, yOffset;
        BasicStroke wallStroke = new BasicStroke(10,
                BasicStroke.CAP_ROUND,
                BasicStroke.JOIN_ROUND);
        //fix the tunnelling problem
        int pressed, clipDir;
    //dots
        Ellipse2D dots[] = new Ellipse2D[10];
        Ellipse2D.Double ghost = new Ellipse2D.Double();
        Rectangle ghostRect = new Rectangle();
        Rectangle ghostRect2 = new Rectangle();//draw ghost
        int gx = 100;
        int gy = 40;
        int gxm = -1;       //ghost movement
        int gym = -1;
         public void setDots() {
            for (int x = 0; x < dots.length - 1; x++) {
                dots[x] = new Ellipse2D.Double();   //subclass
                dots[x].setFrame(x * 30, 40, 10, 10);
             }
        }
         public void setMaze() {
            //left border
            borderLeft.addPoint(0, 0);
            borderLeft.addPoint(0, 600);
            borderLeft.addPoint(10, 600);
            borderLeft.addPoint(10, 0);
            borderLeft.addPoint(0, 0);//back to starting point
              borderRight.addPoint(600,600);
            borderRight.addPoint(600,0);
            borderRight.addPoint(600,10);
            borderRight.addPoint(600,600);
             borderbot.addPoint(0, 600);
            borderbot.addPoint(10, 600);
           
      
             //draw.fillRect(x, y, WIDTH, start);
            triangle.addPoint(50, 50);
            triangle.addPoint(25, 100);
            triangle.addPoint(75, 100);
            triangle.addPoint(50, 50);
        }
         public void clipMaze() {
            Rectangle pacRec = new Rectangle(x - 5, y - 5, 25, 25);
            //clipDir is a flag
            clipDir = -1;
            if (borderLeft.intersects(pacRec)) {
                xMove = 0;
                yMove = 0;
                clipDir = pressed;
                //set the direction of travel when clipping
            }
             if (triangle.intersects(pacRec)) {
                xMove = 0;
                yMove = 0;
                clipDir = pressed;
            }
             //determine if ghost clips
            //determine which direction to go
            //random - bad idea
            //move in an intelligent direction
            //bounce back if clip
            switch (clipDir) {
                case 37:
                case 65:
                    x -= 2;
                    okMove = false;
                //break;
                case 38:
                    y -= 2;
                    okMove = false;
                //  break;
                case 39:
                    x += 2;
                    okMove = false;
                // break;
                case 40:
                    y += 2;
                    okMove = false;
            }
             for (int x = 0; x < dots.length - 1; x++) {
                if (dots[x].intersects(pacRec)) {
                    draw2d.setColor(Color.black);
                    draw2d.fill(dots[x]);
                    dots[x].setFrame(-100, -100, 0, 0);
                }
            }
        }
         /** Creates new form keyPressedClassUI */
        public pacManPlanUI() {
            initComponents();
            draw = this.jPanel1.getGraphics();
            addKeyListener(this);
            move = new Thread(this);
            move.start();
            draw2d = (Graphics2D) draw;
             ghostRect.setSize(30, 30);
            ghostRect2.setSize(20, 20);
            setMaze();
            setDots();
         }
         public void paint(Graphics g) {
            super.paint(g);
            //kill flicker
            g.setColor(Color.black);
            g.fillRect(0, 0, jPanel1.getWidth(), jPanel1.getHeight());
             update(draw);
             //update reduces flicker in animation
         }
         public void update(Graphics g) {
             //erase pac
             g.setColor(Color.black);
            g.fillOval(x - 3, y - 3, 25, 25);
             g.setColor(Color.yellow);
            g.fillArc(x, y, 20, 20, start, angle);
            g.setColor(Color.red);
            draw2d.setStroke(wallStroke);
            draw2d.drawPolygon(triangle);
            draw2d.drawPolygon(borderLeft);
     draw2d.drawPolygon(borderRight);
            //draw ghost
             draw2d.setColor(Color.black);
            draw2d.fillOval(gx-2, gy -2, 35,35);
            draw2d.setColor(Color.pink);
            ghostRect.setLocation(gx, gy);
            ghost.setFrame(ghostRect);
             draw2d.fill(ghost);
             //ghost2
             draw2d.setColor(Color.black);
            draw2d.fillOval(gx-2, gy -2, 35,35);
            draw2d.setColor(Color.green);
            ghostRect.setLocation(150, 25);
            ghost.setFrame(ghostRect2);
            draw2d.fill(ghost);
             draw2d.setColor(Color.white);
    //draw dots
            for (int x = 0; x < dots.length - 1; x++) {
                draw2d.fill(dots[x]);
            }
            //  clipMaze();
        }
         /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
             jPanel1 = new javax.swing.JPanel();
             setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
             jPanel1.setMaximumSize(new java.awt.Dimension(600, 600));
            jPanel1.setMinimumSize(new java.awt.Dimension(600, 600));
             javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 808, Short.MAX_VALUE)
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 600, Short.MAX_VALUE)
            );
             javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
             pack();
        }// </editor-fold>
         /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            //java.awt.EventQueue.invokeLater(new Runnable() {
            //  public void run() {
            new pacManPlanUI().setVisible(true);
    //            }
            //});
        }
         public void keyTyped(KeyEvent e) {
            int pressed = e.getKeyCode();
            String keyP = String.valueOf(pressed);
     
        }
         public void keyPressed(KeyEvent e) {
            int pressed = e.getKeyCode();
            String keyP = String.valueOf(pressed);
             //this.setTitle(okMove + " ");
      
         }
         public void keyReleased(KeyEvent e) {
            pressed = e.getKeyCode();
            String keyP = String.valueOf(pressed);
            //okMove = true;
            //        draw.clearRect(0,0,100,100);
    //        draw.drawString(keyP + " pressed", 10, 30);
            //        draw.clearRect(0,0,100,100);
    //        draw.drawString(keyP + " released", 10,50);
            //37 = left     = 100 number pad 4 = 65 A
            //38 = up
            //39 = right
            //40 = down
            //arrow keys
            //start the thread
            // if(move == null)
            //{
            //stop();
            //move.stop();
             //requires a Runnable frame
            //requires the run method
            // }
      
             // okMove = false;
            this.setTitle(pressed + " ");
            if (pressed == 37 || pressed == 100 || pressed == 65) {
                //draw.drawString("went left", 10, 70);
                //x-=2;
                //redraw the screen
                start = 220;
                //repaint();
                 xMove = -2;
                yMove = 0;
                okMove = true;
     
            }
             if (pressed == 39) {
                start = 40;
    //            x += 2;
    //            repaint();
                xMove = 2;
                yMove = 0;
                okMove = true;
            }
            //up
            if (pressed == 38) {
                start = 130;
    //            y -= 2;
    //            repaint();
                xMove = 0;
                yMove = -2;
                okMove = true;
            }
             //down
            if (pressed == 40) {
                start = 310;
    //            y += 2;
    //            repaint();
                xMove = 0;
                yMove = 2;
                okMove = true;
            }
            if (pressed == 32) {
                okMove = false;
            }
            if (okMove) {
                time = System.currentTimeMillis();
                 //reset when the movement starts
            }
         }
        // Variables declaration - do not modify
        private javax.swing.JPanel jPanel1;
        // End of variables declaration
         public void run() {
            //throw new UnsupportedOperationException("Not supported yet.");
            //long time = System.currentTimeMillis();
            //get the time the thread is started
            long sleepInterval = delay; //set to fps
            //this.setTitle(okMove + "in run");
    //int counter = 0;
            while (true) {
               
                if (okMove) {
                    clipMaze();
                    x += xMove;
                    y += yMove;
                    // this.setTitle("in run");
                    
                    //calculate the sleep interval
                    time += delay;
                    sleepInterval = Math.max(delay, time - System.currentTimeMillis());
                    //this.setTitle(sleepInterval + " ");
                    try {
                        move.sleep(sleepInterval * 2);
                    } catch (InterruptedException ex) {
                        //Logger.getLogger(keyPressedClassUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                 //chase pacman
                if(x < gx)
                {
                    gxm = -1;//move ghost to left
                }
                else if(x > gx)
                {
                    gxm = 1;
                }
                else if(x == gx)
                {
                    gxm = 0;
                }
                  if(y < gy)
                {
                    gym = -1;//move ghost to left
                }
                else if(y > gy)
                {
                    gym = 1;
                }
                else if(y == gy)
                {
                    gym = 0;
                }
                //move the ghost
                 //clipghost here
                gx += gxm;
                gy += gym;
                try {
                        move.sleep(sleepInterval * 2);
                    } catch (InterruptedException ex) {
                        //Logger.getLogger(keyPressedClassUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                 update(draw);
            }//end of while
        }
    }


    has mistakes

    -maze it NOT DRAWN (easy shit look for setMaze() and draw it
    -clipping WORKS LIKE SHIT! (help with that would be nice)
    -pacman artifacts and ghost artifacts are shit
    -ALSO insi ghost easy fix

    this i got so far

    I got a 67/100 on this...
    meh...



    -

  2. #2
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Why such a low score? seems like a lot of work.... even if bad organization.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  3. #3
    Gourav2122's Avatar
    Join Date
    Sep 2006
    Gender
    male
    Location
    Pittsburgh, Pennsylvania, United States
    Posts
    12,531
    Reputation
    212
    Thanks
    2,026
    My Mood
    Dead
    i didn't do shit.. in time..
    most of the code was spoon fed to us in school
    so, i did SHIT for the overall enhancements
    but i understand the basic of shit

    -maze looks like shit
    -ghosts have trail
    -not clipped properly so magic wall hacks for ghost
    i was spouse make them smart and chase and make a proper turn after hitting the wall depending on your position


    simple i did squat
    low score ftw
    i pass i happy

  4. #4
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Lol. Understood. Well least u passed. =/

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  5. #5
    Calard's Avatar
    Join Date
    Jun 2006
    Gender
    male
    Posts
    305
    Reputation
    13
    Thanks
    172
    Is this using double buffering?

  6. #6
    BassSR's Avatar
    Join Date
    May 2010
    Gender
    male
    Posts
    5,824
    Reputation
    570
    Thanks
    330
    i don't believe so.

  7. #7
    Calard's Avatar
    Join Date
    Jun 2006
    Gender
    male
    Posts
    305
    Reputation
    13
    Thanks
    172
    well thats a good place to start, makes everything look smoother and generally fuck up less

Similar Threads

  1. Program Used To Code Java???
    By <(O_o)> in forum Java
    Replies: 25
    Last Post: 08-01-2012, 10:07 AM
  2. Java Editing code!!!
    By Osama_Farooq in forum CrossFire Tutorials
    Replies: 14
    Last Post: 07-25-2010, 07:44 PM
  3. Replies: 0
    Last Post: 06-12-2010, 04:03 PM
  4. Compiling Java into native Object Code
    By radnomguywfq3 in forum Java
    Replies: 10
    Last Post: 03-09-2010, 03:11 PM
  5. wat da f java classes?!?!?!
    By Dested in forum General
    Replies: 14
    Last Post: 08-30-2009, 01:10 AM