AssoAndrea

FirstGame

Nov 26th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Aiv.Draw;
  7.  
  8. namespace first_game
  9. {
  10.     class Program
  11.     {
  12.         struct Color
  13.         {
  14.             public byte r;
  15.             public byte g;
  16.             public byte b;
  17.         }
  18.         struct Rect
  19.         {
  20.             public int height;
  21.             public int width;
  22.             public int x;
  23.             public int y;
  24.             public Color color;
  25.         }
  26.         static void PrintPixel(Window window, int x, int y, Color color)
  27.         {
  28.             int index = (y * window.width*3) + (x * 3);
  29.             window.bitmap[index] = color.r;
  30.             window.bitmap[index+1] = color.g;
  31.             window.bitmap[index+2] = color.b;
  32.  
  33.         }
  34.         static void InitColor(out Color color1, out Color color2)
  35.         {
  36.             //color1
  37.             color1.r = 200;
  38.             color1.g = 0;
  39.             color1.b = 0;
  40.             //color2
  41.             color2.r = 0;
  42.             color2.g = 255;
  43.             color2.b = 120;
  44.         }
  45.         static void Clear(Window window)
  46.         {
  47.             for (int i = 0; i < window.bitmap.Length; i++)
  48.             {
  49.                 window.bitmap[i] = 0;
  50.             }
  51.         }
  52.         static void InitRect(out Rect rect, int h, int l,int x, int y, Color color)
  53.         {
  54.             rect.color = color;
  55.             rect.height = h;
  56.             rect.width = l;
  57.             rect.x = x;
  58.             rect.y = y;
  59.             rect.color = color;
  60.         }
  61.         static void PrintRect(Rect rect,int x, int y, Window window)
  62.         {
  63.             HorizzontalLine(window, x, y, rect.width, rect.color);
  64.             HorizzontalLine(window, x, y + rect.height, rect.width, rect.color);
  65.             VerticalLine(window, x, y, rect.height, rect.color);
  66.             VerticalLine(window, x + rect.width, y, rect.height, rect.color);
  67.         }
  68.         static void PrintSolidRect(Window window, Rect rect,int x, int y)
  69.         {
  70.             for (int i = y; i < y + rect.height; i++)
  71.             {
  72.                 HorizzontalLine(window, x, i, rect.width, rect.color);
  73.             }
  74.         }
  75.         static void HorizzontalLine(Window window,int x,int y, int width, Color color)
  76.         {
  77.             for (int i = x; i < x + width; i++)
  78.             {
  79.                 PrintPixel(window, i, y, color);
  80.             }
  81.         }
  82.         static void Main(string[] args)
  83.         {
  84.             Color color1;
  85.             Color color2;
  86.             InitColor(out color1, out color2);
  87.             Rect rect1;
  88.             int h1 = 20;
  89.             int l1 = h1;
  90.             int x1 = 200, y1 = 200;
  91.             InitRect(out rect1, h1, l1, x1, y1, color1);
  92.             Rect rect2;
  93.             int x2 = 100, y2 = 100;
  94.             int h2 = 90;
  95.             int l2 = h2;
  96.             InitRect(out rect2, h2, l2, x2, y2, color2);
  97.  
  98.             Window window = new Window(640, 480, "Game", PixelFormat.RGB);
  99.             KeyCode key1=KeyCode.Z;
  100.             KeyCode key2 = KeyCode.Z;
  101.             while (window.opened)
  102.             {
  103.                 Clear(window);
  104.                 if (!CheckRect1Collsion(window,ref rect1, rect2))
  105.                 {
  106.                     MoveRect1(ref key1,window, ref rect1);
  107.                 }
  108.                 else
  109.                 {
  110.                     DelPrevMove(key1, ref rect1);
  111.                 }
  112.                 if (!CheckRect1Collsion(window, ref rect2, rect1))
  113.                 {
  114.                     MoverRect2(window, ref rect2);
  115.                 }
  116.                 else
  117.                 {
  118.                     DelPrevMove(key2, ref rect2);
  119.                 }
  120.  
  121.                 CheckBorderCollision(window, ref rect1, ref rect2);
  122.                 PrintRect(rect1, rect1.x, rect1.y, window);
  123.                 PrintSolidRect(window, rect2, rect2.x, rect2.y);
  124.                 window.Blit();
  125.             }
  126.         }
  127.  
  128.         static void DelPrevMove(KeyCode key, ref Rect rect)
  129.         {
  130.             switch (key)
  131.             {
  132.                 //rect1 space
  133.                 case KeyCode.W:
  134.                     rect.y++;
  135.                     break;
  136.                 case KeyCode.A:
  137.                     rect.x++;
  138.                     break;
  139.                 case KeyCode.S:
  140.                     rect.y--;
  141.                     break;
  142.                 case KeyCode.D:
  143.                     rect.x--;
  144.                     break;
  145.  
  146.                 // rect2 space
  147.                 case KeyCode.Up:
  148.                     rect.y++;
  149.                     break;
  150.                 case KeyCode.Left:
  151.                     rect.x++;
  152.                     break;
  153.                 case KeyCode.Down:
  154.                     rect.y--;
  155.                     break;
  156.                 case KeyCode.Right:
  157.                     rect.x--;
  158.                     break;
  159.             }
  160.         }
  161.  
  162.         static bool CheckX(Rect rect1, Rect rect2)
  163.         {
  164.             if (((rect1.x > rect2.x && rect1.x < rect2.x + rect2.width) || (rect1.x + rect1.width > rect2.x && rect1.x + rect1.width < rect2.x + rect2.width))
  165.                 || ((rect2.x > rect1.x && rect2.x < rect1.x + rect1.width) || (rect2.x + rect2.width > rect1.x && rect2.x + rect2.width < rect1.x + rect1.width)))
  166.             {
  167.                 return true;
  168.             }
  169.             else return false;
  170.         }
  171.         static bool CheckY(Rect rect1, Rect rect2)
  172.         {
  173.             if (((rect1.y > rect2.y && rect1.y < rect2.y + rect2.height) || (rect1.y + rect1.height > rect2.y && rect1.y + rect1.height < rect2.y + rect2.height))
  174.                 || ((rect2.y > rect1.y && rect2.y < rect1.y + rect1.height) || (rect2.y + rect2.height > rect1.y && rect2.y + rect2.height < rect1.y + rect1.height)))
  175.             {
  176.                 return true;
  177.             }
  178.             else return false;
  179.         }
  180.  
  181.         static bool CheckRect1Collsion(Window window, ref Rect rect1, Rect rect2)
  182.         {
  183.             if (CheckX(rect1, rect2) && CheckY(rect1, rect2))
  184.             {
  185.                 return true;
  186.             }
  187.             return false;
  188.  
  189.         }
  190.  
  191.         static void MoveRect1(ref KeyCode keyCode, Window window, ref Rect rect1)
  192.         {
  193.             if (window.GetKey(KeyCode.W))
  194.             {
  195.                 keyCode = KeyCode.W;
  196.                 rect1.y--;
  197.             }
  198.             if (window.GetKey(KeyCode.A))
  199.             {
  200.                 keyCode = KeyCode.A;
  201.                 rect1.x--;
  202.             }
  203.             if (window.GetKey(KeyCode.S))
  204.             {
  205.                 keyCode = KeyCode.S;
  206.                 rect1.y++;
  207.             }
  208.             if (window.GetKey(KeyCode.D))
  209.             {
  210.                 keyCode = KeyCode.D;
  211.                 rect1.x++;
  212.             }
  213.         }
  214.         static void MoverRect2(Window window, ref Rect rect2)
  215.         {
  216.             if (window.GetKey(KeyCode.Up))
  217.             {
  218.                 rect2.y--;
  219.             }
  220.             if (window.GetKey(KeyCode.Left))
  221.             {
  222.                 rect2.x--;
  223.             }
  224.             if (window.GetKey(KeyCode.Down))
  225.             {
  226.                 rect2.y++;
  227.             }
  228.             if (window.GetKey(KeyCode.Right))
  229.             {
  230.                 rect2.x++;
  231.             }
  232.         }
  233.  
  234.         static void CheckBorderCollision(Window window, ref Rect rect1, ref Rect rect2)
  235.         {
  236.             //rect1 collision
  237.             if (rect1.x <= 0)
  238.             {
  239.                 rect1.x = 0;
  240.             }
  241.             if (rect1.x +rect1.width >= window.width)
  242.             {
  243.                 rect1.x = window.width - rect1.width - 1;
  244.             }
  245.             if (rect1.y<0)
  246.             {
  247.                 rect1.y = 0;
  248.             }
  249.             if (rect1.y + rect1.height>=window.height)
  250.             {
  251.                 rect1.y = window.height-rect1.height-1;
  252.             }
  253.             //rect2 collision
  254.             if (rect2.x <= 0)
  255.             {
  256.                 rect2.x = 0;
  257.             }
  258.             if (rect2.x + rect2.width >= window.width)
  259.             {
  260.                 rect2.x = window.width - rect2.width - 1;
  261.             }
  262.             if (rect2.y < 0)
  263.             {
  264.                 rect2.y = 0;
  265.             }
  266.             if (rect2.y + rect2.height >= window.height)
  267.             {
  268.                 rect2.y = window.height - rect2.height - 1;
  269.             }
  270.         }
  271.  
  272.         static void VerticalLine(Window window, int x, int y, int height, Color color2)
  273.         {
  274.             for (int i = y; i < y + height; i++)
  275.             {
  276.                 PrintPixel(window, x, i, color2);
  277.             }
  278.            
  279.         }
  280.     }
  281. }
Add Comment
Please, Sign In to add comment