Created
November 18, 2018 11:00
-
-
Save dinarname/36bb4b87adda861c25f05900171413ac to your computer and use it in GitHub Desktop.
without arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MySpaceship S1; | |
AllienSpaceship A1, A2, A3; | |
float x = 30; | |
int counter; | |
void setup() { | |
size(400, 400); | |
counter = 0; | |
S1 = new MySpaceship(); | |
A1 = new AllienSpaceship(); | |
A2 = new AllienSpaceship(); | |
A3 = new AllienSpaceship(); | |
} | |
void draw() { | |
background(0); | |
textSize(10); | |
fill(#42FC1F); | |
text(">>SCORE:" + counter, 10, 20); | |
noStroke(); | |
fill(#42FC1F); | |
rectMode(CORNER); | |
rect(0, height - 2, width, 2); | |
S1.display(); | |
S1.control(); | |
A1.display(); | |
A1.attack(); | |
A2.display(); | |
A2.attack(); | |
A3.display(); | |
A3.attack(); | |
if (mousePressed == true) { | |
S1.laser(); | |
if (S1.hit(A1)) { | |
counter += 1; | |
A1 = new AllienSpaceship(); | |
} | |
if (S1.hit(A2)) { | |
counter += 1; | |
A2 = new AllienSpaceship(); | |
} | |
if (S1.hit(A3)) { | |
counter += 1; | |
A3 = new AllienSpaceship(); | |
} | |
} | |
if (A1.win(S1)) { | |
background(0); | |
textSize(10); | |
fill(#42FC1F); | |
text(">>SCORE:" + counter, width / 2, height / 2); | |
noLoop(); | |
} | |
if (A2.win(S1)) { | |
background(0); | |
textSize(10); | |
fill(#42FC1F); | |
text(">>SCORE:" + counter, width / 2, height / 2); | |
noLoop(); | |
} | |
if (A3.win(S1)) { | |
background(0); | |
textSize(10); | |
fill(#42FC1F); | |
text(">>SCORE:" + counter, width / 2, height / 2); | |
noLoop(); | |
} | |
} | |
class AllienSpaceship { | |
float x, y, w, h, speedY; | |
AllienSpaceship() { | |
w = 30; | |
h = w; | |
x = random(50, width - 50); | |
y = h; | |
speedY = 1; | |
} | |
void display() { | |
rectMode(CENTER); | |
noStroke(); | |
fill(255); | |
rect(x, y, w, 1.6 * h / 5); | |
rect(x, y + h / 5, w / 1.6, h / 5); | |
rect(x, y + 2 * h / 5, w / 1.3, h / 5); | |
fill(#2C0E20); | |
rect(x, y, w / 10, h / 4); | |
} | |
void attack() { | |
x = x + random(- 5, 5); | |
y = y + speedY; | |
} | |
boolean win(MySpaceship other) { | |
if (y > height) return true; | |
if (x + w/2 >= other.x - other.w/2 && x - w/2 <= other.x + other.w/2) { | |
if (y + h/2 >= other.y - other.h/2) { | |
return true; | |
} | |
} | |
return false; | |
} | |
} | |
class MySpaceship { | |
float x, y, w, h, speed; | |
// w = spaceShipWidth; h = spaceShipHeight; | |
// spaceShip consist of 3 parts. | |
MySpaceship() { | |
speed = 5; | |
w = 30; | |
h = w; | |
x = width / 2; | |
y = height - h; | |
} | |
void display() { | |
rectMode(CENTER); | |
noStroke(); | |
fill(#42FC1F); | |
rect(x, y, w, 1.6 * h / 5); | |
rect(x, y - h / 5, w / 1.6, h / 5); | |
rect(x, y - 2 * h / 5, w / 10, h / 5); | |
} | |
void control() { | |
if (keyPressed) { | |
if (key == 'd') { | |
x = x + speed; | |
} else if (key =='a') { | |
x = x - speed; | |
} | |
} | |
if (x + w / 2 > width) { | |
x = - w / 2; | |
} else if (x + w / 2 < 0) { | |
x = width - w / 2; | |
} | |
} | |
void laser() { | |
fill(#FF21A7); | |
rectMode(CORNERS); | |
rect(x, y - h / 1.6, x + w / 20, 0); | |
} | |
boolean hit(AllienSpaceship other) { | |
if (x > other.x - other.w / 2 && x < other.x + other.w / 2) return true; | |
return false; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment