Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.17 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Drawing;
  4. using System.Text;
  5. using System.Windows.Forms;
  6.  
  7. partial class MainForm : Form {
  8.  
  9.     public static Bitmap[,] mapa = new Bitmap[12,20];
  10.     public static String[] policka = new string[12];
  11.     public Indy indy;
  12.     public Netopier[] netopiere;
  13.     const int DOLE = 0;
  14.     const int HORE = 1;
  15.     const int VLAVO = 2;
  16.     const int VPRAVO = 3;
  17.     int netFaza = 0;
  18.     public int tehlickyVHre = 0;
  19.     public static int tehlickyZozbierane = 0;
  20.  
  21.     void MainFormLoad(object sender, EventArgs e) {
  22.         StreamReader r = new StreamReader("hrad.txt");
  23.         for (int i = 0; i < 12; i++) {
  24.             String line = r.ReadLine();
  25.             policka[i] = line;
  26.             for (int j = 0; j < 20; j++) {
  27.                 if (line[j].Equals(' ')) {
  28.                     mapa[i,j] = new Bitmap("Volne0.png");
  29.                 }
  30.                 else if (line[j].Equals('_')) {
  31.                     mapa[i,j] = new Bitmap("Volne1.png");
  32.                 }
  33.                 else if (line[j].Equals('@')) {
  34.                     tehlickyVHre++;
  35.                     mapa[i,j] = new Bitmap("Tehla.png");
  36.                 }
  37.                 else if (line[j].Equals('#')) {
  38.                     mapa[i,j] = new Bitmap("Rebrik.png");
  39.                 }
  40.                 else if (line[j].Equals('A')) {
  41.                     mapa[i,j] = new Bitmap("StenaA.png");
  42.                 }
  43.                 else if (line[j].Equals('B')) {
  44.                     mapa[i,j] = new Bitmap("StenaB.png");
  45.                 }
  46.                 else if (line[j].Equals('C')) {
  47.                     mapa[i,j] = new Bitmap("StenaC.png");
  48.                 }
  49.             }
  50.         }
  51.        
  52.         String[] indyXY = r.ReadLine().Split();
  53.         indy = new Indy(Convert.ToInt32(indyXY[0]), Convert.ToInt32(indyXY[1]));
  54.         int batNum = Convert.ToInt32(r.ReadLine());
  55.         netopiere = new Netopier[batNum];
  56.         int count = 0;
  57.         while (!r.EndOfStream) {
  58.             String[] batXY = r.ReadLine().Split();
  59.             netopiere[count] = new Netopier(Convert.ToInt32(batXY[0]), Convert.ToInt32(batXY[1]));
  60.             count++;
  61.         }
  62.     }
  63.     void MainFormPaint(object sender, PaintEventArgs e) {
  64.         netFaza = (netFaza+1)%8;
  65.         for (int i = 0; i < mapa.GetLength(0); i++) {
  66.             for (int j = 0; j < mapa.GetLength(1); j++) {
  67.                 e.Graphics.DrawImage(mapa[i,j], 25*j, 25*i);
  68.             }
  69.         }
  70.         foreach (Netopier net in netopiere) {
  71.             net.Kresli(e.Graphics, netFaza);
  72.         }
  73.         indy.Kresli(e.Graphics, -1);
  74.     }
  75.  
  76.     void MainFormKeyDown(object sender, KeyEventArgs e) {
  77.         int dx = 0;
  78.         int dy = 0;
  79.         int direction = -1;
  80.         switch (e.KeyCode) {
  81.             case Keys.Left:
  82.                 dx = -5;
  83.                 direction = VLAVO;
  84.                 break;
  85.             case Keys.Right:
  86.                 dx = 5;
  87.                 direction = VPRAVO;
  88.                 break;
  89.             case Keys.Up:
  90.                 dy = -5;
  91.                 direction = HORE;
  92.                 break;
  93.             case Keys.Down:
  94.                 dy = 5;
  95.                 direction = DOLE;
  96.                 break;
  97.         }
  98.         if (indy.direction == direction) {
  99.             indy.faza = (indy.faza+1)%6;
  100.         }
  101.         else {
  102.             indy.faza = 0;
  103.             indy.direction = direction;
  104.         }
  105.         indy.Pohni(dx, dy);
  106.         Invalidate();
  107.     }
  108.    
  109.     void Timer1Tick(object sender, EventArgs e) {
  110.         foreach (Netopier net in netopiere) {
  111.             net.Pohni(10, 0);
  112.         }
  113.         Invalidate();
  114.         if (tehlickyVHre == tehlickyZozbierane) {
  115.             timer1.Enabled = false;
  116.             MessageBox.Show("Hura");
  117.         }
  118.     }
  119.    
  120.     public abstract class Postava {
  121.         public int X, Y;
  122.        
  123.         public Postava(int x, int y) {
  124.             X = x;
  125.             Y = y;
  126.         }
  127.        
  128.         public abstract void Pohni(int dx, int dy);
  129.        
  130.         public abstract void Kresli(Graphics g, int f);
  131.     }
  132.    
  133.     public class Indy : Postava {
  134.         Bitmap[,] bmp = new Bitmap[4,6];
  135.         public int faza = 0;
  136.         public int direction = DOLE;
  137.    
  138.         public Indy(int nX, int nY) : base(nX, nY) {
  139.             for (int j = 0; j < bmp.GetLength(1); j++) {
  140.                 bmp[DOLE,j] = new Bitmap("Dole\\" + j + ".png");
  141.                 bmp[HORE,j] = new Bitmap("Hore\\" + j + ".png");
  142.                 bmp[VLAVO,j] = new Bitmap("Vlavo\\" + j + ".png");
  143.                 bmp[VPRAVO,j] = new Bitmap("Vpravo\\" + j + ".png");
  144.             }
  145.         }
  146.        
  147.         public override void Pohni(int dx, int dy) {
  148.             Char pos = policka[(Y+dy)/25][(X+dx)/25];
  149.             if (pos.Equals('_') || pos.Equals('#') || pos.Equals('@')) {
  150.                 X += dx;
  151.                 Y += dy;
  152.             }
  153.             if (pos.Equals('@')) {
  154.                 StringBuilder sb = new StringBuilder(policka[(Y+dy)/25]);
  155.                 sb[(X+dx)/25] = '_';
  156.                 policka[(Y+dy)/25] = sb.ToString();
  157.                 mapa[(Y+dy)/25, (X+dx)/25] = new Bitmap("Volne1.png");
  158.                 tehlickyZozbierane++;
  159.             }
  160.             System.Diagnostics.Debug.WriteLine((Y+dy)/25 + " " + (X+dx)/25);
  161.            
  162.            
  163.         }
  164.        
  165.         public override void Kresli(Graphics g, int f) {
  166.             //System.Diagnostics.Debug.WriteLine(X/25 + " " + Y/25);
  167.             g.DrawImage(bmp[direction, faza], this.X, this.Y);
  168.            
  169.         }
  170.     }
  171.    
  172.     public class Netopier : Postava {
  173.         Bitmap[] fazy = new Bitmap[8];
  174.         int direction = 3;
  175.    
  176.         public Netopier(int nX, int nY) : base(nX, nY) {
  177.             for (int i = 0; i < 8; i++){
  178.                 fazy[i] = new Bitmap("net\\" + i + ".png");
  179.             }
  180.             Random rnd = new Random();
  181.             direction = rnd.Next(0, 2) == 0 ? VPRAVO : VLAVO;
  182.         }
  183.        
  184.         public override void Pohni(int dx, int dy) {
  185.             Char pos = ' ';
  186.             //System.Diagnostics.Debug.WriteLine((X+dx)/25 + " " + Y/25);
  187.             if (direction == VPRAVO) {
  188.                 pos = policka[Y/25][(X+dx)/25];
  189.             }
  190.             else {
  191.                 pos = policka[Y/25][(X-dx)/25];
  192.             }
  193.             if (pos.Equals('A') || pos.Equals('B') || pos.Equals('C')) {
  194.                 direction = direction == VPRAVO ? VLAVO : VPRAVO;
  195.             }
  196.  
  197.             if (direction == VPRAVO) {
  198.                 X += dx;
  199.             }
  200.             else {
  201.                 X -= dx;
  202.             }
  203.         }
  204.        
  205.         public override void Kresli(Graphics g, int f) {
  206.             g.DrawImage(fazy[f], X, Y);
  207.         }
  208.     }
  209.    
  210.    
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement