trouble with bouncing from left to right and shotting.
in
Programming Questions
•
10 months ago
im trying to make a small game where you control a character with your mouse and shoot with left clicking while your opponent bounces from left to right and shots randomly at you. i'm having trouble figuring out how to code the bouncing and having the shot stay in a straight line when fired instead of moving with the mouseX.
i am a beginner with this program so please don't judge me and my stupidity :(
also my names are very stupid because i couldn't think of anything better, i apologies
gumgoose VS Dingus
Character gumgoose;
Enemy dingus;
Weapon fire;
Weapon2 arrow;
Wall lb;
Wall rb;
int speed = 1;
int shot = 500;
void setup()
{
size (450,600);
noCursor();
gumgoose = new Character( "gumgoose");
dingus = new Enemy ("dingus");
fire = new Weapon ("fire");
arrow = new Weapon2 ("arrow");
lb = new Wall (0,300, 3, height);
rb = new Wall (447,300,3,height);
}
void draw()
{
background (193, 165, 72);
gumgoose.moveMe (mouseX, 500);
dingus.moveMe (speed, 50);
//if( dingus.isHitting (rb) )
//{
//speed = speed*-1;
// }
fire.moveMe(mouseX, shot);
dingus.drawMe();
gumgoose.drawMe();
fire.drawMe();
arrow.drawMe();
lb.drawMe();
rb.drawMe();
}
Box
class Box
{
// THIS BOX ASSUMES rectMode( CORNER );
// Properties
int x;
int y;
int w;
int h;
color c = color( 0, 255, 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()
{
rectMode(CENTER);
strokeWeight( 1 );
stroke( c );
noFill();
rect( x, y, w, h );
line( x+w/2, y+h/2, x-w/2, y-h/2 );
line( x-h/2, y+w/2, x+h/2, y-w/2 );
}
void moveTo( int newX, int newY )
{
x = newX;
y = newY;
}
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
{
String name;
float x;
float y;
int dia = 50;
Box collider;
boolean DEBUG = true;
Character (String name)
{
this.x = 0;
this.y = 0;
this.name = name;
this.collider = new Box( (int)x, (int)y, dia, dia );
}
void moveMe( float x, float y)
{
this.x = x;
this.y = y;
this.collider.moveTo( (int)x, (int)y );
}
void drawMe()
{
ellipseMode( CENTER );
fill(27, 167, 108);
stroke (0);
ellipse( x, y, dia, dia);
if ( DEBUG )
{
collider.drawMe();
}
}
}
Wall
class Wall
{
int x;
int y;
int w;
int h;
color c = color( 0 );
Box collider;
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 );
}
}
arrow
class Weapon2
{
String name;
float x;
float y;
int dia = 20;
Box collider;
boolean DEBUG = true;
Weapon2( String name )
{
this.x = 10;
this.y = 0;
this.name = name;
this.collider = new Box( (int)x, (int)y, dia, dia );
}
void moveMe( float x, float y)
{
this.x = x;
this.y = y;
this.collider.moveTo( (int)x, (int)y++ );
}
void drawMe()
{
ellipseMode( CENTER );
fill(128);
stroke( 0 );
ellipse( x, y, dia, dia);
if ( DEBUG ) collider.drawMe();
}
}
dingus
class Weapon2
{
String name;
float x;
float y;
int dia = 20;
Box collider;
boolean DEBUG = true;
Weapon2( String name )
{
this.x = 10;
this.y = 0;
this.name = name;
this.collider = new Box( (int)x, (int)y, dia, dia );
}
void moveMe( float x, float y)
{
this.x = x;
this.y = y;
this.collider.moveTo( (int)x, (int)y++ );
}
void drawMe()
{
ellipseMode( CENTER );
fill(128);
stroke( 0 );
ellipse( x, y, dia, dia);
if ( DEBUG ) collider.drawMe();
}
}
fire
class Weapon
{
String name;
float x;
float y;
int dia = 20;
Box collider;
boolean DEBUG = true;
Weapon( String name )
{
this.x = mouseX;
this.y = 600;
this.name = name;
this.collider = new Box( (int)x, (int)y, dia, dia );
}
void moveMe( float x, float y)
{
this.x = x;
this.y = y;
this.collider.moveTo( (int)x, (int)y );
}
void drawMe()
{
ellipseMode( CENTER );
fill(247,128,15);
stroke( 0 );
ellipse( x, y, dia, dia);
shot = shot -5;
if(shot < 0)
{
shot = 500;
}
if ( DEBUG ) collider.drawMe();
}
}
id really appreciate the help if anybody can. thank you :D
1