daily pastebin goal
41%
SHARE
TWEET

Pointgame

Crafteo03 Jan 29th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace WindowsFormsApplication3
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         BufferedGraphics myBuffer;
  17.         Graphics gt; // echter Zeichenbereich
  18.         Graphics g; // gepufferter Zeichenbereich
  19.         TPoint runner;
  20.         TPoint[] food;
  21.         int food_count = 10;
  22.         //TPoint essen;
  23.         //TPoint essen2;
  24.         int counter = 0;
  25.         static Random rnd;
  26.         public Form1()
  27.         {
  28.             InitializeComponent();
  29.             if (rnd == null)
  30.             {
  31.                 rnd = new Random();
  32.             }
  33.             runner = new TPoint(Color.Black ,60);
  34.             //essen = new TPoint(Color.Red, 30, 100, 345);
  35.             //essen2 = new TPoint(Color.DarkBlue, 30, 150, 300);
  36.             food = new TPoint[food_count];
  37.             for (int i = 0; i < food_count; i++)
  38.             {
  39.                 food[i] = new TPoint(ClientRectangle);
  40.                 food[i].Value = i + 1;
  41.             }
  42.             //essen.Value = 1;
  43.             //essen2.Value = 4;
  44.             InitializeGraphics();
  45.             ResetAll();
  46.         }
  47.  
  48.         static public int Get_Number(int min, int max)
  49.         {
  50.             return rnd.Next(min,max);
  51.         }
  52.  
  53.         private void ResetGraphics()
  54.         {
  55.             if (myBuffer != null) {
  56.                 myBuffer.Dispose();
  57.                 gt.Dispose();
  58.              
  59.             }
  60.         }
  61.  
  62.         private void InitializeGraphics()
  63.         {
  64.             ResetGraphics();
  65.             this.DoubleBuffered = true;
  66.             gt = this.CreateGraphics();
  67.             myBuffer = BufferedGraphicsManager.Current.Allocate(gt, this.DisplayRectangle);
  68.             g = myBuffer.Graphics;
  69.             SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  70.             g.SmoothingMode = SmoothingMode.AntiAlias;
  71.  
  72.         }
  73.  
  74.         private bool IsKollision(TPoint p1, TPoint p2)
  75.         {
  76.             return p1.IsKollision(p2);
  77.         }
  78.  
  79.         private void Set_Random_Position(TPoint p)
  80.         {
  81.             p.Set_Random_Position(ClientRectangle);
  82.  
  83.  
  84.         }
  85.  
  86.         private void Compute_Next()
  87.         {
  88.             runner.Compute_Next(ClientRectangle);
  89.             //essen.Compute_Next(ClientRectangle);
  90.             //essen2.Compute_Next(ClientRectangle);
  91.  
  92.             for (int i = 0; i < food_count; i++)
  93.             {
  94.                 food[i].Compute_Next(ClientRectangle);
  95.             }
  96.  
  97.             /*if (IsKollision(runner, essen))
  98.             {
  99.                 counter = counter + essen.Value;
  100.                 essen.Set_Random_Value();
  101.                 Set_Random_Position(essen);
  102.             }
  103.             if (IsKollision(runner, essen2))
  104.             {
  105.                 counter = counter + essen2.Value;
  106.                 essen2.Set_Random_Value();
  107.                 Set_Random_Position(essen2);
  108.             }
  109.             */
  110.             for (int i = 0; i < food_count; i++)
  111.             {
  112.                 if (IsKollision(runner, food[i]))
  113.                 {
  114.                     counter = counter + food[i].Value;
  115.                     runner.Add_Size(food[i].Value);
  116.                     food[i].Set_Random_Value();
  117.                     Set_Random_Position(food[i]);
  118.  
  119.                 }
  120.             }
  121.  
  122.         }
  123.         private void change_modi()
  124.         {
  125.             runner.Change_Modus();
  126.          
  127.         }
  128.         private void Draw_Things()
  129.         {
  130.             g.SmoothingMode = SmoothingMode.AntiAlias;
  131.             g.Clear(Color.White);
  132.             Draw_Food();
  133.             //essen.Draw(g);                    
  134.             //essen2.Draw(g);
  135.             runner.Draw(g);
  136.             lbl_kolisionsnmber.Text = counter.ToString();
  137.             myBuffer.Render();
  138.  
  139.         }
  140.  
  141.         private void Draw_Food()
  142.         {
  143.             for (int i = 0; i < food_count; i++)
  144.             {
  145.                 food[i].Draw(g);
  146.             }
  147.  
  148.         }
  149.  
  150.         private void timer1_Tick(object sender, EventArgs e)
  151.         {
  152.             Compute_Next();
  153.             Draw_Things();  
  154.         }
  155.  
  156.         private void button2_Click(object sender, EventArgs e)
  157.         {
  158.  
  159.             {
  160.                 Schneller();
  161.             }
  162.         }
  163.  
  164.         private void button3_Click(object sender, EventArgs e)
  165.         {
  166.             Langsamer();
  167.         }
  168.  
  169.         private void ResetAll()
  170.         {
  171.             Reset();
  172.         }
  173.  
  174.         private void button4_Click(object sender, EventArgs e)
  175.         {
  176.             ResetAll();
  177.         }
  178.  
  179.         private void btn_nachoben_Click(object sender, EventArgs e)
  180.         {
  181.  
  182.             GoUp();
  183.         }
  184.  
  185.         private void btn_nachunten_Click(object sender, EventArgs e)
  186.         {
  187.             GoDown();
  188.         }
  189.  
  190.         private void btn_nachlinks_Click(object sender, EventArgs e)
  191.         {
  192.             GoLeft();
  193.         }
  194.  
  195.         private void btn_nachrechts_Click(object sender, EventArgs e)
  196.         {
  197.             GoRight();
  198.         }
  199.  
  200.  
  201.         private void btn_pause_Click(object sender, EventArgs e)
  202.         {
  203.  
  204.             Pause();
  205.  
  206.            
  207.         }
  208.  
  209.         private void GoDown()  
  210.         {
  211.                        runner.GoDown();
  212.         }
  213.         private void GoUp()
  214.         {
  215.            
  216.             runner.GoUp();
  217.         }
  218.         private void GoRight()
  219.         {
  220.            
  221.             runner.GoRight();
  222.         }
  223.         private void GoLeft()
  224.         {
  225.            
  226.             runner.GoLeft();
  227.         }
  228.         private void Pause()
  229.         {
  230.             timer1.Enabled = !timer1.Enabled;
  231.         }
  232.         private void Schneller()
  233.         {
  234.             if (timer1.Interval > 100)
  235.             {
  236.                 timer1.Interval = timer1.Interval - 100;
  237.             }
  238.             else if (timer1.Interval > 1)
  239.             {
  240.                 timer1.Interval = timer1.Interval / 2;
  241.             }
  242.         }
  243.  
  244.         private void Langsamer()
  245.         {
  246.             if (timer1.Interval < 100)
  247.             {
  248.                 timer1.Interval = timer1.Interval * 2;
  249.             }
  250.             else
  251.             {
  252.                 timer1.Interval = timer1.Interval + 100;
  253.             }
  254.            
  255.         }
  256.         private void Reset()
  257.         {
  258.             timer1.Interval = 500;
  259.             timer1.Enabled = false;
  260.            
  261.         }
  262.  
  263.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  264.         {
  265.             if (e.KeyValue == 98 || e.KeyValue == 40)  // 2
  266.             {
  267.                 GoDown();
  268.             }
  269.             else if (e.KeyValue == 100 || e.KeyValue == 37) // 4
  270.             {
  271.                 GoLeft();
  272.             }
  273.  
  274.             else if (e.KeyValue == 102 || e.KeyValue == 39) // 6
  275.             {
  276.                 GoRight();
  277.  
  278.             }
  279.             else if (e.KeyValue == 104 || e.KeyValue == 38) // 8
  280.             {
  281.                 GoUp();
  282.             }
  283.  
  284.             else if (e.KeyValue == 101) // 5
  285.             {
  286.                 Pause();
  287.             }
  288.  
  289.             else if (e.KeyValue == 107) // +
  290.             {
  291.                 Schneller();
  292.             }
  293.             else if (e.KeyValue == 109) // -
  294.             {
  295.                 Langsamer();
  296.             }
  297.             else if (e.KeyValue == 110) // ,
  298.             {
  299.                 Reset();
  300.             }
  301.             else if (e.KeyValue == 96) // 0
  302.             {
  303.                 DoStep();  
  304.             }
  305.             else if (e.KeyValue == 111) // /
  306.             {
  307.                 change_modi();
  308.             }
  309.         }
  310.  
  311.         private void DoStep()
  312.         {
  313.             Compute_Next();
  314.             Draw_Things();
  315.         }
  316.  
  317.         private void btn_step_Click(object sender, EventArgs e)
  318.         {
  319.             DoStep();
  320.         }
  321.  
  322.         private void btn_modus_Click(object sender, EventArgs e)
  323.         {
  324.             change_modi();
  325.         }
  326.  
  327.         private void Form1_FormClosed(object sender, FormClosedEventArgs e)
  328.         {
  329.             ResetGraphics();
  330.         }
  331.  
  332.         private void Form1_SizeChanged(object sender, EventArgs e)
  333.         {
  334.             InitializeGraphics();
  335.         }
  336.  
  337.         protected override bool ProcessDialogKey(Keys keyData)
  338.         {
  339.             return false;
  340.         }
  341.     }
  342.  
  343.     public partial class TPoint
  344.     {
  345.  
  346.         Brush brush;
  347.         Brush lblbrush;
  348.         Font fnt;
  349.         Pen pen;
  350.         int x;
  351.         int y;
  352.         int stephrz; // horizontal
  353.         int stepvrt; // vertical
  354.         int width;
  355.         int height;
  356.         int def_stephrz;
  357.         int def_stepvrt;
  358.         int modus; // 1: Abprallen 2: Durchlaufen
  359.         int value;
  360.         int minsize=30;
  361.  
  362.         public int Value
  363.         {
  364.             get
  365.             {
  366.                 return value;
  367.             }
  368.             set
  369.             {
  370.                 if (value >= -10|| value <= 10)
  371.                 {
  372.                     this.value = value;
  373.                     Value_Set_Color();
  374.                 }
  375.                
  376.             }
  377.         }
  378.  
  379.         private void Value_Set_Color()
  380.         {
  381.             switch (Math.Abs(value))
  382.             {
  383.                 case 1: SetColor(Color.Red); break;
  384.                 case 2: SetColor(Color.Blue); break;
  385.                 case 3: SetColor(Color.Yellow); break;
  386.                 case 4: SetColor(Color.Brown); break;
  387.                 case 5: SetColor(Color.Green); break;
  388.                 case 6: SetColor(Color.Orange); break;
  389.                 case 7: SetColor(Color.Purple); break;
  390.                 case 8: SetColor(Color.Pink); break;
  391.                 case 9: SetColor(Color.Gray); break;
  392.                 case 10: SetColor(Color.Turquoise); break;
  393.                  
  394.             }
  395.  
  396.         }
  397.  
  398.         public TPoint(Color col, int size = 50,
  399.              int startx = 10, int starty = 10, // Startposition
  400.              int starthrz = 0, int startvrt = 0, // Anfangsgeschwindigkeit
  401.              int defhrz = 15, int defvrt = 15, // Geschwindigkeit beim Wechsel
  402.              int defmodus = 1
  403.             )
  404.         {
  405.             brush = new SolidBrush(col);
  406.             pen = new Pen(col);
  407.         lblbrush = new SolidBrush(Color.Black);
  408.             fnt = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Bold);
  409.             def_stephrz = defhrz;
  410.             def_stepvrt = defvrt;
  411.             stepvrt = startvrt;
  412.             stephrz = starthrz;
  413.             x = startx;
  414.             y = starty;
  415.             width = size;
  416.             height = size;
  417.             modus = defmodus;
  418.             value = 0;
  419.         }
  420.  
  421.         public TPoint(Rectangle rect)
  422.         {
  423.             fnt = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Bold);
  424.             pen = new Pen(Color.Black);
  425.             def_stephrz = 15;
  426.             def_stepvrt = 15;
  427.             stepvrt = 0;
  428.             stephrz = 0;
  429.             width = 30;
  430.             height = 30;
  431.             Set_Random_Position(rect);
  432.             modus = 1;
  433.             value = 0;
  434.         }
  435.  
  436.         public bool IsKollision(TPoint p)
  437.         {
  438.  
  439.             // Ist der Mittelpunkt von p innerhalb unserer eigenen Ellipse
  440.  
  441.             // Test Rechteck
  442.             if ((p.x + p.width / 2 < x) || (p.x + p.width / 2 > x + width))
  443.             {
  444.                 return false;
  445.             }
  446.             else if ((p.y + p.height / 2 < y) || (p.y + p.height / 2 > y + height))
  447.             {
  448.                 return false;
  449.             }
  450.             else
  451.             {
  452.                 // hier genauer berechnen, ob p in Ellipse
  453.                 return true;
  454.             }
  455.         }
  456.  
  457.         public void SetColor(Color col/*, int kind = 0*/)
  458.         {
  459.             Color gridcol;
  460.             Color newtextcol;
  461.             if (col.R * 2 + col.G * 5 + col.B > 1024)
  462.             {
  463.                 newtextcol = Color.Black;
  464.                 gridcol = Color.DarkGray;
  465.             }
  466.             else
  467.             {
  468.                 newtextcol = Color.White;
  469.                 gridcol = Color.Yellow;
  470.             }
  471.             if (brush != null) {
  472.                 brush.Dispose();
  473.                 lblbrush.Dispose();
  474.             }
  475.            
  476.          /*   if (Value < 0)
  477.             {
  478.                 brush = new HatchBrush(HatchStyle.Cross, gridcol, col);
  479.             }
  480.             else
  481.             {*/
  482.                 brush = new SolidBrush(col);
  483.             // }
  484.             // brush = new SolidBrush(col);
  485.  
  486.            
  487.             lblbrush = new SolidBrush(newtextcol);
  488.           /*  lblbrush = new SolidBrush(
  489.                 Color.FromArgb(col.A,
  490.                   Color.White.R - col.R,
  491.                   Color.White.G - col.G,
  492.                   Color.White.B - col.B)
  493.                 );*/
  494.         }
  495.  
  496.         public void Draw(Graphics g)
  497.         {
  498.             if (Value < 0) {
  499.                 //g.DrawEllipse(pen, x, y, width, height);
  500.                 g.FillRectangle(brush, x, y, width, height);
  501.                 g.DrawRectangle(pen, x, y, width, height);
  502.             }
  503.             else
  504.             {
  505.                 g.FillEllipse(brush, x, y, width, height);
  506.                 g.DrawEllipse(pen, x, y, width, height);
  507.             }
  508.             if (Value != 0)
  509.             {
  510.                 string lbl;
  511.                 int t = Math.Abs(Value);
  512.  
  513.                 lbl = t.ToString();
  514.  
  515.                 int l = x + width / 2 - 8;
  516.                 int h = y + height / 2 - 9;
  517.  
  518.                 if (t > 9)
  519.                 {
  520.                     l = l - 5;                    
  521.                 }
  522.  
  523.                 g.DrawString(lbl, fnt, lblbrush, l, h);
  524.             }
  525.  
  526.         }
  527.  
  528.         private void Compute_Next_Umkehr(Rectangle ClientRectangle)
  529.         {
  530.             if (stephrz < 0 && x <= 0) // nach links und linker Rand erreicht
  531.             {
  532.                 stephrz = -stephrz; // Umkehr
  533.                 x = 0; // linker Rand = 0
  534.             }
  535.             else if (stephrz > 0 && x + width >= ClientRectangle.Width)
  536.             {
  537.                 stephrz = -stephrz;
  538.                 x = ClientRectangle.Width - width;
  539.             }
  540.             if (stepvrt < 0 && y <= 0) // nach oben und oben Rand erreicht
  541.             {
  542.                 stepvrt = -stepvrt; // Umkehr
  543.                 y = 0; // oberer Rand = 0
  544.             }
  545.             else if (stepvrt > 0 && y + height >= ClientRectangle.Height)
  546.             {
  547.                 stepvrt = -stepvrt;
  548.                 y = ClientRectangle.Height - height;
  549.             }
  550.         }
  551.         private void Compute_Next_Durchlauf(Rectangle ClientRectangle)
  552.         {
  553.             if (stephrz < 0 && x + width <= 0) // nach links und linker Rand erreicht
  554.             {
  555.                 x = ClientRectangle.Width;
  556.  
  557.             }
  558.             else if (stephrz > 0 && x >= ClientRectangle.Width) // nach rechts rechter rand erreicht
  559.             {
  560.                 x = 0 - width;
  561.             }
  562.             if (stepvrt < 0 && y + height <= 0) // nach oben und oben Rand erreicht
  563.             {
  564.                 y = ClientRectangle.Height;
  565.             }
  566.             else if (stepvrt > 0 && y >= ClientRectangle.Height)// u7nterrer rand erreicht nach unten
  567.             {
  568.                 y = 0 - height;
  569.             }
  570.         }
  571.  
  572.         public void Compute_Next(Rectangle ClientRectangle)
  573.         {
  574.             y = y + stepvrt;
  575.             x = x + stephrz;
  576.  
  577.             if (modus == 1)
  578.             {
  579.                 Compute_Next_Umkehr(ClientRectangle);
  580.             }
  581.             else if (modus == 2)
  582.             {
  583.                 Compute_Next_Durchlauf(ClientRectangle);
  584.             }
  585.         }
  586.         public void Change_Modus()
  587.         {
  588.  
  589.             if (modus == 1)
  590.             {
  591.                 modus = 2;
  592.             }
  593.             else if (modus == 2)
  594.             {
  595.                 modus = 1;
  596.             }
  597.         }
  598.  
  599.         public void GoDown()
  600.         {
  601.             stepvrt = +def_stepvrt;
  602.             stephrz = 0;
  603.         }
  604.         public void GoUp()
  605.         {
  606.             stepvrt = -def_stepvrt;
  607.             stephrz = 0;
  608.         }
  609.         public void GoRight()
  610.         {
  611.             stephrz = +def_stephrz;
  612.             stepvrt = 0;
  613.         }
  614.         public void GoLeft()
  615.         {
  616.             stephrz = -def_stephrz;
  617.             stepvrt = 0;
  618.         }
  619.         public void SetPosition(int newx, int newy)
  620.         {
  621.             x = newx;
  622.             y = newy;
  623.         }
  624.  
  625.         public void Set_Random_Position(Rectangle rect)
  626.         {
  627.             int lx;
  628.             int ly;
  629.             lx = Form1.Get_Number(0, rect.Width-width);
  630.             ly = Form1.Get_Number(0, rect.Height-height);
  631.             SetPosition(lx, ly);
  632.         }
  633.  
  634.         public void Set_Random_Value()
  635.         {
  636.             do
  637.             {
  638.                 Value = Form1.Get_Number(-10, 10);
  639.             }
  640.             while (Value == 0);
  641.  
  642.         }
  643.  
  644.         public void Add_Size(int value)
  645.         {
  646.             if (width + value > minsize)
  647.             {
  648.                 width = width + value;
  649.             }
  650.  
  651.             if (height + value > minsize)
  652.             {
  653.                 height = height + value;
  654.             }
  655.            
  656.         }
  657.     }
  658. }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top