I checked through similar codes and was still unable to figure out exactly why my issue is happening. Once the spaceships bounce off the wall for the fifth time, the don't change direction and the y+=20 loops until they are off the screen. I have tried probably going on fifteen or so different combinations of changes to figure out why this is happening but I'm drawing a blank. In addition, the ship on the second row from the top and all the way to the left drifts away from the other ships, even though they entirely share code and that shouldn't be possible.
The code is listed below. Thanks in advance for any help.
Bullet[]bullets = new Bullet[100];
Ship[]ships = new Ship[30];
Base base1 = new Base();
int bulletCounter=0;
int xloc=400;
int yloc;
float s=2;
int y=50;
boolean posCheck;
int shipY=50;
int timer;
void setup(){
size(800,800);
smooth();
int d=50; //placement of ships x coordinate at start
int shipwid=15; //size of ship (wanted it to be easily changable)
for(int i = 0; i < 15; i++){
ships[i] = new Ship(d,shipwid,shipY); //initializes the ships (first row)
d=d+45; //spaces ships 40 apart
}
d=50;
for(int i = 15; i < 30; i++){
ships[i] = new Ship(d,shipwid,shipY+50); //initializes the ships (second row)
d=d+45; //spaces ships 40 apart
}
}
void draw(){
background(255);
noCursor();
base1.display();
base1.drive();
for(int i=0; i<bulletCounter; i++){ //allows bullets to fire upon mousePress
bullets[i].fire();
bullets[i].display();
}
for(int i = 0; i<ships.length; i++){
ships[i].fly(s); //shows and flies ships
ships[i].display();
if(timer>4){
s=1;}
if(timer>8){
s=1.5;
}
for(int i2=0; i2 < bulletCounter; i2++){
if(bullets[i2].intersect(ships[i])){ //checks to see if ships hit
ships[i].damage(); //damages ships
bullets[i2].bulletHit();
}
}
y=0;
posCheck= ships[i].check();
if(posCheck==true){ //moves ships as a group
s=s*-1;
y=20;
for(int j = 0; j<ships.length; j++){
ships[j].drop(y);
}
timer+=1;
}
}
}
void mousePressed(){
xloc=base1.x;
yloc=height-(base1.hei/2);
if(bulletCounter>=bullets.length){ //resets bullets to first element of array
bulletCounter=0;
}
bullets[bulletCounter] = new Bullet(xloc,yloc); //creates a new Bullet at xloc
bulletCounter++;
}
class Base{
int x;
int y;
int wid;
int hei;
int speed;
Base(){
x=width/2;
wid=100;
hei=50;
y=800-(hei/2);
speed=5;
}
void display(){
rectMode(CENTER);
stroke(0);
strokeWeight(1);
fill(0);
rect(x,y,wid,hei);
}
void drive(){
if(mouseX>x){
x+=speed;
}
if(mouseX<x){
x-=speed;
}
}
}
class Bullet{
int x;
int y;
int speed;
int vol;
Bullet(int a,int b){
x=a;
y=b;
speed=5;
vol=3;
}
void display(){
fill(0);
rectMode(CENTER);
rect(x,y,vol,vol*2); //displays bullet
}
void fire(){
y=y-speed; //moves bullet
if(mousePressed){ //ensures bullets start moving upon mousePress.
speed=5;
}
}
boolean intersect(Ship ship){ //checks for bullet hitting ship
I am fairly new to programming so I don't know the finer points unfortunately. I am working on creating a game with targets and arrows. Very simply put, it decides the speed with how far you pull the mouse after your mousePress. With collisions on the targets, this unfortunately leads to some issues. The arrows are just simple lines but I am using the front point of the arrow to determine collisions and if the speed is greater than the window for hitting the target, it simply jumps over the target and does doesn't activate the breaking of the target. Is there any way to say if any part of the line touches it, to break the target?
I have included the code below in case you feel it would help.
Arrow[]arrows = new Arrow[1];
Target[]targets = new Target[1];
Timer timer;
float mX1=-1;
float mX2=-1;
float mY1=-1;
float mY2=-1;
int t=2000;
int timercount=0;
int rounds=5;
int score=0;
void setup(){
size(800,800);
smooth();
arrows[0] = new Arrow(mX1,mY1,mX2,mY2);
targets[0] = new Target();
timer = new Timer(t);
timer.start(); //begins timer
}
void draw(){
if(timercount<rounds){ //sets a max number of targets that will spawn before game over
background(255);
if(timer.finished()){
Target t = new Target();
targets = (Target[]) append(targets,t);
timer.start();
timercount+=1;
}
for(int i = 0; i<targets.length; i++){ //displays targets
I am trying to create a from-scratch version of Space Invaders and as a complete beginner, I'm still working on getting collisions down and I know my work probably looks sloppy compared to most. The issue I am having with the program however, is that only half of the arrays follow the position limits I put in. The ships are obviously supposed to reverse direction and drop upon hitting the edge. Instead, only every other one does this, while the ones that don't fly off into infinity. I have gotten it so that the move as one unit instead of each ship responding individually upon reaching the edge, but it still seems to have the same problem with not acknowledging half of the elements. It also seems to be dependent upon the size of the ships. For instance: if the size of the ships is even, the far right element the odd numbered elements won't work properly, but if the size of the ship is an odd number, the even numbered elements won't work. I added the code below. Thanks in advance.
Ship[]ships = new Ship[10];
int s=2;
int y=50;
boolean posCheck;
void setup(){
size(800,800);
smooth();
int d=100; //size of ship (wanted it to be easily changable)
I recently got bored at work and (as I'm only just learning) decided to screw around and make a tic tac toe game. I got partway done when i realized i didn't set up variables to make it easier to place the player symbols (X and O). upon setting it up though i tried to set it like so:
int x1=width/3;
int x2=(width/3)*2;
int y1=height/3;
int y2=(height/3)*2;
I had until then, written out these numbers in full in the line placement for the board, and for the x and y placement of the player symbols. However, upon setting up these variables and switching them in, it destroyed the whole board. It treats width and height as 0 when they are used in variables like this. When I tried finding out if it actively changed the number, I then used the variable as a size for an ellipse to test and it used the previous size instead of the width/2 that i had requested. I set the screen to size(800,800); and also tried using float instead of int to see if it changed anything.
I am just unsure as to why these are not numbers that can be used for anything or why those functions have abandoned me.