“Saucer” by Trevor
https://openprocessing.org/sketch/839197
License CreativeCommons Attribution ShareAlike
https://creativecommons.org/licenses/by-sa/3.0
{{filePath}}
{{width}} x {{height}}
Report Sketch
Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
WASD/Arrow Keys, Enter to restart after death
CC Attribution ShareAlike
Saucer
xxxxxxxxxx
/*
Note to self: clean this code up...
*/
let wW;
let wH;
let x;
let y;
let ship; //ship
let ast = []; //asteroid array
let buttons = [];
let counter = 0; //asteroid spawning counter
let score = 0; //starting score
let invul = 0; //invulnerability
let flash = false; //toggles invulnerability display
let toggleFlash = false; //toggles invulnerability display states
let invulFlashes = 1; //currently deprecated as lag
let i = 0;
let notifySpawnInc = false;
let notifTimer = 120;
let timer = 60;
let seconds = 0;
let second = 0;
let minutes = 0;
let minute = 0;
let hours = 0;
let hour = 0;
let title = false;
let txtNorm;
let txtBig;
let gamestate = title;
let trueTime = 0;
let deltaTrueTime = 0;
let trueSeconds = 0;
let trueSecond = 0;
let trueMinutes = 0;
let trueMinute = 0;
let trueHours = 0;
let trueHour = 0;
/*
EDITABLE VARIABLES
*/
let maxTickSpawn = 10; //Asteroid spawn rate
let cooldown = 15; //Cooldown before gas recharge
let hpMax = 3; //Health
let maxGas = 20; //Gas ticks
let maxInvul = 40; //Invulnerability ticks
let scoreAdd = 100; //Score to add after scoreTime is up
let scoreTime = 60; //timer to add to score in ticks (60 ticks per second)
let spawnIncrease = scoreAdd * 10; //Interval of time that will tick the tickSpawn down by 1, measured by when score surpasses it
let notifLinger = 120; //time in ticks per second that the top spawn increase notification will stay
let hp = hpMax;
let tickSpawn = maxTickSpawn;
let time = scoreTime; //Ticks per time score
function setup() {
wW = windowWidth;
wH = windowHeight;
txtNorm = wH / 15;
txtBig = wH / 7.5;
createCanvas(windowWidth, windowHeight);
background(10, 10, 15); //Black Russian
x = width / 2;
y = height / 2;
textAlign(CENTER, CENTER);
util = new Util();
ship = new Ship(); //ship constructor
title = new Title(); //title constructor
ast.push(new Asteroid()); //asteroid constructor being pushed through an array
buttons = [
new Button(wW / 5, wH / 2, wW / 5, wH / 10, 'Easy', 'easy'),
new Button(wW / 2, wH / 2, wW / 5, wH / 10, 'Normal', 'normal'),
new Button(wW / 1.25, wH / 2, wW / 5, wH / 10, 'Hard', 'hard'),
new Button(wW / 1.125, wH / 1.05, wW / 5, wH / 10, 'HELL', 'hell', true)
];
gamestate = 'title';
}
function draw() {
clear();
noStroke(); //nobody wants outlines
textSize(txtNorm);
//text('Invulnerability = ' + invul, wW / 21, wH / 5); //invulnerability counter
if (gamestate == 'title') {
title.display();
title.functionality();
}
if (hp <= 0) gamestate = 'dead'; //if HP is 0 or less, dead is true
if (gamestate == 'dead') { //Death screen
fill(10, 10, 15);
rect(0, 0, wW, wH);
fill(255, 0, 0);
textSize(txtBig);
textStyle(BOLD);
text('GAME OVER', wW / 2, wH / 2);
textSize(txtNorm);
textStyle(NORMAL);
text('Score: ' + score, wW / 2, wH / 1.5);
text('Time Alive: ' + hour + ':' + minute + ':' + second, wW / 2, wH / 1.25);
textSize(wH / 30);
text('Real Time Alive: ' + trueHour + ':' + trueMinute + ':' + trueSecond, wW / 2, wH / 1.15);
if (keyIsDown(13) || keyIsDown(32)) {
util.restart();
ship.y = wH / 2;
gamestate = 'inplay';
}
if (keyIsDown(192)) {
util.restart();
ship.y = wH / 1.5;
gamestate = 'title';
}
}
ship.move(); //moves ship
ship.display(); //displays ship
for (let i = 0; i < ast.length; i++) { //for each asteroid
if (gamestate == 'inplay') { //when not dead
ast[i].move(); //move
ast[i].display(); //display
} else ast.length = 0; //else kill them all
}
counter++; //add 1 to counter
if (counter >= tickSpawn) { //when counter is above tickSpawn
counter = 0; //reset counter
ast.push(new Asteroid()); //create asteroid
}
if (gamestate == 'inplay') { //if not dead
fill(255);
textSize(txtNorm);
textAlign(LEFT, CENTER);
text('Score: ' + score, wW / 50, wH / 21); //display score
text('HP: ' + hp, wW / 50, wH / 10); //display health
text('Time Alive: ' + hour + ':' + minute + ':' + second, wW / 50, wH / 1.1);
textSize(wH / 30);
text('Real Time Alive: ' + trueHour + ':' + trueMinute + ':' + trueSecond, wW / 50, wH / 1.04);
textAlign(CENTER, CENTER);
trueTime = millis() - deltaTrueTime; //Time since start (see Utilities)
if (trueTime >= 1000) {
trueSeconds++;
deltaTrueTime = millis();
}
if (trueSeconds >= 60) {
trueMinutes++;
trueSeconds = 0;
}
if (trueMinutes >= 60) {
trueHours++;
trueMinutes = 0;
}
if (trueSeconds <= 9) {
trueSecond = '0' + trueSeconds;
} else trueSecond = trueSeconds;
if (trueMinutes <= 9) {
trueMinute = '0' + trueMinutes;
} else trueMinute = trueMinutes;
if (trueHours <= 9) {
trueHour = '0' + trueHours;
} else trueHour = trueHours;
//Timer
timer--;
if (timer <= 0) {
seconds++;
timer = 60;
}
if (seconds >= 60) {
minute++;
seconds = 0;
}
if (minutes >= 60) {
hour++;
minutes = 0;
}
if (seconds <= 9) {
second = '0' + seconds;
} else second = seconds;
if (minutes <= 9) {
minute = '0' + minutes;
} else minute = minutes;
if (hours <= 9) {
hour = '0' + hours;
} else hour = hours;
time--; //subtract from time
if (time <= 0) { //when time isn't below 0
score += scoreAdd; //add to the score
time = scoreTime; //set time equal to scoreTime
}
}
if (score >= spawnIncrease && notifySpawnInc == false && tickSpawn > 0) { //if score is above the interval for increasing spawnrate
spawnIncrease += spawnIncrease; //add interval to interval
tickSpawn--; //increase spawnrate
score += scoreAdd / 10; //increase score
notifySpawnInc = true;
}
if (notifySpawnInc == true) {
textSize(txtNorm);
fill(255);
text('The Spawn Rate Has Increased!', wW / 2, wH / 15);
if (notifTimer >= 0) {
notifTimer--;
} else {
notifySpawnInc = false;
notifTimer = notifLinger;
}
}
for (var a of ast) { //for individual asteroid
if ((dist(ship.x, ship.y, a.x, a.y) < ((ship.diameter / 2) + a.size / 2)) && invul <= 0) { //when ship touches asteroid
hp -= 1; //subtract health
invul = maxInvul; //set invulnerability
}
}
invul--; //subtract from invulnerability
if (invul > 0) { //if invulnerability is greater than 0
flash = true;
}
if (invul <= 0) { //if invulnerability is greater than or equal to 0
flash = false;
}
if (flash == true) { //if flash is true
ship.color = 100; //set ship color to gray
// for (i = maxInvul / invulFlashes; i > 0; i--) {
// if (toggleFlash == false) {
// ship.transparency = 255;
// toggleFlash = true;
// }
// }
// for (i = maxInvul / invulFlashes; i > 0; i--) {
// if (toggleFlash == true) {
// ship.transparency = 0;
// toggleFlash = false;
// }
// }
}
if (flash == false) { //if flash is false
ship.transparency = 255; //set ship transparency to opaque
ship.color = 255; //set ship color to white
if (((mouseX > this.x - this.w / 2) && (mouseX < this.x + this.w / 2)) && ((mouseY > this.y - this.h / 2) && (mouseY < this.y + this.h / 2))) {
print(true);
this.transparency = -1;
}
}
}
See More Shortcuts