Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Aiv.Draw;
- namespace first_game
- {
- class Program
- {
- struct Color
- {
- public byte r;
- public byte g;
- public byte b;
- }
- struct Rect
- {
- public int height;
- public int width;
- public int x;
- public int y;
- public Color color;
- }
- static void PrintPixel(Window window, int x, int y, Color color)
- {
- int index = (y * window.width*3) + (x * 3);
- window.bitmap[index] = color.r;
- window.bitmap[index+1] = color.g;
- window.bitmap[index+2] = color.b;
- }
- static void InitColor(out Color color1, out Color color2)
- {
- //color1
- color1.r = 200;
- color1.g = 0;
- color1.b = 0;
- //color2
- color2.r = 0;
- color2.g = 255;
- color2.b = 120;
- }
- static void Clear(Window window)
- {
- for (int i = 0; i < window.bitmap.Length; i++)
- {
- window.bitmap[i] = 0;
- }
- }
- static void InitRect(out Rect rect, int h, int l,int x, int y, Color color)
- {
- rect.color = color;
- rect.height = h;
- rect.width = l;
- rect.x = x;
- rect.y = y;
- rect.color = color;
- }
- static void PrintRect(Rect rect,int x, int y, Window window)
- {
- HorizzontalLine(window, x, y, rect.width, rect.color);
- HorizzontalLine(window, x, y + rect.height, rect.width, rect.color);
- VerticalLine(window, x, y, rect.height, rect.color);
- VerticalLine(window, x + rect.width, y, rect.height, rect.color);
- }
- static void PrintSolidRect(Window window, Rect rect,int x, int y)
- {
- for (int i = y; i < y + rect.height; i++)
- {
- HorizzontalLine(window, x, i, rect.width, rect.color);
- }
- }
- static void HorizzontalLine(Window window,int x,int y, int width, Color color)
- {
- for (int i = x; i < x + width; i++)
- {
- PrintPixel(window, i, y, color);
- }
- }
- static void Main(string[] args)
- {
- Color color1;
- Color color2;
- InitColor(out color1, out color2);
- Rect rect1;
- int h1 = 20;
- int l1 = h1;
- int x1 = 200, y1 = 200;
- InitRect(out rect1, h1, l1, x1, y1, color1);
- Rect rect2;
- int x2 = 100, y2 = 100;
- int h2 = 90;
- int l2 = h2;
- InitRect(out rect2, h2, l2, x2, y2, color2);
- Window window = new Window(640, 480, "Game", PixelFormat.RGB);
- KeyCode key1=KeyCode.Z;
- KeyCode key2 = KeyCode.Z;
- while (window.opened)
- {
- Clear(window);
- if (!CheckRect1Collsion(window,ref rect1, rect2))
- {
- MoveRect1(ref key1,window, ref rect1);
- }
- else
- {
- DelPrevMove(key1, ref rect1);
- }
- if (!CheckRect1Collsion(window, ref rect2, rect1))
- {
- MoverRect2(window, ref rect2);
- }
- else
- {
- DelPrevMove(key2, ref rect2);
- }
- CheckBorderCollision(window, ref rect1, ref rect2);
- PrintRect(rect1, rect1.x, rect1.y, window);
- PrintSolidRect(window, rect2, rect2.x, rect2.y);
- window.Blit();
- }
- }
- static void DelPrevMove(KeyCode key, ref Rect rect)
- {
- switch (key)
- {
- //rect1 space
- case KeyCode.W:
- rect.y++;
- break;
- case KeyCode.A:
- rect.x++;
- break;
- case KeyCode.S:
- rect.y--;
- break;
- case KeyCode.D:
- rect.x--;
- break;
- // rect2 space
- case KeyCode.Up:
- rect.y++;
- break;
- case KeyCode.Left:
- rect.x++;
- break;
- case KeyCode.Down:
- rect.y--;
- break;
- case KeyCode.Right:
- rect.x--;
- break;
- }
- }
- static bool CheckX(Rect rect1, Rect rect2)
- {
- 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))
- || ((rect2.x > rect1.x && rect2.x < rect1.x + rect1.width) || (rect2.x + rect2.width > rect1.x && rect2.x + rect2.width < rect1.x + rect1.width)))
- {
- return true;
- }
- else return false;
- }
- static bool CheckY(Rect rect1, Rect rect2)
- {
- 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))
- || ((rect2.y > rect1.y && rect2.y < rect1.y + rect1.height) || (rect2.y + rect2.height > rect1.y && rect2.y + rect2.height < rect1.y + rect1.height)))
- {
- return true;
- }
- else return false;
- }
- static bool CheckRect1Collsion(Window window, ref Rect rect1, Rect rect2)
- {
- if (CheckX(rect1, rect2) && CheckY(rect1, rect2))
- {
- return true;
- }
- return false;
- }
- static void MoveRect1(ref KeyCode keyCode, Window window, ref Rect rect1)
- {
- if (window.GetKey(KeyCode.W))
- {
- keyCode = KeyCode.W;
- rect1.y--;
- }
- if (window.GetKey(KeyCode.A))
- {
- keyCode = KeyCode.A;
- rect1.x--;
- }
- if (window.GetKey(KeyCode.S))
- {
- keyCode = KeyCode.S;
- rect1.y++;
- }
- if (window.GetKey(KeyCode.D))
- {
- keyCode = KeyCode.D;
- rect1.x++;
- }
- }
- static void MoverRect2(Window window, ref Rect rect2)
- {
- if (window.GetKey(KeyCode.Up))
- {
- rect2.y--;
- }
- if (window.GetKey(KeyCode.Left))
- {
- rect2.x--;
- }
- if (window.GetKey(KeyCode.Down))
- {
- rect2.y++;
- }
- if (window.GetKey(KeyCode.Right))
- {
- rect2.x++;
- }
- }
- static void CheckBorderCollision(Window window, ref Rect rect1, ref Rect rect2)
- {
- //rect1 collision
- if (rect1.x <= 0)
- {
- rect1.x = 0;
- }
- if (rect1.x +rect1.width >= window.width)
- {
- rect1.x = window.width - rect1.width - 1;
- }
- if (rect1.y<0)
- {
- rect1.y = 0;
- }
- if (rect1.y + rect1.height>=window.height)
- {
- rect1.y = window.height-rect1.height-1;
- }
- //rect2 collision
- if (rect2.x <= 0)
- {
- rect2.x = 0;
- }
- if (rect2.x + rect2.width >= window.width)
- {
- rect2.x = window.width - rect2.width - 1;
- }
- if (rect2.y < 0)
- {
- rect2.y = 0;
- }
- if (rect2.y + rect2.height >= window.height)
- {
- rect2.y = window.height - rect2.height - 1;
- }
- }
- static void VerticalLine(Window window, int x, int y, int height, Color color2)
- {
- for (int i = y; i < y + height; i++)
- {
- PrintPixel(window, x, i, color2);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment