We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi! I'm extremely new to Processing and wanted to learn how to use it. For a project of mine we were given the choice to use it so I went ahead and started to make a game with it. It's a game where your a spaceship trying to dodge enemy spaceships. Here's the code:
int Screen = 0;
int timersecs;
int milliseconds;
int shipX = 40, shipY = 300;
boolean intersect = false;
PImage Ship;
PImage Enemy1;
PImage Enemy2;
PImage Enemy3;
PImage Enemy4;
PImage Enemy5;
PImage SpaceWarLoad;
PImage SpaceWarBack;
Ships s[];
void setup ()
{
size(600,600);
Ship = loadImage("Smallship.png");
SpaceWarLoad = loadImage("SpaceWarLoad.png");
SpaceWarBack = loadImage("SpaceWarBack.jpg");
s = new Ships[5];
for (int z = 0; z < 5; z++)
{
s[z] = new Ships();
}
}
void draw ()
{ //Loading screen and Background
if (Screen == 0)
{
imageMode(CENTER);
image(SpaceWarLoad,width/2,height/2);
}
else
{
imageMode(CENTER);
image(SpaceWarBack,width/2,height/2);
image(Ship, shipX, shipY); //Ship Sprite
timer();
}
if (shipY <= 30) //Border control
{
shipY=30;
}
else if (shipY >= 560)
{
shipY = 560;
}
for (int z = 0; z < 5; z++) //array to create 5 enemy ships
{
if (Screen == 1)
{
s[z].display();
}
}
}
void keyPressed() //Movement
{ if (key==CODED)
{
if(keyCode==UP)
{
shipY=(shipY)-10;
}
else if(keyCode==DOWN)
{
shipY=(shipY)+10;
}
}
}
class Ships
{
float xposition, yposition, speed;
int spriteSelector;
Ships ()
{
command();
}
void command()
{
yposition = (int) random(30,561);
xposition = 600;
speed = 5;
}
void display()
{
spriteSelector = (int) random(1,6);
if (spriteSelector>0)
{
if (spriteSelector<2)
{
Enemy1 = loadImage("Smallship2.png");
image(Enemy1, xposition, yposition);
xposition = xposition-speed;
}
}
if (spriteSelector>1)
{
if (spriteSelector<3)
{
Enemy1 = loadImage("Smallship3.png");
image(Enemy1, xposition, yposition);
xposition = xposition-speed;
}
}
if (spriteSelector>2)
{
if (spriteSelector<4)
{
Enemy1 = loadImage("Smallship4.png");
image(Enemy1, xposition, yposition);
xposition = xposition-speed;
}
}
if (spriteSelector>3)
{
if (spriteSelector<5)
{
Enemy1 = loadImage("Smallship5.png");
image(Enemy1, xposition, yposition);
xposition = xposition-speed;
}
}
if (spriteSelector>4)
{
if (spriteSelector<6)
{
Enemy1 = loadImage("Smallship6.png");
image(Enemy1, xposition, yposition);
xposition = xposition-speed;
}
}
if (shipX-xposition > 0 & shipY >= yposition)
{
intersect = true;
}
if (intersect == true)
{
image(Enemy1, shipX, shipY);
}
}
}
void mousePressed() //Start state detection
{
Screen=1;
}
void timer() //Timer for score
{
milliseconds = millis();
timersecs = milliseconds/100; //speed of timer
textSize(20);
text(timersecs,540, 600);
text("Score:",480, 600);
fill(#FFFFFF);
}
Now I've run into a couple of problems. The game loads in the enemy sprites through an array but I don't want it to be a one and done deal. Instead, I want the array to make 5 ships on the screen consistently, every 5 seconds (lets say). How would I do this?
Also, my hit detection is really bad. I tried treating the sprites like rectangles but it seems it didn't work. How would I go about making the collisions between the player controlled sprite and each of the sprites spawned by the array work?
Answers
The best advice I can give you is to break your problem down into smaller steps and to take those steps on one at a time.
Start with a simpler program that shows 5 new circles every 5 seconds. Don't worry about the rest of the game. Just get that bit working. The
ArrayList
class might help you, as might themillis()
function orframeCount
variable.Separately from that, create a small example that tests collision detection between two rectangles. You might want to read more about collision detection.
The key is to get something simple working. Then if you get stuck on one of those simple steps, you can post a MCVE of just that one step. Good luck.
So if I'm able to do both of those I could potentially integrate into my code?
Yeah. The idea is, instead of trying to do everything at once, focus on one small thing. That way if you get stuck, you can ask a more specific question.