Results 1 to 2 of 2
  1. #1
    finnlizzy is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Default Making image appear in random places (snake game)

    Hello, I'm using Processing to make a game similar to snake except it's nyan cat themed.

    I have sorted out most of the aesthetics and rules (Nyan cat cannot move outside of the perimeter).

    I want to make a cookie appear in random spots on the screen (the screen is 600 by 600).

    I was wondering what function or method should I use to make this happen?

    Please help as I am a n00b at this,

    Here's my code:


    int x = 50;
    int y = 50;
    int speedY;
    int dir;
    PImage b;
    PImage n;
    PImage l;
    PImage c;

    void setup()
    {
    size(600, 600);

    b = loadImage("nyan-background.jpg");
    n = loadImage("nyan.jpg");
    l = loadImage("youlose.jpg");
    c = loadImage("cookie.jpg");
    background(b);

    frameRate(100);
    }

    void draw() {
    background(b);
    nyan();
    keyPressed();
    }


    void nyan() //the snake head that you control
    {
    image(n, x, y, 40, 40); //image of nyancat

    if (x <= 0)
    {
    image(l, 0, 0, 600, 600);
    }
    if (y <= 0)
    {
    image(l, 0, 0, 600, 600);
    }
    if (x >= 600)
    {
    image(l, 0, 0, 600, 600);
    }
    if (y >= 600)
    {
    image(l, 0, 0, 600, 600);
    }
    }

    void cookie() // the object that I want to appear randomly onscreen
    {
    image(c, x, y, 40, 40);

    }



    void keyPressed()
    {
    if (key==CODED)
    {
    if (keyCode==UP)
    {
    y--;
    }
    if (keyCode==DOWN)
    {
    y++;
    }
    if (keyCode==RIGHT)
    {
    x++;
    }
    if (keyCode==LEFT)
    {
    x--;
    }
    }

    }

  2. #2
    Diargg is offline Senior Member
    Join Date
    Feb 2012
    Posts
    117
    Rep Power
    0

    Default Re: Making image appear in random places (snake game)

    Please use code tags ( [Code] [ /code])

    And have you looked into random numbers? They seem like a good way of doing things randomly.

Similar Threads

  1. Snake Game
    By LuluMM in forum New To Java
    Replies: 0
    Last Post: 03-08-2012, 06:48 AM
  2. Replies: 7
    Last Post: 09-14-2011, 03:06 PM
  3. Snake Game in Java
    By Shyamz1 in forum New To Java
    Replies: 4
    Last Post: 02-10-2011, 02:49 PM
  4. Java Applet Help - Making "Snake" Game
    By Alphimeda in forum Java Applets
    Replies: 15
    Last Post: 04-04-2010, 05:39 PM
  5. Snake Game
    By mustachMan in forum New To Java
    Replies: 2
    Last Post: 12-10-2009, 10:35 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
  •