Hey guys! So I'm creating a "predator/prey" simulation! I got most of this code down! I'm sure just struggling with a couple things. I want to make it so
1. the amount of images on screen is random every time
2. im3 is scared of img2, and img 2 is scared of img1
(img3 is a block of cheese, img2 is a mouse, and img1 is a cat)
3. I dont want them to leave my screen! For some reason they keep going off screen at the bottom. Also, these images used to be ellipses but I changed them to actual pictures so just ignore all the ellipse stuff
HERES MY CODE! thank you so much for your time
PImage img;
PImage img1;
PImage img2;
PImage img3;
float pY = 0; // variable that controls y coordinate of prey ellipse
float pX = 0; // variable that controls x coordinate of prey ellipse
float gY = 0; // variable that controls y coordinate of green predator ellipse
float gX = 0; // variable that controls x coordinate of green predator ellipse
float rY = 0; // variable that controls y coordinate of red predator ellipse
float rX = 0; // variable that controls x coordinate of red predator ellipse
float pMoveY; // variable that changes in the y to move the prey ellipse
float pMoveX; // variable that changes in the x to move the prey ellipse
float gMoveY; // variable that changes in the y to move the green predator ellipse
float gMoveX; // variable that changes in the x to move the green predator ellipse
float rMoveY; // variable that changes in the y to move the red predator ellipse
float rMoveX; // variable that changes in the x to move the red predator ellipse
float maxSpeed = 4.0; // variable that sets the maximum speed at which creatures can move
float radius = 80.0; // variable that sets the size of the ellipses
boolean pScared = false; // boolean variable that lets the white ellipse to remain scared
void setup() {
img = loadImage("rave.jpg");
img1 = loadImage("nyan.png");
img2 = loadImage("mouse.png");
img3 = loadImage ("cheese.png");
size(800, 600); // sets the size of the sketch
pMoveX=random(-maxSpeed, maxSpeed); // sets the white ellipse speed in the x
pMoveY=random(-maxSpeed, maxSpeed); // sets the white ellipse speed in the y
gMoveX=random(-maxSpeed, maxSpeed); // sets the green ellipse speed in the x
gMoveY=random(-maxSpeed, maxSpeed); // sets the green ellipse speed in the y
rMoveX=random(-maxSpeed, maxSpeed); // sets the red ellipse speed in the x
rMoveY=random(-maxSpeed, maxSpeed); // sets the red ellipse speed in the y
rX=random(width-radius); // sets the starting position of the red ellipse in the x
rY=random(height-radius); // sets the starting position of the red ellipse in the y
gX=random(width-radius); // sets the starting position of the green ellipse in the x
gY=random(height-radius); // sets the starting position of the green ellipse in the y
pX=random(radius*2, width-radius*2); // sets the starting position of the white ellipse in the x
pY=random(radius*2, height-radius*2); // sets the starting position of the white ellipse in the y
ellipseMode(RADIUS); // sets ellipses to draw using the last two numbers as radius width and radius height
}
void draw() {
background(img);
//////////RED "PREDATOR" ELLIPSE LOGIC
rX+=rMoveX; // these 2 lines move the red ellipse
rY+=rMoveY;
if (rX>width-radius || rX<radius) { // these two if statements check to see if the red ellipse is still on screen and if not, then reverses the direction in which it is moving
rMoveX=-rMoveX;
}
if (rY>width-radius || rY<radius) {
rMoveY=-rMoveY;
}
fill(255, 0, 0); //sets the fill to red so the red ellipse is drawn as red
image(img1, rX, rY, radius, radius); // draws red ellipse
//////////GREEN "PREDATOR" ELLIPSE LOGIC
gX+=gMoveX; // these 2 lines move the green ellipse
gY+=gMoveY;
if (gX>width-radius || gX<radius) { // these two if statements check to see if the green ellipse is still on screen and if not, then reverses the direction in which it is moving
gMoveX=-gMoveX;
}
if (gY>width-radius || gY<radius) {
gMoveY=-gMoveY;
}
fill(0, 255, 0);
image(img3, gX, gY, radius, radius);
//////////WHITE "PREY" ELLIPSE LOGIC
pX+=pMoveX; // these 2 lines move the white "prey" ellipse
pY+=pMoveY;
if (pX>width-radius*2 || pX<radius*2) { // these two if statements check to see if the white ellipse is still on screen and if not, then reverses the direction in which it is moving
pMoveX=-pMoveX;
}
if (pY>width-radius*2 || pY<radius*2) {
pMoveY=-pMoveY;
}
if (dist(rX, rY, pX, pY)<radius*2 || dist(gX, gY, pX, pY)<radius*2) { // this if statement checks to see if the white ellipse is near either of the predator ellipses
if (pScared==false) {
pMoveY=-pMoveY; // if the white ellipse is close to a predator, then it reverses direction in the y
pMoveX=-pMoveX; // if the white ellipse is close to a predator, then it reverses direction in the x
pScared=true;
}
}
else {
pScared=false; // if the white ellipse will remain "scared" until it is not near either predator
}
if (pScared==true) { // if the white ellipse is "scared" then it remains yellow
fill(255, 255, 0);
}
else { // // if the white ellipse is not "scared" then it is white
fill(255);
}
image(img2, pX, pY, radius*2, radius*2); // draw the white ellipse
There is my code! The object of the game is to fill the screen with as many cat heads as possible before the timer hits zero. I want help writing a statement so that if lets say the player generates 40 cat heads, text shows up and says something like "YOU WIN!" and if they fail to get to 40, it comes up as you "YOU LOSE"
you get the basic idea :) Let me know if I can provide you with any more information. Thanks for your time!
Hello everyone! So my assigned project for this week is to make a microgame. For my game, i thought it would be fun if I did a "match the picture" sort of game. The basic premise is that a series of cat heads will appear, and you have to press the corresponding keyboard keys to match it. I need some help with two things:
1. I have a majority of the code down but I need a way to
randomize the cat heads that are appearing in the top left corner of the screen. I also want them to stay on screen for a little bit longer.
2. I need to let the player know when they succeeded or failed to match the cat head. What
"if" statement would you guys write in order to do that? I want it to say something like "FAIL" when they fail and "WIN" when they succeed.
I included all the images I use at the bottom of this post if anyone has the time to mess around with it. Thank you SO much for your time and any help! Here's my code:
PImage img;
PImage img2;
PImage img3;
PImage img4;
int i;
void setup()
{
size(800, 577);
frameRate(1000);
img = loadImage ("nyan.png");
img2 = loadImage ("kitty.png");
img3 = loadImage ("cat.png");
img4 = loadImage ("kitten.png");
int i = 1;
}
void draw() {
{
PImage img;
img = loadImage("space.jpg");
background(img);
}
{
{
{
if ( i == 1 )
{
i= i + 1;
image(img, 0, 0);
}
else if (i == 20)
{
i= i + 1;
image(img2, 0, 0);
}
else if (i == 40)
{
i= i + 1;
image(img3, 0, 0);
}
else if (i == 60)
{
i= i + 1;
image(img4, 0, 0);
}
else
{
i = i + 1;
if ( i == 80)
{
i = 1;
}
}
}
}
{
if (keyPressed) {
{
if (key=='x' || key =='X') {
image(img, 300, 200);
}
{
if (keyPressed) {
if (key=='c' || key =='C') {
image(img2, 300, 200);
}
{
if (keyPressed) {
{
if (key=='v' || key == 'V') {
image(img3, 300, 200);
}
if (keyPressed) {
if (key=='b' || key =='B') {
image (img4, 300, 200);
}
}
}
}
}
}
}
}
}
}
}
}
Here are my images
(cat.png)
(kitten.png)
(kitty.png)
(nyan.png)
(space.jpg)
So we've been instructed to make a "paint toy" in my programming class. I'm trying to make a picture load with the press of a key. I want the image to load when I'm holding the key and clicking my mouse to paint the screen with the image. I got the color changes to work but I can't seem to do the same with an image. It runs when I play it, but the image doesn't show up. I do get a bit of slow down though, which means something is working....
Both of these codes work fine seperately but I wanna make it so that I have the burgers flying in the background AND have the guy opening and closing his mouth in the bottom right hand corner.
To see the first code in action visit:
http://www.openprocessing.org/user/21091 and click on "project 4 (in progress)
thanks much!
int numFrames = 3; //The Number of frames in the animation
int frame = 0;
PImage[] images = new PImage[numFrames];
void setup()
{
size(800,600);
frameRate(10);
images[0] = loadImage("Bob1.png");
images[1] = loadImage("Bob2.png");
images[2] = loadImage("Bob3.png");
}
void draw()
{
PImage img;
img = loadImage("burger.JPG");
background(img);
frame = frame +1;
if (frame>=numFrames){
frame=0;
}
//frame= (frame+1) % numFrames; // use % to cycle through frames
image(images[frame],50,50);
}
void mousePressed() {
PImage img;
img = loadImage("Cheeseburger.png");
image(img, 550, 450);
image(img, 450,400);
image(img,0,0);
image(img,650,0);
image(img,0,450);
}
and then:
Burger[] Burger = new Burger[12];
void setup() {
size(800,600);
for (int i = 0; i < Burger.length; i++ ) {
Burger[i] = new Burger(random(width), random(height), random(1,3));