using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; using System.Threading; namespace SSWProject { public class Game : System.Windows.Forms.Form { private const int POINTS = 54; private const int MAXHEIGHT = 100; private const int MINHEIGHT = 30; private const int HEIGHT = 70; private const int PBWIDTH = 535; private const int PBHEIGHT = 300; private const int POSPLAY1 = 2; private const int POSPLAY2 = 51; private bool initialized = false; private bool playerwon = false; private Container components = null; private System.Windows.Forms.PictureBox pb; private System.Windows.Forms.Button btn_Start; private System.Windows.Forms.Button btn_Powder_Up; private System.Windows.Forms.Button btn_Powder_Dw; private System.Windows.Forms.Button btn_Angle_Up; private System.Windows.Forms.Button btn_Angle_Dw; private System.Windows.Forms.Button btn_fire; private Label lb_Powder; private Label lb_Angle; private Label lb_Powder_t; private Label lb_Angle_t; private Label lb_Player1; private Label lb_Player2; private Label lb_Dam_Player1; private Label lb_Dam_Player2; private Label lb_Wind; private Label lb_Wind_Dir; private int[] landscape = null; private bool[] trees = null; private Random rand = null; private Point[] points = null; private System.Windows.Forms.TextBox tb_info; private Ball ball = null; private Bitmap bmp_alps = null; private Bitmap bmp_desert = null; private Bitmap bmp_canyon = null; private Bitmap bmp_title = null; private Player player1 = null; private Player player2 = null; private Player currplayer = null; private short curr_level = 1; private int wind = 3; private bool wind_dir = true; private bool ball_flight = false; private bool stop = false; private short style; private Thread ball_thread = null; private System.Windows.Forms.ComboBox cmb_level; public Game(){ InitializeComponent(); } protected override void Dispose (bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } //Initialisierung der Komponenten private void InitializeComponent() { this.btn_Start = new System.Windows.Forms.Button(); this.btn_Powder_Up = new System.Windows.Forms.Button(); this.btn_Powder_Dw = new System.Windows.Forms.Button(); this.btn_Angle_Up = new System.Windows.Forms.Button(); this.btn_Angle_Dw = new System.Windows.Forms.Button(); this.btn_fire = new System.Windows.Forms.Button(); this.pb = new System.Windows.Forms.PictureBox(); this.tb_info = new System.Windows.Forms.TextBox(); this.cmb_level = new System.Windows.Forms.ComboBox(); this.lb_Wind_Dir = new Label(); this.lb_Wind = new Label(); this.lb_Player1 = new Label(); this.lb_Player2 = new Label(); this.lb_Dam_Player1 = new Label(); this.lb_Dam_Player2 = new Label(); this.lb_Angle = new Label(); this.lb_Angle_t = new Label(); this.lb_Powder = new Label(); this.lb_Powder_t = new Label(); this.SuspendLayout(); // // lb_Player1 // this.lb_Player1.Location = new Point(20,333); this.lb_Player1.Size = new Size (45, 16); this.lb_Player1.Text = "Player1"; // // lb_Dam_Player1 // this.lb_Dam_Player1.Location = new Point(13,354); this.lb_Dam_Player1.Size = new Size (100, 16); this.lb_Dam_Player1.Text = "Damage:"; // // lb_Player2 // this.lb_Player2.Location = new Point(480,333); this.lb_Player2.Size = new Size (45, 16); this.lb_Player2.Text = "Player2"; // // lb_Dam_Player2 // this.lb_Dam_Player2.Location = new Point(473,354); this.lb_Dam_Player2.Size = new Size (100, 16); this.lb_Dam_Player2.Text = "Damage:"; // // lb_Wind // this.lb_Wind.Location = new Point(380,333); this.lb_Wind.Size = new Size(100,20); this.lb_Wind.Text = "Wind"; // // lb_Wind_text // this.lb_Wind_Dir.Location = new Point(365, 354); this.lb_Wind_Dir.Size = new Size(100 ,16); this.lb_Wind_Dir.Text = "<----0----<<"; // // lb_Angle_t // this.lb_Angle_t.Location = new Point(232,333); this.lb_Angle_t.Size = new Size(80,16); this.lb_Angle_t.Text = "Angle"; // // lb_Angle // this.lb_Angle.Location = new Point(240, 354); this.lb_Angle.Size = new Size(40,16); this.lb_Angle.Text = "Angle"; // // lb_Powder // this.lb_Powder.Location = new Point(140, 354); this.lb_Powder.Size = new Size(40,16); this.lb_Powder.Text = "Powder"; // // lb_Powder_t // this.lb_Powder_t.Location = new Point(130,333); this.lb_Powder_t.Size = new Size(80,16); this.lb_Powder_t.Text = "Powder"; // // btn_Start // this.btn_Start.Location = new System.Drawing.Point(6, 5); this.btn_Start.Name = "btn_Start"; this.btn_Start.Size = new System.Drawing.Size(70, 20); this.btn_Start.TabIndex = 1; this.btn_Start.Text = "New Game"; this.btn_Start.Click += new System.EventHandler(this.StartButtonClick); // // btn_Powder_Up // this.btn_Powder_Up.Location = new System.Drawing.Point(180, 332); this.btn_Powder_Up.Name = "btn_Powder_Up"; this.btn_Powder_Up.Size = new System.Drawing.Size(30, 20); this.btn_Powder_Up.TabIndex = 3; this.btn_Powder_Up.Text = "Up"; this.btn_Powder_Up.Click += new System.EventHandler(this.ButtonClick); // // btn_Powder_Dw // this.btn_Powder_Dw.Location = new System.Drawing.Point(180, 355); this.btn_Powder_Dw.Name = "btn_Powder_Dw"; this.btn_Powder_Dw.Size = new System.Drawing.Size(30, 20); this.btn_Powder_Dw.TabIndex = 4; this.btn_Powder_Dw.Text = "Down"; this.btn_Powder_Dw.Click += new System.EventHandler(this.ButtonClick); // // btn_Angle_Up // this.btn_Angle_Up.Location = new System.Drawing.Point(270, 332); this.btn_Angle_Up.Name = "btn_Angle_Up"; this.btn_Angle_Up.Size = new System.Drawing.Size(30, 20); this.btn_Angle_Up.TabIndex = 5; this.btn_Angle_Up.Text = "Up"; this.btn_Angle_Up.Click += new System.EventHandler(this.ButtonClick); // // btn_Angle_Dw // this.btn_Angle_Dw.Location = new System.Drawing.Point(270, 355); this.btn_Angle_Dw.Name = "btn_Angle_Dw"; this.btn_Angle_Dw.Size = new System.Drawing.Size(30, 20); this.btn_Angle_Dw.TabIndex = 6; this.btn_Angle_Dw.Text = "Down"; this.btn_Angle_Dw.Click += new System.EventHandler(this.ButtonClick); // // btn_fire // this.btn_fire.Location = new System.Drawing.Point(305, 332); this.btn_fire.Name = "btn_fire"; this.btn_fire.Size = new System.Drawing.Size(50, 43); this.btn_fire.TabIndex = 9; this.btn_fire.Text = "Fire"; this.btn_fire.Click += new System.EventHandler(this.ButtonClick); // // pb // this.pb.BackColor = System.Drawing.Color.White; this.pb.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.pb.Location = new System.Drawing.Point(6, 30); this.pb.Name = "pb"; this.pb.Size = new System.Drawing.Size(535, 300); this.pb.TabIndex = 0; this.pb.TabStop = false; this.pb.Paint += new PaintEventHandler(this.OnPaint_Pb); // // tb_info // this.tb_info.Enabled = false; this.tb_info.Location = new System.Drawing.Point(215, 5); this.tb_info.Name = "tb_info"; this.tb_info.Size = new System.Drawing.Size(325, 20); this.tb_info.TabIndex = 2; this.tb_info.Text = ""; // // cmb_level // this.cmb_level.Items.AddRange(new object[] { "Alps", "Desert", "Canyon"}); this.cmb_level.Location = new System.Drawing.Point(85, 5); this.cmb_level.Name = "cmb_level"; this.cmb_level.Size = new System.Drawing.Size(120, 21); this.cmb_level.TabIndex = 11; this.cmb_level.Text = "Choose a Level"; // // Game // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(545, 375); this.Controls.Add(this.cmb_level); this.Controls.Add(this.pb); this.Controls.Add(this.btn_Start); this.Controls.Add(this.tb_info); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.Name = "Game"; this.Text = "Fire"; this.ResumeLayout(false); loadImages(); initGame(); } private void loadImages() { if (File.Exists("Westen.bmp")) { tb_info.Text = "Bitmap Westen exists"; bmp_canyon = (Bitmap)Bitmap.FromFile("Westen.bmp"); } else { tb_info.Text = "Bitmap Westen does not exist"; return; } if (File.Exists("Wueste.bmp")) { tb_info.Text = "Bitmap Wueste exists"; bmp_desert = (Bitmap)Bitmap.FromFile("Wueste.bmp"); } else { tb_info.Text = "Bitmap Wueste does not exist"; return; } if (File.Exists("Alpen.bmp")) { tb_info.Text = "Bitmap Alpen exists"; bmp_alps = (Bitmap)Bitmap.FromFile("Alpen.bmp"); } else { tb_info.Text = "Bitmap Alpen does not exist"; return; } if (File.Exists("Title.bmp")) { tb_info.Text = "Bitmaps loaded"; bmp_title = (Bitmap)Bitmap.FromFile("Title.bmp"); } else { tb_info.Text = "Bitmap Title does not exist"; return; } } private void initGame() { //initialisierung des Zufallsgenerators rand = new Random(); //Wind calcWind(); //Anzeigen des Titelbildes pb.BackgroundImage = bmp_title; } //Initialisierung der Zufallsgegend und der Spieler private void createLandscape() { //Initialisierungen landscape = new int[POINTS]; trees = new bool[POINTS]; points = new Point[POINTS+2]; //Erstellen der Fraktalen Landschaft int start; int end; start = rand.Next(MINHEIGHT, MAXHEIGHT); end = rand.Next(MINHEIGHT, MAXHEIGHT); landscape[0] = start; landscape[POINTS-1] = end; switch (style) { case 1: this.pb.BackgroundImage = this.bmp_desert; calcHeightsDesert(0, POINTS-1, HEIGHT/2); //Erstellen der Zierobjektpositionen for (int i=0; i landscape[idx1]) { landscape[idx2-1] = landscape[idx1] + (landscape[idx2] - landscape[idx1])/2; } else { landscape[idx2-1] = landscape[idx2] + (landscape[idx1] - landscape[idx2])/2; } idx1++; idx2++; } } private void calcHeightsCanyon() { int pos; int height; int length; pos = 0; height = 5 + rand.Next() % 150; length = 2 + rand.Next() % 20; while (pos < POINTS) { length--; if (length != 0) { landscape[pos] = height; } else { height = 5 + rand.Next() % 150; length = 2 + rand.Next() % 20; landscape[pos] = height; } pos++; } //Plattform of the Player this.landscape[0] = this.landscape[POSPLAY1]; this.landscape[1] = this.landscape[POSPLAY1]; this.landscape[3] = this.landscape[POSPLAY1]; this.landscape[4] = this.landscape[POSPLAY1]; this.landscape[POINTS-1] = this.landscape[POSPLAY2]; this.landscape[POINTS-2] = this.landscape[POSPLAY2]; this.landscape[POINTS-4] = this.landscape[POSPLAY2]; this.landscape[POINTS-5] = this.landscape[POSPLAY2]; } //Hilfsmethode für die Höhenwerte der Gegend private void calcHeightsAlps(int p1, int p2, int max) { int pm; int hm; int help; //Mittlere Höhe zwischen den beiden Endpunkten berechnen if (landscape[p1] < landscape[p2]) { hm = landscape[p1] + ((landscape[p2] - landscape[p1]) / 2); } else { hm = landscape[p2] + ((landscape[p1] - landscape[p2]) / 2); } //Zufälligen Wert von der mittleren Höhe abziehen oder addieren help = rand.Next(); if ((help % 2)== 0){ hm += rand.Next(max); } else { hm -= rand.Next(max); } //Neuen Mittelpunkt berechen //Wenn dieser Existiert wird Höhe eingetragen und die //Methode rekursiv aufgerufen pm = p1 + ((p2 - p1) / 2); if ((pm != p1)&&(pm != p2)) { landscape[pm] = hm; calcHeightsAlps(p1, pm, max/2); calcHeightsAlps(pm, p2, max/2); } } private void OnPaint_Pb(Object o, PaintEventArgs e) { if (initialized) { //Gelände zeichen switch (this.style) { case 1: e.Graphics.FillPolygon(Brushes.Yellow, points); break; case 2: e.Graphics.FillPolygon(Brushes.Green, points); break; case 3: e.Graphics.FillPolygon(Brushes.SandyBrown, points); break; default: break; } //Bäume zeichen for (int i=0; i 180) currplayer.angle = 180; lb_Angle.Text = "" + currplayer.angle; } else if (o == btn_Angle_Dw) { currplayer.angle--; if (currplayer.angle < 0) currplayer.angle = 0; lb_Angle.Text = "" + currplayer.angle; } else if (o == btn_Powder_Up) { currplayer.powder++; if (currplayer.powder > 80) currplayer.powder = 80; lb_Powder.Text = "" + currplayer.powder; } else if (o == btn_Powder_Dw) { currplayer.powder--; if (currplayer.powder < 10) currplayer.powder = 10; lb_Powder.Text = "" + currplayer.powder; } else if (o == btn_fire) { if (!this.ball_flight) { if (!playerwon) { if (currplayer == player1) { speedy = (currplayer.powder * 2) * (float)Math.Sin((currplayer.angle*Math.PI)/180); speedx = (currplayer.powder * 2) * (float)Math.Cos((currplayer.angle*Math.PI)/180); } else { speedy = (currplayer.powder * 2) * (float)Math.Sin(((180 - currplayer.angle)*Math.PI)/180); speedx = (currplayer.powder * 2) * (float)Math.Cos(((180 - currplayer.angle)*Math.PI)/180); } ball = new Ball(currplayer.px, currplayer.py, wind, speedx, speedy); ball_thread = new Thread(new ThreadStart(ball_st)); ball_flight = true; stop = false; this.btn_fire.Enabled = false; this.tb_info.Text = "Cannon fired ... please wait"; ball_thread.Start(); } else { this.tb_info.Text = "Please start a new Game!"; } } } pb.Refresh(); } private void ball_st() { int h; while ((ball.getX() > 0) && (ball.getX() < PBWIDTH)&& (ball.getY() < PBHEIGHT)&& (!stop)) { h = PBHEIGHT - points[(ball.getX()/10)+1].Y; //h = landscape[ball.getX() / 10]; //PBHEIGHT-landscape[i]; //Kugel eingeschlagen if (ball.getY() > PBHEIGHT - h) { stop = true; ball_flight = false; points[(ball.getX() / 10)+1].Y += 2; //landscape[ball.getX() / 10] += 2; //Explosion } pb.Refresh(); ball.Next(); Thread.Sleep(50); } stop = true; ball_flight = false; this.btn_fire.Enabled = true; calcDamage(ball.getX()); changePlayer(); calcWind(); pb.Refresh(); } private void calcDamage(int px) { int d; Player enemy; //Damage of the Enemy if (this.currplayer == this.player1) { d = this.player2.px - px; enemy = player2; } else { d = this.player1.px - px; enemy = player1; } if (d < 0) { d = d * -1; } switch(d) { case 1: case 2: case 3: case 4: case 5: enemy.damage += 50; break; case 6: enemy.damage += 40; break; case 7: enemy.damage += 28; break; case 8: enemy.damage += 23; break; case 9: enemy.damage += 20; break; case 10: enemy.damage += 16; break; case 11: enemy.damage += 12; break; case 12: enemy.damage += 10; break; case 13: enemy.damage += 8; break; case 14: enemy.damage += 6; break; case 15: enemy.damage += 3; break; case 16: enemy.damage += 1; break; default: break; } //Own damage if (this.currplayer == this.player1) { d = this.player1.px - px; enemy = player1; } else { d = this.player2.px - px; enemy = player2; } if (d < 0) { d = d * -1; } switch(d) { case 0: case 1: case 2: case 3: case 4: case 5: enemy.damage += 50; break; case 6: enemy.damage += 40; break; case 7: enemy.damage += 28; break; case 8: enemy.damage += 23; break; case 9: enemy.damage += 20; break; case 10: enemy.damage += 16; break; case 11: enemy.damage += 12; break; case 12: enemy.damage += 10; break; case 13: enemy.damage += 8; break; case 14: enemy.damage += 6; break; case 15: enemy.damage += 3; break; case 16: enemy.damage += 1; break; default: break; } // this.player1.py = this.points[POSPLAY1+1].Y; this.player2.py = this.points[POSPLAY2+1].Y; //TreeDamage if (this.trees[px/10]) { this.trees[px/10] = false; } if (this.player1.damage >= 100) { this.tb_info.Text = "Player2 is the winner!"; this.playerwon = true; } if (this.player2.damage >= 100) { this.tb_info.Text = "Player1 is the winner!"; this.playerwon = true; } } private void changePlayer() { this.lb_Dam_Player1.Text = "Damage: "+this.player1.damage + "%"; this.lb_Dam_Player2.Text = "Damage: "+this.player2.damage + "%"; if (this.currplayer == this.player1) { this.currplayer = this.player2; this.tb_info.Text = "Player2 ... it´s your turn!"; this.lb_Player2.BackColor = Color.Red; this.lb_Player1.BackColor = Color.LightGray; } else { this.currplayer = this.player1; this.tb_info.Text = "Player1 ... it´s your turn!"; this.lb_Player1.BackColor = Color.Red; this.lb_Player2.BackColor = Color.LightGray; } this.lb_Angle.Text = "" + this.currplayer.angle; this.lb_Powder.Text = "" + this.currplayer.powder; } private void calcWind() { //Windspeed if (this.style == 1) this.wind = rand.Next() % 4; else if (this.style == 2) this.wind = rand.Next() % 5; else this.wind = rand.Next() % 6; //Direction if ((this.rand.Next() % 2) == 1) { this.wind *= -1; } //Labeltext if (this.wind < 0) { this.lb_Wind_Dir.Text = " <----"+this.wind*(-1)+"----<<"; } else { this.lb_Wind_Dir.Text = " >>----"+this.wind+"---->"; } } static void Main() { Application.Run(new Game()); } } }