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;
- namespace WindowsFormsApp1
- {
- public partial class Form1 : Form
- {
- private bool moveup = false;
- private bool movedown = false;
- private bool moveleft = false;
- private bool moveright = false;
- private const double a = 4;
- private double tx = 0;
- private double ty = 0;
- private int startingVelocityX;
- private int startingVelocityY;
- private int velocityX;
- private int velocityY;
- public Form1()
- {
- InitializeComponent();
- }
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- if(e.KeyCode == Keys.W)
- {
- moveup = true;
- }
- if(e.KeyCode == Keys.S)
- {
- movedown = true;
- }
- if (e.KeyCode == Keys.D)
- {
- moveright = true;
- }
- if (e.KeyCode == Keys.A)
- {
- moveleft = true;
- }
- if(e.KeyCode == Keys.Space)
- {
- Rectangle c = new Rectangle(ball.Bounds.X - 3, ball.Bounds.Y - 3, ball.Bounds.Width + 6, ball.Bounds.Height + 6);
- if (c.IntersectsWith(player.Bounds) && !timer2.Enabled)
- {
- tx = 0;
- ty = 0;
- startingVelocityX = (ball.Location.X + ball.Width / 2) - (player.Location.X + player.Width / 2);
- startingVelocityY = (ball.Location.Y + player.Height / 2) - (player.Location.Y + player.Height / 2);
- velocityX = startingVelocityX;
- velocityY = startingVelocityY;
- timer2.Enabled = true;
- timer3.Enabled = true;
- }
- }
- }
- private void Form1_KeyUp(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.W)
- {
- moveup = false;
- }
- if (e.KeyCode == Keys.S)
- {
- movedown = false;
- }
- if (e.KeyCode == Keys.D)
- {
- moveright = false;
- }
- if (e.KeyCode == Keys.A)
- {
- moveleft = false;
- }
- }
- private void timer1_Tick(object sender, EventArgs e) // player movement
- {
- if(moveup)
- {
- 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
- // the upper wall and you dont push the
- // ball to the upper wall (if
- // MoveBallUpAndDown returned True because
- // of the wall BELOW you, YOU CAN STILL
- // MOVE UP - thats what the || checks )
- setPlayerY(player.Location.Y - 3);
- }
- else if(movedown)
- {
- if(player.Location.Y + player.Height / 10 + 3 <= panel1.Height && ( moveBallUpAndDown(false) || player.Location.Y > ball.Location.Y)) // wall or ball-wall below you
- setPlayerY(player.Location.Y + 3);
- }
- if(moveright)
- {
- 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
- setPlayerX(player.Location.X + 3);
- }
- else if(moveleft)
- {
- 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
- setPlayerX(player.Location.X - 3);
- }
- if (moveright || moveleft)
- moveBallLeftAndRight(true);
- if (moveup || movedown)
- moveBallUpAndDown(true);
- }
- private void setPlayerY(int y)
- {
- var loc = player.Location;
- loc.Y = y;
- player.Location = loc;
- }
- private void setPlayerX(int x)
- {
- var loc = player.Location;
- loc.X = x;
- player.Location = loc;
- }
- private void setBallX(int x)
- {
- var loc = ball.Location;
- loc.X = x;
- ball.Location = loc;
- }
- private void setBallY(int y)
- {
- var loc = ball.Location;
- loc.Y = y;
- ball.Location = loc;
- }
- private bool moveBallLeftAndRight(bool moveball) // player drag ball left and right
- {
- Rectangle playerFixed = new Rectangle(player.Location.X + 3, player.Location.Y + 6, player.Width - 6, player.Height - 12);
- Rectangle ballFixed = new Rectangle(ball.Location.X, ball.Location.Y, ball.Width, ball.Height);
- if (!playerFixed.IntersectsWith(ballFixed))
- return true;
- int movementX = ball.Location.X + ((ball.Location.X + ball.Width / 2) - (player.Location.X + player.Width / 2)) / 5;
- if (movementX >= 0 && ball.Width + movementX <= panel1.Width)
- {
- if(moveball)
- setBallX(movementX);
- return true;
- }
- return false;
- }
- private bool moveBallUpAndDown(bool moveball) // player drag ball up and down
- {
- Rectangle playerFixed = new Rectangle(player.Location.X + 3, player.Location.Y + 6, player.Width - 6, player.Height - 12);
- Rectangle ballFixed = new Rectangle(ball.Location.X, ball.Location.Y, ball.Width, ball.Height);
- if (!playerFixed.IntersectsWith(ballFixed))
- return true;
- int movementY = ball.Location.Y + ((ball.Location.Y + player.Height / 2) - (player.Location.Y + player.Height / 2)) / 5;
- if (movementY >= 0 && movementY + ball.Height <= panel1.Height)
- {
- if(moveball)
- setBallY(movementY);
- return true;
- }
- return false;
- }
- private void timer2_Tick(object sender, EventArgs e) // the X part of the ball shoot
- {
- tx += 0.2;
- if(startingVelocityX > 0)
- velocityX = startingVelocityX - (int)(a * tx);
- else if (startingVelocityX < 0)
- velocityX = startingVelocityX + (int)(a * tx);
- 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
- {
- startingVelocityX *= -1;
- velocityX *= -1;
- }
- setBallX(ball.Location.X + velocityX / 5);
- if (( (startingVelocityX > 0 && velocityX <= 0) || (startingVelocityX < 0 && velocityX >= 0) || startingVelocityX == 0))
- timer2.Enabled = false;
- }
- private void timer3_Tick(object sender, EventArgs e) // the y part of the ball shoot
- {
- ty += 0.2;
- if (startingVelocityY > 0)
- velocityY = startingVelocityY - (int)(a * ty);
- else if (startingVelocityY < 0)
- velocityY = startingVelocityY + (int)(a * ty);
- 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
- {
- startingVelocityY *= -1;
- velocityY *= -1;
- }
- setBallY(ball.Location.Y + velocityY / 5);
- if (((startingVelocityY > 0 && velocityY <= 0) || (startingVelocityY < 0 && velocityY >= 0) || startingVelocityY == 0))
- timer3.Enabled = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement