Advertisement
Guest User

Untitled

a guest
May 29th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. package {
  2. //imports
  3. import flash.display.MovieClip;
  4. import flash.events.KeyboardEvent;
  5. import flash.ui.Keyboard;
  6. import flash.events.Event;
  7.  
  8. public class Main extends MovieClip {
  9. //constants
  10. const ballspeed:int = 10;
  11. const playerspeed:int = 7;
  12. const computerspeed:int = 10;
  13. const computerIntelligence:int = 7;//intelligence is 7 out of 10
  14.  
  15. //global variables
  16. var vx:int = -ballspeed; // x component of velocity of ball (velocity is speed with direction)
  17. var vy:int = ballspeed; // y component of velocity of ball
  18. var v1:int = 0; // initial velocity of player
  19. var v2:int = 0; // initial velocity of computer
  20. var playerScore:int = 0;
  21. var computerScore:int = 0;
  22.  
  23. public function Main() {
  24. init();
  25. }
  26. //this function will add all event listeners
  27. function init():void {
  28. stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
  29. stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
  30. addEventListener(Event.ENTER_FRAME,onEnterFrame);
  31. }
  32. // this function resets the game
  33. function reset():void {
  34. player.y = stage.stageHeight/2;
  35. computer.y = stage.stageHeight/2;
  36. ball.x = stage.stageWidth/2;
  37. ball.y = stage.stageHeight/2;
  38. if(Math.abs(Math.random()*2) > 1){
  39. vx = -ballspeed;
  40. }else{
  41. vx = ballspeed;
  42. }
  43. if(Math.abs(Math.random()*2) > 1){
  44. vy = -ballspeed;
  45. }else{
  46. vy = ballspeed;
  47. }
  48. }
  49. //this function sets the velocity of player when key is pressed
  50. function onKeyDown(event:KeyboardEvent):void {
  51. if(event.keyCode == Keyboard.UP) {
  52. v1 = -playerspeed;
  53. }else if(event.keyCode == Keyboard.DOWN) {
  54. v1 = playerspeed;
  55. }
  56. }
  57. //this function sets the velocity of player to 0 if key is released
  58. function onKeyUp(event:KeyboardEvent):void {
  59. if(event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN) {
  60. v1 = 0;
  61. }
  62. }
  63.  
  64. //This function is executed when a frame changes
  65. function onEnterFrame(event:Event):void {
  66. //variable decleration
  67. var pHalfHeight = player.height/2; // half height of player(used for collisions)
  68. var pHalfWidth = player.width/2; // half width of player (used for collisions)
  69. var bHalfHeight = ball.height/2; // half height of ball(used for collisions)
  70. var bHalfWidth = ball.width/2; // half width of ball (used for collisions)
  71.  
  72. //moving the player
  73. player.y += v1;
  74. //limiting the motion of player (it should not move beyond the stageheight)
  75. if(player.y + pHalfHeight > stage.stageHeight) {
  76. player.y = stage.stageHeight - pHalfHeight;
  77. }else if(player.y - pHalfHeight < 0) {
  78. player.y = 0 + pHalfHeight;
  79. }
  80.  
  81. //moving the ball
  82. ball.x += vx;
  83. ball.y += vy;
  84.  
  85. //moving the computer automatically
  86. if(Math.abs(Math.random()*10) < computerIntelligence){
  87. var d:int = computer.y - ball.y;
  88. if(Math.abs(d) > pHalfHeight){
  89. if(d>0) {
  90. v2 = -computerspeed;
  91. }else{
  92. v2 = computerspeed;
  93. }
  94. }
  95. }
  96. computer.y += v2;
  97. //limiting the motion of computer (it should not move beyond the stageheight)
  98. if(computer.y + pHalfHeight > stage.stageHeight) {
  99. computer.y = stage.stageHeight - pHalfHeight;
  100. }else if(computer.y - pHalfHeight < 0) {
  101. computer.y = 0 + pHalfHeight;
  102. }
  103.  
  104. //collision with horizontal walls
  105. if(ball.y + bHalfHeight >= stage.stageHeight || ball.y - bHalfHeight <= 0) {
  106. vy *= -1;
  107. }
  108.  
  109. //collision with player and computer
  110. if(ball.x - bHalfWidth <= player.x + pHalfWidth) {
  111. if(Math.abs(ball.y - player.y) <= pHalfHeight) {
  112. vx = ballspeed;
  113. if(v1!=0){
  114. vy = 2*v1;
  115. }
  116. }
  117. }else if(ball.x + bHalfWidth >= computer.x - pHalfWidth) {
  118. if(Math.abs(ball.y - computer.y) <= pHalfHeight) {
  119. vx = -ballspeed;
  120. if(v2!=0){
  121. vy = v2;
  122. }
  123. }
  124. }
  125.  
  126. //collision with vertical walls & updating scores
  127. if(ball.x + bHalfWidth >= stage.stageWidth) {
  128. playerScore += 1;
  129. reset();
  130. }else if(ball.x - bHalfWidth <= 0) {
  131. computerScore += 1;
  132. reset();
  133. }
  134.  
  135. //display the score on the textfield
  136. txtPlayer.text = String(playerScore);
  137. txtComputer.text = String(computerScore);
  138. }
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement