Page 1 of 5 123 ... LastLast
Results 1 to 20 of 85
Like Tree1Likes

Thread: i want write 2d java game

  1. #1
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default i want write 2d java game

    i want create basic 2d java game. where do i start? can i find any tutorials or examples? i start learning threads now.

    like this:
    i want write 2d java game-2upak4h.png

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: i want write 2d java game

    One thing you should look at is how to make animated drawings. There are a lot of moving ball programs (like pong) here on the Forum for code samples.

  3. #3
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    i try find some examples but i only find some advanced examples. most of them using game engines. i want use only threads. it will do not much things. my plane will move, enemies will move, we will shoot each other.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: i want write 2d java game

    I've never seen the "game engines" you talk about here on this Forum. There are many "moving ball" programs heret that would show thread usages.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: i want write 2d java game

    Start here: Programing An Applet Game Of Pong - Java Tutorials | Dream.In.Code

    Although some of his practices aren't the greatest, it should give you a decent starting point.

  6. #6
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    i found a website :The Java 2D games tutorial i practices these example.



    Java Code:
    public class Board extends JPanel implements ActionListener {
    
        private Timer timer;
        private Craft craft;
    
        public Board() {
    
            addKeyListener(new TAdapter());
            setFocusable(true);
            setBackground(Color.BLACK);
            setDoubleBuffered(true);
    
            craft = new Craft();
    
            timer = new Timer(5, this);
            timer.start();
        }
    
    
        public void paint(Graphics g) {
            super.paint(g);
    
            Graphics2D g2d = (Graphics2D)g;
            g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);
    
            Toolkit.getDefaultToolkit().sync();
            g.dispose();
        }
    
    
        public void actionPerformed(ActionEvent e) {
            craft.move();
            repaint();  
        }
    
    
        private class TAdapter extends KeyAdapter {
    
            public void keyReleased(KeyEvent e) {
                craft.keyReleased(e);
            }
    
            public void keyPressed(KeyEvent e) {
                craft.keyPressed(e);
            }
        }
    
    }
    how can i add background image to this code and can i use keylistener instead of keyadapter, timer ....

    i wrote this code null pointer exception help i do same thing but it move not smoothly.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: i want write 2d java game

    how can i add background image to this code
    Look at the drawImage method used in the paint method. That is where you would put the image you want as background
    can i use keylistener instead of keyadapter
    The KeyAdapter class implements the KeyListener interface and provides empty methods that you need to fill in.

  8. #8
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    Quote Originally Posted by Norm View Post
    Look at the drawImage method used in the paint method. That is where you would put the image you want as background

    The KeyAdapter class implements the KeyListener interface and provides empty methods that you need to fill in.
    thanks, i added background image. i wrote another drawImage.

    what is difference between keyAdapter and keyListener? why should i use adapter?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: i want write 2d java game

    difference between keyAdapter and keyListener?
    Read the API doc for the KeyAdapter class.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: i want write 2d java game

    Was just about to post what norm said. Get used to searching the API, often it is all you need. Key adapter is just easier than using a listener.

  11. #11
    dheaven is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: i want write 2d java game

    Quote Originally Posted by captain_turkiye View Post
    i want create basic 2d java game. where do i start? can i find any tutorials or examples? i start learning threads now.

    like this:
    Click image for larger version. 

Name:	2upak4h.png 
Views:	4 
Size:	39.1 KB 
ID:	2105
    You could also checkout this:
    Minesweeper Clone in Java

    The graphics are not that pretty, but the logical engine behind it is quite ok.

  12. #12
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    missiles code doesn't work in :Moving sprites is there any simple examples like this?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: i want write 2d java game

    missiles code doesn't work
    Can you post the code that "doesn't work" and explain what the problem is?

  14. #14
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    ok, i solved my problem. i must used thread for enemies, right? is there any basic thread for games examples?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: i want write 2d java game

    is there any basic thread for games examples?
    I don't remember seeing one on this forum.

  16. #16
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    how many thread should i use and which classes are runnable? now i have board, craft, missiles and test classes. i will add enemyplane class.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: i want write 2d java game

    how many thread should i use
    Depends on your program design. At least two, one for the user's GUI and one that is called by a timer to change positions of the things you are displaying.

  18. #18
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    i used actionperformed and time functions. can it work with threads or should i change my code?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: i want write 2d java game

    The Timer class uses a Thread when it calls your actionPerformed method.
    Read the API doc for the Timer class you are using.

  20. #20
    Join Date
    Oct 2011
    Posts
    50
    Rep Power
    0

    Default Re: i want write 2d java game

    timer class calls actionPerformed, threads calls run method. so what is the different?

Page 1 of 5 123 ... LastLast

Similar Threads

  1. Replies: 7
    Last Post: 11-30-2011, 06:58 PM
  2. trying to write a program for hangman word game
    By durdanto in forum New To Java
    Replies: 1
    Last Post: 02-12-2011, 02:53 AM
  3. How to write it in Java
    By agajantorayev in forum New To Java
    Replies: 8
    Last Post: 08-11-2010, 07:37 AM
  4. how to write except in java??
    By napi1234 in forum New To Java
    Replies: 1
    Last Post: 08-09-2010, 04:49 AM
  5. How do i write this in java...?
    By johnny7white in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 07:05 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
  •