Results 1 to 2 of 2
  1. #1
    Bigchickenmac is offline Member
    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Default Processing Snake game help

    Hi guys I'm relatively new to java and processing and I am trying to make a snake game. I can move the snake left and right but not up and down. I have posted the code below which consists of two files.Any help will be much appreciated.
    Java Code:
    //Snake 
    
    import processing.core.PApplet;
    
    public class Snake extends PApplet{
    
     Rect1 play1 = new Rect1(150,10,150,10,this);
     int keyPressed = 0;
     int sensitivity = 3;
     int directionY;
     int  directionX;
     
    	//Instantiate object with input values
    
    	
    	public void setup(){
    		size(400,400);
    		smooth();
    	     }
    	
    	public void keyPressed(){
    		if(key==CODED) {
    		if (keyCode==LEFT){
    			keyPressed = (-1*sensitivity);
    			play1.move(keyPressed);
    		}
    		else if(keyCode==RIGHT){
    			keyPressed = (1*sensitivity);
    			play1.move(keyPressed);
    		}
    		
    		
    		else if (keyCode == UP) {
    			 keyPressed();
    			 play1.move(keyPressed);
    		    println("UP");
    		  } else if (keyCode == DOWN) {
    			  play1.move(keyPressed);
    		    println("DOWN");
    		  }
    		}
    		
    		}
    	
    		
    	
    
    	//Draw and move the object
    	public void draw(){
    		this.frameRate(25);
    		//PApplet.println(ra1,x1);
    		background(150,240,200);
    	
    		play1.display();
    		
    		// Bright red
    		fill(255,200,0);
    		ellipse(20,20,16,16);
    
    	
    	}
    	
    }
    
    
    //Rect1
    
    import processing.core.PApplet;
    
    public class Rect1 {
    	
    	int x1,y1,x2,y2;
    	
        float speed;
        PApplet parent;
        
        Rect1(int startX, int startY, int endX, int endY, PApplet p){
        	parent = p;
        	x1 = startX;
        	y1 = startY;
        	x2 = endX;
        	y2 = endY;
        }
    
        void display(){
        	parent.fill(255,100,100);
        	parent.noStroke();
        	parent.rect(x1,y1,x2,y2);
        	
        }
        
        void move(int move){
        	x1 +=move;
        }
    }
    Last edited by JosAH; 04-11-2013 at 06:34 PM. Reason: added [code] ... [/code] tags

  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: Processing Snake game help

    When posting code, please use the code tags to preserve formatting.

    Time to step through this with a debugger. When does the program's execution differ from what you expect?
    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. Snake Game
    By FrankElliot9 in forum New To Java
    Replies: 5
    Last Post: 12-03-2012, 06:01 AM
  2. Snake Game
    By LuluMM in forum New To Java
    Replies: 0
    Last Post: 03-08-2012, 07:48 AM
  3. Snake game in java
    By freaky in forum New To Java
    Replies: 5
    Last Post: 04-20-2010, 07:34 PM
  4. Snake Game
    By mustachMan in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 11:35 PM
  5. Snake game movement
    By BeerMonkey in forum New To Java
    Replies: 9
    Last Post: 11-27-2008, 01:48 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
  •