Partage
  • Partager sur Facebook
  • Partager sur Twitter

Mario bros

    24 mai 2011 à 19:34:00

    Bonjour a tous,
    je cree en ce moment le jeux maroio bros et j'ai un petit probleme, quand je saute sur une boite ou n'importe quoi mario s'arrete 20 pixel au dessus de la boite et apres 1-2 seconde descend sur la boite. desole pour les accents et tous, mon clavier est en qwerty et je previens d'avance mon code est en anglais ce sera peut etre un peu confus desole d'avance. :p
    Voici mes 3 classes:

    Citation

    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    
    
    public class Background {
    	private static final int GRIDSIZEY = 10;
    	private static final int GRIDSIZEX = 300;
    	Piece[][] back=new Piece[10][300];
    	int xLoc=0;
    	AllClouds clouds=new AllClouds();
    	public Background()
    	{
    		for (int i=0;i<10;i++)
    			for (int j=0;j<300;j++)
    				back[i][j]=new Sky();
    		for(int i=0;i<300;i++){
    		back[9][i]=new Floor();
    		}
    		back[5][19]=new Box();
    		back[5][21]=new Box();
    		back[5][20]=new SpecialBox();
    		back[6][30]=new Box();
    		back[7][30]=new Box();
    		back[8][30]=new Box();
    		back[6][0]=new Box();
    		back[7][0]=new Box();
    		back[8][0]=new Box();
    		
    		
    		
    		//randomBackground();
    		
    		
    	}
    	
    	public void randomBackground()
    	{
    		for (int i=0;i<10;i++)
    			for (int j=0;j<300;j++)
    				back[i][j]=new Sky();
    		for (int j=0;j<300;j++)
    		{
    			int row=(int)(Math.random()*10);
    			int col=(int)(Math.random()*300);
    			back[row][col]=new Floor();
    		}
    	}
    	
    	public void move()
    	{
    		xLoc++;
    	}
    	public void moveBack()
    	{
    		if (xLoc>0)
    			xLoc--;
    	}
    	public void draw(Applet myApplet,Graphics g)
    	{
    		g.setColor(new Color (63,191,255));
    		g.fillRect(0,0,900,400);
    		clouds.draw(myApplet, g,xLoc*40);
    		
    		for (int i=0;i<10;i++)
    			for (int j=xLoc;j<xLoc+23;j++)
    			{
    				int y=j-xLoc;
    				if (!(back[i][j] instanceof Sky))
    					back[i][j].draw(myApplet,g,y*40,i*40);
    			}	
    		
    	}
    
    	public boolean isOpen(int xLocation, int yLocation) {
    		
    		 int gridX=xLocation/40+xLoc;
    		 int gridY=yLocation/40;
    		 int gridX2=(xLocation+25)/40+xLoc;
    		 int gridY2=(yLocation+42)/40;
    		 
    		 
    		 
    		if (gridX<GRIDSIZEX && gridY<GRIDSIZEY && !(back[gridY][gridX] instanceof Sky))
    			 return false;
    		else if (gridX2<GRIDSIZEX && gridY2<GRIDSIZEY && !(back[gridY2][gridX2] instanceof Sky))
    			 return false;
    		else if (gridX<GRIDSIZEX && gridY2<GRIDSIZEY && !(back[gridY2][gridX] instanceof Sky))
    			 return false;
    		else if (gridX2<GRIDSIZEX && gridY<GRIDSIZEY && !(back[gridY][gridX2] instanceof Sky))
    			 return false;
    		else
    		     return true;
    	}
    	
    }
    


    // This class is to test animation
    // Designed by Jeff Borland
    // Date 12:10:10
    import java.awt.*; 
    
    import javax.swing.*;
    import java.awt.event.*;
    
    
    
     
    public class Main extends JApplet implements  Runnable, KeyListener 
    {
    	Background background=new Background();
        MarioChar mario=new MarioChar();
        Graphics bufferGraphics;
    
        Image offscreen; 
    	
    
    
    
    	  
        public void init()
        {
        	addKeyListener(this);
        	offscreen = createImage(900,400);
        	bufferGraphics = offscreen.getGraphics(); 
        }
     
        public void paint (Graphics g)
        {
        	g.drawImage(offscreen,0,0,this); 
            super.paint(g);
            
            
        }
     
        //update gets called automatically for each frame of the animation
        //this will then get called ever 50 milliseconds (the delay [below])
        public void update()
        {
            Graphics g=getGraphics();
            mario.move(background);
            background.draw(this,bufferGraphics);
            mario.draw(this,bufferGraphics);
            g.drawImage(offscreen,0,0,this); 
        }  
        public void keyPressed(KeyEvent e) { 
            char theChar=e.getKeyChar();
            int theCode=e.getKeyCode();
            if (theCode==KeyEvent.VK_UP)           
            {
            	if (background.isOpen(mario.XLocation, mario.YLocation-1))
            	{
                mario.setJump(background);
            	}
            }
            if (theCode==KeyEvent.VK_RIGHT)           
            {
            	mario.setMoveDirection("right");
    
                
            }
            if (theCode==KeyEvent.VK_LEFT)           
            {
            	mario.setMoveDirection("left");
            	
            }
            if (theCode==KeyEvent.VK_DOWN)           
            {
            	if (background.isOpen(mario.XLocation, mario.YLocation+1))
            	{
                mario.bend();
            	}
            }
    
            
    
        }
        
        public void keyReleased(KeyEvent e) { 
        	 char theChar=e.getKeyChar();
             int theCode=e.getKeyCode();
        	if (theCode==KeyEvent.VK_LEFT || theCode==KeyEvent.VK_RIGHT)
        	mario.setMoveDirection("");
        }
        
        public void keyTyped(KeyEvent e) { 
        }  
    
     
         
         
        /*********************************************************************************************/
        /* BELOW IS FOR ANIMATION.  THE ONLY THING THAT YOU NEED TO CHANGE IS DELAY */
     
        int frame;
        int delay=75;   // this is the time of the delay in milliseconds.
        Thread animator;
     
        /**
         * This method is called when the applet becomes visible on
         * the screen. Create a thread and start it.
         */
        public void start()
        {
            animator = new Thread(this);
            animator.start();
        }
     
        /**
         * This method is called by the thread that was created in
         * the start method. It does the main animation.
         */
        public void run()
        {
            // Remember the starting time
            long tm = System.currentTimeMillis();
            while (Thread.currentThread() == animator)
            {
                // Display the next frame of animation.
                update();
                try
                {
                    tm += delay;
                    Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));
                }
                catch (InterruptedException e)
                {
                    break;
                }
                // Advance the frame
                frame++;
            }
        }
     
        /**
         * This method is called when the applet is no longer
         * visible. Set the animator variable to null so that the
         * thread will exit before displaying the next frame.
         */
        public void stop()
        {
            animator = null;
        }
    }
    


    import java.applet.Applet;
    
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.ImageObserver;
    
    import javax.swing.JApplet;
    
    
    public class MarioChar {
    	String moveDirection="";
    	int jump=0;
    	int JUMPLENGTH=20; //how many frames he jumps for
        
    	int XLocation=300;
        int YLocation=313;
    	Image mario;
    	
    	public void setMoveDirection(String moveDirection) {
    		this.moveDirection = moveDirection;
    	}
    	
    	public void setJump(Background background)
    	{
    		
    		if (jump==0)
    			if (!background.isOpen(XLocation, YLocation+10))
    				jump=JUMPLENGTH;
    	
    	}
    	
       
    /*    public static void wait (double n){
            long t0,t1;
            t0=System.currentTimeMillis();
            do{
                t1=System.currentTimeMillis();
            }
            while (t1-t0<1000.0);
    }
    */
    
        public void cantMove(){
         XLocation+=0;
        }
    	public void run(){
          XLocation+=10;
    
    	}
    	
    	public void goBackward(){
          XLocation-=10;
    
    	}
    	public void bend(){
    		YLocation++;
    
    	}
    
    
    	public MarioChar()
    	{
    
    
    
    	}
    	
    	public void move(Background background)
    	{
    		
    		if (moveDirection.equals("right"))
    		{
    			if (background.isOpen(XLocation+12, YLocation))
    			{
    				if (isInMiddle())
    					background.move();
    				else
    					run();
    			}
    		}
    		if (moveDirection.equals("left"))
    		{
    			if (background.isOpen(XLocation-1, YLocation))
    			{
    				if(isOnLeftSide() && background.xLoc>0)
    					background.moveBack();
    				else
    					goBackward();
    			}
    		}
    
    		if (jump==0)
    			if (background.isOpen(XLocation, YLocation+5))
    				YLocation+=20;
    		
    		if (jump>0)
    		{
    			if (jump>JUMPLENGTH/2){
    				if (background.isOpen(XLocation, YLocation-35))
    					YLocation-=10;
    			}        	
    			else 
    				if (background.isOpen(XLocation, YLocation+35))
    					YLocation+=10;        		
    			jump--;
    		}
    		
    		
    
    		
    	}
    	
    	public void draw(Applet myApplet,Graphics g) {
    		Image mario = myApplet.getImage(myApplet.getCodeBase(), "Mario2.GIF");
    		g.drawImage(mario,XLocation,YLocation,myApplet); 
    
    
    
    	}
    
    	public boolean isInMiddle() {
    		if (XLocation>=450)
    			return true;
    		else
    			return false;
    	}
    	public boolean isOnLeftSide(){
    		if(XLocation<=430)
    			return true;
    		else
    			return false;
    		
    	}
    
    }
    
    • Partager sur Facebook
    • Partager sur Twitter

    Mario bros

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown