Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Trying to make a snake game

810754Dec 14 2010 — edited Dec 18 2010
Hi, I'm trying to turn this into a snake game like the one on nokia except a little bit different. I want it to start from a position (user is asked a co-ordinate to choose from), have like small sports as obstacles (about 20?) Then every time it touches one of the obstacles they disappear and the snake changes direction until all obstacles are touched. I don't want any controls, just the snake to move randomly. I've done some of the coding but am really new and would appreciate if anyone would help out so I can gain more experience with java.

Here are the java class files:
http://rapidshare.com/files/436967518/MyDraw.class
http://rapidshare.com/files/436967564/Draw.class

Below is the code:
{/*************************************************************************
 *  Compilation:  javac Spot0.java
 *  Dependencies: Draw.class MyDraw.class
 *************************************************************************/
import java.awt.Color;
public class Spot0 {
   
  public static void main(String args[]) {
    //define size of canvas 
    final int canvassize = 600;
            
    //number of overall steps and time to wait in game
    final int overallsteps = 2500;
    final int timeToWait = 5; 
    
    // startposition and size of the spot
    final int spotsize = 50; 
    int x = 200;
    int y = 300;
    
    //change in horizontal and vertical direction
    int dx = 2;
    int dy = 3;
        
 
   
    MyDraw.create(canvassize, canvassize);
    drawpoint(x,y,spotsize,Color.blue,timeToWait);
   
    //move spot here
    for (int i = 0; i < overallsteps; i++) {
      x = x + dx;
      y = y + dy;
      drawpoint(x,y,spotsize,Color.red,timeToWait);
      if (y <= 0 || y >= canvassize) {
        dy = -dy;
      }
      if (x <= 0 || x >= canvassize) {
        dx = -dx;
      }
    }
      
    
    MyDraw.show();
    System.exit(0);
  } 
 
  
  
  //draw a spot with coordinates (a,b) with given size and color, and wait
  private static void drawpoint(double a,double b,int size,Color color, int wait){
    MyDraw.setColor(color);
    MyDraw.go(a,b);
    MyDraw.spot(size);
    MyDraw.pause(wait);
  }
  
}
Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 15 2011
Added on Dec 14 2010
19 comments
512 views