Advertisement
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.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Threading;
- namespace Pong
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- Graphics g;
- Graphics bbg;
- Bitmap bb;
- public int firstDisplacement = 0;
- public int secondDisplacement = 0;
- public Rectangle paddle1 ;
- public Rectangle paddle2 ;
- public Thread mainThread;
- Font transitionFont;
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- public bool gameEnd = false;
- public void Game_Loop()
- {
- Rectangle ball = new Rectangle(this.Width / 2, this.Height / 2, 16, 16);
- Point velocity = new Point(1, 1);
- do
- {
- g.Clear(Color.Black);
- if (gameEnd == false)
- {
- if (CheckCollision(paddle1, ball))
- {
- velocity.X *= -1;
- }
- if (CheckCollision(paddle2, ball))
- {
- velocity.X *= -1;
- }
- if (ball.Location.Y <= 0 || ball.Location.Y >= this.Height - ball.Height - 34)
- {
- velocity.Y *= -1;
- }
- if (firstDisplacement == 1)
- {
- paddle1.Y -= 3;
- }
- else if (firstDisplacement == 2)
- {
- paddle1.Y += 3;
- }
- else
- {
- }
- if (secondDisplacement == 1)
- {
- paddle2.Y -= 3;
- }
- else if (secondDisplacement == 2)
- {
- paddle2.Y += 3;
- }
- else
- {
- }
- ball.X += velocity.X;
- ball.Y += velocity.Y;
- if (ball.X > this.Width || ball.X < 0)
- {
- gameEnd = true;
- }
- g.FillRectangle(Brushes.White, paddle1);
- g.FillRectangle(Brushes.White, paddle2);
- g.FillEllipse(Brushes.White, ball);
- }
- else
- {
- if (ball.X > this.Width) {
- g.DrawString("Right lost!", transitionFont, Brushes.White, new Point(this.Width / 2 - 40, this.Height / 2 - 15));
- } else
- {
- g.DrawString("Left lost!", transitionFont, Brushes.White, new Point(this.Width / 2 - 40, this.Height / 2 - 15));
- }
- }
- bbg.DrawImageUnscaled(bb, 0, 0);
- } while (true);
- }
- public bool CheckCollision(Rectangle mainREct, Rectangle Collider)
- {
- if (mainREct.Contains(Collider))
- {
- return true;
- } else
- {
- return false;
- }
- }
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.W)
- {
- firstDisplacement = 1;
- }
- if (e.KeyCode == Keys.Up)
- {
- secondDisplacement = 1;
- }
- if (e.KeyCode == Keys.S)
- {
- firstDisplacement = 2;
- }
- if (e.KeyCode == Keys.Down )
- {
- secondDisplacement = 2;
- }
- }
- private void Form1_KeyUp(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.W) {
- firstDisplacement = 0;
- }
- if (e.KeyCode == Keys.Up)
- {
- secondDisplacement = 0;
- }
- if (e.KeyCode == Keys.S)
- {
- firstDisplacement = 0;
- }
- if (e.KeyCode == Keys.Down)
- {
- secondDisplacement = 0;
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- this.Show();
- bb = new Bitmap(this.Width, this.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
- g = Graphics.FromImage(bb);
- bbg = this.CreateGraphics();
- transitionFont = new Font("Arial", 24);
- paddle1 = new Rectangle(0, this.Height / 2 - 150, 20, 100);
- paddle2 = new Rectangle(this.Width - 35, this.Height / 2 - 150, 20, 100);
- mainThread = new Thread(Game_Loop);
- button1.Hide();
- this.Focus();
- mainThread.Start();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement