Trying to make a snake game

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.

807751 wrote:
Well thats all I could really do. Don't know any more about java. Could someone show me how to do it so then I could look at it and try something different myself? And if possible put comments in so I know what's going on? Thanks.Sorry mate, it doesn't really work like that here. Not a code factory.
Is this part of a course you're taking? Do you have a peer or teacher that could help you out?
Otherwise, what I'd recommend is writing down what you're trying to do in English for each individual problem, then try and translate that into Java. When you hit a problem with a particular problem in Java, we can then nudge you in the right direction...
EDIT: The other thing is, if you're really new, you might want to start with something a bit simpler. Like: http://download.oracle.com/javase/tutorial/
Edited by: meacod on Dec 15, 2010 4:12 AM

Similar Messages

  • I'm trying to make an android game and I want that when a collision with another object change of sc

    I'm trying to make an android game and I want that when a collision with another object change of scene
    how i can do this

    here is the doc on htiTestObject for detecting collisions.
    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayOb ject.html#hitTestObject()
    for scene change use the second parameter in gotoandplay() to define scene name doc below
    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/MovieClip .html#gotoAndPlay()

  • HT204053 I'm Trying to make an in-game purchase and have been prompted to contact iTunes support to complete this transaction.

    I'm Trying to make an in-game purchase and have been prompted to contact iTunes support to complete this transaction.

    You need to contact support then.
    http://www.apple.com/support/itunes/purchases/

  • Im trying to make a in game purchase but it keeps telling me to check my internet connection

    how can i fix it so i can make in app purchases it keeps telling me to check my internet connections

    And I have gone to the support and it was no help

  • I tried to make my image larger by typing ctrl + a fatal error was created in typer shark on yahoo games

    When I tried to make the game larger I pressed control plus. The image became different and all of the parts did not fit together like it was designed. I tried to make it like it was by pressing control minus (ctrl - ) nothing happened. When I start playing the game there is a gap between where the sharks enter the playing field and after playing three rounds the screen goes to a fatal error message and after a few seconds it goes to some other screen that I pasted below. I want to put the game size back to the original size.

    When I tried to make the game larger I pressed control plus. The image became different and all of the parts did not fit together like it was designed. I tried to make it like it was by pressing control minus (ctrl - ) nothing happened. When I start playing the game there is a gap between where the sharks enter the playing field and after playing three rounds the screen goes to a fatal error message and after a few seconds it goes to some other screen that I pasted below. I want to put the game size back to the original size.

  • I am getting an error when trying to download  the new Game of Thrones shows. The error is err = -61 You do not have the privilege to make changes. (err = -61) you do not have the privilege  to make changes. I have not had this problem before

    I am getting an error when trying to download  the new Game of Thrones shows. The error is err = -61 You do not have the privilege to make changes. (err = -61) you do not have the privilege  to make changes. I have not had this problem before

    Solution:
    Open Disk Utility.
    Select your external hard disk and run a “verify” on it. After a few minutes (depending on the size of your drive), you should be good to go. You don’t need to run a “repair” on it... (at least I didn’t have to). See photo for reference.
    This should fix the permissions issue, because on my Mac running Mountain Lion I was unable to edit the permissions in the get info dialog window even after "unlocking" the drive.

  • Trying to make in game purchases,settings are all on, keep's telling me purchase could not be completed

    Trying to make in game purchase. Keep's telling me purchase could not be made go to apple support

    Try here
    iTunes Store Support
    http://www.apple.com/emea/support/itunes/contact.html

  • Ive tried to make a purchase with my other apple id but i keep getting prompted to purchase this app again....i want to continue in the game im working on but i dont want to wait the 30 days until i can use my new prepaid credit card(mastercard)....both i

    Ive tried to make a purchase with my other apple id but i keep getting prompted to purchase this app again....i want to continue in the game im working on but i dont want to wait the 30 days until i can use my new prepaid credit card(mastercard)....both ids are linked to my facebook does that help?

    Ive tried to make a purchase with my other apple id but i keep getting prompted to purchase this app again....i want to continue in the game im working on but i dont want to wait the 30 days until i can use my new prepaid credit card(mastercard)....both ids are linked to my facebook does that help?

  • Trying to make a game

    Okey so im trying to make a small flash game, so i can learn some more about flash....
    and i ran in to a small problem, in my game i would like there to be a time thats starts on 30 sec and going down to 0 sec, and when it hits 0 U have won and its sends U to the next lvl, in the game U need to avoid an enemy in the 30 sec.
    so this is what i need so far:
    timer there go´s from 30 sec to 0 sec and then sends U on to lets say frame 3

    If you are just starting with Flash and want to learn then what I want U to do is to learn how to use the Flash help documentation... before you do anything else.  It isn't likely you'll be able to do any more than copy what others have done if you do not learn how to look things up and learn for yourself.  So to start, open the Flash Help documentation (Help -> Flash Help) and look up the setInterval() function.  Before you worry about using it in your game, figure out how it works by reading about it and using it in a separate testing file.  Then think about how you can use it to reduce a value by one every second.

  • New to Java and trying to make a game

    I'm really new to Java so please try not to roll over laughing at the following code. At the moment, as you can hopefully pick up from the code, i'm trying to get a shape(Polygon) to respond to keyboard events and i'm trying to make the object primarily respond to the cursor keys and to move the Polygon in the corresponding directions of the cursors. I'm stuck and i think i have initialised everything but i can't get the shape to move. I'm pretty sure i'm missing required code in the paint method.
    <code>
    import java.awt.*;
    import java.awt.Event.*;
    import java.applet.*;
    import java.io.*;
    public class PlodInvaders extends Applet implements Runnable {
    Point start, end, fire, shield, target;
    int co_x, x, y;
    int co_y;
    int current_x, current_y;
    char current_key;
    Font theFont;
    Thread th;
    Image ship;
    Polygon p;
    public void init() {
    setBackground(Color.black);
    // mouse co-oridnates
    co_x = this.size().width + 5;
    co_y = this.size().height + 5;
    theFont= new Font("Courier", Font.BOLD, 24);
    current_x = 150;
    current_y = 100;
    public void start() {
    th = new Thread(this);
    th.start();
    public boolean KeyDown(Event evt, int key) {
    switch(key) {
    case Event.LEFT: current_x = current_x - 10; break;
    case Event.RIGHT: current_x = current_x +10; break;
    case Event.UP: current_y = current_y + 10; break;
    case Event.DOWN: current_y = current_y - 10; break;
    default: current_key = (char) key;
    start = new Point(x, y);
    repaint();
    return true;
    public boolean MouseDown(Event evt, int x, int y) {
    fire = new Point(x, y);
    return true;
    public boolean MouseUp(Event evt, int x, int y) {
    target = new Point(x, y);
    repaint();
    return true;
    public void run() {
    public void paint(Graphics g){
    Polygon p = new Polygon();
    g.setColor(Color.white);
    p.addPoint(20,20);
    p.addPoint(50,25);
    p.addPoint(50,50);
    p.addPoint(25,50);
    p.addPoint(20,20);
    g.fillPolygon(p);
    </code>

    this is not prefect and it is not a applet, but it will make you start. you can email if you have any qeustions
    import java.awt.*;
    import java.awt.event.*;
    public class MoveP extends Frame implements KeyListener
         Polygon po = new Polygon();
            int ix=0;
         int iy=0;
    public MoveP()  
         addWindowListener(new WindowAdapter()
        {     public void windowClosing(WindowEvent ev)
              {     dispose();
                   System.exit(0);
         addKeyListener(this);
         setSize(300,372);
         po.addPoint(20,20);
         po.addPoint(50,25);
         po.addPoint(50,50);
         po.addPoint(25,50);
         po.addPoint(20,20);
         setVisible(true); 
    public void paint(Graphics g)
         g.setColor(Color.black);
         for (int i=0; i < po.npoints; i++)
              po.xpoints[i]+=ix;
              po.ypoints[i]+=iy;
         g.fillPolygon(po);
    public void keyTyped(KeyEvent e)
    public void keyPressed(KeyEvent e)
         ix = 0;
         iy = 0;
         int k = e.getKeyCode();
         if (k == 40) iy++;
         if (k == 38) iy--;
         if (k == 39) ix++;
         if (k == 37) ix--;
         repaint();
    public void keyReleased(KeyEvent e)
    public static void main (String[] args)
         new MoveP();
          Noah

  • Trying to code a pool game but the initial shape dissapears!!! im stuck!

    Hi ,
    I basically want to write a java graphics game application ......At this point in time all I want to do is add a green rectangle to the content pane.......I can do this but I want to stick with OO concepts so I have various classes......the thing is ....when a user resizes the window the graphic I have painted dissapeared
    I will show the classes I have used and the code below......I want to do the program conforming to the way I was tought java which is to have a main controlling class that orchestrates communication between unrelated classes and their methods. In addition to my question if anyone sees stuff fundementally wrong with my code and has a better solution please feel free to enlighten me :-)
    main controlling class:
    import java.awt.Graphics;
    * <p>Title: </p>
    * <p>Description: </p>
    * <p>Copyright: Copyright (c) 2005</p>
    * <p>Company: </p>
    * @author not attributable
    * @version 1.0
    public class PoolApp {
    public PoolApp() {
    //creates new gamescreen object
    GameScreen aScreen = new GameScreen(this);
    public static void main(String[] args) {
    PoolApp poolApp1 = new PoolApp();
    public Graphics getPaintedTable(GameScreen aGameScreen) {
    Paint aPaint = new Paint(this);
    Graphics tblObj = aPaint.getTbleGraphic(aGameScreen);
    return tblObj;
    ------end main controlling class
    ------TLayer Class start
    import javax.swing.*;
    /* The purpose of this class was to create a generic frame that would
    be inherited from all screen classes ...I initially had this creating a black background that would be inherited from child classes but that created
    more problems when the screen was resized by one of the child classes*/
    public class TLayer extends JFrame {
    public TLayer() {
    -----TLayer Class end
    -----GameScreen class start--------
    public class GameScreen extends TLayer{
    private PoolApp thePoolApp;
    public GameScreen(PoolApp aPoolApp) {
    thePoolApp = aPoolApp; //set a reference from here to the controlling PoolApp initGameScreen(); //initialise and show GameScreen .....screen
    public void initGameScreen() {
    this.setSize(800,600);
    this.show();
    //now make a request to the poolapp controlling class to ask for a painted green table and get the paint class to paint it to screen
    thePoolApp.getPaintedTable(this);
    ----GameScreen class end------------
    ----PaintScreen class start-------------
    PoolApp thePoolApp;
    public Paint() {
    public Paint(PoolApp apoolApp) {
    thePoolApp = apoolApp;
    public Graphics getTbleGraphic(GameScreen aGameScreen) {
    Container theCont = aGameScreen.getContentPane(); //assign the gamescreen content pane to a container
    theCont.setSize(200,200); //set a viewable size to the container
    Graphics thetbl = theCont.getGraphics(); //get container graphics context and assign it to a graphics object
    thetbl.setColor(Color.green); //color the object
    thetbl.fillRect(30,30,20,60); //fill the rectangle
    return thetbl; //return the object
    ---PaintScreen class end---------------
    This code actually draws the green rectangle to the gamescreen from the paint class .......so it works to a degree! ......whenever I resize the window the painted image dissapears, could anyone suggest a way around this also ...am I going about this the correct way? Im open to making mass changes to better the program ......the only thing I do not want is to use applets! .....as I want to learn the fundementals of custom painting my stuff
    also I tried altering the paint class like so:<as well as many other suggested ways>
    public class Paint extends TLayer
    Graphics g;
    PoolApp thePoolApp;
    GameScreen theGameScreen;
    public Paint() {
    public Paint(PoolApp apoolApp) {
    thePoolApp = apoolApp;
    public Graphics getTbleGraphic(GameScreen aGameScreen) {
    theGameScreen = aGameScreen;
    paint(g,aGameScreen);
    return g;
    public void paint(Graphics g,GameScreen aGameScreen) {
    super.repaint();
    // draw your green rectangle here
    Container c = aGameScreen.getContentPane();
    Graphics thetbl = c.getGraphics();
    thetbl.setColor(Color.green);
    thetbl.fillRect(30,30,20,60);
    I can get painting working but ...to be honest it would mean lumping things all together and Im trying to make
    my program OO ...so that kind of defeats the purpose...Im really looking for an answer that is not generic ....in the sense that it will work for this program specifically.....
    I have done lots of reading and tried the usual things .....I know reasnably well whats happening for the code to be drawn
    but the fact that I have seperated classes based on there specific purposes ...it has made it tricky to stop the painted image vanishing when I
    resize the screen.......I ask whoever is viewing this code to check out what classes inherits from each other. I have also tried overidding the paint and paintComponents methods but
    the same annoying thing still happens
    Any help is much needed and appreciated
    David

    Hello,
    In order to make it painted all the time you have to override the method
    paint(Graphics g) in the JFrame class. In your program you have to override it either in the TLayer class or in the GameScreen class.
    This is the new GameScreen class after this modification; this makes the rectangle painted after resizing.
    import java.awt.Graphics;
    public class GameScreen extends TLayer {
         private PoolApp thePoolApp;
         public GameScreen(PoolApp aPoolApp) {
              thePoolApp = aPoolApp; // set a reference from here to the controlling
                                            // PoolApp initGameScreen(); //initialise and
                                            // show GameScreen .....screen
              initGameScreen();
         public void initGameScreen() {
              this.setSize(800, 600);
              this.show();
              // now make a request to the poolapp controlling class to ask for a
              // painted green table and get the paint class to paint it to screen
              thePoolApp.getPaintedTable(this);
         public void paint(Graphics arg0) {
              super.paint(arg0);
              thePoolApp.getPaintedTable(this);
    }Hope this helps you.
    Feel free to ask, if you have any question .
    Regards,
    Ahmed Saad

  • HT4009 "Sorry, your device doesn't support the payment". Trying to make in app purchase.

    Hi, i am trying to make an in app purchase in the game Iron Force, but keep getting a screen pop up that says "Sorry, your device doesn't support the payment". I have already tried to trouble shoot this, I went into settings and made sure in app purchases were on, I also checked my I tunes account and verified my info. Also I have hard reset the phone, and the app, turned the phone off and on, deleted the app and reinstalled it on my iphone. I am stuck, if anyone can help me that would be great. Thank you.

    Have you done as the message says and contacted iTunes Support (these are user-to-user forums) ? If not then you can do so via this link and ask them why the message is appearing : http://www.apple.com/support/itunes/contact/ - click on Contact iTunes Store Support on the right-hand side of the page, then Purchases, Billing & Redemption

  • TS1702 i still cant make purchase in game app

    i still cant make purchase in game app it still show me contact itunes store to complete this transaction. i was send the problem to you guys few times but still din have any respond from it.

    I'm sure he did. I've e-mailed iTunes Store Support several times over the last few days, I still can't make purchases, and now I can't access my already purchased music. I thought Apple was supposed to have good support for their products. This seems bad for Apple, this is happening to many people judging by the number of times this comes up in the user forums I've explored trying to find a fix for this problem. Apple has to be losing money because customers aren't able to purchase, and then the customers give up because they can't get help from Apple's support team.

  • Tried to install a new game but got error message "You can't open the application "XIII Install" because PowerPC applications are no longer supported."  I have since read that Yosemite doesn't have the program. What can I do?

    Tried to install a new game that I bought (Xiii) for my MBP. All system req have been met and exceeded, however when trying to install it I get an error message saying "You can’t open the application “XIII Install” because PowerPC applications are no longer supported."
    I have since read online that the operating software Yosemite has got rid of PowerPC applications, meaning I can't install my new game. 
    Is there anything simple that I can do to get this game to work. I'll be gutted if not as I have opened the game therefore the shop will not give a full refund, I'm sure .
    Regards
    Simon

    A lot longer ago than that. PowerPC apps haven't been supported since Lion, 10.7.x.
    You may have just purchased the game, but the developers obviously haven't kept up to date with the requirements for newer versions of the Mac OS.
    You have three options, depending on how old your Mac is.
    1) If it were ever able to boot directly to Snow Leopard, you could create a separate partition and install SL onto that partition. Make sure to turn on the option during the installation to install Rosetta. That is OS X's PPC emulator.
    2) If your Mac is too new to install Snow Leopard, then you can purchase and install Snow Leopard Server within a VM, such as Parallels, VirtualBox, or Vmware.
    3) Use a separate older Mac for running outdated software.

  • Trying to make a Ping Pong Ball generate in random location

    Hey guys and Gals,
    im trying to make a pingpong ball, well 2 of them spawn in random locations so that it makes the game a little harder as of right now the ping pong balls spawn right next to each other and untill score 5-6 there right next to each other

    I disagree with Caillen.   If somebody is trying to learn a programming language writing a game is an excellent way of learning. In the constructor to the form (or form load function ) you can change the X,Y location of the ball(s). You also want
    to make sure the two balls don't overlap. To make the programming simpler I would start the two balls at the same Y coordinate (or X).
    See code below.  I used a little clever programming to prevent the two ball from overlapping. The code assumes the left side of the panel x = 0.  If it is not zero then add the X of the left side of the panel to the results.
    int widthPanel = 10;
    int diameterBall = 2;
    Random rand = new Random();
    int ball1X = rand.Next(diameterBall / 2, widthPanel - (diameterBall / 2));
    //now get a random number that is space not taken up by ball one
    int ran2 = rand(0, widthPanel - diameterBall);
    int ball1Left = ballX - (diameterBall / 2);
    int ball2X = 0;
    if(rand2 <= ball1X)
    ball2X = rand2;
    else
    int ball1Right = ballX + (diameterBall / 2);
    ran2 -= ball1Left;
    ball2X = ball1Right + ran2;
    jdweng

Maybe you are looking for

  • How to set my MacPro to not go to sleep

    I am running Euphonix MC control. They recommend: It is recommended that you do not set your EuCon workstation(s) to Sleep. We recommend setting your Mac to not go to sleep and never put hard drives to sleep.

  • Oracle database 8i

    i'm having a problem in installing oracle database 8i. After the download complete and when i start SQL* i'm getting a TNS error. what does that mean?And what login name should be given?

  • Wacom Bamboo tablets with kernel26 = 2.6.36

    1. Bamboo tablets do not work with mainline kernel code (yet). 2. There is a module provided by linuxwacom project. 3. It worked for me with 2.6.35. (in AUR: http://aur.archlinux.org/packages.php?ID=31540). 4. But it doesn't compile with 2.6.36, whic

  • OSX becomes totally unresponsive, reboot required.

    This week I've had my computer lock up and become totally unresponsive three times, forcing me to hold the power switch to force a restart. Each time, I had the same apps open and the problem was triggered the same way. I have the following open: Saf

  • Porque no puedo usar facetime? ya cambie el DNS a 8.8.8.8,8.8.4.4 y ni asi puede

    cuando entro a facetime me pide mi contraseña y luego cuando la pongo me manda a confirmar mi direccion de correo y cuando pongo siguiente solo dice ''verificando'' y luego me regresa al principio. Como le hago? ya me cace de intentar