Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.87 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.  
  11. namespace WindowsFormsApp1
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         private bool moveup = false;
  16.         private bool movedown = false;
  17.         private bool moveleft = false;
  18.         private bool moveright = false;
  19.  
  20.         private const double a = 4;
  21.         private double tx = 0;
  22.         private double ty = 0;
  23.         private int startingVelocityX;
  24.         private int startingVelocityY;
  25.         private int velocityX;
  26.         private int velocityY;
  27.  
  28.         public Form1()
  29.         {
  30.             InitializeComponent();
  31.         }
  32.  
  33.         private void Form1_KeyDown(object sender, KeyEventArgs e)
  34.         {
  35.             if(e.KeyCode == Keys.W)
  36.             {
  37.                 moveup = true;
  38.             }
  39.              if(e.KeyCode == Keys.S)
  40.             {
  41.                 movedown = true;
  42.             }
  43.              if (e.KeyCode == Keys.D)
  44.             {
  45.                 moveright = true;
  46.             }
  47.              if (e.KeyCode == Keys.A)
  48.             {
  49.                 moveleft = true;
  50.             }
  51.              if(e.KeyCode == Keys.Space)
  52.             {
  53.                 Rectangle c = new Rectangle(ball.Bounds.X - 3, ball.Bounds.Y - 3, ball.Bounds.Width + 6, ball.Bounds.Height + 6);
  54.                 if (c.IntersectsWith(player.Bounds) && !timer2.Enabled)
  55.                 {
  56.                     tx = 0;
  57.                     ty = 0;
  58.                     startingVelocityX = (ball.Location.X + ball.Width / 2) - (player.Location.X + player.Width / 2);
  59.                     startingVelocityY = (ball.Location.Y + player.Height / 2) - (player.Location.Y + player.Height / 2);
  60.                     velocityX = startingVelocityX;
  61.                     velocityY = startingVelocityY;
  62.                     timer2.Enabled = true;
  63.                     timer3.Enabled = true;
  64.                 }
  65.             }
  66.  
  67.         }
  68.  
  69.         private void Form1_KeyUp(object sender, KeyEventArgs e)
  70.         {
  71.             if (e.KeyCode == Keys.W)
  72.             {
  73.                 moveup = false;
  74.             }
  75.              if (e.KeyCode == Keys.S)
  76.             {
  77.                 movedown = false;
  78.             }
  79.              if (e.KeyCode == Keys.D)
  80.             {
  81.                 moveright = false;
  82.             }
  83.              if (e.KeyCode == Keys.A)
  84.             {
  85.                 moveleft = false;
  86.             }
  87.  
  88.         }
  89.  
  90.         private void timer1_Tick(object sender, EventArgs e) // player movement
  91.         {
  92.             if(moveup)
  93.             {
  94.                 if(player.Location.Y - 3 + player.Height * 9 / 10 >= 0 && ( moveBallUpAndDown(false) || player.Location.Y < ball.Location.Y))  // you can move up if you dont touch
  95.                                                                                                                                                // the upper wall and you dont push the
  96.                                                                                                                                                // ball to the upper wall (if  
  97.                                                                                                                                                // MoveBallUpAndDown returned True because
  98.                                                                                                                                                // of the wall BELOW you, YOU CAN STILL
  99.                                                                                                                                                // MOVE UP - thats what the || checks )
  100.                     setPlayerY(player.Location.Y - 3);
  101.             }
  102.             else if(movedown)
  103.             {
  104.                 if(player.Location.Y + player.Height / 10 + 3 <= panel1.Height && ( moveBallUpAndDown(false) || player.Location.Y > ball.Location.Y)) // wall or ball-wall below you
  105.                     setPlayerY(player.Location.Y + 3);
  106.             }
  107.             if(moveright)
  108.             {
  109.                 if(player.Location.X + player.Width / 10 + 3 <= panel1.Width && (moveBallLeftAndRight(false) || player.Location.X > ball.Location.X)) // wall or ball-wall to your right
  110.                     setPlayerX(player.Location.X + 3);
  111.             }
  112.             else if(moveleft)
  113.             {
  114.                 if(player.Location.X - 3 + player.Width * 9 / 10 >= 0 &&  (moveBallLeftAndRight(false) || player.Location.X < ball.Location.X)) // wall or ball-wall to your left
  115.                     setPlayerX(player.Location.X - 3);
  116.             }
  117.            
  118.             if (moveright || moveleft)
  119.                 moveBallLeftAndRight(true);
  120.             if (moveup || movedown)
  121.                 moveBallUpAndDown(true);
  122.  
  123.         }
  124.  
  125.         private void setPlayerY(int y)
  126.         {
  127.             var loc = player.Location;
  128.             loc.Y = y;
  129.             player.Location = loc;
  130.  
  131.         }
  132.  
  133.         private void setPlayerX(int x)
  134.         {
  135.             var loc = player.Location;
  136.             loc.X = x;
  137.             player.Location = loc;
  138.         }
  139.  
  140.         private void setBallX(int x)
  141.         {
  142.             var loc = ball.Location;
  143.             loc.X = x;
  144.             ball.Location = loc;
  145.         }
  146.  
  147.         private void setBallY(int y)
  148.         {
  149.             var loc = ball.Location;
  150.             loc.Y = y;
  151.             ball.Location = loc;
  152.         }
  153.  
  154.         private bool moveBallLeftAndRight(bool moveball) // player drag ball left and right
  155.         {
  156.             Rectangle playerFixed = new Rectangle(player.Location.X + 3, player.Location.Y + 6, player.Width - 6, player.Height - 12);
  157.             Rectangle ballFixed = new Rectangle(ball.Location.X, ball.Location.Y, ball.Width, ball.Height);
  158.             if (!playerFixed.IntersectsWith(ballFixed))
  159.                 return true;
  160.  
  161.             int movementX = ball.Location.X + ((ball.Location.X + ball.Width / 2) - (player.Location.X + player.Width / 2)) / 5;
  162.             if (movementX >= 0 && ball.Width + movementX <= panel1.Width)
  163.             {
  164.                 if(moveball)
  165.                     setBallX(movementX);
  166.                 return true;
  167.             }
  168.             return false;
  169.         }
  170.  
  171.         private bool moveBallUpAndDown(bool moveball) // player drag ball up and down
  172.         {
  173.             Rectangle playerFixed = new Rectangle(player.Location.X + 3, player.Location.Y + 6, player.Width - 6, player.Height - 12);
  174.             Rectangle ballFixed = new Rectangle(ball.Location.X, ball.Location.Y, ball.Width, ball.Height);
  175.             if (!playerFixed.IntersectsWith(ballFixed))
  176.                 return true;
  177.  
  178.             int movementY = ball.Location.Y + ((ball.Location.Y + player.Height / 2) - (player.Location.Y + player.Height / 2)) / 5;
  179.             if (movementY >= 0 && movementY + ball.Height <= panel1.Height)
  180.             {
  181.                 if(moveball)
  182.                     setBallY(movementY);
  183.                 return true;
  184.             }
  185.             return false;
  186.         }
  187.  
  188.         private void timer2_Tick(object sender, EventArgs e) // the X part of the ball shoot
  189.         {
  190.             tx += 0.2;
  191.             if(startingVelocityX > 0)
  192.                 velocityX = startingVelocityX - (int)(a * tx);
  193.             else if (startingVelocityX < 0)
  194.                 velocityX = startingVelocityX + (int)(a * tx);
  195.  
  196.             if ((ball.Location.X + ball.Width + velocityX / 5 > panel1.Width || ball.Location.X + velocityX / 5 < 0) || (ball.Bounds.IntersectsWith(player.Bounds) && tx >= 1)) // ball touch wall or player
  197.             {
  198.                 startingVelocityX *= -1;
  199.                 velocityX *= -1;
  200.             }
  201.  
  202.             setBallX(ball.Location.X + velocityX / 5);
  203.             if (( (startingVelocityX > 0 && velocityX <= 0) || (startingVelocityX < 0 && velocityX >= 0) || startingVelocityX == 0))
  204.                 timer2.Enabled = false;
  205.  
  206.         }
  207.  
  208.         private void timer3_Tick(object sender, EventArgs e) // the y part of the ball shoot
  209.         {
  210.             ty += 0.2;
  211.             if (startingVelocityY > 0)
  212.                 velocityY = startingVelocityY - (int)(a * ty);
  213.             else if (startingVelocityY < 0)
  214.                 velocityY = startingVelocityY + (int)(a * ty);
  215.  
  216.             if ((ball.Location.Y + ball.Height + velocityY / 5 > panel1.Height || ball.Location.Y + velocityY / 5 < 0) || (ball.Bounds.IntersectsWith(player.Bounds) && ty >= 1)) // ball touch wall or player
  217.             {
  218.                 startingVelocityY *= -1;
  219.                 velocityY *= -1;
  220.             }
  221.  
  222.  
  223.             setBallY(ball.Location.Y + velocityY / 5);
  224.             if (((startingVelocityY > 0 && velocityY <= 0) || (startingVelocityY < 0 && velocityY >= 0) || startingVelocityY == 0))
  225.                 timer3.Enabled = false;
  226.         }
  227.  
  228.     }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement