Results 1 to 5 of 5

Thread: Thread problem.

  1. #1
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default Thread problem.

    HI all..I made a simple shoot game using Threads.. I used only one Thread . my problem is when I run my game it works wery fast .But on my compiler it works fine.but when i try on other computer it works wery fast .BUt after changing size of window it slows..I used simple thread start and in run method used thread.sleep(250).
    Am I missed something?or some configuration need?

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Every iteration of your thread two things happen (i presume):
    * redraw the game
    * Thread.sleep (250).


    So, the longer it takes to redraw the game, the slower the game becomes, since you're always sleeping 250 millis no matter what... What you can do instead is this:

    long timeStart = System.currentTimeMillis ();
    // draw the game
    long timeEnd = System.currentTimeMillis ();

    // how long did it take to paint the game?
    int timeElapsed = (int)(timeEnd - timeStart);

    // not long enough - need to sleep
    if (timeElapsed < 250)
    {
    Thread.sleep (250 - timeElapsed);
    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  4. #4
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    I used only drawing movements of my game and fireson painComponent. and used apponent movements in thread.Now I changed it all in thread but it is ery slow .I also changed Thread.sleep(1) but it is slow and creashes ..Am I have to use my ship movements on paintcomponent and apponnets on Thread? I used both in thread because i want to make Pause on game..NOw it Pauses only apponent ship but it continious firing..:)) I also draw fire on paintComponent...
    Here My main class with paint component and thread.. IT is a little garbage .))
    and this is the link for game
    RapidShare: 1-CLICK Web hosting - Easy Filehosting
    Java Code:
    public class Shooter extends JFrame implements KeyListener, Runnable {
    
        Font font;
        Image img;
        Image backGround;
        Graphics dbi;
        URL url1 = this.getClass().getResource("Images/p2.gif");
        URL url2 = this.getClass().getResource("Images/p3.gif");
        URL url3 = this.getClass().getResource("Images/p1.gif");
        URL url4 = this.getClass().getResource("Images/p4.gif");
        URL urlMap = this.getClass().getResource("Images/zemin.jpg");
        Player p1 = new Player(5, 150, 10, 40, Color.GREEN, url3);
        Computer p2 = new Computer(750, 150, 10, 40, Color.BLUE, url1);
        Computer p3 = new Computer(0, 0, 10, 40, Color.BLUE, url2);
        Computer p4 = new Computer(0, 0, 10, 40, Color.BLUE, url4);
        ArrayList<Bullets> b = new ArrayList<Bullets>();
        ArrayList<CBullets> cb = new ArrayList<CBullets>();
        Thread sheap;
        boolean a, d, w, s;
        boolean toUp, toDown;
        boolean GameOver;
        boolean Level2;
        boolean newGame, resart, pause;
        int S, E;
        int random;
        int cbSpeed = 0;
    
        public Shooter() {
    
            sheap = new Thread(this);
            sheap.start();
            font = new Font("Tiresias PCFont Z", Font.BOLD + Font.ITALIC, 32);
            setTitle("Shooter 1.5.0");
            setBounds(350, 250, 800, 600);
    
            setResizable(false);
            setBackground(Color.black);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            addKeyListener(this);
            a = d = w = s = false;
            toUp = toDown = true;
            GameOver = true;
            newGame = true;
            Level2 = false;
            S = E = 0;
    
            setVisible(true);
        }
    
        public void paint(Graphics g) {
    
            img = createImage(getWidth(), getHeight());
            dbi = img.getGraphics();
            paintComponent(dbi);
            g.drawImage(img, 0, 0, this);
            repaint();
        }
    
        public void paintComponent(Graphics g) {
    
            backGround = Toolkit.getDefaultToolkit().getImage(urlMap);
            g.drawImage(backGround, 0, 0, null);
            g.setColor(Color.red);
            g.drawString("Player 1 Health : " + p1.health, 30, 50);
            g.setColor(Color.red);
            g.drawString("Computer Health : " + (p2.health+p3.health+p4.health), 650, 50);
            if (newGame) {
                g.setColor(Color.LIGHT_GRAY);
                g.setFont(font);
                g.drawString("Well Come To Shoot Game", 200, 190);
                g.drawString("Press ENTER To Start", 250, 220);
                newGame();
    
            }
            if (!GameOver) {
    
                for (Bullets b1 : b) {
    
                    b1.draw(g);
                }
                for (CBullets b2 : cb) {
    
                    b2.draw(g);
    
                }
                update(); // Here MOvements for Player and For Fires
            }
            if (p1.health <= 0) {
                g.setColor(p2.col);
                g.setFont(font);
                g.drawString("Computer Wins ", 200, 190);
                
                g.drawString("Press Key R  to Resart ", 200, 220);
                GameOver = true;
    
            } else if (p2.health <= 0 && p3.health <= 0 && p4.health <= 0) {
    
                g.setColor(p1.col);
              
                g.drawString("Player 1 Wins ", 200, 190);
             
                g.drawString("Press Key R  to Resart ", 200, 220);
                GameOver = true;
    
            }
    
            if (Level2) {
                if (p3.health >= 0) {
                    p3.draw(g);
    
                    for (CBullets b3 : cb) {
                        b3.draw(g);
                    }
                } else {
                    p3.x = 1000;
                }
    
                if (p4.health >= 0) {
                    p4.draw(g);
    
                    for (CBullets b4 : cb) {
                        b4.draw(g);
                    }
                } else {
                    p4.x = 1000;
                }
            }
            if (p1.health >= 0) {
                p1.draw(g);
            }
            if (p2.health >= 0) {
                p2.draw(g);
            } else {
                p2.x = 1000;
            }
    
        }
        public void update() {
    
            if (w && p1.y > 54) {
                p1.moveUp();
            }
    
            if (s && p1.y < 547) {
                p1.moveDown();
            }
              if (a && p1.x > 0) {
                p1.moveLeft();
            }
    
            if (d && p1.x < 200) {
                p1.moveRight();
            }
            random = 1 * (int) (Math.random() * 100);
    
            if (random > 94) {
                if (p2.health >= 0) {
                    CBullets bo = p2.getCBull();
    
                    bo.xVel = -2 - cbSpeed;
    
                    cb.add(bo);
                }
                if (Level2) {
                    if (p3.health >= 0) {
                        CBullets bo1 = p3.getCBull();
                        bo1.xVel = -2 - cbSpeed;
                        cb.add(bo1);
                    }
                    if (p4.health >= 0) {
                        CBullets bo2 = p4.getCBull();
                        bo2.xVel = -2 - cbSpeed;
                        cb.add(bo2);
                    }
                }
    
            }
    
            if (S == 1) {
    
                if (p1.health >= 0 ) {
                    Bullets bu = p1.getBull();
                    bu.xVel = 5;
                    b.add(bu);
                    S += 1;
                }
            }
    /*
              //Here Also Problem .. When COmputer have More fire then it gaves Array Exeption . Or Player have More Fire
            for (int i = 0; i < cb.size();i++ ) {
                for (int j = 0; j < b.size();j++ ) {
                    if (b.get(j).rect.intersects(cb.get(i).rect)) {
                     
                        cb.remove(i);
                        b.remove(j);
                      
                      
                        continue;
    
                    }  if (cb.get(i).rect.intersects(b.get(j).rect)) {
    
                       
                         b.remove(j);
                      cb.remove(i);
                       
                        continue;
    
                    }
    
                }
            
    }*/
            for (int i = 0; i < b.size(); i++) {
                b.get(i).move();
    
                if (b.get(i).rect.intersects(p2.rect)) {
    
                    if (p2.health >= 0) {
                        p2.health--;
    
                        b.remove(i);
                        System.out.println("Hited P2");
                        i--;
    
                        continue;
                    }
                }
    
                if (b.get(i).rect.intersects(p3.rect)) {
                    if (p3.health >= 0) {
                        p3.health--;
    
                        b.remove(i);
                        System.out.println("Hited P3");
                        i--;
    
                        continue;
                    }
                }
                if (b.get(i).rect.intersects(p4.rect)) {
                    if (p4.health >= 0) {
                        p4.health--;
    
                        b.remove(i);
                        System.out.println("Hited P4");
                        i--;
    
                        continue;
                    }
                }
            }
    
            for (int j = 0; j < cb.size(); j++) {
                cb.get(j).move();
    
                if (cb.get(j).rect.intersects(p1.rect) && cb.get(j).xVel < 0) {
    
                    p1.health--;
                    cb.remove(j);
                    j--;
                    continue;
                }
            }
        }
    
        public void level2() {
    
            if (p2.health <= 10) {
    
                Level2 = true;
                cbSpeed = 7;
                p3.x = 750;
                p4.x = 750;
                p2.speed = 10;
                p3.speed = 30;
                p4.speed = 80;
            }
        }
    
        public void keyTyped(KeyEvent e) {
        }
    
        public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) {
                case KeyEvent.VK_ENTER:
                    newGame = false;
    
                    break;
                case KeyEvent.VK_P:
                    pause = true;
    
                    break;
                case KeyEvent.VK_R:
                    resart = true;
    
                    break;
                case KeyEvent.VK_A:
                    a = true;
    
                    break;
    
                case KeyEvent.VK_D:
                    d = true;
                    break;
                case KeyEvent.VK_W:
                    w = true;
                    break;
                case KeyEvent.VK_S:
                    s = true;
                    break;
    
                case KeyEvent.VK_SPACE:
                    S += 1;
    
                    break;
            }
        }
        public void keyReleased(KeyEvent e) {
            switch (e.getKeyCode()) {
    
                case KeyEvent.VK_A:
                    a = false;
                    break;
                case KeyEvent.VK_D:
    
                    d = false;
                    break;
                case KeyEvent.VK_W:
                    w = false;
                    break;
                case KeyEvent.VK_S:
                    s = false;
                    break;
    
                case KeyEvent.VK_SPACE:
                    S = 0;
    
                    break;
    
            }
        }
    
        public void newGame() {
    
            p1.health = 20;
            p2.health = 20;
            p3.health = 20;
            p4.health = 20;
            p3.x = 0;
            p4.x = 0;
            p2.x = 750;
    
            Level2 = false;
            cbSpeed = 0;
            p2.speed=9;
            b.removeAll(b);
            cb.removeAll(cb);
            GameOver = false;
        }
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                public void run() {
                    KeyListener k = new Shooter();
                }
            });
    
        }
        @Override
        public void run() {
    
            while (true) {
    
                if (resart) {
                    newGame();
                    resart = false;
                }
                if (pause) {
                    Thread.currentThread().notify();
                }
                try {
    
                    if (!GameOver) {
                        level2();
                        if (p1.y < p2.y) {
                            p2.moveUp();
                        }
                        if (p1.y < p3.y) {
                            p3.moveUp();
                        }
                        if (p1.y < p4.y) {
                            p4.moveUp();
    
                        }
                        if (p1.y > p2.y) {
                            p2.moveDown();
                        }
                        if (p1.y > p3.y) {
                            p3.moveDown();
                        }
                        if (p1.y > p4.y) {
                            p4.moveDown();
                        }
    
                    }
    
                    Thread.currentThread().sleep(25);
                } catch (InterruptedException ex) {
                    System.out.print("FInished");
                }
            }
        }
    }

  5. #5
    Mekonom is offline Member
    Join Date
    Nov 2009
    Posts
    71
    Rep Power
    0

    Default

    Hi again..As I understood reason of my game on other computer ın dıfferent speed ıt ıs about rezulation..I made ıt 800-600 and on my computer ıt has 14" and 1200-860 rezulatin.Is it may problem?Because if I change wındows size when game is runnning it speeds changing..And How can solw this problem???????

Similar Threads

  1. thread problem
    By Harris68 in forum Networking
    Replies: 3
    Last Post: 03-03-2010, 07:53 PM
  2. Problem with a thread
    By dotabyss in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-14-2010, 10:56 PM
  3. [SOLVED] Thread in GUI Problem
    By terox13 in forum Threads and Synchronization
    Replies: 3
    Last Post: 05-27-2008, 08:32 PM
  4. [SOLVED] HELP! Thread Problem
    By nvidia in forum NetBeans
    Replies: 2
    Last Post: 05-24-2008, 04:56 AM
  5. Problem using thread +rmi in my homework
    By IbrahimAbbas in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-14-2008, 09:24 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
  •