/* Game playing is often either strategic or active. For any "shooting"
game, several things must be maintained. The x and y positions of any
objects being shot or drawn must be saved (and updated) for each iteration
of the main draw loop. To make a bullet move, store the initial x,y value
of the bullet. Update this for each new screen displayed (or every 10th
screen displayed) - based on desired display speed - see sample below.
Arrays are used to store these changing x and y values. Often many bullets
are active on the screen at a given moment. Keep the x,y values in two
arrays (of floats).s
float [] bulletX = new float [10]; // creates an array of 10 x values
float [] bulletY = new float [10]; // creates an array of 10 y values
You can use a keypress to trigger a "shot". The shot operation simply
establishes the first position of the bullet. After it has been positioned
its new position is updated independently of the gun or shooter.
*/
int max=10; // max number of bullets
int siz=8; // size of the bullets
int speed=20;
PImage pic;
PImage bullet;
PImage target;
PImage targetDead;
float [] targets = new float [3];
float [] bulletX = new float [max];
float [] bulletY = new float [max];
int [] show = {0,0,0};
float [] [] bullets = new float [max][2];
void setup() {
size(1000, 400); // sets the screen size
pic = loadImage ("pistol.gif");
target = loadImage ("pigeonGood.gif");
targetDead = loadImage ("pigeonDead2.gif");
textSize (32);
smooth(); // makes the graphics smooth
// setup targets
targets [0] = random (0,130); // y axis value for targets
targets [1] = random (140,270);
targets [2] = random (280,380);
for (int i=0; i<max; i++) {
bulletX[i]=-1; // -1 indicates a non-active bullet
}
frameRate (30); // 15 frames per second in display
}
void draw() {
background (0);
for (int i=0; i<max; i++) { // update the bullets already shot
if (bulletX[i] < width && bulletX[i] != -1) {
bulletX[i]+=speed; // move bullet 8 pixels right
ellipse (bulletX[i], bulletY[i], siz, siz); // draw updated bullet
if (bulletX [i] > 920 ) { // within blowup range on the x
if (bulletY[i] > targets[0] && bulletY[i] < targets[0] +50) {
show[0]=1;
}
if (bulletY[i] > targets[1] && bulletY[i] < targets[1] +50) {
// blow up!!!
targets [1] = 600;
}
if (bulletY[i] > targets[2] && bulletY[i] < targets[2] +50) {
// blow up!!!
targets [2] = 600;
}
}
if (targets[0]==600 && targets [1]==600 && targets [2]==600) {
exit();
}
} else {
bulletX[i]=-1;
}
}
image (pic,mouseX, mouseY,150,120);
if (show[0] == 0 ) {
image (target,900,targets[0],50,50);
} else {
image (targetDead,900,targets[0],50,50);
show[0]++;
}
if (show[0] > 10) {
targets[0] = 600;
}
image (target,900,targets[1],50,50);
image (target,900,targets[2],50,50);
// look in the keypressed area for shooting initialization code
if (mouseX > (width - 10)) {
exit(); // move the mouse to right edge to quit
}
}
/* Your task: 1. Using your knowledge of PImages, position some kind
of ship or image (it could be a hand with a pointed finger) on the
screen. Using a gif will allow you to use an irregular shape and
have one colour transparent.
2. (simple task)Place three small targets at the right edge of the screen.
When the x,y value of one of your bullets intersects the target, cause
target to explode (show an expolsion picture and don't draw the target
any more. You can include an exposion sound if you'd like).
3. (harder) Place random targets that are falling from the top of the screen.
You will need x,y arrays for these as well. Your gun will blow up
if one of the falling items hits you (the x,y's intersect). The targets
will blow up and disappear if you hit the target with a bullet.
You may choose to keep score when you hit a target successfully.
Bullet speed can be made faster by increasing the speed variable
To make things really complicated, you can have levels...
*/
void keyPressed() { // an event triggered to the system when a key is hit
if (key == CODED) { // a coded key is special like an --> key
if (keyCode == UP) {
// do something
}
}
if (key == 's') { //shoot
if (mouseX < width / 4) { // only let shots be made from left side
for (int i=0; i < max; i++) {
if (bulletX[i]==-1) {
bulletX [i] = mouseX+130; // use the mouse as a gun
bulletY [i] = mouseY+23;
ellipse (bulletX[i], bulletY[i], siz, siz);
break; // leave loop once you find a -1 in the array
}
}
}
}
}
//void blowUp (float x, float y) {
//}
game, several things must be maintained. The x and y positions of any
objects being shot or drawn must be saved (and updated) for each iteration
of the main draw loop. To make a bullet move, store the initial x,y value
of the bullet. Update this for each new screen displayed (or every 10th
screen displayed) - based on desired display speed - see sample below.
Arrays are used to store these changing x and y values. Often many bullets
are active on the screen at a given moment. Keep the x,y values in two
arrays (of floats).s
float [] bulletX = new float [10]; // creates an array of 10 x values
float [] bulletY = new float [10]; // creates an array of 10 y values
You can use a keypress to trigger a "shot". The shot operation simply
establishes the first position of the bullet. After it has been positioned
its new position is updated independently of the gun or shooter.
*/
int max=10; // max number of bullets
int siz=8; // size of the bullets
int speed=20;
PImage pic;
PImage bullet;
PImage target;
PImage targetDead;
float [] targets = new float [3];
float [] bulletX = new float [max];
float [] bulletY = new float [max];
int [] show = {0,0,0};
float [] [] bullets = new float [max][2];
void setup() {
size(1000, 400); // sets the screen size
pic = loadImage ("pistol.gif");
target = loadImage ("pigeonGood.gif");
targetDead = loadImage ("pigeonDead2.gif");
textSize (32);
smooth(); // makes the graphics smooth
// setup targets
targets [0] = random (0,130); // y axis value for targets
targets [1] = random (140,270);
targets [2] = random (280,380);
for (int i=0; i<max; i++) {
bulletX[i]=-1; // -1 indicates a non-active bullet
}
frameRate (30); // 15 frames per second in display
}
void draw() {
background (0);
for (int i=0; i<max; i++) { // update the bullets already shot
if (bulletX[i] < width && bulletX[i] != -1) {
bulletX[i]+=speed; // move bullet 8 pixels right
ellipse (bulletX[i], bulletY[i], siz, siz); // draw updated bullet
if (bulletX [i] > 920 ) { // within blowup range on the x
if (bulletY[i] > targets[0] && bulletY[i] < targets[0] +50) {
show[0]=1;
}
if (bulletY[i] > targets[1] && bulletY[i] < targets[1] +50) {
// blow up!!!
targets [1] = 600;
}
if (bulletY[i] > targets[2] && bulletY[i] < targets[2] +50) {
// blow up!!!
targets [2] = 600;
}
}
if (targets[0]==600 && targets [1]==600 && targets [2]==600) {
exit();
}
} else {
bulletX[i]=-1;
}
}
image (pic,mouseX, mouseY,150,120);
if (show[0] == 0 ) {
image (target,900,targets[0],50,50);
} else {
image (targetDead,900,targets[0],50,50);
show[0]++;
}
if (show[0] > 10) {
targets[0] = 600;
}
image (target,900,targets[1],50,50);
image (target,900,targets[2],50,50);
// look in the keypressed area for shooting initialization code
if (mouseX > (width - 10)) {
exit(); // move the mouse to right edge to quit
}
}
/* Your task: 1. Using your knowledge of PImages, position some kind
of ship or image (it could be a hand with a pointed finger) on the
screen. Using a gif will allow you to use an irregular shape and
have one colour transparent.
2. (simple task)Place three small targets at the right edge of the screen.
When the x,y value of one of your bullets intersects the target, cause
target to explode (show an expolsion picture and don't draw the target
any more. You can include an exposion sound if you'd like).
3. (harder) Place random targets that are falling from the top of the screen.
You will need x,y arrays for these as well. Your gun will blow up
if one of the falling items hits you (the x,y's intersect). The targets
will blow up and disappear if you hit the target with a bullet.
You may choose to keep score when you hit a target successfully.
Bullet speed can be made faster by increasing the speed variable
To make things really complicated, you can have levels...
*/
void keyPressed() { // an event triggered to the system when a key is hit
if (key == CODED) { // a coded key is special like an --> key
if (keyCode == UP) {
// do something
}
}
if (key == 's') { //shoot
if (mouseX < width / 4) { // only let shots be made from left side
for (int i=0; i < max; i++) {
if (bulletX[i]==-1) {
bulletX [i] = mouseX+130; // use the mouse as a gun
bulletY [i] = mouseY+23;
ellipse (bulletX[i], bulletY[i], siz, siz);
break; // leave loop once you find a -1 in the array
}
}
}
}
}
//void blowUp (float x, float y) {
//}

