Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. W = 800;
  2. H = 600;
  3.  
  4. fps = 60;
  5.  
  6. newFrame = (new Date).getTime()
  7.  
  8. let enemies = [];
  9. enemyLimit = 15;
  10.  
  11. function setup() {
  12. frameRate(fps);
  13. createCanvas(W, H);
  14.  
  15. windowResized();
  16.  
  17.  
  18.  
  19. angleMode(DEGREES);
  20.  
  21. startGame();
  22. }
  23.  
  24. function draw() {
  25. strokeWeight(0);
  26. background(bg);
  27.  
  28. lastFrame = newFrame
  29. newFrame = (new Date).getTime()
  30.  
  31. if (alive) {
  32. translate(W / 2 - player.x, H / 2 - player.y);
  33.  
  34. c = 0;
  35. for (e of enemies) {
  36. e.update();
  37. e.display();
  38. distance = (((e.x - player.x) ** 2) + ((e.y - player.y) ** 2)) ** 0.5
  39. if (distance >= (((W * 2) ** 2) + ((H * 2) ** 2)) ** 0.5) {
  40. enemies.splice(c, 1);
  41. enemies.push(new enemy);
  42. }
  43. c++;
  44. }
  45.  
  46. player.trailCounter = 0;
  47. for (t of player.trail) {
  48. t.display();
  49. if (t.ttl <= 0) {
  50. player.trail.splice(this.trailCounter, 1);
  51. }
  52. player.trailCounter++;
  53. }
  54.  
  55. for (b of player.bullets) {
  56. b.update();
  57. b.display();
  58. }
  59.  
  60. translate(-(W / 2 - player.x), -(H / 2 - player.y))
  61.  
  62. player.update();
  63. player.display();
  64.  
  65. fill(255)
  66. fps = round(1000 / (newFrame - lastFrame))
  67. text(fps, 10, 20)
  68. text(((player.xSpeed ** 2) + (player.ySpeed ** 2)) ** 0.5, 10, 40)
  69.  
  70. if (player.health <= 0) {
  71. alive = false;
  72. enemies = [];
  73. score = player.score;
  74. player = null;
  75. }
  76. } else {
  77. textSize(72);
  78. textAlign(CENTER);
  79. fill(200, 200, 0);
  80. text("Score: ".concat(score), W / 2, H / 4);
  81. fill(255)
  82. textSize(16)
  83. }
  84. }
  85.  
  86. function startGame() {
  87. bg = [48, 48, 48];
  88. player = new character
  89. c = 0;
  90. while (c < enemyLimit) {
  91. enemies.push(new enemy);
  92. c++;
  93. }
  94. score = 0;
  95. alive = true;
  96. textSize(16)
  97. }
  98.  
  99. function windowResized() {
  100. resizeCanvas(windowWidth, windowHeight);
  101. W = windowWidth, H = windowHeight;
  102. }
  103.  
  104. class particle {
  105. constructor() {
  106. this.x = player.x + round(random(-10, 10)), this.y = player.y + round(random(-10, 10));
  107. this.radius = round(random(2, 20));
  108. this.Initialttl = fps;
  109. this.ttl = this.Initialttl;
  110. this.color = round(random(48, 200));
  111.  
  112. this.xSpeed = -0.01 * player.xSpeed + round(random(-30, 30) / fps);
  113. this.ySpeed = -0.01 * player.ySpeed + round(random(-30, 30) / fps);
  114. }
  115.  
  116. display() {
  117. this.x += this.xSpeed;
  118. this.y += this.ySpeed;
  119.  
  120. fill(this.color, 10, 10);
  121. ellipse(this.x, this.y, 10, 10)
  122. ellipse(this.x, this.y, this.radius * (this.tll / this.Initialttl), this.radius * (this.tll / this.Initialttl));
  123. fill(255, 255, 255);
  124. this.ttl -= 1;
  125. }
  126. }
  127.  
  128. function mouseReleased() {
  129. if (alive) {
  130. player.shooting = true;
  131. }
  132. }
  133.  
  134. function mousePressed() {
  135. if (alive) {
  136. player.shooting = false;
  137. }
  138. }
  139.  
  140. class character {
  141. constructor() {
  142. this.x = 0;
  143. this.y = 0;
  144.  
  145. this.health = 100;
  146. this.score = 0;
  147.  
  148. this.xSpeed = 0;
  149. this.ySpeed = 0;
  150.  
  151. this.speed = 500;
  152.  
  153. this.drag = 0.9;
  154.  
  155. this.orientation = 45;
  156.  
  157. this.width = 40, this.height = 40;
  158.  
  159. this.trail = [];
  160. this.bullets = [];
  161.  
  162. this.bulletSpeed = 2000;
  163. this.fireRate = 30;
  164. this.damage = 20;
  165.  
  166. this.shotCounter = 0;
  167. }
  168.  
  169. update() {
  170. this.shotCounter += 1;
  171.  
  172. if(this.shotCounter >= (fps/this.fireRate) && this.shooting){
  173. this.shoot();
  174. }
  175.  
  176. if (keyIsDown(87)) {
  177. this.xSpeed += sin(this.orientation) * this.speed / fps
  178. this.ySpeed += sin(this.orientation - 90) * this.speed / fps
  179. this.trail.push(new particle)
  180. }
  181.  
  182. if (keyIsDown(83)) {
  183. this.xSpeed -= sin(this.orientation) * this.speed / fps
  184. this.ySpeed -= sin(this.orientation - 90) * this.speed / fps
  185. this.trail.push(new particle)
  186. }
  187.  
  188. this.x += this.xSpeed / fps;
  189. this.y += this.ySpeed / fps;
  190.  
  191. this.xSpeed = this.xSpeed * (1 - (this.drag / fps));
  192. this.ySpeed = this.ySpeed * (1 - (this.drag / fps));
  193.  
  194. this.orientation = aimToMouse(mouseX, mouseY, W / 2, H / 2);
  195. }
  196.  
  197. shoot() {
  198. this.bullets.push(new bullet(this.x, this.y, (sin(this.orientation) * this.bulletSpeed) + this.xSpeed, (sin(this.orientation - 90) * this.bulletSpeed) + this.ySpeed), this.damage)
  199. }
  200.  
  201. display() {
  202. fill(48, 255, 48);
  203. translate(W / 2, H / 2)
  204. rotate(this.orientation)
  205. triangle(-this.width / 2, this.height / 2, 0, -this.height / 2, this.width / 2, this.height / 2);
  206. rotate(-this.orientation)
  207. translate(-W / 2, -H / 2)
  208. }
  209. }
  210.  
  211. class bullet {
  212. constructor(x, y, xSpeed, ySpeed, damage) {
  213. this.x = x;
  214. this.y = y;
  215.  
  216. this.xSpeed = xSpeed;
  217. this.ySpeed = ySpeed;
  218.  
  219. this.damage = damage;
  220.  
  221. this.radius = 8;
  222. }
  223.  
  224. update() {
  225. this.x += this.xSpeed / fps;
  226. this.y += this.ySpeed / fps;
  227.  
  228. this.c = 0
  229.  
  230. for (this.e of enemies) {
  231. if (((((this.x - this.e.x) ** 2) + ((this.y - this.e.y) ** 2)) ** 0.5) < (this.radius + this.e.radius) * 0.75) {
  232. this.e.health -= this.damage;
  233. player.bullets.splice(player.bullets.indexOf(this), 1);
  234. }
  235. this.c++;
  236. }
  237. }
  238.  
  239. display() {
  240. ellipse(this.x, this.y, this.radius, this.radius);
  241. }
  242. }
  243.  
  244. class enemy {
  245. constructor() {
  246. this.x = round(random(-W * 2, -W)) * ((round(random(0, 1)) * 2) - 1) + player.x
  247. this.y = round(random(-H * 2, -H)) * ((round(random(0, 1)) * 2) - 1) + player.y
  248.  
  249. this.radius = round(random(50, 250));
  250.  
  251. this.xSpeed = 0;
  252. this.ySpeed = 0;
  253.  
  254. this.health = 100;
  255. this.speed = round(random(400, 500));
  256.  
  257. this.drag = 0.9;
  258. this.playerDist = W;
  259. }
  260.  
  261. update() {
  262.  
  263. if(this.health <= 0){
  264. enemies.splice(enemies.indexOf(this), 1);
  265. }
  266.  
  267. this.playerDist = ((this.x - player.x) ** 2 + (this.y - player.y) ** 2) ** 0.5
  268.  
  269. if (this.radius >= this.playerDist) {
  270. bg = [128, 48, 48]
  271. player.health = 0;
  272. }
  273.  
  274. if (this.x < player.x) {
  275. this.xSpeed += this.speed / fps;
  276. } else {
  277. this.xSpeed -= this.speed / fps;
  278. }
  279.  
  280. if (this.y < player.y) {
  281. this.ySpeed += this.speed / fps;
  282. } else {
  283. this.ySpeed -= this.speed / fps;
  284. }
  285.  
  286. this.x += this.xSpeed / fps;
  287. this.y += this.ySpeed / fps;
  288.  
  289. this.xSpeed = this.xSpeed * (1 - (this.drag / fps));
  290. this.ySpeed = this.ySpeed * (1 - (this.drag / fps));
  291. }
  292.  
  293. display() {
  294. ellipse(this.x, this.y, this.radius, this.radius)
  295. }
  296. }
  297.  
  298. function aimToMouse(x1, y1, x2, y2) {
  299. xDif = x1 - x2
  300. yDif = y1 - y2
  301. angle = 0
  302. if (xDif > 0 && yDif < 0) {
  303. angle = atan(xDif / -yDif)
  304. }
  305. if (xDif >= 0 && yDif >= 0) {
  306. angle = atan(yDif / xDif) + 90
  307. }
  308. if (xDif <= 0 && yDif >= 0) {
  309. angle = atan(-xDif / yDif) + 180
  310. }
  311. if (xDif <= 0 && yDif <= 0) {
  312. angle = atan(yDif / xDif) + 270
  313. }
  314.  
  315. if (xDif == 0 && yDif <= 0) {
  316. angle = 0
  317. }
  318.  
  319. return angle;
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement