Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var canvas: HTMLCanvasElement;
  2. var context: CanvasRenderingContext2D;
  3.  
  4. interface iShape {
  5.     draw(fill: boolean): void;
  6.     x: number;
  7.     y: number;
  8.     color: string;
  9.     lineWidth: number;
  10. }
  11.  
  12. class Point {
  13.     public x: number;
  14.     public y: number;
  15. }
  16.  
  17. enum Direction {
  18.     Left,
  19.     Right,
  20.     Up,
  21.     Down
  22. }
  23.  
  24. var SnakeDirection: Direction = Direction.Right;
  25.  
  26. class cCircle implements iShape {
  27.  
  28.     public x: number;
  29.     public y: number;
  30.     public radius: number;
  31.     public lineWidth: number;
  32.     public color: string;
  33.  
  34.     constructor(x: number, y: number, radius: number, lineWidth: number = 2, color: string = "red") {
  35.         this.x = x;
  36.         this.y = y;
  37.         this.radius = radius;
  38.         this.lineWidth = lineWidth;
  39.         this.color = color;
  40.     }
  41.  
  42.     public draw = (fill: boolean = false): void => { //ten zapis pozwala używać słowa 'this' inaczej działa to źle
  43.         context.save();
  44.         context.beginPath();
  45.         context.strokeStyle = this.color;
  46.         context.lineWidth = this.lineWidth;
  47.         context.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
  48.  
  49.         context.stroke();
  50.  
  51.         if (fill) {
  52.             context.fillStyle = this.color;
  53.             context.fill();
  54.         }
  55.  
  56.         context.restore();
  57.     }
  58.  
  59. }
  60.  
  61. class SnakeSegment {
  62.     private position: Point;
  63.     private circle: cCircle;
  64.  
  65.     constructor(position: Point) {
  66.         this.position = position;
  67.         this.circle = new cCircle(position.x, position.y, 10, 2, "red");
  68.         this.circle.draw(true);
  69.     }
  70.  
  71.     public moveTo = (position: Point) => {
  72.  
  73.         if (SnakeDirection === Direction.Left) {
  74.             this.circle.x--;
  75.         }
  76.         else if (SnakeDirection === Direction.Right) {
  77.             this.circle.x++;
  78.         }
  79.         else if (SnakeDirection === Direction.Down) {
  80.             this.circle.y++;
  81.         }
  82.         else if (SnakeDirection === Direction.Up) {
  83.             this.circle.y--;
  84.         }
  85.        
  86.         this.circle.draw(true);
  87.     }
  88.  
  89.     private clearLastPosition() {
  90.  
  91.     }
  92. }
  93.  
  94. class Snake
  95. {
  96.     private segmentsCount: number = 0;
  97. }
  98.  
  99. var circle1: cCircle = new cCircle(200, 300, 50);
  100. var circle2: cCircle = new cCircle(400, 550, 150, 5, "blue");
  101.  
  102. var point: Point = new Point();
  103. point.x = 100;
  104. point.y = 200;
  105.  
  106. var snake: SnakeSegment;
  107.  
  108. function gameLoop() {
  109.     requestAnimationFrame(gameLoop);
  110.     drawEmptyGameBorder();
  111.     snake.moveTo(point);
  112.     circle1.draw();
  113.     circle2.draw();
  114.    
  115.  
  116.  
  117. }
  118.  
  119. document.addEventListener('keydown', function(event) {
  120.     if (event.keyCode === 37) {
  121.         SnakeDirection = Direction.Left;
  122.     }
  123.     else if (event.keyCode === 39) {
  124.         SnakeDirection = Direction.Right;
  125.     }
  126.     else if (event.keyCode === 38) {
  127.         SnakeDirection = Direction.Up;
  128.     }
  129.     else if (event.keyCode === 40) {
  130.         SnakeDirection = Direction.Down;
  131.     }
  132. });
  133.  
  134. function drawEmptyGameBorder() {
  135.  
  136.     //draw white rect
  137.     context.fillStyle = "white";
  138.     context.fillRect(0, 0, 1280, 720);
  139.  
  140.     //draw border around rect
  141.     context.beginPath();
  142.  
  143.     context.lineWidth = 10;
  144.     context.moveTo(0, 0);
  145.     context.lineTo(1280, 0);
  146.     context.lineTo(1280, 720);
  147.     context.lineTo(0, 720);
  148.     context.lineTo(0, 0);
  149.  
  150.     context.strokeStyle = "red";
  151.     context.stroke();
  152. }
  153.  
  154. window.onload = () => {
  155.     canvas = document.getElementById("cnvs") as HTMLCanvasElement ;
  156.     context = canvas.getContext("2d");
  157.     snake = new SnakeSegment(point);
  158.     gameLoop();
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement