Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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.
- 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*/
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace BubbleWars
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- Timer timer = new Timer();
- Random random1 = new Random();
- Random random2 = new Random();
- Random random3 = new Random();
- Random random4 = new Random();
- short headX = 100; //player position X
- short headY = 100; //player position Y
- short head2X = 200;
- short head2Y = 200;
- byte circleW = 10; //Breite des Kreises
- byte circleH = 10; //Höhe des Kreises
- int score = 0; //Punktzahl natürlich!
- int score2 = 0;
- int rndFutterX = 0; //random number for food x-coordinate
- int rndFutterY = 0; //random number for food y-coordinate
- int rnd2FutterX = 0;
- int rnd2FutterY = 0;
- byte richtung = 0; //variable for direction
- byte richtung2 = 0;
- private void Form1_Load(object sender, EventArgs e)
- {
- rndFutterX = random1.Next(0, 35);
- rndFutterY = random2.Next(0, 35);
- rnd2FutterX = random3.Next(0, 35);
- rnd2FutterY = random4.Next(0, 35);
- rndFutterX = rndFutterX * 10;
- rndFutterY = rndFutterY * 10;
- rnd2FutterX = rnd2FutterX * 10;
- rnd2FutterY = rnd2FutterY * 10;
- this.Paint += Form1_Paint; //Aufruf Paint-Event
- timer.Interval = 1000;
- timer.Tick += new EventHandler(Update);
- timer.Start();
- }
- private void Form1_Paint(object sender, PaintEventArgs e)
- {
- SolidBrush playerBrush = new SolidBrush(Color.Red);
- Graphics grafik = this.CreateGraphics();
- Rectangle circle = new Rectangle(headX, headY, circleH, circleW);
- grafik.FillEllipse(playerBrush, circle);
- SolidBrush player2Brush = new SolidBrush(Color.DarkRed);
- Graphics grafik2 = this.CreateGraphics();
- Rectangle circle2 = new Rectangle(head2X, head2Y, circleH, circleW);
- grafik2.FillEllipse(player2Brush, circle2);
- e.Graphics.DrawRectangle(Pens.Black, rndFutterX, rndFutterY, 10, 10);
- e.Graphics.DrawRectangle(Pens.Black, rnd2FutterX, rnd2FutterY, 10, 10);
- }
- private void Update(object sender, EventArgs e)
- {
- if (richtung == 1)
- {
- headY += 10;
- this.Refresh();
- }
- if (richtung == 2)
- {
- headY -= 10;
- this.Refresh();
- }
- if (richtung == 3)
- {
- headX -= 10;
- this.Refresh();
- }
- if (richtung == 4)
- {
- headX += 10;
- this.Refresh();
- }
- if (richtung2 == 1)
- {
- head2Y += 10;
- this.Refresh();
- }
- if (richtung2 == 2)
- {
- head2Y -= 10;
- this.Refresh();
- }
- if (richtung2 == 3)
- {
- head2X -= 10;
- this.Refresh();
- }
- if (richtung2 == 4)
- {
- head2X += 10;
- this.Refresh();
- }
- //snake on corner
- if (headY == 360 || headY == -10 || headX == 360 || headX == -10)
- {
- timer.Stop();
- MessageBox.Show("Game Over. Player 1 ran into a wall.\nPlayer 1 score: "+score+"\nPlayer 2 score: "+score2);
- }
- if (head2Y == 360 || head2Y == -10 || head2X == 360 || head2X == -10)
- {
- timer.Stop();
- MessageBox.Show("Game Over. Player 2 ran into a wall.\nPlayer 1 score: " + score + "\nPlayer 2 score: " + score2);
- }
- if (head2Y == headX && head2Y == headY)
- {
- timer.Stop();
- MessageBox.Show("Game Over. You idiots crashed into each other!\nPlayer 1 score: " + score + "\nPlayer 2 score: " + score2);
- }
- //snake on food
- if (headY == rndFutterY && headX == rndFutterX)
- {
- rndFutterX = 10 * random1.Next(0, 35);
- rndFutterY = 10 * random2.Next(0, 35);
- score += 1;
- if (timer.Interval > 250)
- {
- timer.Interval = timer.Interval - 250;
- }
- if (timer.Interval > 100 && timer.Interval <= 250)
- {
- timer.Interval = timer.Interval - 50;
- }
- if (timer.Interval > 20 && timer.Interval <= 100)
- {
- timer.Interval = timer.Interval - 20;
- }
- if (timer.Interval > 2 && timer.Interval <= 20)
- {
- timer.Interval = timer.Interval - 2;
- }
- }
- if (head2Y == rndFutterY && head2X == rndFutterX)
- {
- rndFutterX = 10 * random1.Next(0, 35);
- rndFutterY = 10 * random2.Next(0, 35);
- score2 += 1;
- if (timer.Interval > 250)
- {
- timer.Interval = timer.Interval - 250;
- }
- if (timer.Interval > 100 && timer.Interval <= 250)
- {
- timer.Interval = timer.Interval - 50;
- }
- if (timer.Interval > 20 && timer.Interval <= 100)
- {
- timer.Interval = timer.Interval - 20;
- }
- if (timer.Interval > 2 && timer.Interval <= 20)
- {
- timer.Interval = timer.Interval - 2;
- }
- }
- if (headY == rnd2FutterY && headX == rnd2FutterX)
- {
- rnd2FutterX = 10 * random3.Next(0, 35);
- rnd2FutterY = 10 * random4.Next(0, 35);
- score += 1;
- if (timer.Interval > 250)
- {
- timer.Interval = timer.Interval - 250;
- }
- if (timer.Interval > 100 && timer.Interval <= 250)
- {
- timer.Interval = timer.Interval - 50;
- }
- if (timer.Interval > 20 && timer.Interval <= 100)
- {
- timer.Interval = timer.Interval - 20;
- }
- if (timer.Interval > 2 && timer.Interval <= 20)
- {
- timer.Interval = timer.Interval - 2;
- }
- }
- if (head2Y == rnd2FutterY && head2X == rnd2FutterX)
- {
- rnd2FutterX = 10 * random3.Next(0, 35);
- rnd2FutterY = 10 * random4.Next(0, 35);
- score2 += 1;
- if (timer.Interval > 250)
- {
- timer.Interval = timer.Interval - 250;
- }
- if (timer.Interval > 100 && timer.Interval <= 250)
- {
- timer.Interval = timer.Interval - 50;
- }
- if (timer.Interval > 20 && timer.Interval <= 100)
- {
- timer.Interval = timer.Interval - 20;
- }
- if (timer.Interval > 2 && timer.Interval <= 20)
- {
- timer.Interval = timer.Interval - 2;
- }
- }
- }
- public void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Down)
- {
- richtung = 1;
- }
- if (e.KeyCode == Keys.Up)
- {
- richtung = 2;
- }
- if (e.KeyCode == Keys.Left)
- {
- richtung = 3;
- }
- if (e.KeyCode == Keys.Right)
- {
- richtung = 4;
- }
- if (e.KeyCode == Keys.S)
- {
- richtung2 = 1;
- }
- if (e.KeyCode == Keys.W)
- {
- richtung2 = 2;
- }
- if (e.KeyCode == Keys.A)
- {
- richtung2 = 3;
- }
- if (e.KeyCode == Keys.D)
- {
- richtung2 = 4;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement