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, :P:
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--;
}
}
}
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.