/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.D1uF1iZ7IDX/rev.2439
*
* authors:
*
*
*
*
* Peter Garrity
*
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
/* @pjs preload="/static/uploaded_resources/p.1135/space.png"; */
/* @pjs preload="/static/uploaded_resources/p.1135/space2.png"; */
/* @pjs preload="/static/uploaded_resources/p.1135/spaceship.png"; */
/* @pjs preload="/static/uploaded_resources/p.1135/laser.png"; */
/* @pjs preload="/static/uploaded_resources/p.1135/missile.png"; */
/* @pjs preload="/static/uploaded_resources/p.1135/enemy2.png"; */
PImage space = null;
PImage ship = null;
PImage laser = null;
PImage missile = null;
PImage enemy = null;
int pos_x = 200;
int pos_y = 200;
boolean firing_laser = false;
boolean firing_missile = false;
int ray_x = 0;
int ray_y = 0;
float ray_dx = 1.;
float ray_dy = 0.;
int missile_x = 0;
int missile_y = 0;
float missile_dx = 1.;
float missile_dy = 0.;
int bx1 = 0;
int bx2 = 0;
int by = 0;
int bw = 600;
float accel = 1;
float lf = 0;
float mf = 0;
float angle = 0;
float ex = random(600);
float ey = random(350);
float extimer = 0;
float eytimer = 0;
float ex2 = 0;
float ey2 = 0;
void setup() { // this is run once.
noCursor();
imageMode(CENTER);
// set the background color
background(0);
// canvas size (Variable aren't evaluated. Integers only, please.)
size( 600, 350);
bx1 = width / 2;
bx2 = bx1+bw;
by = height/2;
// smooth edges
smooth();
// limit the number of frames per second
frameRate(30);
// set the width of the line.
strokeWeight(12);
ship = loadImage("/static/uploaded_resources/p.1135/spaceship.png");
space = loadImage("/static/uploaded_resources/p.1135/space.png");
space2 = loadImage("/static/uploaded_resources/p.1135/space2.png");
laser = loadImage("/static/uploaded_resources/p.1135/laser.png");
missile = loadImage("/static/uploaded_resources/p.1135/missile.png");
enemy = loadImage("/static/uploaded_resources/p.1135/enemy2.png");
}
void draw() { // this is run repeatedly.
if (keyPressed == true) {
if (key == CODED) {
if (keyCode == UP) {
accel += cos(angle);
}
else if (keyCode == DOWN) {
accel -= cos(angle);
}
}
}
if (keyPressed == true) {
if (key == ' ') {
accel += 3*cos(angle);
}
}
accel = accel*.998;
accel = max(0,min(accel,20));
image(space,bx1,by);
image(space2,bx2,by);
bx1 -= accel;
bx2 -= accel;
if (bx1+bw/2 < 0) {
bx1 = bx2 + bw;
}
if (bx2+bw/2 < 0) {
bx2 = bx1 + bw;
}
if (firing_laser == true) {
ray_x += 20. * ray_dx - accel/2.2;
ray_y += 20. * ray_dy;
pushMatrix();
translate(ray_x,ray_y,0);
rotate(atan2(ray_dy,ray_dx));
image(laser,0,0);
popMatrix();
if (ray_x > width || ray_x < 0 || ray_y > height || ray_y < 0) {
firing_laser = false;
}
}
else if (mousePressed == true) {
if (mouseButton == LEFT) {
lf += 1
firing_laser = true;
ray_x = 300;
ray_y = 175;
ray_dx = cos(angle);
ray_dy = sin(angle);
}
}
if (firing_missile == true) {
missile_x += 10. * missile_dx - accel/2.2;
missile_y += 10. * missile_dy;
pushMatrix();
translate(missile_x,missile_y);
rotate(atan2(missile_dy,missile_dx));
image(missile,0,0);
popMatrix();
if (missile_x > width || missile_x < 0 || missile_y > height || missile_y < 0) {
firing_missile = false;
}
}
else if (mousePressed == true) {
if (mouseButton == RIGHT) {
mf += 1
firing_missile = true;
missile_x = 300;
missile_y = 175;
missile_dx = cos(angle);
missile_dy = sin(angle);
}
}
if (keyPressed == true) {
if (key == CODED) {
if (keyCode == LEFT) {
angle -= .02;
}
else if (keyCode == RIGHT) {
angle += .02;
}
}
}
pos_x = (pos_x +19*mouseX)/20;
pos_y = (pos_y +19*mouseY)/20;
pushMatrix();
translate( 300, 175,0);
rotate(angle);
image(ship,0,0);
popMatrix();
if (extimer >= 90) {
ex2 = random(600);
extimer = 0
} else {
extimer += 1
}
if (eytimer >= 90) {
ey2 = random(350);
eytimer = 0
} else {
eytimer += 1
}
ex = ex + .05 * (ex2 - ex) - accel / 2;
ey = ey + .05 * (ey2 - ey);
image (enemy, ex, ey);
}