Skip to content

Instantly share code, notes, and snippets.

@hanjae-jea
Created August 20, 2020 13:36
Show Gist options
  • Save hanjae-jea/9c3891f11b21ad922d46dda99ee980ae to your computer and use it in GitHub Desktop.
Save hanjae-jea/9c3891f11b21ad922d46dda99ee980ae to your computer and use it in GitHub Desktop.
var play = 1;
var death;
var isDead = false;
var start, respawn, foodCollide, info, back;
var score = 0;
var player = {
x: 300,
y: 200,
speed: 200,
};
var monster = {
x: -20,
y: -20,
speed: -1,
};
var food = {
x: 100,
y: 100,
};
function collideCircleCircle(x, y, d, x2, y2, d2) {
//2d
if (Math.sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2)) <= d / 2 + d2 / 2) {
return true;
}
return false;
}
function collidePointRect(pointX, pointY, x, y, xW, yW) {
//2d
return (
pointX >= x && // right of the left edge AND
pointX <= x + xW && // left of the right edge AND
pointY >= y && // below the top AND
pointY <= y + yW
);
}
function setup() {
createCanvas(600, 400);
play = 1;
}
//-----------------------------------------------
function draw() {
background(255);
stroke(0);
strokeWeight(10);
fill(255);
rect(0, 0, 600, 400);
//this will set the speed of the dot and the player related to the points
player.speed = 20 + score / 4;
monster.speed = 1 + score / 4;
fill("orange");
strokeWeight(1);
if( isDead ) return;
//keeps it's X equal with mouse X
for (var i = 0; i < player.speed; i++) {
//first thing checks to see if the dot dies
death = collideCircleCircle(
monster.x,
monster.y,
40,
player.x,
player.y,
20
);
if (death === true) {
isDead = true;
}
// this is some stuff about the dot.
fill(random(0, 255), random(0, 255), random(0, 255));
ellipse(food.x, food.y, 10, 10);
// if the player hits the food...
foodCollide = collideCircleCircle(
player.x,
player.y,
20,
food.x,
food.y,
10
);
if (foodCollide === true) {
food.x = random(10, 590);
food.y = random(10, 390);
score += 1;
}
//moves the player
if (player.x > mouseX) {
player.x -= 1;
}
if (player.x < mouseX) {
player.x += 1;
}
//keeps it's Y equal with mouse Y
if (player.y > mouseY) {
player.y -= 1;
}
if (player.y < mouseY) {
player.y += 1;
}
}
fill("orange");
textSize(30);
text(score, 10, 30);
ellipse(player.x, player.y, 20, 20);
//the evil dot... moves the monster
for (var q = 0; q < monster.speed; q++) {
if (monster.x > player.x) {
monster.x -= 1;
}
if (monster.x < player.x) {
monster.x += 1;
}
if (monster.y > player.y) {
monster.y -= 1;
}
if (monster.y < player.y) {
monster.y += 1;
}
}
fill("red");
strokeWeight(4);
stroke(163, 31, 31);
ellipse(monster.x, monster.y, 40, 40);
}
function setup() {
// put setup code here
createCanvas(400, 400);
background(20);
}
let t = 0;
function draw() {
// put drawing code here
stroke(255);
strokeWeight(5);
translate(width/2, height/2);
// point(t, 0);
// point(t, sin(t / 10) * 100);
// point(t, sin(t * 2) * 100);
// point(sin(t / 10) * 100, sin(t / 10) * 100);
// point(sin(t / 10) * 100 + sin(t / 20) * 20, cos(t / 10) * 100);
// point(sin(t / 10) * 100 + sin(t / 15) * 20, cos(t / 10) * 100);
t++;
}
function setup() {
// put setup code here
createCanvas(500, 500);
background(20);
}
let t = 0;
function draw() {
// put drawing code here
background(20);
stroke(255);
strokeWeight(5);
translate(width/2, height/2);
//line(x1(t), y1(t), x2(t), y2(t));
for( let i = 0 ; i < 10 ; i ++ ){
line(x1(t+i), y1(t+i), x2(t+i), y2(t+i));
}
t++;
}
function x1(t) {
return sin(t/10)*100+sin(t/5)*20;
}
function y1(t) {
return cos(t/10)*100;
}
function x2(t) {
return sin(t/10)*200+sin(t)*2;
}
function y2(t) {
return cos(t/20)*200+cos(t/12)*20;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment