Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- partial class MainForm : Form {
- public static Bitmap[,] mapa = new Bitmap[12,20];
- public static String[] policka = new string[12];
- public Indy indy;
- public Netopier[] netopiere;
- const int DOLE = 0;
- const int HORE = 1;
- const int VLAVO = 2;
- const int VPRAVO = 3;
- int netFaza = 0;
- public int tehlickyVHre = 0;
- public static int tehlickyZozbierane = 0;
- void MainFormLoad(object sender, EventArgs e) {
- StreamReader r = new StreamReader("hrad.txt");
- for (int i = 0; i < 12; i++) {
- String line = r.ReadLine();
- policka[i] = line;
- for (int j = 0; j < 20; j++) {
- if (line[j].Equals(' ')) {
- mapa[i,j] = new Bitmap("Volne0.png");
- }
- else if (line[j].Equals('_')) {
- mapa[i,j] = new Bitmap("Volne1.png");
- }
- else if (line[j].Equals('@')) {
- tehlickyVHre++;
- mapa[i,j] = new Bitmap("Tehla.png");
- }
- else if (line[j].Equals('#')) {
- mapa[i,j] = new Bitmap("Rebrik.png");
- }
- else if (line[j].Equals('A')) {
- mapa[i,j] = new Bitmap("StenaA.png");
- }
- else if (line[j].Equals('B')) {
- mapa[i,j] = new Bitmap("StenaB.png");
- }
- else if (line[j].Equals('C')) {
- mapa[i,j] = new Bitmap("StenaC.png");
- }
- }
- }
- String[] indyXY = r.ReadLine().Split();
- indy = new Indy(Convert.ToInt32(indyXY[0]), Convert.ToInt32(indyXY[1]));
- int batNum = Convert.ToInt32(r.ReadLine());
- netopiere = new Netopier[batNum];
- int count = 0;
- while (!r.EndOfStream) {
- String[] batXY = r.ReadLine().Split();
- netopiere[count] = new Netopier(Convert.ToInt32(batXY[0]), Convert.ToInt32(batXY[1]));
- count++;
- }
- }
- void MainFormPaint(object sender, PaintEventArgs e) {
- netFaza = (netFaza+1)%8;
- for (int i = 0; i < mapa.GetLength(0); i++) {
- for (int j = 0; j < mapa.GetLength(1); j++) {
- e.Graphics.DrawImage(mapa[i,j], 25*j, 25*i);
- }
- }
- foreach (Netopier net in netopiere) {
- net.Kresli(e.Graphics, netFaza);
- }
- indy.Kresli(e.Graphics, -1);
- }
- void MainFormKeyDown(object sender, KeyEventArgs e) {
- int dx = 0;
- int dy = 0;
- int direction = -1;
- switch (e.KeyCode) {
- case Keys.Left:
- dx = -5;
- direction = VLAVO;
- break;
- case Keys.Right:
- dx = 5;
- direction = VPRAVO;
- break;
- case Keys.Up:
- dy = -5;
- direction = HORE;
- break;
- case Keys.Down:
- dy = 5;
- direction = DOLE;
- break;
- }
- if (indy.direction == direction) {
- indy.faza = (indy.faza+1)%6;
- }
- else {
- indy.faza = 0;
- indy.direction = direction;
- }
- indy.Pohni(dx, dy);
- Invalidate();
- }
- void Timer1Tick(object sender, EventArgs e) {
- foreach (Netopier net in netopiere) {
- net.Pohni(10, 0);
- }
- Invalidate();
- if (tehlickyVHre == tehlickyZozbierane) {
- timer1.Enabled = false;
- MessageBox.Show("Hura");
- }
- }
- public abstract class Postava {
- public int X, Y;
- public Postava(int x, int y) {
- X = x;
- Y = y;
- }
- public abstract void Pohni(int dx, int dy);
- public abstract void Kresli(Graphics g, int f);
- }
- public class Indy : Postava {
- Bitmap[,] bmp = new Bitmap[4,6];
- public int faza = 0;
- public int direction = DOLE;
- public Indy(int nX, int nY) : base(nX, nY) {
- for (int j = 0; j < bmp.GetLength(1); j++) {
- bmp[DOLE,j] = new Bitmap("Dole\\" + j + ".png");
- bmp[HORE,j] = new Bitmap("Hore\\" + j + ".png");
- bmp[VLAVO,j] = new Bitmap("Vlavo\\" + j + ".png");
- bmp[VPRAVO,j] = new Bitmap("Vpravo\\" + j + ".png");
- }
- }
- public override void Pohni(int dx, int dy) {
- Char pos = policka[(Y+dy)/25][(X+dx)/25];
- if (pos.Equals('_') || pos.Equals('#') || pos.Equals('@')) {
- X += dx;
- Y += dy;
- }
- if (pos.Equals('@')) {
- StringBuilder sb = new StringBuilder(policka[(Y+dy)/25]);
- sb[(X+dx)/25] = '_';
- policka[(Y+dy)/25] = sb.ToString();
- mapa[(Y+dy)/25, (X+dx)/25] = new Bitmap("Volne1.png");
- tehlickyZozbierane++;
- }
- System.Diagnostics.Debug.WriteLine((Y+dy)/25 + " " + (X+dx)/25);
- }
- public override void Kresli(Graphics g, int f) {
- //System.Diagnostics.Debug.WriteLine(X/25 + " " + Y/25);
- g.DrawImage(bmp[direction, faza], this.X, this.Y);
- }
- }
- public class Netopier : Postava {
- Bitmap[] fazy = new Bitmap[8];
- int direction = 3;
- public Netopier(int nX, int nY) : base(nX, nY) {
- for (int i = 0; i < 8; i++){
- fazy[i] = new Bitmap("net\\" + i + ".png");
- }
- Random rnd = new Random();
- direction = rnd.Next(0, 2) == 0 ? VPRAVO : VLAVO;
- }
- public override void Pohni(int dx, int dy) {
- Char pos = ' ';
- //System.Diagnostics.Debug.WriteLine((X+dx)/25 + " " + Y/25);
- if (direction == VPRAVO) {
- pos = policka[Y/25][(X+dx)/25];
- }
- else {
- pos = policka[Y/25][(X-dx)/25];
- }
- if (pos.Equals('A') || pos.Equals('B') || pos.Equals('C')) {
- direction = direction == VPRAVO ? VLAVO : VPRAVO;
- }
- if (direction == VPRAVO) {
- X += dx;
- }
- else {
- X -= dx;
- }
- }
- public override void Kresli(Graphics g, int f) {
- g.DrawImage(fazy[f], X, Y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement