Help with a few things
in
Programming Questions
•
1 years ago
I'm a novice at Programming so I was hoping to consult the forum for some help.
I wanted my code to be able to run a rect avoiding a number of ellipses coming down. My rect cannot move and the code that allows the ellipses to return from the other side, does not work either. Also, if anyone could help me with some kind of "collision" code, would be greatly appreciated!
int screenNumber;
enemy[] e;
PFont font;
float x, y;
boolean wPressed, aPressed, sPressed, dPressed;
boolean upPressed, leftPressed, downPressed, rightPressed;
void setup()
{
size(600, 600);
noStroke();
font = loadFont("BernardMT-Condensed-25.vlw");
textFont(font, 32);
screenNumber = 0;
e = new enemy[20];
for (int i=0; i<20; i++)
{
e[i] = new enemy();
e[i].x = random(0, 600);
e[i].y = random(0, 600);
}
}
void draw()
{
background(100);
if (screenNumber == 0)
{
TitleScreen();
}
if (screenNumber == 1)
for (int i=0; i<20; i++)
{
fill(200, 200, 200);
e[i].movement();
e[i].show();
{
if (x < 0)
{
x = 600;
}
if (x > 600)
{
x = 0;
}
if (y < 0)
{
y = 600;
}
if (y > 600)
{
y = 0;
}
}
}
if (wPressed) y = y-5;
if (aPressed) x = x-5;
if (sPressed) y = y+5;
if (dPressed) x = x+5;
rect(x, y, 20, 20);
if (screenNumber == 0);
}
void TitleScreen()
{
text("Press ENTER to play.", 180, 100);
if (keyCode == ENTER) screenNumber = 1;
}
{
}
class enemy
{
float x, y;
void show()
{
fill(200);
ellipse(x, y, 20, 20);
}
void movement() {
x=x+1;
y+=random(1, 20);
}
void keyPressed()
{
if (key == 'w') wPressed = true;
if (key == 'a') aPressed = true;
if (key == 's') sPressed = true;
if (key == 'd') dPressed = true;
}
void keyReleased()
{
if (key == 'w') wPressed = false;
if (key == 'a') aPressed = false;
if (key == 's') sPressed = false;
if (key == 'd') dPressed = false;
}
}
I wanted my code to be able to run a rect avoiding a number of ellipses coming down. My rect cannot move and the code that allows the ellipses to return from the other side, does not work either. Also, if anyone could help me with some kind of "collision" code, would be greatly appreciated!
int screenNumber;
enemy[] e;
PFont font;
float x, y;
boolean wPressed, aPressed, sPressed, dPressed;
boolean upPressed, leftPressed, downPressed, rightPressed;
void setup()
{
size(600, 600);
noStroke();
font = loadFont("BernardMT-Condensed-25.vlw");
textFont(font, 32);
screenNumber = 0;
e = new enemy[20];
for (int i=0; i<20; i++)
{
e[i] = new enemy();
e[i].x = random(0, 600);
e[i].y = random(0, 600);
}
}
void draw()
{
background(100);
if (screenNumber == 0)
{
TitleScreen();
}
if (screenNumber == 1)
for (int i=0; i<20; i++)
{
fill(200, 200, 200);
e[i].movement();
e[i].show();
{
if (x < 0)
{
x = 600;
}
if (x > 600)
{
x = 0;
}
if (y < 0)
{
y = 600;
}
if (y > 600)
{
y = 0;
}
}
}
if (wPressed) y = y-5;
if (aPressed) x = x-5;
if (sPressed) y = y+5;
if (dPressed) x = x+5;
rect(x, y, 20, 20);
if (screenNumber == 0);
}
void TitleScreen()
{
text("Press ENTER to play.", 180, 100);
if (keyCode == ENTER) screenNumber = 1;
}
{
}
class enemy
{
float x, y;
void show()
{
fill(200);
ellipse(x, y, 20, 20);
}
void movement() {
x=x+1;
y+=random(1, 20);
}
void keyPressed()
{
if (key == 'w') wPressed = true;
if (key == 'a') aPressed = true;
if (key == 's') sPressed = true;
if (key == 'd') dPressed = true;
}
void keyReleased()
{
if (key == 'w') wPressed = false;
if (key == 'a') aPressed = false;
if (key == 's') sPressed = false;
if (key == 'd') dPressed = false;
}
}
1