Results 1 to 8 of 8
  1. #1
    QRS5 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Avoider - a game project!

    Hey!

    My name is Fred and right now I'm studying at a secondary school.
    I've been programming for a few days now and trying to make a very simple game.

    So what the game will do is:
    • Having a object which is the player
    • Having objects that are enemies, they're just going down and up the screen
    • Having a score table where you see your score


    It would be very nice if you guys could check my code and reply with perhaps smarter changes, what I could implement instead etc.
    Thanks! (Also I don't really know how I should implement the score table - any ideas?)
    /Fred

    Java Code:
    //Spel
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.*;
    import java.applet.AudioClip;   //ljud
    
    public class Spel extends Applet implements KeyListener,Runnable
    {
      // Deklarationer
      Label ledtext;
      Image player, enemy,enemy2,enemy3,enemy4,enemy5, bg;
      int x,y,x2,x3,x4,x5,x6,y2,y3,y4,y5,y6,xb,xeb,yh,yeh,position,position2;
      int random, score,totalt;
      AudioClip ljud1,ljud2,ljud3;   //ljud
      
      boolean kor;
      Thread trad;
      private Image dbImage; 
      private Graphics dbg; 
      
    
      public void init()
      { 
        
        player = this.getImage(getCodeBase(),"player.png");
        enemy = this.getImage(getCodeBase(),"enemy.png");
        enemy2 = this.getImage(getCodeBase(),"enemy.png");
        enemy3 = this.getImage(getCodeBase(),"enemy.png");
        enemy4 = this.getImage(getCodeBase(),"enemy.png");
        enemy5 = this.getImage(getCodeBase(),"enemy.png");
        bg = this.getImage(getCodeBase(),"bg.png");
        
        ljud1 = this.getAudioClip(getDocumentBase(), "score2.wav");   //ljud
        ljud2 = this.getAudioClip(getDocumentBase(), "swosh.wav");   //ljud
        ljud3 = this.getAudioClip(getDocumentBase(), "Heist.wav");   //ljud
        
        this.addKeyListener(this);
        
        score = 0;
        
        x = 180; 
        y = 100;
        x2 = 180;
        x3 = 121;
        x4 = 62;
        x5 = 239;
        x6 = 298;
        y2 = 600;
        y3 = 600;
        y4 = 600;
        y5 = 600;
        y6 = 600;
        
        this.ljud3.loop();
        
      }
      
      public void start() {
            if (trad == null) {
                trad = new Thread(this);
                trad.start();
            }
            
        }
        
        
        public void run() {
            
           while(kor = true){
                
               
               xb = player.getWidth(this);
               
               xeb = enemy.getWidth(this);
         
               yh = player.getHeight(this);
               
               yeh = enemy.getHeight(this);
               
               System.out.println("! = " +  score );
               
               y2 = y2 - (int)(4*Math.random()+1);
               y3 = y3 - (int)(8*Math.random()+1);
               y4 = y4 - (int)(6*Math.random()+1);
               y5 = y5 - (int)(2*Math.random()+1);
               y6 = y6 - (int)(3*Math.random()+1);
               
               
               if (score == 200){
                   
                   bg = this.getImage(getCodeBase(),"bg2.png");
                   
                }
    
            
            if (y2 <= 63){
                
                y2 = 600;
                score = score + 10;
                
                
                
               }
               
            if (y3 <= 63){
                
                y3 = 600;
                score = score + 10;
                
                
               }
               
            if (y4 <= 63){
                
                y4 = 600;
                score = score + 10;
                
                
               }
               
            if (y5 <= 63){
                
                y5 = 600;
                score = score + 10;
                
                
               }
               
            if (y6 <= 63){
                
                y6 = 600;
                score = score + 10;
                
                
               }
               
            if ( x == 0 ){
            
                x = 190;
            
                }
                
            if ( x +xb > x2 && x < x2+ xeb && y + yh > y2 && y < y2 + yeh){
               
               y2 = 600; 
               JOptionPane.showMessageDialog(null, "Du förlora! \nDin poäng [" + score + "]"); 
               score = 0;
               x = 180;
               y = 100;
               bg = this.getImage(getCodeBase(),"bg.png");
               
            }
            
            if ( x +xb > x3 && x < x3+ xeb && y + yh > y3 && y < y3 + yeh){
               
               y3 = 600; 
               JOptionPane.showMessageDialog(null, "Du förlora! \nDin poäng [" + score + "]"); 
               score = 0;
               x = 180;
               y = 100;
               bg = this.getImage(getCodeBase(),"bg.png");
                
            }
            
            if ( x +xb > x4 && x < x4+ xeb && y + yh > y4 && y < y4 + yeh){
               
               y4 = 600; 
               JOptionPane.showMessageDialog(null, "Du förlora! \nDin poäng [" + score + "]"); 
               score = 0;
               x = 180;
               y = 100;
               bg = this.getImage(getCodeBase(),"bg.png");
                
            }
            
            if ( x +xb > x5 && x < x5+ xeb && y + yh > y5 && y < y5 + yeh){
               
               y5 = 600; 
               JOptionPane.showMessageDialog(null, "Du förlora! \nDin poäng [" + score + "]"); 
               score= 0;
               x = 180;
               y = 100;
               bg = this.getImage(getCodeBase(),"bg.png");
                
            }
            
            if ( x +xb > x6 && x < x6+ xeb && y + yh > y6 && y < y6 + yeh){
                   
                y6 = 600; 
                JOptionPane.showMessageDialog(null, "Du förlora! \nDin poäng [" + score + "]"); 
                score = 0;
                x = 180;
                y = 100;
                bg = this.getImage(getCodeBase(),"bg.png");
               
            }
            
            
               
            repaint();
            try { trad.sleep(10); }
            catch (InterruptedException e) { }
    }
        }
        
      public void paint(Graphics g)
      { 
        this.requestFocus();    
        g.drawImage(bg,0,0,this);
        g.drawImage(player,x,y,this);
        g.drawImage(enemy,x2,y2,this);
        g.drawImage(enemy,x3,y3,this);
        g.drawImage(enemy,x4,y4,this);
        g.drawImage(enemy,x5,y5,this);
        g.drawImage(enemy,x6,y6,this);
        
        
    }
      
      public void update (Graphics g) 
    {
    // initialize buffer 
        if (dbImage == null) 
            {
                dbImage = createImage (this.getSize().width, this.getSize().height); 
                dbg = dbImage.getGraphics (); 
        } 
    
            // clear screen in background 
            dbg.setColor (getBackground ()); 
            dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); 
    
            // draw elements in background 
            dbg.setColor (getForeground()); 
            paint (dbg); 
    
            // draw image on the screen 
            g.drawImage (dbImage, 0, 0, this); 
        } 
        
      public void keyPressed(KeyEvent e)
      {
        if (e.getKeyCode() == 37){
            
            if ( x == 62 ){
            
                x = x + 0;
                
            }else
         
            x = x - 59;
            this.ljud2.play();
        
        }
        if (e.getKeyCode() == 38) y = y - 20 ;
        if (e.getKeyCode() == 39){
            
            if ( x == 298 ){
            
                x = x + 0;
                
            }else
            
            x = x + 59;
            this.ljud2.play();
        }
        if (e.getKeyCode() == 40) y = y + 20 ;
        this.repaint();
      }
        
      public void keyReleased(KeyEvent e){}
      public void keyTyped(KeyEvent e){}
    
      
      }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default Re: Avoider - a game project!

    Why are you loading your background image so much?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    QRS5 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Avoider - a game project!

    The background is loaded one time in the init()function and then it changes when the score is 200, that is just one time or?
    /Fred

  4. #4
    QRS5 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Avoider - a game project!

    Oh I get it, you mean there.
    It's because if you reach above 200 points the bg will be bg2 and if you loose it will reset to default bg.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default Re: Avoider - a game project!

    Quote Originally Posted by QRS5 View Post
    The background is loaded one time in the init()function and then it changes when the score is 200, that is just one time or?
    /Fred
    I don't know, I'm counting six times where you load an image. What I'm suggesting you do is do all the loading at init time. Save the images to variables, and then use those variables instead of loading images every time you want to change it.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    QRS5 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Avoider - a game project!

    Wont that be the same thing?
    The variable will still be loaded or?

    /Fred

  7. #7
    QRS5 is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Avoider - a game project!

    Could you give me an example on how I should do it?
    Because I'm not sure.

    /Fred

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,013
    Rep Power
    10

    Default Re: Avoider - a game project!

    If it works, then it's not a big deal. Don't worry about making everything perfect. Does it work?

    But think about a more complicated game, with dozens or hundreds (thousands?) of images. You don't want to load them every time you need them, because file IO is a performance bottleneck. Instead, you load all of them at the beginning and use them as you need them. That way you don't tie up the EDT doing file IO.

    But like I said, if it works, stop worrying about it and move on to the next thing.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Tic-Tac-Toe Game - New Project
    By tabchas in forum New To Java
    Replies: 20
    Last Post: 08-09-2011, 01:49 AM
  2. Replies: 1
    Last Post: 06-02-2011, 10:29 AM
  3. New Game Project
    By haxic in forum Java Gaming
    Replies: 1
    Last Post: 06-02-2011, 05:39 AM
  4. New Game Project
    By haxic in forum Java Gaming
    Replies: 2
    Last Post: 06-02-2011, 03:27 AM

Posting Permissions

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