Advertisement
Guest User

Untitled

a guest
Aug 15th, 2021
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.21 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Threading;
  11. namespace Pong
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.  
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         Graphics g;
  22.         Graphics bbg;
  23.         Bitmap bb;
  24.  
  25.         public int firstDisplacement = 0;
  26.         public int secondDisplacement = 0;
  27.  
  28.         public Rectangle paddle1 ;
  29.         public Rectangle paddle2 ;
  30.  
  31.         public Thread mainThread;
  32.         Font transitionFont;
  33.  
  34.         private void Form1_Load(object sender, EventArgs e)
  35.         {
  36.            
  37.         }
  38.         public bool gameEnd = false;
  39.         public void Game_Loop()
  40.         {
  41.            
  42.             Rectangle ball = new Rectangle(this.Width / 2, this.Height / 2, 16, 16);
  43.             Point velocity = new Point(1, 1);
  44.             do
  45.             {
  46.                 g.Clear(Color.Black);
  47.  
  48.                 if (gameEnd == false)
  49.                 {
  50.                     if (CheckCollision(paddle1, ball))
  51.                     {
  52.                         velocity.X *= -1;
  53.                     }
  54.                     if (CheckCollision(paddle2, ball))
  55.                     {
  56.                         velocity.X *= -1;
  57.                     }
  58.                     if (ball.Location.Y <= 0 || ball.Location.Y >= this.Height - ball.Height - 34)
  59.                     {
  60.                         velocity.Y *= -1;
  61.                     }
  62.  
  63.                     if (firstDisplacement == 1)
  64.                     {
  65.                         paddle1.Y -= 3;
  66.                     }
  67.                     else if (firstDisplacement == 2)
  68.                     {
  69.                         paddle1.Y += 3;
  70.  
  71.                     }
  72.                     else
  73.                     {
  74.  
  75.                     }
  76.  
  77.                     if (secondDisplacement == 1)
  78.                     {
  79.                         paddle2.Y -= 3;
  80.  
  81.                     }
  82.                     else if (secondDisplacement == 2)
  83.                     {
  84.                         paddle2.Y += 3;
  85.  
  86.                     }
  87.                     else
  88.                     {
  89.  
  90.                     }
  91.                     ball.X += velocity.X;
  92.                     ball.Y += velocity.Y;
  93.  
  94.                     if (ball.X > this.Width || ball.X < 0)
  95.                     {
  96.                         gameEnd = true;
  97.                     }
  98.  
  99.                     g.FillRectangle(Brushes.White, paddle1);
  100.                     g.FillRectangle(Brushes.White, paddle2);
  101.                     g.FillEllipse(Brushes.White, ball);
  102.                 }
  103.                 else
  104.                 {
  105.                     if (ball.X > this.Width) {
  106.                         g.DrawString("Right lost!", transitionFont, Brushes.White, new Point(this.Width / 2 - 40, this.Height / 2 - 15));
  107.                     }  else
  108.                     {
  109.                     g.DrawString("Left lost!", transitionFont, Brushes.White, new Point(this.Width / 2 - 40, this.Height / 2 - 15));
  110.  
  111.                     }
  112.                 }
  113.            
  114.  
  115.                 bbg.DrawImageUnscaled(bb, 0, 0);
  116.             } while (true);
  117.         }
  118.  
  119.         public bool CheckCollision(Rectangle mainREct, Rectangle Collider)
  120.         {
  121.             if (mainREct.Contains(Collider))
  122.             {
  123.                 return true;
  124.             } else
  125.             {
  126.                 return false;
  127.             }
  128.         }
  129.  
  130.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  131.         {
  132.             if (e.KeyCode == Keys.W)
  133.             {
  134.                 firstDisplacement = 1;
  135.  
  136.             }
  137.             if (e.KeyCode == Keys.Up)
  138.             {
  139.                 secondDisplacement = 1;
  140.             }
  141.             if (e.KeyCode == Keys.S)
  142.             {
  143.                 firstDisplacement = 2;
  144.             }
  145.             if (e.KeyCode == Keys.Down )
  146.             {
  147.                 secondDisplacement = 2;
  148.             }
  149.         }
  150.  
  151.         private void Form1_KeyUp(object sender, KeyEventArgs e)
  152.         {
  153.  
  154.             if (e.KeyCode == Keys.W) {
  155.                 firstDisplacement = 0;
  156.  
  157.             }
  158.             if (e.KeyCode == Keys.Up)
  159.             {
  160.                 secondDisplacement = 0;
  161.             }
  162.             if (e.KeyCode == Keys.S)
  163.             {
  164.                firstDisplacement = 0;
  165.             }
  166.             if (e.KeyCode == Keys.Down)
  167.             {
  168.                secondDisplacement = 0;
  169.             }
  170.         }
  171.  
  172.         private void button1_Click(object sender, EventArgs e)
  173.         {
  174.             this.Show();
  175.             bb = new Bitmap(this.Width, this.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
  176.             g = Graphics.FromImage(bb);
  177.             bbg = this.CreateGraphics();
  178.             transitionFont = new Font("Arial", 24);
  179.  
  180.  
  181.             paddle1 = new Rectangle(0, this.Height / 2 - 150, 20, 100);
  182.             paddle2 = new Rectangle(this.Width - 35, this.Height / 2 - 150, 20, 100);
  183.             mainThread = new Thread(Game_Loop);
  184.             button1.Hide();
  185.             this.Focus();
  186.             mainThread.Start();
  187.         }
  188.     }
  189. }
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement