Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication3
- {
- public partial class Form1 : Form
- {
- BufferedGraphics myBuffer;
- Graphics gt; // echter Zeichenbereich
- Graphics g; // gepufferter Zeichenbereich
- TPoint runner;
- TPoint[] food;
- int food_count = 10;
- //TPoint essen;
- //TPoint essen2;
- int counter = 0;
- static Random rnd;
- public Form1()
- {
- InitializeComponent();
- if (rnd == null)
- {
- rnd = new Random();
- }
- runner = new TPoint(Color.Black ,60);
- //essen = new TPoint(Color.Red, 30, 100, 345);
- //essen2 = new TPoint(Color.DarkBlue, 30, 150, 300);
- food = new TPoint[food_count];
- for (int i = 0; i < food_count; i++)
- {
- food[i] = new TPoint(ClientRectangle);
- food[i].Value = i + 1;
- }
- //essen.Value = 1;
- //essen2.Value = 4;
- InitializeGraphics();
- ResetAll();
- }
- static public int Get_Number(int min, int max)
- {
- return rnd.Next(min,max);
- }
- private void ResetGraphics()
- {
- if (myBuffer != null) {
- myBuffer.Dispose();
- gt.Dispose();
- }
- }
- private void InitializeGraphics()
- {
- ResetGraphics();
- this.DoubleBuffered = true;
- gt = this.CreateGraphics();
- myBuffer = BufferedGraphicsManager.Current.Allocate(gt, this.DisplayRectangle);
- g = myBuffer.Graphics;
- SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
- g.SmoothingMode = SmoothingMode.AntiAlias;
- }
- private bool IsKollision(TPoint p1, TPoint p2)
- {
- return p1.IsKollision(p2);
- }
- private void Set_Random_Position(TPoint p)
- {
- p.Set_Random_Position(ClientRectangle);
- }
- private void Compute_Next()
- {
- runner.Compute_Next(ClientRectangle);
- //essen.Compute_Next(ClientRectangle);
- //essen2.Compute_Next(ClientRectangle);
- for (int i = 0; i < food_count; i++)
- {
- food[i].Compute_Next(ClientRectangle);
- }
- /*if (IsKollision(runner, essen))
- {
- counter = counter + essen.Value;
- essen.Set_Random_Value();
- Set_Random_Position(essen);
- }
- if (IsKollision(runner, essen2))
- {
- counter = counter + essen2.Value;
- essen2.Set_Random_Value();
- Set_Random_Position(essen2);
- }
- */
- for (int i = 0; i < food_count; i++)
- {
- if (IsKollision(runner, food[i]))
- {
- counter = counter + food[i].Value;
- runner.Add_Size(food[i].Value);
- food[i].Set_Random_Value();
- Set_Random_Position(food[i]);
- }
- }
- }
- private void change_modi()
- {
- runner.Change_Modus();
- }
- private void Draw_Things()
- {
- g.SmoothingMode = SmoothingMode.AntiAlias;
- g.Clear(Color.White);
- Draw_Food();
- //essen.Draw(g);
- //essen2.Draw(g);
- runner.Draw(g);
- lbl_kolisionsnmber.Text = counter.ToString();
- myBuffer.Render();
- }
- private void Draw_Food()
- {
- for (int i = 0; i < food_count; i++)
- {
- food[i].Draw(g);
- }
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- Compute_Next();
- Draw_Things();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- {
- Schneller();
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- Langsamer();
- }
- private void ResetAll()
- {
- Reset();
- }
- private void button4_Click(object sender, EventArgs e)
- {
- ResetAll();
- }
- private void btn_nachoben_Click(object sender, EventArgs e)
- {
- GoUp();
- }
- private void btn_nachunten_Click(object sender, EventArgs e)
- {
- GoDown();
- }
- private void btn_nachlinks_Click(object sender, EventArgs e)
- {
- GoLeft();
- }
- private void btn_nachrechts_Click(object sender, EventArgs e)
- {
- GoRight();
- }
- private void btn_pause_Click(object sender, EventArgs e)
- {
- Pause();
- }
- private void GoDown()
- {
- runner.GoDown();
- }
- private void GoUp()
- {
- runner.GoUp();
- }
- private void GoRight()
- {
- runner.GoRight();
- }
- private void GoLeft()
- {
- runner.GoLeft();
- }
- private void Pause()
- {
- timer1.Enabled = !timer1.Enabled;
- }
- private void Schneller()
- {
- if (timer1.Interval > 100)
- {
- timer1.Interval = timer1.Interval - 100;
- }
- else if (timer1.Interval > 1)
- {
- timer1.Interval = timer1.Interval / 2;
- }
- }
- private void Langsamer()
- {
- if (timer1.Interval < 100)
- {
- timer1.Interval = timer1.Interval * 2;
- }
- else
- {
- timer1.Interval = timer1.Interval + 100;
- }
- }
- private void Reset()
- {
- timer1.Interval = 500;
- timer1.Enabled = false;
- }
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyValue == 98 || e.KeyValue == 40) // 2
- {
- GoDown();
- }
- else if (e.KeyValue == 100 || e.KeyValue == 37) // 4
- {
- GoLeft();
- }
- else if (e.KeyValue == 102 || e.KeyValue == 39) // 6
- {
- GoRight();
- }
- else if (e.KeyValue == 104 || e.KeyValue == 38) // 8
- {
- GoUp();
- }
- else if (e.KeyValue == 101) // 5
- {
- Pause();
- }
- else if (e.KeyValue == 107) // +
- {
- Schneller();
- }
- else if (e.KeyValue == 109) // -
- {
- Langsamer();
- }
- else if (e.KeyValue == 110) // ,
- {
- Reset();
- }
- else if (e.KeyValue == 96) // 0
- {
- DoStep();
- }
- else if (e.KeyValue == 111) // /
- {
- change_modi();
- }
- }
- private void DoStep()
- {
- Compute_Next();
- Draw_Things();
- }
- private void btn_step_Click(object sender, EventArgs e)
- {
- DoStep();
- }
- private void btn_modus_Click(object sender, EventArgs e)
- {
- change_modi();
- }
- private void Form1_FormClosed(object sender, FormClosedEventArgs e)
- {
- ResetGraphics();
- }
- private void Form1_SizeChanged(object sender, EventArgs e)
- {
- InitializeGraphics();
- }
- protected override bool ProcessDialogKey(Keys keyData)
- {
- return false;
- }
- }
- public partial class TPoint
- {
- Brush brush;
- Brush lblbrush;
- Font fnt;
- Pen pen;
- int x;
- int y;
- int stephrz; // horizontal
- int stepvrt; // vertical
- int width;
- int height;
- int def_stephrz;
- int def_stepvrt;
- int modus; // 1: Abprallen 2: Durchlaufen
- int value;
- int minsize=30;
- public int Value
- {
- get
- {
- return value;
- }
- set
- {
- if (value >= -10|| value <= 10)
- {
- this.value = value;
- Value_Set_Color();
- }
- }
- }
- private void Value_Set_Color()
- {
- switch (Math.Abs(value))
- {
- case 1: SetColor(Color.Red); break;
- case 2: SetColor(Color.Blue); break;
- case 3: SetColor(Color.Yellow); break;
- case 4: SetColor(Color.Brown); break;
- case 5: SetColor(Color.Green); break;
- case 6: SetColor(Color.Orange); break;
- case 7: SetColor(Color.Purple); break;
- case 8: SetColor(Color.Pink); break;
- case 9: SetColor(Color.Gray); break;
- case 10: SetColor(Color.Turquoise); break;
- }
- }
- public TPoint(Color col, int size = 50,
- int startx = 10, int starty = 10, // Startposition
- int starthrz = 0, int startvrt = 0, // Anfangsgeschwindigkeit
- int defhrz = 15, int defvrt = 15, // Geschwindigkeit beim Wechsel
- int defmodus = 1
- )
- {
- brush = new SolidBrush(col);
- pen = new Pen(col);
- lblbrush = new SolidBrush(Color.Black);
- fnt = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Bold);
- def_stephrz = defhrz;
- def_stepvrt = defvrt;
- stepvrt = startvrt;
- stephrz = starthrz;
- x = startx;
- y = starty;
- width = size;
- height = size;
- modus = defmodus;
- value = 0;
- }
- public TPoint(Rectangle rect)
- {
- fnt = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Bold);
- pen = new Pen(Color.Black);
- def_stephrz = 15;
- def_stepvrt = 15;
- stepvrt = 0;
- stephrz = 0;
- width = 30;
- height = 30;
- Set_Random_Position(rect);
- modus = 1;
- value = 0;
- }
- public bool IsKollision(TPoint p)
- {
- // Ist der Mittelpunkt von p innerhalb unserer eigenen Ellipse
- // Test Rechteck
- if ((p.x + p.width / 2 < x) || (p.x + p.width / 2 > x + width))
- {
- return false;
- }
- else if ((p.y + p.height / 2 < y) || (p.y + p.height / 2 > y + height))
- {
- return false;
- }
- else
- {
- // hier genauer berechnen, ob p in Ellipse
- return true;
- }
- }
- public void SetColor(Color col/*, int kind = 0*/)
- {
- Color gridcol;
- Color newtextcol;
- if (col.R * 2 + col.G * 5 + col.B > 1024)
- {
- newtextcol = Color.Black;
- gridcol = Color.DarkGray;
- }
- else
- {
- newtextcol = Color.White;
- gridcol = Color.Yellow;
- }
- if (brush != null) {
- brush.Dispose();
- lblbrush.Dispose();
- }
- /* if (Value < 0)
- {
- brush = new HatchBrush(HatchStyle.Cross, gridcol, col);
- }
- else
- {*/
- brush = new SolidBrush(col);
- // }
- // brush = new SolidBrush(col);
- lblbrush = new SolidBrush(newtextcol);
- /* lblbrush = new SolidBrush(
- Color.FromArgb(col.A,
- Color.White.R - col.R,
- Color.White.G - col.G,
- Color.White.B - col.B)
- );*/
- }
- public void Draw(Graphics g)
- {
- if (Value < 0) {
- //g.DrawEllipse(pen, x, y, width, height);
- g.FillRectangle(brush, x, y, width, height);
- g.DrawRectangle(pen, x, y, width, height);
- }
- else
- {
- g.FillEllipse(brush, x, y, width, height);
- g.DrawEllipse(pen, x, y, width, height);
- }
- if (Value != 0)
- {
- string lbl;
- int t = Math.Abs(Value);
- lbl = t.ToString();
- int l = x + width / 2 - 8;
- int h = y + height / 2 - 9;
- if (t > 9)
- {
- l = l - 5;
- }
- g.DrawString(lbl, fnt, lblbrush, l, h);
- }
- }
- private void Compute_Next_Umkehr(Rectangle ClientRectangle)
- {
- if (stephrz < 0 && x <= 0) // nach links und linker Rand erreicht
- {
- stephrz = -stephrz; // Umkehr
- x = 0; // linker Rand = 0
- }
- else if (stephrz > 0 && x + width >= ClientRectangle.Width)
- {
- stephrz = -stephrz;
- x = ClientRectangle.Width - width;
- }
- if (stepvrt < 0 && y <= 0) // nach oben und oben Rand erreicht
- {
- stepvrt = -stepvrt; // Umkehr
- y = 0; // oberer Rand = 0
- }
- else if (stepvrt > 0 && y + height >= ClientRectangle.Height)
- {
- stepvrt = -stepvrt;
- y = ClientRectangle.Height - height;
- }
- }
- private void Compute_Next_Durchlauf(Rectangle ClientRectangle)
- {
- if (stephrz < 0 && x + width <= 0) // nach links und linker Rand erreicht
- {
- x = ClientRectangle.Width;
- }
- else if (stephrz > 0 && x >= ClientRectangle.Width) // nach rechts rechter rand erreicht
- {
- x = 0 - width;
- }
- if (stepvrt < 0 && y + height <= 0) // nach oben und oben Rand erreicht
- {
- y = ClientRectangle.Height;
- }
- else if (stepvrt > 0 && y >= ClientRectangle.Height)// u7nterrer rand erreicht nach unten
- {
- y = 0 - height;
- }
- }
- public void Compute_Next(Rectangle ClientRectangle)
- {
- y = y + stepvrt;
- x = x + stephrz;
- if (modus == 1)
- {
- Compute_Next_Umkehr(ClientRectangle);
- }
- else if (modus == 2)
- {
- Compute_Next_Durchlauf(ClientRectangle);
- }
- }
- public void Change_Modus()
- {
- if (modus == 1)
- {
- modus = 2;
- }
- else if (modus == 2)
- {
- modus = 1;
- }
- }
- public void GoDown()
- {
- stepvrt = +def_stepvrt;
- stephrz = 0;
- }
- public void GoUp()
- {
- stepvrt = -def_stepvrt;
- stephrz = 0;
- }
- public void GoRight()
- {
- stephrz = +def_stephrz;
- stepvrt = 0;
- }
- public void GoLeft()
- {
- stephrz = -def_stephrz;
- stepvrt = 0;
- }
- public void SetPosition(int newx, int newy)
- {
- x = newx;
- y = newy;
- }
- public void Set_Random_Position(Rectangle rect)
- {
- int lx;
- int ly;
- lx = Form1.Get_Number(0, rect.Width-width);
- ly = Form1.Get_Number(0, rect.Height-height);
- SetPosition(lx, ly);
- }
- public void Set_Random_Value()
- {
- do
- {
- Value = Form1.Get_Number(-10, 10);
- }
- while (Value == 0);
- }
- public void Add_Size(int value)
- {
- if (width + value > minsize)
- {
- width = width + value;
- }
- if (height + value > minsize)
- {
- height = height + value;
- }
- }
- }
- }
RAW Paste Data



