Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: typing game stuck up

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default typing game stuck up

    i dont know what specific title should i post for this topic, its a typing game that I'm writing at the moment.. the codes that i will post here is just the original code where it came from, well any way ill drop the codes first. it consist of 6 classes and i need to included for the program to run properly except some 3-4 images file to show the entire game, its not actually the game that im writing.. i just modify it (returned to its original form) to make it short as much as possible.

    Alien.Class
    import java.awt.Image;
    import java.awt.Rectangle;
     
    import javax.swing.ImageIcon;
     
     
    public class Alien {
     
        private int x;
        private int y;
        private int width;
        private int height;
        private boolean visible;
        private Image image;
     
        public Alien(int x, int y) {
     
            ImageIcon ii = new ImageIcon("C:\\Space\\alien.png");
            image = ii.getImage();
            width = image.getWidth(null);
            height = image.getHeight(null);
            visible = true;
            this.x = x;
            this.y = y;
        }
     
     
        public void move() {
     
            if (x < 0) {
     
                x = 1400;
            }
     
            x -= .01;
        }
     
        public int getX() {
     
            return x;
        }
     
        public int getY() {
     
            return y;
        }
     
        public boolean isVisible() {
     
            return visible;
        }
     
        public void setVisible(Boolean visible) {
     
            this.visible = visible;
        }
     
        public Image getImage() {
     
            return image;
        }
     
        public Rectangle getBounds() {
     
            return new Rectangle(x, y, width, height);
        }
    }


    Craft.class
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
     
    import java.util.ArrayList;
     
    import javax.swing.ImageIcon;
     
    public class Craft {
     
        private int dx;
        private int x;
        private int width;
        private int height;
        private boolean visible;
        private Image image;
        private ArrayList missiles;
     
     
        public Craft() {
     
            ImageIcon ii = new ImageIcon("C:\\Space\\spaceCraft.png");
            image = ii.getImage();
            width = image.getWidth(null);
            height = image.getHeight(null);
            missiles = new ArrayList();
            visible = true;
            x = 40;
        }
     
     
        public void move() {
     
            x += dx;
     
            if (x < 1) {
                x = 1;
            }
        }
     
        public int getX() {
            return x;
        }
     
     
        public Image getImage() {
            return image;
        }
     
        public ArrayList getMissiles() {
            return missiles;
        }
     
        public void setVisible(boolean visible) {
            this.visible = visible;
        }
     
        public boolean isVisible() {
            return visible;
        }
     
        public void keyPressed(KeyEvent e) {
     
            int key = e.getKeyCode();
     
            if (key == KeyEvent.VK_SPACE) {
     
                fire();
            }
        }
     
        public void fire() {
     
            missiles.add(new Missile(x + width, 59));
        }
    }

    Missile.class
    import java.awt.Image;
    import java.awt.Rectangle;
    import javax.swing.ImageIcon;
     
    public class Missile {
     
        private int x, y;
        private Image image;
        boolean visible;
        private int width, height;
     
        private final int BOARD_WIDTH = 900;
        private final int MISSILE_SPEED = 15;
     
        public Missile(int x, int y) {
     
            ImageIcon ii = new ImageIcon("C:\\Space\\missile.png");
            image = ii.getImage();
            visible = true;
            width = image.getWidth(null);
            height = image.getHeight(null);
            this.x = x;
            this.y = y;
        }
     
     
        public Image getImage() {
     
            return image;
        }
     
        public int getX() {
     
            return x;
        }
     
        public int getY() {
     
            return y;
        }
     
        public boolean isVisible() {
     
            return visible;
        }
     
        public void setVisible(Boolean visible) {
     
            this.visible = visible;
        }
     
        public Rectangle getBounds() {
     
            return new Rectangle(x, y, width, height);
        }
     
        public void move() {
     
            x += MISSILE_SPEED;
     
            if (x > BOARD_WIDTH) {
     
                visible = false;
            }
        }
    }

    Collision.class
     
     
    import javax.swing.JFrame;
     
    /**
     *
     * @author zab
     */
    public class Collision extends JFrame {
     
        public Collision() {
            add(new Board());
     
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(550, 300);
            setLocationRelativeTo(null);
            setTitle("Collision");
            setResizable(false);
            setVisible(true);
        }
     
        public static void main(String[] args) {
     
            new Collision();
        }
    }

    Board.class (where the whole game logic goes)
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
     
    import java.util.ArrayList;
     
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
     
    public class Board extends JPanel implements ActionListener {
     
        private Timer timer;
        private Craft craft;
        private ArrayList alienList;
        private Image bg;
        private Character charac;
        private String userTyped;
        private RandomWords rWords;
     
        private int[][] alienPosition = {
     
            {450, 59}, {400, 59}, {500, 59},
         };
     
        public Board() {
     
            addKeyListener(new TAdapter());
            setFocusable(true);
            setBackground(Color.BLACK);
            setSize(400, 300);
            craft = new Craft();
            rWords = new RandomWords();
            userTyped = "";
            timer = new Timer(15, this);
            timer.start();
            initAliens();
        }
     
        public void initAliens() {
     
            alienList = new ArrayList();
     
            for (int i = 0; i < alienPosition.length; i++ ) {
     
                alienList.add(new Alien(alienPosition[i][0], alienPosition[i][1]));
            }
        }
     
        public void paint(Graphics g) {
     
            super.paint(g);
            Graphics2D g2d = (Graphics2D) g;
     
     
            g2d.drawImage(bg, 0, 0, null);
            g2d.setColor(Color.WHITE);
            g2d.drawString("Type the Word and Press Space Bar To Shoot, Press Back space to restart the word", 0, 10);
            g2d.drawImage(craft.getImage(), craft.getX(), 59, this);
     
            ArrayList ms = craft.getMissiles();
     
            // draws the missiles
            for (int i = 0; i < ms.size(); i++) {
     
                Missile m = (Missile) ms.get(i);
                g2d.drawImage(m.getImage(), m.getX(), m.getY(), this);
            }
     
            //draws the aliens
            for (int i = 0; i < alienList.size(); i++) {
     
                Alien alien = (Alien) alienList.get(i);
     
                if (alien.isVisible()) {
     
                    g2d.drawImage(alien.getImage(), alien.getX(), alien.getY(), this);
                    g2d.setColor(Color.WHITE);
                    g2d.drawString(rWords.getWord(), alien.getX(), alien.getY());
                }
            }
     
            g2d.setFont(new Font("Monospaced", Font.BOLD, 15));
            g2d.drawString(userTyped, 10, 30);
        }
     
     
        public void actionPerformed(ActionEvent e) {
     
            ArrayList missileList = craft.getMissiles();
     
            // movement of missiles
            for (int i = 0; i < missileList.size(); i++) {
     
                Missile missile = (Missile) missileList.get(i);
     
                if (missile.isVisible()) {
     
                    missile.move();
                }
                else {
     
                    missileList.remove(i);
                }
            }
     
            // movement of aliens
            for (int i = 0; i < alienList.size(); i++) {
     
                Alien alien = (Alien) alienList.get(i);
     
                if (alien.isVisible()) {
     
                    alien.move();
                }
                else {
     
                    alienList.remove(i);
                }
            }
     
            craft.move();
            checkCollisions();
            repaint();
        }
     
        public void checkCollisions() {
     
            ArrayList missileList = craft.getMissiles();
     
            for (int i = 0; i < missileList.size(); i++) {
     
                Missile missile = (Missile) missileList.get(i);
                Rectangle r1 = missile.getBounds();
     
                for (int j = 0; j<alienList.size(); j++) {
     
                    Alien a = (Alien) alienList.get(j);
                    Rectangle r2 = a.getBounds();
     
                    if (r1.intersects(r2)) {
     
                        missile.setVisible(false);
                        a.setVisible(false);
                        rWords.generateWord();
                        userTyped = "";
                    }
                }
            }
        }
     
        private class TAdapter extends KeyAdapter {
     
            @Override
            public void keyReleased(KeyEvent e) {
     
                if (userTyped.equals(rWords.getWord()) && e.getKeyCode() == KeyEvent.VK_SPACE) {
     
                    craft.keyPressed(e);
                }
                else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
     
                    userTyped = "";
                    charac = null;
                }
            }
     
            @Override
            public void keyPressed(KeyEvent e) {
     
                if (userTyped.equals(rWords.getWord()) && e.getKeyCode() == KeyEvent.VK_SPACE) {
     
                    craft.keyPressed(e);
                }
                else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
     
                    userTyped = "";
                    charac = null;
                }
            }
     
            public void keyTyped(KeyEvent e) {
     
                charac = e.getKeyChar();
                userTyped = userTyped + charac;
            }
        }
    }


    RandomWords.class
    public class RandomWords {
     
        private String temp[] = {"as", "qe", "uu"};
        private String word;
     
        public RandomWords() {
     
            generateWord();
        }
     
        public String getWord() {
     
            return word;
        }
     
        public void generateWord() {
     
            int randomWord = (int) Math.round(Math.random() * 2);
            word = temp[randomWord];
        }
     
        public void clearWord() {
     
            word = "";
        }
    }

    although its a very long code.. the only focus of my problem is on the Board class where all the game logic happens, and RandomWords class that supports the idea of my problem

    if you run the program assuming that you already got the images to see whats happening, you can see that you can only fire a missile to an enemy if you type the words brought by the enemies

    actually the program runs almost exactly what i want..

    The only problem is .. Im stuck on a part where i need to show the words one by one,
    the program shows all the words that i needed to type, in this case when you run the program.. the 3 words are already shown... together with the 3 enemies(aliens). What i want is when the program runs.. the first enemy will only show the first word.. the 2 will not (YET)
    until the first word(enemy) will be shot down.. then the second word will appear .. then so on

    i tried to use some boolean (shouldPaint) switching to true or false. everytime the craft shoots the enemy.. but i just messing it up.. i need help with this to continue on my next step on the game that im writing..

    i know its too long.. but this is as far as i can do to make it short as possible..

    only the Board class.. the rest doesnt have anything to do with the problem.. i just included it to make the program run... and sorry for the bad english
    Last edited by chronoz13; April 22nd, 2011 at 12:54 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: typing game stuck up

    Your English is very good.

    But you're right, that is way too much code. Usually people post an SSCCE that demonstrates what they're talking about.

    But to get you started, I think what you want to do is keep track of the current word and only draw and check against that one. So instead of an ArrayList of words, you might just have one word variable, or maybe an index for the current word.

    Does that make any sense?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    chronoz13 (April 23rd, 2011)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: typing game stuck up

    regarding with SSCCE.. since i started going on forums... though i use to messed up a lot before... i tried to follow some rules as much as possible... but just like what i've said. this is as far as i can go making the code as short as possible, the orginal code that I'm writing is already too long.. this is an original code from a tutorial that i modified. Well all the codes are ready to be compiled, just need some small images to run it perfectly. Anyway bro thanks for giving some idea.. though i dont get it perfectly.. i appreciate it..

    keep track of the current word and only draw and check against that one.
    well i tried doing it... by using some boolean values true and false... but still it paints 3 words from the same index
    instead of painting only 1... well i think the problem is on the paint area... but i dont know how can i resolve

    well still thanks for giving your time looking at it..

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: typing game stuck up

    oh atlast i solved it... i just created an Arraylist of String in a seperate class,a method which returns an arraylist(same capacity as the enemy arrayList) of randomly generated string.. then in the board class...

    i made a simple if statement that monitors the INDEX of the enemy that is being painted

    where i is the iterator that paints the enemies

    if (i == 0) {
     
           paint the words from the arralist
    }

    the words will only be painted if the enemy that is going to be painted is from 0 index (which is the first enemy) the enemy in front

    so everytime the program removes the enemy in the list if it has been hit (removed) , the enemy on index 1 will be on index 0 again, then "i" will always be true and will always print the string one at a time (always the first enemy)

    say that i == 0.. print the enemy and the string,, and if i == 1 then print the 2nd enemy but not the string.. until the enemy on index 0 (i = 0) has been hit.. then so on.. i hope you understand what i said..

    and if its ok.. i wont mark this thread as solved for a while.. in case i got another problems again.. ill just modify those codes above.. and formulate clear statements to for that particular problem.. and so i dont need to repost another same codes on a different topic... thanks bro for giving that idea.. of keeping track of the enemies..

    i didnt expect that a very little simple if statement with 2 values being compared will solved the problem.. maybe i was thinking too much logic and my mind was going out of bounds.. i didnt know the answer was already there LOL

    -- every time i try to post my unsolved problem on this forum... in just a few hours or a day.. suddenly i use to solve the problem.. it always happens since i started posting here... lol what a lucky charm this forums is to me LOL

    thanks for viewing my post LOL

    as of now problem SOLVED
    Last edited by chronoz13; April 24th, 2011 at 05:35 AM.

  6. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: typing game stuck up

    Animating the AI to shoot in a certain point:

    ok,now i think i need to post another one again.. i've been dealing with this in days. trying to work on different logics to resolve the issue.. but still i ended up DEAD again.

    but first, im gonna post another modification of the classes above instead of modifying those, so viewers can use those above if they want for their games as well,

    Collision Class (Main class that runs the whole program)
    import javax.swing.JFrame;
     
    public class Collision extends JFrame {
     
        public Collision() {
     
            add(new Board());
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(400, 300);
            setLocationRelativeTo(null);
            setTitle("Collision");
            setResizable(false);
            setVisible(true);
        }
     
        public static void main(String[] args) {
     
            new Collision();
        }
    }

    AlienMissile Class
    import java.awt.Image;
    import java.awt.Rectangle;
    import javax.swing.ImageIcon;
     
    public class AlienMissile {
     
        private int x, y;
        private Image image;
        boolean visible;
        private int width, height;
     
        private final int BOARD_WIDTH = 900;
        private final int MISSILE_SPEED = 15;
     
        public AlienMissile(int x, int y) {
     
            ImageIcon ii = new ImageIcon("C:\\Space\\missile.png");
            image = ii.getImage();
            visible = true;
            width = image.getWidth(null);
            height = image.getHeight(null);
            this.x = x;
            this.y = y;
        }
     
     
        public Image getImage() {
     
            return image;
        }
     
        public int getX() {
     
            return x;
        }
     
        public int getY() {
     
            return y;
        }
     
        public boolean isVisible() {
     
            return visible;
        }
     
        public void setVisible(Boolean visible) {
     
            this.visible = visible;
        }
     
        public Rectangle getBounds() {
     
            return new Rectangle(x, y, width, height);
        }
     
        public void move() {
     
            x -= MISSILE_SPEED;
     
            if (x < BOARD_WIDTH) {
     
                visible = false;
            }
        }
    }

    Alien Class
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.util.ArrayList;
    import javax.swing.ImageIcon;
     
     
    public class Alien {
     
        private int x;
        private int y;
        private int width;
        private int height;
        private boolean visible;
        private Image image;
        private ArrayList aMissiles;
     
        public Alien(int x, int y) {
     
            ImageIcon ii = new ImageIcon("C:\\Space\\alien.png");
            image = ii.getImage();
            width = image.getWidth(null);
            height = image.getHeight(null);
            visible = true;
            aMissiles = new ArrayList();
            this.x = x;
            this.y = y;
        }
     
     
        public void move() {
     
            x -= .01;
        }
     
        public int getX() {
     
            return x;
        }
     
        public int getY() {
     
            return y;
        }
     
        public boolean isVisible() {
     
            return visible;
        }
     
        public void setVisible(Boolean visible) {
     
            this.visible = visible;
        }
     
        public Image getImage() {
     
            return image;
        }
     
        public Rectangle getBounds() {
     
            return new Rectangle(x, y, width, height);
        }
     
        public void fire() {
     
            aMissiles.add(new AlienMissile(x + width, y + height/2));
        }
     
        public ArrayList getAlienMissiles() {
     
            return aMissiles;
        }
    }

    Board Class(where the whole logic of program goes)
    import java.awt.*;
    import java.awt.event.*;
    import java.util.ArrayList;
    import javax.swing.*;
     
    public class Board extends JPanel implements ActionListener {
     
        private Timer timer;;
        private Alien alien;
        private ArrayList alienList;
        private Image bg;
     
        public Board() {
     
            setFocusable(true);
            setDoubleBuffered(true);
            setBackground(Color.BLACK);
            setSize(400, 300);
            initAliens();
            timer = new Timer(15, this);
            timer.start();
        }
     
        public void initAliens() {
            alienList = new ArrayList();
            alienList.add(new Alien(450, 29));
        }
     
        public void paint(Graphics g) {
            super.paint(g);
     
            Graphics2D g2d = (Graphics2D) g;
            g2d.drawImage(bg, 0, 0, null);
     
            alien = (Alien) alienList.get(0);
     
            g2d.drawImage(alien.getImage(), alien.getX(), alien.getY(),this);
     
            // this part will check the certain point of the  alien
            // and SHOULD FIRE a missile from that point
            if (alien.getX() < 400) {
                alien.fire();
     
                ArrayList aMissileList = alien.getAlienMissiles();
                AlienMissile aMissile = (AlienMissile) aMissileList.get(0);
     
                g2d.drawImage(aMissile.getImage(), aMissile.getX(), aMissile.getY(),this);
            }
        }
     
        public void actionPerformed(ActionEvent e) {
     
            try {
                ArrayList aMissileList = alien.getAlienMissiles();
                AlienMissile aMissile = (AlienMissile) aMissileList.get(0);
     
                // this part should move the alien's missile
                // or updates the alien missile's position on the screen
                aMissile.move(); 
            }
            catch (NullPointerException ex) {}
     
            alien = (Alien) alienList.get(0);
            alien.move();
     
            repaint();
        }
    }

    I included another 3 classes to support the whole game.. but the problems always occurs in the Board class.

    now my problem is basically making the enemies from the right side SHOOT when they get on a certain 'x' position (to left). but instead, the paint method is just painting the AlienMissile image together with the alien when it moves. im really stuck up here
    please help again

    I attached the 3 images needed to run this program
    Attached Files Attached Files
    Last edited by chronoz13; April 29th, 2011 at 08:06 AM.

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: typing game stuck up

    Again, that's way too much code. Without an SSCCE, we can't really help you.

    But it sounds like you have logic in a paint method. That's bad. Do the logic in the game loop before calling paint. Typically, the objects in the game will have an step() method (or act, or whatever you want to call it) that gets called each frame. That method handles any logic. Then the game JPanel is repainted, and its paintComponent() method calls the paint methods of the visible game Objects. Don't put logic in the paint methods.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: typing game stuck up

    Again, that's way too much code
    oh yes.. i didnt notice that i can still make the board class shorter than the one i posted.. sorry bro

    ok now. i tried to reduce all the unnecessary parts that is not needed as much as possible... i have edited the board class and update the Board.class above.
    Don't put logic in the paint methods
    well i got youre idea.. let me play with some ideas and logics for a moment.. but if anyone can help me out please, it would be greatly appreciated, thanks for that idea again bro

    by the way ! greetings to all British mates around the forum!

Similar Threads

  1. Im stuck, please help
    By bigsmoke101 in forum Loops & Control Statements
    Replies: 3
    Last Post: April 12th, 2011, 04:34 PM
  2. Stuck again
    By Tate in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 28th, 2010, 10:22 AM
  3. Stuck :(
    By hing09 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 17th, 2010, 05:20 PM
  4. I'm stuck
    By iank in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 5th, 2009, 09:21 AM
  5. Problem while programming a simple game of Car moving on a road
    By rojroj in forum Java Theory & Questions
    Replies: 3
    Last Post: April 2nd, 2009, 10:24 AM