Guest User

TIC-80 code

a guest
Feb 21st, 2021
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // title:  space invaders
  2. // author: nooh alavi
  3. // desc:   pls subscribe <3
  4. // script: js
  5.  
  6. var ship;
  7.  
  8. var bullets = [];
  9. var enemies = [];
  10.  
  11. var spawnRate = 5; //seconds before enemies spawn
  12. var eTime = 0;
  13. var secs = 0;
  14.  
  15. //
  16.  
  17. function Bullet(_x, _y, _e) {
  18.     this.x = _x;
  19.     this.y = _y;
  20.     this.isEnemyBullet = _e;
  21.    
  22.     this.render = function() {
  23.         if (this.isEnemyBullet) {
  24.             spr(4, this.x, this.y, 0);
  25.         } else {
  26.             spr(2, this.x, this.y, 0);
  27.         }
  28.     }
  29.    
  30.     this.update = function() {
  31.         if (this.isEnemyBullet) {
  32.             this.y++;
  33.         } else {
  34.             this.y--;
  35.         }
  36.     }
  37.    
  38.     this.isCollidingWith = function(obj) {
  39.         return (this.x + 8 > obj.x && this.y + 8 > obj.y && obj.x + 8 > this.x && obj.y + 8 > this.y    );
  40.     }
  41. }
  42.  
  43. function Enemy(_x, _y) {
  44.     this.x = _x;
  45.     this.y = _y;
  46.     this.dir = 1;
  47.    
  48.     this.render = function() {
  49.         spr(3, this.x, this.y, 0);
  50.     }
  51.    
  52.     this.update = function() {
  53.         this.x += this.dir;
  54.        
  55.         if (this.x < 0) {
  56.             this.x = 0;
  57.             this.y += 8;
  58.             this.dir = 1;
  59.         } else if (this.x > 240-8) {
  60.             this.x = 240-8;
  61.             this.y += 8;
  62.             this.dir = -1;
  63.         }
  64.        
  65.         if (Math.random() <= 0.01) {
  66.             this.shoot();
  67.         }
  68.     }  
  69.    
  70.     this.shoot = function() {
  71.         bullets.push(new Bullet(this.x, this.y, true));
  72.     }
  73. }
  74.  
  75. //
  76.  
  77. function init() {
  78.     ship = {
  79.         x: 120,
  80.         y: 100
  81.     }
  82.  
  83.     bullets = [];
  84.     enemies = [];
  85.  
  86.     var secs = 0;
  87. }
  88.  
  89. function shoot(x, y, e) {  
  90.     bullets.push(new Bullet(x, y, e));
  91. }
  92.  
  93. function spawnEnemies() {
  94.     for (var i = 0; i < 240/8; i++) {
  95.         enemies.push(new Enemy(i * 8, 0));
  96.     }
  97. }
  98.  
  99. function update() {
  100.  
  101.     eTime++;
  102.  
  103.     if (key(23)) {
  104.         ship.y--;
  105.     } else if (key(19)) {
  106.         ship.y++;
  107.     }
  108.     if (key(1)) {
  109.         ship.x--;
  110.     } else if (key(4)) {
  111.         ship.x++;
  112.     }
  113.    
  114.     if (keyp(48)) {
  115.         shoot(ship.x, ship.y, false);
  116.     }
  117.    
  118.     if (ship.x + 8 > 240) {
  119.         ship.x = 240 - 8;
  120.     } if (ship.x < 0) {
  121.         ship.x = 0;
  122.     } if (ship.y < 0) {
  123.         ship.y = 0;
  124.     } if (ship.y + 8 > 136) {
  125.         ship.y = 136 - 8;
  126.     }
  127.    
  128.     for (var i =0; i < bullets.length; i++) {
  129.         bullets[i].update();
  130.         for (var j = 0; j < enemies.length; j++) {
  131.             if (bullets[i].isCollidingWith(enemies[j]) && !bullets[i].isEnemyBullet) {
  132.                 enemies.splice(j, i)
  133.             }
  134.             if (bullets[i].isCollidingWith(ship)) {
  135.                 init();
  136.             }
  137.         }
  138.         if (bullets[i].y < 0 || bullets[i].y > 240) {
  139.             bullets.splice(i, 1);
  140.         }
  141.     }
  142.    
  143.     for (var i =0; i < enemies.length; i++) {
  144.         enemies[i].update();
  145.     }
  146.    
  147.     secs = (eTime/60);
  148.    
  149.     if (secs % spawnRate == 0.5) {
  150.         spawnEnemies();
  151.     }
  152. }
  153.  
  154. function render() {
  155.     cls();
  156.     map(0);
  157.     spr(1, ship.x, ship.y, 0);
  158.    
  159.     for (var i=0; i < bullets.length; i++) {
  160.         bullets[i].render();
  161.     }
  162.     for (var i=0; i < enemies.length; i++) {
  163.         enemies[i].render();
  164.     }
  165. }
  166.  
  167. init()
  168. function TIC() {
  169.     update();
  170.     render();
  171. }
Advertisement
Add Comment
Please, Sign In to add comment