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 PingPong
- {
- public partial class Form1 : Form
- {
- public int speed_left = 4; //Speed of the ball
- public int speed_top = 4;
- public int points = 0; //Scored points
- public Form1()
- {
- InitializeComponent();
- timer1.Enabled = true;
- Cursor.Hide(); //Hide the cursor
- this.FormBorderStyle = FormBorderStyle.None; //Remove any border
- this.TopMost = true; //Bring the form to the front
- this.Bounds = Screen.PrimaryScreen.Bounds; //Make it fullscreen
- racket.Top = playarea.Bottom - (playarea.Bottom / 10); //Set the position of racket
- }
- private void Timer1_Tick(object sender, EventArgs e)
- {
- racket.Left = Cursor.Position.X - (racket.Width / 2); //Set the center of the racket to the position of the cursor
- ball.Left += speed_left; //Move the ball
- ball.Top += speed_top;
- if (ball.Bottom >= racket.Top && ball.Bottom <= racket.Bottom && ball.Left >= racket.Left && ball.Right <= racket.Right) //Racket collision
- {
- speed_top += 2;
- speed_left += 2;
- speed_top = -speed_top; //Change direction
- points += 1;
- points_lbl.Text = points.ToString();
- }
- if (ball.Left <= playarea.Left)
- {
- speed_left = -speed_left;
- }
- if (ball.Right <= playarea.Right)
- {
- speed_left = -speed_left;
- }
- if (ball.Top <= playarea.Top)
- {
- speed_top = -speed_top;
- }
- if (ball.Bottom >= playarea.Bottom)
- {
- timer1.Enabled = false; //Ball is out -> Stop the game
- }
- }
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Escape) { this.Close(); } //Press Escape to Quit
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement