Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 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 PingPong
  12. {
  13. public partial class Form1 : Form
  14. {
  15.  
  16. public int speed_left = 4; //Speed of the ball
  17. public int speed_top = 4;
  18. public int points = 0; //Scored points
  19.  
  20. public Form1()
  21. {
  22. InitializeComponent();
  23.  
  24. timer1.Enabled = true;
  25. Cursor.Hide(); //Hide the cursor
  26.  
  27. this.FormBorderStyle = FormBorderStyle.None; //Remove any border
  28. this.TopMost = true; //Bring the form to the front
  29. this.Bounds = Screen.PrimaryScreen.Bounds; //Make it fullscreen
  30.  
  31. racket.Top = playarea.Bottom - (playarea.Bottom / 10); //Set the position of racket
  32.  
  33.  
  34.  
  35. }
  36.  
  37. private void Timer1_Tick(object sender, EventArgs e)
  38. {
  39.  
  40. racket.Left = Cursor.Position.X - (racket.Width / 2); //Set the center of the racket to the position of the cursor
  41.  
  42. ball.Left += speed_left; //Move the ball
  43. ball.Top += speed_top;
  44.  
  45. if (ball.Bottom >= racket.Top && ball.Bottom <= racket.Bottom && ball.Left >= racket.Left && ball.Right <= racket.Right) //Racket collision
  46. {
  47. speed_top += 2;
  48. speed_left += 2;
  49. speed_top = -speed_top; //Change direction
  50. points += 1;
  51. points_lbl.Text = points.ToString();
  52.  
  53.  
  54.  
  55. }
  56.  
  57. if (ball.Left <= playarea.Left)
  58. {
  59. speed_left = -speed_left;
  60. }
  61. if (ball.Right <= playarea.Right)
  62. {
  63. speed_left = -speed_left;
  64. }
  65. if (ball.Top <= playarea.Top)
  66. {
  67. speed_top = -speed_top;
  68. }
  69.  
  70. if (ball.Bottom >= playarea.Bottom)
  71. {
  72. timer1.Enabled = false; //Ball is out -> Stop the game
  73. }
  74.  
  75.  
  76.  
  77.  
  78. }
  79.  
  80. private void Form1_KeyDown(object sender, KeyEventArgs e)
  81. {
  82. if (e.KeyCode == Keys.Escape) { this.Close(); } //Press Escape to Quit
  83. }
  84.  
  85.  
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement