We have a word game which clicks and drags word images across the screen into certain boxes and then locks into place.
Code:
Sprite[] sprites;
Box[] boxes;
int bx=500;
int by=600;
int s_size=150;
boolean spritesDragging;
int boxX=630;
int boxY=100;
PImage b;
PImage CorrectImg;
void setup()
{
size(798, 760);
b = loadImage("EiffelTower.jpg");
CorrectImg = loadImage("Correct.jpg");
boxes= new Box[7];
sprites = new Sprite[7];
for(int i = 0; i < sprites.length; i++)
{
sprites[i] = new Sprite(loadImage(i + ".png"),s_size,random(bx), random(by), i);
}
for(int i =0; i< boxes.length; i++)
{
boxY+=80;
boxes[i] = new Box(boxX, boxY, 25, 130);
}
}
void draw()
{
background(b);
boolean bDragging = false;
//rectangle x, y, w , h
//rect(boxX, boxY, 140, 30);
for (int i=0; i <boxes.length; i++)
{
boxes[i].Draw();
}
for (int i = 0; i < sprites.length; i++)
{
// Check if the user tries to drag it
sprites[i].Update(spritesDragging);
if (sprites[i].IsDragged())
{
// We will remember a dragging is being done
bDragging = true;
// And move it to mouse position
sprites[i].Move();
}
// In all case, we redraw the sprite
sprites[i].Draw();
}
spritesDragging = bDragging;
}
class Sprite
{
float x;
float y;
PImage img;
float SpriteSize;
int i;
private boolean m_bIsHovered, m_bDragged;
private float m_clickDX, m_clickDY;
int boxID;
Sprite(PImage img, int SpriteSize,float x,float y, int boxID)
{
this.x = x;
this.y = y;
this.img = img;
this.boxID = boxID;
this.SpriteSize=SpriteSize;
}
void Update(boolean bAlreadyDragging)
{
// Check if mouse is over the sprite
m_bIsHovered = mouseX > x && mouseX < x + SpriteSize && mouseY > y && mouseY < y + SpriteSize/10;
if (!bAlreadyDragging && mousePressed && mouseButton == LEFT && m_bIsHovered)
{
// We record the state
m_bDragged = true;
m_clickDX = mouseX - x;
m_clickDY = mouseY - y;
}
// If mouse isn't pressed
if (!mousePressed)
{
// Any possible dragging is stopped
m_bDragged = false;
}
//method for locking image in place
for(int i =0; i< boxes.length; i++)
{
//method for locking image in place
if(x> boxes[i].x && x< (boxes[i].x+boxes[i].w-22) &&y <(boxes[i].y+boxes[i].h-20) && y >boxes[i].y)
{
m_bDragged = false;
if(i == boxID )
{
image(CorrectImg, 78, 590);
}
}
}
}
boolean IsDragged()
{
return m_bDragged;
}
void Move()
{
if (m_bDragged)
{
x = mouseX - m_clickDX;
y = mouseY - m_clickDY;
}
}
void Draw()
{
image(img, x, y);
}
}
class Box
{
float x;
float y;
float h;
float w;
Box(float x,float y, float h, float w)
{
this.x = x;
this.y = y;
this.h= h;
this.w= w;
}
boolean checkBox(float spriteX, float spriteY, Sprite theSprite)
{
return false;
}
void Draw()
{
rect(x,y, w, h);
}
}
We have the code so that it locks images into place and returns true if one image is in the correct position. For example if image 1 is in box 1 then return 'correct' image and so forth.
Code:
for(int i =0; i< boxes.length; i++)
{
//method for locking image in place
if(x> boxes[i].x && x< (boxes[i].x+boxes[i].w-22) &&y <(boxes[i].y+boxes[i].h-20) && y >boxes[i].y)
{
m_bDragged = false;
if(i == boxID )
{
image(CorrectImg, 78, 590);
}
}
}
How can we edit this code so that it returns true only if all images are in the correct position. We tried adding another loop but it was unsuccessful.
Any suggestions?