[Game]Help with coding

Discussion in 'Programming General' started by ironblade87, May 8, 2008.

[Game]Help with coding
  1. Unread #1 - May 8, 2008 at 3:07 AM
  2. ironblade87
    Joined:
    Jul 19, 2007
    Posts:
    2,900
    Referrals:
    4
    Sythe Gold:
    11

    ironblade87 Grand Master
    $5 USD Donor

    [Game]Help with coding

    Ok, I'm trying to make Mario on Java. How do I make Mario jump in an arc instead of just (1,-1)? And how to I fix my intersects method so that when my Mario image touches the coin, the coin disappears/shifts to the outer edges?

    Thanks in advance.

    Code:
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.awt.image.*;
    import javax.swing.*; 	//need this for the timer to work.
    
    class myRectangle			
    {
    	int xpos=0, ypos=0, height=0, width=0;	
    	Rectangle r;
    		
    	public myRectangle (int x, int y, int w, int h) 	
    	{
    		xpos = x;
    		ypos = y;
    		width = w;
    		height = h;
    		r = new Rectangle (xpos, ypos,  width,  height);		
    	}
    	public boolean intersects(int x, int y, int w, int h) 
    	{
    		//System.out.println ("test3");
    		return r.intersects(x, y, w, h);
    	}
    	public void paint(Graphics2D g)
    	{
    		g.fill(r);
    	}
    }
    class mySky			
    {
    	int xpos, ypos, height, width;	
    	Image img;
    		
    	public mySky (Image i, int x, int y, int w, int h) 	
    	{
    		img = i;
    		xpos = x;
    		ypos = y;
    		width = w;
    		height = h;		
    	}
    	public Image getImage()
    	{
    		return img;
    	}
    	public int getX() { return xpos; }
    	public int getY() { return ypos; }
    	public int getHeight() { return height; }
    	public int getWidth() { return width; }
    	
    	public void displaymySky(Graphics2D g, Applet a)
    	{
    		g.drawImage(img, xpos, ypos, width, height, a);
    	}
    	public void paint(Graphics2D g2)
    	{
    		
    	}
    }
    class Mario			
    {
    	int xpos, ypos, height, width, speed, xdir, ydir, Amplitude = 300;
    	Image img;
    	myRectangle r;
    	public Mario (Image i, int x, int y, int w, int h, int s, int xd, int yd)
    	{
    		img = i;
    		xpos = x;
    		ypos = y;
    		width = w;
    		height = h;
    		speed = s;
    		xdir = xd;
    		ydir = yd;
    		r = new myRectangle (x,y,w,h);
    	}
    	public void move()
    	{
    		xpos = xpos + speed*xdir;
    		ypos = ypos + speed*ydir;
    	}
    	public void jump()
    	{
    		xpos = xpos + speed*xdir;
    		ypos = 300 + (int)(Amplitude * Math.abs(Math.cos(xpos/100.0)));
    	}
    	public Image getImage()
    	{
    		return img; 
    	}
    	public int getX() { return xpos; }
    	public int getY() { return ypos; }
    	public int getHeight() { return height; }
    	public int getWidth() { return width; }
    	public int getXDir() { return xdir; }
    	public int getYDir() { return ydir; }
    	public int getSpeed() { return speed; }
    
    	public void setDir(int x, int y)
    	{
    		xdir = x;
    		ydir = y;
    	}
    	public boolean intersects(int x, int y, int w, int h) 
    	{
    		//System.out.println ("test2");
    		return r.intersects(x, y, w, h);
    	}
    	public void displayMarioBrothers(Graphics2D g, Applet a)
    	{
    		g.drawImage(img, xpos, ypos, width, height, a);
    	}
    	public void paint(Graphics2D g2)
    	{
    		
    	}
    }	
    class Coin			
    {
    	int xpos, ypos, height, width;	
    	myRectangle r;
    	Image img;
    	
    	public Coin (Image i, int x, int y, int w, int h)
    	{
    		img = i;
    		xpos = x;
    		ypos = y;
    		width = w;
    		height = h;
    		r = new myRectangle (x,y,w,h);
    	}
    	public Image getImage()
    	{
    		return img; 
    	}
    	public int getX() { return xpos; }
    	public int getY() { return ypos; }
    	public int getHeight() { return height; }
    	public int getWidth() { return width; }
    	
    	public boolean intersects(int x, int y, int w, int h) 
    	{
    		return r.intersects(x, y, w, h);
    	}
    	public void displayCoin(Graphics2D g, Applet a)
    	{
    		g.drawImage(img, xpos, ypos, width, height, a);
    	}
    	public void paint(Graphics2D g2)
    	{
    		
    	}
    }	
    
    public class MarioBrosProject extends Applet implements KeyListener
    {
    	mySky Sky;
    	Mario MarioBrothers;
    	Coin coin;
    	Image i,j,k;
    	Timer t,t1;
    	boolean pressed1 = true;
    	BufferedImage imageBuffer;
    	Graphics2D  graphicsBuffer;
    	
    	public void init() 
    	{
    		imageBuffer = (BufferedImage)createImage(getWidth(), getHeight());
    		graphicsBuffer = (Graphics2D) imageBuffer.getGraphics();
    		i = getImage(getCodeBase(), "MarioBrothers.gif");
    		j = getImage(getCodeBase(), "coin.gif");
    		k = getImage(getCodeBase(), "background.png");
    		
    		MarioBrothers = new Mario(i,0,550,40,50,5,0,0);
    		coin = new Coin(j,50,550,40,50);
    		Sky = new mySky(k,0,0,1500,680);
    		
    		addKeyListener(this); 
    		setFocusable(true); 
    		
    		ActionListener s = new ActionListener()
    			{
    				public void actionPerformed(ActionEvent evt)
    				{
    					Graphics g = getGraphics();
    					MarioBrothers.move();
    
    					
    					if (MarioBrothers.intersects(coin.getX(), coin.getY(), coin.getWidth(), coin.getHeight()))
    					{
    							System.out.println("test 1");
    							coin = new Coin(j,1000,450,40,50);
    					}	
    					if (MarioBrothers.getY() > 550)
    					{
    						MarioBrothers.setDir(0,0);
    						MarioBrothers = new Mario(i,MarioBrothers.getX(),550,40,50,5,0,0);
    					}
    					if (MarioBrothers.getY() < 400 && MarioBrothers.getXDir() == 1)
    					{
    						pressed1 = false;
    						MarioBrothers.setDir(1,1);
    					}
    					if (MarioBrothers.getY() < 400 && MarioBrothers.getXDir() == -1)
    					{
    						pressed1 = false;
    						MarioBrothers.setDir(-1,1);
    					}
    					if (MarioBrothers.getY() < 400)
    					{
    						pressed1 = false;
    						MarioBrothers.setDir(0,1);
    					}		
    					if (MarioBrothers.getY() > 548)
    					{
    						pressed1 = true;
    					}		
    					paint(g);   //not repaint()!!!!
    				}
    			};			
    			t=new Timer(10, s);
    			
    		ActionListener s1 = new ActionListener()
    			{
    				public void actionPerformed(ActionEvent evt)
    				{
    					Graphics g = getGraphics();
    					MarioBrothers.jump();
    					paint(g);
    				}
    			};
    			t1=new Timer(10,s);
    	}
    
    	public void keyPressed(KeyEvent e)	
    	{
    		processKeyPress(e);
    //		if (e.getKeyCode() == e.VK_SPACE)
    //		{
    //			t.start();
    //		}
    //		if (e.getKeyCode() == e.VK_UP)
    //		{
    //			MarioBrothers.setDir(0,-1);
    //		}
    //		if (e.getKeyCode() == e.VK_DOWN)
    //		{
    //			MarioBrothers.setDir(0,1);
    //		}
    //		if (e.getKeyCode() == e.VK_RIGHT)
    //		{
    //			MarioBrothers.setDir(1,0);
    //		}
    //		if (e.getKeyCode() == e.VK_LEFT)
    //		{
    //			MarioBrothers.setDir(-1,0);
    //		}
    //		if (e.getKeyCode() == e.VK_M)
    //		{
    //			t1.start();
    //		}
    	}   
    	
     	public void keyReleased(KeyEvent e)	
    	{
    		processKeyRelease(e);
    //		if (e.getKeyCode() == e.VK_UP)
    //		{
    //			MarioBrothers.setDir(0,2);  
    //		}
    //		if (e.getKeyCode() == e.VK_DOWN)
    //		{
    //			MarioBrothers.setDir(0,2);
    //		}
    //		if (e.getKeyCode() == e.VK_RIGHT)
    //		{
    //			MarioBrothers.setDir(0,2);
    //		}
    //		if (e.getKeyCode() == e.VK_LEFT)
    //		{
    //			MarioBrothers.setDir(0,2);
    //		}
    //		if (e.getKeyCode() == e.VK_M)
    //		{
    //			t1.stop();
    //		}
    	}	
    	
    	public boolean upKeyPressed = false;
    	public boolean downKeyPressed = false;
    	public boolean rightKeyPressed = false;
    	public boolean leftKeyPressed = false;
    	
    	public void processKeyPress(KeyEvent e)
    	{
    		int keyCode = e.getKeyCode();
    		
    		// record the key press in a Boolean
    		if (e.getKeyCode() == e.VK_SPACE)
    		{
    			t.start();
    		}
    		else if (keyCode == KeyEvent.VK_UP && pressed1)
    		{
    			upKeyPressed = true;
    			pressed1 = false;
    			MarioBrothers.setDir(0,-1);
    		}
    		else if (keyCode == KeyEvent.VK_DOWN)
    		{
    			downKeyPressed = true;
    			MarioBrothers.setDir(0,1);
    		}
    		else if (keyCode == KeyEvent.VK_RIGHT)
    		{
    			rightKeyPressed = true;
    			MarioBrothers.setDir(1,0);
    		}
    		else if (keyCode == KeyEvent.VK_LEFT)
    		{
    			leftKeyPressed = true;
    			MarioBrothers.setDir(-1,0);
    		}
    		else if (keyCode == KeyEvent.VK_M)
    		{
    			t1.start();
    		}
    //		if(keyCode == KeyEvent.VK_LEFT)
    //			leftKeyPressed = true;
    //		else if (keyCode == KeyEvent.VK_RIGHT)
    //			rightKeyPressed = true;
    //		else if (keyCode == KeyEvent.VK_UP)
    //			upKeyPressed = true;
    		
    		if (leftKeyPressed && upKeyPressed)
    		{
    			MarioBrothers.setDir(-1,MarioBrothers.getYDir());
    			pressed1 = false;
    		}
    		else if (rightKeyPressed && upKeyPressed)
    		{
    			MarioBrothers.setDir(1,MarioBrothers.getYDir());
    			pressed1 = false;
    		}
    		else if (leftKeyPressed && downKeyPressed)
    		{
    			MarioBrothers.setDir(-1,MarioBrothers.getYDir());
    			pressed1 = false;
    		}
    		else if (rightKeyPressed && downKeyPressed)
    		{
    			MarioBrothers.setDir(1,MarioBrothers.getYDir());
    			pressed1 = false;
    		}
    		
    		// use the combined key presses
    ////		if (leftKeyPressed && upKeyPressed)
    			// do a combined left and up action
    ////		else if (rightKeyPressed && upKeyPressed)
    			// do a combined right and up action
    	}
    	
    	public void processKeyRelease(KeyEvent e)
    	{
    		int keyCode = e.getKeyCode();
    		
    		// record the key release in a Boolean
    		if (keyCode == KeyEvent.VK_UP)
    		{
    			upKeyPressed = false;
    			pressed1 = false;
    			MarioBrothers.setDir(0,2);  
    		}
    		if (keyCode == KeyEvent.VK_DOWN)
    		{
    			downKeyPressed = false;
    			pressed1 = false;
    			MarioBrothers.setDir(0,2);
    		}
    		if (keyCode == KeyEvent.VK_RIGHT)
    		{
    			rightKeyPressed = false;
    			pressed1 = false;
    			MarioBrothers.setDir(0,2);
    		}
    		if (keyCode == KeyEvent.VK_LEFT)
    		{
    			leftKeyPressed = false;
    			pressed1 = false;
    			MarioBrothers.setDir(0,2);
    		}
    //		if (keyCode == KeyEvent.VK_LEFT)
    //			leftKeyPressed = false;
    //		else if (keyCode == KeyEvent.VK_RIGHT)
    //			rightKeyPressed = false;
    //		else if (keyCode == KeyEvent.VK_UP)
    //			upKeyPressed = false;
    	}
    	
    	public void keyTyped(KeyEvent e) {}	 //not needed but has to be here
    	
    	public void paint(Graphics canvas) 
    	{
    		Graphics2D g2 = (Graphics2D) canvas;
    		
    		graphicsBuffer.setColor(Color.cyan);
    		graphicsBuffer.fillRect(0, 0, getWidth(), getHeight());		
    		Sky.displaymySky(graphicsBuffer,this);
    		MarioBrothers.displayMarioBrothers(graphicsBuffer,this);
    		coin.displayCoin(graphicsBuffer,this);
    			
    		g2.drawImage(imageBuffer, 0,0, getWidth(), getHeight(), this);  //must be the last line!!!
    	}
    }
     
  3. Unread #2 - May 8, 2008 at 9:57 AM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    [Game]Help with coding

    I'm thinking maybe add listeners to check if more than one directional key is pressed at the same time, example: if UP and RIGHT are pressed simultaneously. If that's the case have it do a series of direction changes quickly; (0, 1) [up] , (1,0)
    , (0, -1) [down] or you can make some of the steps bigger than 1 unit and play around to see which combinations make for the biggest illusion that it is an arced jump.

    Add image position parameters to your coin class' display function. This makes it so that when the paint() function displays the coin, it will have changed position if you've updated the position coordinates of the coin. Similarly you could do the same with the size elements of the coin although I'm not sure if that would be useful.
    Code:
    public void displayCoin(Graphics2D g, Applet a, xpos, ypos)
    Sorry, if this wasn't too helpful but I don't really program in Java so I just do what I can to help.​
     
  5. Unread #3 - May 8, 2008 at 7:21 PM
  6. ironblade87
    Joined:
    Jul 19, 2007
    Posts:
    2,900
    Referrals:
    4
    Sythe Gold:
    11

    ironblade87 Grand Master
    $5 USD Donor

    [Game]Help with coding

    Thanks Nullware.

    Updated code. However, the coin only disappears when I stop moving on it. If I keep moving, the coin won't go anywhere.
     
< Ideas! (I need some) | Help with games. >

Users viewing this thread
1 guest


 
 
Adblock breaks this site