Moving Walls Up and Down
in
Programming Questions
•
10 months ago
All I would simply like to do is move the walls up and down. I will post the code in its entirety ( each new tab will be in bold ). I hope someone can help me because it is irritating me.
BOX
class Box
{
// THIS BOX ASSUMES rectMode( CORNER );
// Properties
int x;
int y;
int w;
int h;
color c = color( 0 );
// Constructors
Box( int xx, int yy, int ww, int hh )
{
this.x = xx;
this.y = yy;
this.w = ww;
this.h = hh;
}
// Methods
void drawMe()
{
strokeWeight( 1 );
stroke( c );
noFill();
rect( x, y, w, h );
line( x, y, x+w, y+h );
line( x+w, y, x, y+h );
}
void moveTo( int newX, int newY )
{
x = newX;
y = newY;
}
/** This function returns true if 'this' Box is
* overlapping the 'otherBox'
*/
boolean isOverlapping( Box otherBox )
{
if ( this.x > otherBox.x + otherBox.w ) return false;
if ( this.x + this.w < otherBox.x ) return false;
if ( this.y > otherBox.y + otherBox .h ) return false;
if ( this.y + this.h < otherBox.y ) return false;
return true;
}
}
CHARACTER
class Character
{
// Properties
String name;
float x;
float y;
Box collider;
// Character constants
int dia = 20;
boolean DEBUG = true;
// Constructor
Character( String name )
{
this.x = 0;
this.y = 0;
this.name = name;
this.collider = new Box( (int)x-2, (int)y-2, dia, dia );
}
//. Methods
void moveMe( float x, float y)
{
this.x = x;
this.y = y;
this.collider.moveTo( (int)x, (int)y );
}
void drawMe()
{
ellipseMode( CORNER );
fill( 0, 255, 255 );
stroke( 0 );
ellipse( x, y, dia, dia );
if ( DEBUG )
{
collider.drawMe();
}
}
boolean seeIfHittingWall( Wall w )
{
boolean overlapping;
overlapping = collider.isOverlapping( w.collider );
print ( overlapping + " " );
return overlapping;
}
}
GAME CODE
Character george;
Wall doppy;
Wall grumpy;
Wall doc;
Wall sneezy;
Wall meanie;
int numWalls = 5;
int speed[] = new int[numWalls];
int sy[] = new int [numWalls];
void setup()
{
size( 300, 300 );
noCursor();
george = new Character( "george" );
doppy = new Wall( 273, 250, 25, 25 );
grumpy = new Wall( 210, 25, 50, 100 );
doc = new Wall ( 100, 140, 100, 25 );
sneezy = new Wall ( 35, 175, 50, 100 );
meanie = new Wall ( 1, 25, 25, 25 );
}
void draw()
{
background( 127 );
george.moveMe( mouseX, mouseY );
doppy.drawMe();
grumpy.drawMe();
doc.drawMe();
sneezy.drawMe();
meanie.drawMe();
if ( george.seeIfHittingWall( doppy ) )
{
doppy.setColour( 150, 0, 0 );
}
else
{
doppy.setColour( 255, 255, 255 );
}
if ( george.seeIfHittingWall( grumpy ) )
{
grumpy.setColour ( 150, 0, 0 );
}
else
{
grumpy.setColour (255, 255, 255 );
}
if ( george.seeIfHittingWall ( doc ) )
{
doc.setColour ( 150, 0, 0 );
}
else
{
doc.setColour ( 255, 255, 255 );
}
if ( george.seeIfHittingWall ( sneezy ) )
{
sneezy.setColour ( 150, 0, 0 );
}
else
{
sneezy.setColour ( 255, 255, 255 );
}
if ( george.seeIfHittingWall ( meanie) )
{
meanie.setColour ( 150, 0, 0 );
}
else
{
meanie.setColour ( 255, 255, 255 );
}
george.drawMe();
}
WALL
class Wall
{
int x;
int y;
int w;
int h;
color c = color( 0 );
Box collider;
boolean DEBUG = true;
Wall( int x, int y, int w, int h )
{
this.x = x;
this.y = y;
this.w = w;
this.h = h;
collider = new Box( x, y, w, h );
}
void drawMe()
{
fill(c);
stroke( 0 );
rect( x, y, w, h );
if ( DEBUG ) collider.drawMe();
}
void setColour( int r, int g, int b )
{
c = color( r, g, b );
}
}
THE END
Thanks I hope someone can help me finish this project.
1