Advertisement
Tician

C# Simple game for 2 players

Jul 15th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.15 KB | None | 0 0
  1. /* Some simple game I created with my class-mate for school. Requirement is a simple Form1-Window with the size 378*400 . Planned is to add networking to this.
  2. Edit: I totally forgot: You have to manually add the Paint-Event and KeyDown-Event. For this just go to the properties of your windows and click on the small sign that looks like a thunderbolt. Find the two events and (if the code is already pasted) you can select the Event next to it*/
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13.  
  14. namespace BubbleWars
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         Timer timer = new Timer();
  24.         Random random1 = new Random();
  25.         Random random2 = new Random();
  26.         Random random3 = new Random();
  27.         Random random4 = new Random();
  28.  
  29.         short headX = 100; //player position X
  30.         short headY = 100; //player position Y
  31.         short head2X = 200;
  32.         short head2Y = 200;
  33.         byte circleW = 10; //Breite des Kreises
  34.         byte circleH = 10; //Höhe des Kreises
  35.         int score = 0; //Punktzahl natürlich!
  36.         int score2 = 0;
  37.         int rndFutterX = 0; //random number for food x-coordinate
  38.         int rndFutterY = 0; //random number for food y-coordinate
  39.         int rnd2FutterX = 0;
  40.         int rnd2FutterY = 0;
  41.  
  42.         byte richtung = 0; //variable for direction
  43.         byte richtung2 = 0;
  44.  
  45.         private void Form1_Load(object sender, EventArgs e)
  46.         {
  47.             rndFutterX = random1.Next(0, 35);
  48.             rndFutterY = random2.Next(0, 35);
  49.             rnd2FutterX = random3.Next(0, 35);
  50.             rnd2FutterY = random4.Next(0, 35);
  51.             rndFutterX = rndFutterX * 10;
  52.             rndFutterY = rndFutterY * 10;
  53.             rnd2FutterX = rnd2FutterX * 10;
  54.             rnd2FutterY = rnd2FutterY * 10;
  55.  
  56.             this.Paint += Form1_Paint; //Aufruf Paint-Event
  57.             timer.Interval = 1000;
  58.             timer.Tick += new EventHandler(Update);
  59.             timer.Start();
  60.         }
  61.  
  62.         private void Form1_Paint(object sender, PaintEventArgs e)
  63.         {
  64.             SolidBrush playerBrush = new SolidBrush(Color.Red);
  65.             Graphics grafik = this.CreateGraphics();
  66.             Rectangle circle = new Rectangle(headX, headY, circleH, circleW);
  67.             grafik.FillEllipse(playerBrush, circle);
  68.  
  69.             SolidBrush player2Brush = new SolidBrush(Color.DarkRed);
  70.             Graphics grafik2 = this.CreateGraphics();
  71.             Rectangle circle2 = new Rectangle(head2X, head2Y, circleH, circleW);
  72.             grafik2.FillEllipse(player2Brush, circle2);
  73.  
  74.             e.Graphics.DrawRectangle(Pens.Black, rndFutterX, rndFutterY, 10, 10);
  75.             e.Graphics.DrawRectangle(Pens.Black, rnd2FutterX, rnd2FutterY, 10, 10);
  76.         }
  77.  
  78.         private void Update(object sender, EventArgs e)
  79.         {
  80.             if (richtung == 1)
  81.             {
  82.                 headY += 10;
  83.                 this.Refresh();
  84.             }
  85.             if (richtung == 2)
  86.             {
  87.                 headY -= 10;
  88.                 this.Refresh();
  89.             }
  90.             if (richtung == 3)
  91.             {
  92.                 headX -= 10;
  93.                 this.Refresh();
  94.             }
  95.             if (richtung == 4)
  96.             {
  97.                 headX += 10;
  98.                 this.Refresh();
  99.             }
  100.             if (richtung2 == 1)
  101.             {
  102.                 head2Y += 10;
  103.                 this.Refresh();
  104.             }
  105.             if (richtung2 == 2)
  106.             {
  107.                 head2Y -= 10;
  108.                 this.Refresh();
  109.             }
  110.             if (richtung2 == 3)
  111.             {
  112.                 head2X -= 10;
  113.                 this.Refresh();
  114.             }
  115.             if (richtung2 == 4)
  116.             {
  117.                 head2X += 10;
  118.                 this.Refresh();
  119.             }
  120.             //snake on corner
  121.             if (headY == 360 || headY == -10 || headX == 360 || headX == -10)
  122.             {
  123.                 timer.Stop();
  124.                 MessageBox.Show("Game Over. Player 1 ran into a wall.\nPlayer 1 score: "+score+"\nPlayer 2 score: "+score2);
  125.             }
  126.             if (head2Y == 360 || head2Y == -10 || head2X == 360 || head2X == -10)
  127.             {
  128.                 timer.Stop();
  129.                 MessageBox.Show("Game Over. Player 2 ran into a wall.\nPlayer 1 score: " + score + "\nPlayer 2 score: " + score2);
  130.             }
  131.             if (head2Y == headX && head2Y == headY)
  132.             {
  133.                 timer.Stop();
  134.                 MessageBox.Show("Game Over. You idiots crashed into each other!\nPlayer 1 score: " + score + "\nPlayer 2 score: " + score2);
  135.             }
  136.  
  137.  
  138.             //snake on food
  139.             if (headY == rndFutterY && headX == rndFutterX)
  140.             {
  141.                 rndFutterX = 10 * random1.Next(0, 35);
  142.                 rndFutterY = 10 * random2.Next(0, 35);
  143.                 score += 1;
  144.                 if (timer.Interval > 250)
  145.                 {
  146.                     timer.Interval = timer.Interval - 250;
  147.                 }
  148.                 if (timer.Interval > 100 && timer.Interval <= 250)
  149.                 {
  150.                     timer.Interval = timer.Interval - 50;
  151.                 }
  152.                 if (timer.Interval > 20 && timer.Interval <= 100)
  153.                 {
  154.                     timer.Interval = timer.Interval - 20;
  155.                 }
  156.                 if (timer.Interval > 2 && timer.Interval <= 20)
  157.                 {
  158.                     timer.Interval = timer.Interval - 2;
  159.                 }
  160.             }
  161.             if (head2Y == rndFutterY && head2X == rndFutterX)
  162.             {
  163.                 rndFutterX = 10 * random1.Next(0, 35);
  164.                 rndFutterY = 10 * random2.Next(0, 35);
  165.                 score2 += 1;
  166.                 if (timer.Interval > 250)
  167.                 {
  168.                     timer.Interval = timer.Interval - 250;
  169.                 }
  170.                 if (timer.Interval > 100 && timer.Interval <= 250)
  171.                 {
  172.                     timer.Interval = timer.Interval - 50;
  173.                 }
  174.                 if (timer.Interval > 20 && timer.Interval <= 100)
  175.                 {
  176.                     timer.Interval = timer.Interval - 20;
  177.                 }
  178.                 if (timer.Interval > 2 && timer.Interval <= 20)
  179.                 {
  180.                     timer.Interval = timer.Interval - 2;
  181.                 }
  182.             }
  183.             if (headY == rnd2FutterY && headX == rnd2FutterX)
  184.             {
  185.                 rnd2FutterX = 10 * random3.Next(0, 35);
  186.                 rnd2FutterY = 10 * random4.Next(0, 35);
  187.                 score += 1;
  188.                 if (timer.Interval > 250)
  189.                 {
  190.                     timer.Interval = timer.Interval - 250;
  191.                 }
  192.                 if (timer.Interval > 100 && timer.Interval <= 250)
  193.                 {
  194.                     timer.Interval = timer.Interval - 50;
  195.                 }
  196.                 if (timer.Interval > 20 && timer.Interval <= 100)
  197.                 {
  198.                     timer.Interval = timer.Interval - 20;
  199.                 }
  200.                 if (timer.Interval > 2 && timer.Interval <= 20)
  201.                 {
  202.                     timer.Interval = timer.Interval - 2;
  203.                 }
  204.             }
  205.             if (head2Y == rnd2FutterY && head2X == rnd2FutterX)
  206.             {
  207.                 rnd2FutterX = 10 * random3.Next(0, 35);
  208.                 rnd2FutterY = 10 * random4.Next(0, 35);
  209.                 score2 += 1;
  210.                 if (timer.Interval > 250)
  211.                 {
  212.                     timer.Interval = timer.Interval - 250;
  213.                 }
  214.                 if (timer.Interval > 100 && timer.Interval <= 250)
  215.                 {
  216.                     timer.Interval = timer.Interval - 50;
  217.                 }
  218.                 if (timer.Interval > 20 && timer.Interval <= 100)
  219.                 {
  220.                     timer.Interval = timer.Interval - 20;
  221.                 }
  222.                 if (timer.Interval > 2 && timer.Interval <= 20)
  223.                 {
  224.                     timer.Interval = timer.Interval - 2;
  225.                 }
  226.             }
  227.         }
  228.  
  229.  
  230.         public void Form1_KeyDown(object sender, KeyEventArgs e)
  231.         {
  232.             if (e.KeyCode == Keys.Down)
  233.             {
  234.                 richtung = 1;
  235.             }
  236.             if (e.KeyCode == Keys.Up)
  237.             {
  238.                 richtung = 2;
  239.             }
  240.             if (e.KeyCode == Keys.Left)
  241.             {
  242.                 richtung = 3;
  243.             }
  244.             if (e.KeyCode == Keys.Right)
  245.             {
  246.                 richtung = 4;
  247.             }
  248.             if (e.KeyCode == Keys.S)
  249.             {
  250.                 richtung2 = 1;
  251.             }
  252.             if (e.KeyCode == Keys.W)
  253.             {
  254.                 richtung2 = 2;
  255.             }
  256.             if (e.KeyCode == Keys.A)
  257.             {
  258.                 richtung2 = 3;
  259.             }
  260.             if (e.KeyCode == Keys.D)
  261.             {
  262.                 richtung2 = 4;
  263.             }
  264.         }
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement