aznnytezx 0 Newbie Poster
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class snake1
{
	public static void main(String args[])
	{
		String name1= JOptionPane.showInputDialog("Enter Player one's name");
		Windows myW=new Windows(name1);
		
		myW.setSize(800,600);
		myW.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent event)
			{
				System.exit(0);
			}
		});
		myW.show();
		while(true)
		{
			myW.repaint();
			try{Thread.sleep(60/myW.getLevel());}catch(Exception e){}
		}
	}
}
class Windows extends Frame implements KeyListener
{
	private int x,y,xold,yold;
	private boolean go,move,firsttime,goTitle,play,selectLevel;
	private int level = 0;
	private int AppleX, AppleY;
	private boolean left,right,up,down;
	private boolean hitWall;
	private String nam1
	private int growth;

	public Windows(String n1)
	{
		nam1=n1;
		play=false;
		x=265;
		y=265;
		xold=x;
		yold=y;
		firsttime =true;
		go =false;
		move =false;
		goTitle=true;
		addKeyListener(this);
		selectLevel=false;
		level=0;
		
		left=false;
		right=true;
		up=false;
		down=false;
		size=10;
		hitWall=false;
		start=false;
		growth = 1;
	 
	}
	
	public int getLevel()
	{
		return level;
	}
	public void update(Graphics g)
	{
		paint(g);
	}
	public void paint(Graphics g)
	{
		if(goTitle)
			title(g);
		if(firsttime){
			selectLevel=true;
			startgame(g);
			movebox(g);
			firsttime=false;
		}
		if(go){
			selectLevel=true;
			snakeGame(g);
		}
		if(play)
		{
			snakeGame(g);
			selectLevel=false;
		}
		
			 
			
			
		g.setFont(new Font("Courier",Font.BOLD,30));
		g.setColor(Color.yellow);
		g.drawString("X = ",490,100); // display x,y on screen
		String xnum=Integer.toString(x);
		g.drawString(xnum,550,100);
		g.drawString("Y = ",640,100);
		String ynum=Integer.toString(y);
		g.drawString(ynum,700,100);
		if(move)
			movebox(g);
	}


public void title(Graphics g)
	{

		g.setColor(Color.red);
		g.fillOval(100,100,600,450);
		g.fillRect(390,30,20,80);
		g.setColor(Color.black);
		g.fillRect(0,0,800,600);
		g.setColor(Color.red);
		g.setFont(new Font("Courier",Font.ITALIC,36));
		g.drawString("Snake Game", 300,200);
		g.setColor(Color.white);
		try{Thread.sleep(75);}catch(Exception e){}
		g.drawOval(410,250,6,6);
		try{Thread.sleep(75);}catch(Exception e){}
		g.drawOval(404,250,6,6);
		try{Thread.sleep(75);}catch(Exception e){}
		try{Thread.sleep(75);}catch(Exception e){}
		g.drawOval(400,240,20,20);
		try{Thread.sleep(75);}catch(Exception e){}
		g.drawOval(400,220,20,20);
		try{Thread.sleep(75);}catch(Exception e){}
		g.drawOval(320,200,20,20);
		try{Thread.sleep(75);}catch(Exception e){}
		g.drawOval(340,200,20,20);
		try{Thread.sleep(75);}catch(Exception e){}
		g.drawOval(360,200,20,20);
		try{Thread.sleep(75);}catch(Exception e){}
		g.drawOval(380,200,20,20);
		try{Thread.sleep(75);}catch(Exception e){}
		g.drawOval(400,200,20,20);
		try{Thread.sleep(400);}catch(Exception e){}
		for(int k=0; k<4; k++)
		{
			g.setColor(Color.green);
			g.setFont(new Font("Courier",Font.ITALIC,36));
			g.drawString("How you like them APPLES!", 140,400);
			try{Thread.sleep(400);}catch(Exception e){}
			g.setColor(Color.red);
			g.setFont(new Font("Courier",Font.ITALIC,36));
			g.drawString("How you like them APPLES!", 140,400);
			try{Thread.sleep(400);}catch(Exception e){}
		}
		goTitle=false;
		firsttime=true;
	}
	public void startgame(Graphics g)
	{
		g.setColor(Color.black);
		g.fillRect(0,0,800,600);
		g.setColor(Color.white);
		g.setFont(new Font("Courier",Font.ITALIC,30));
		g.drawString("Snake Game", 300,75);
		firsttime=false;
		g.setColor(Color.yellow);
		g.drawString("Easy",250,265);
		g.setColor(Color.yellow);
		g.drawString("Normal",250,365);
		g.setColor(Color.yellow);
		g.drawString("Hard",250,465);
	}
	
	public void keyPressed(KeyEvent event)
	{
		if(selectLevel)
		{
			xold=x;
			yold=y;
			switch (event.getKeyCode())
			{
				case KeyEvent.VK_UP:
					if(y<=265)
						y=265;
					else
						y-=100;
					move=true;
				break;
		 
				case KeyEvent.VK_DOWN:
					if(y>=465)
						y=465;
					else
						y+=100;
					move=true;
				break;
				
				case KeyEvent.VK_ENTER: 
					if(y==465) //HARD
						level=3;
					else if (y == 365) //MEDIUM
						level=2;
					else // EASY
						level=1;
						
					if(x==xold && y==yold)
					{
						play=true;
					}
					break;
			}
		}
		else
		if(play)
			{
			if (event.getKeyCode() == KeyEvent.VK_DOWN)
			{
			down=true;
			up=false;
			left=false;
			right=false;
			move=false;
			selectLevel=false;
			}
	
			if (event.getKeyCode() == KeyEvent.VK_UP)
			{
			up=true;
			down=false;
			left=false;
			right=false;
			move=false;
			selectLevel=false;
			}
	
			if (event.getKeyCode() == KeyEvent.VK_LEFT)
			{
			left=true;
			right=false;
			up=false;
			down=false;
			move=false;
			selectLevel=false;
			}
	
			if (event.getKeyCode() == KeyEvent.VK_RIGHT)
			{
			right=true;
			left=false;
			up=false;
			down=false;
			move=false;
			selectLevel=false;
			}
	
			if (event.getKeyCode() == KeyEvent.VK_ENTER)
				play=true;

		}
	}
	public void keyReleased(KeyEvent event)
	{
	}
 
	public void keyTyped(KeyEvent event)
	{
	}
	
	public void movebox(Graphics screen)
	{
		screen.setColor(Color.black); //blue rectangle to clear
		screen.fillRect(490,80,260,30);
		screen.setFont(new Font("Courier",Font.BOLD,30));
		screen.setColor(Color.yellow);
		screen.drawString("X = ",490,100); // display x,y on screen
		String xnum=Integer.toString(x);
		screen.drawString(xnum,550,100);
		screen.drawString("Y = ",640,100);
		String ynum=Integer.toString(y);
		screen.drawString(ynum,700,100);
		if(x!=xold || y!=yold)
			{ 
				screen.setColor(Color.black);
				screen.fillRect(xold-10,yold,xold+110,yold);
				screen.setFont(new Font("Courier",Font.ITALIC,30));	
				screen.setColor(Color.yellow);
				screen.drawString("Easy",250,265);
				screen.setColor(Color.yellow);
				screen.drawString("Normal",250,365);
				screen.setColor(Color.yellow);
				screen.drawString("Hard",250,465);	
				screen.setColor(Color.red);
				screen.drawLine(x-10,y,x+110,y);
			}
		xold=x;
		yold=y;
		move=false;
		selectLevel=true;
	}
	
	public void snakeGame(Graphics g)
	{
		
		g.setColor(Color.gray);
		g.fillRect(0,0,800,600);
		g.setColor(Color.white);
		g.setFont(new Font("Courier",Font.ITALIC,30));
		if(play)
		{
			g.fillRect(x,y,10,10);
			if (x==5)
			{
				left=false;
				hitWall=true;
			}
			if(x==785)
			{
				right=false;
				hitWall=true;
			}
			if(y==25)
			{
				up=false;
				hitWall=true;
			}
			if(y==585)
			{
				down=false;
				hitWall=true;
			}
			
			if(left)
				x-=size;
			if(right)
				x+=size;
			
			if (up)
				y-=size;
			if(down)
				y+=size;
		}
		selectLevel=false;	
	}	
}

I really need help with the apples and making him eating the apples, if you can, can you please give me the code and explain it to me??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.