Guest User

Untitled

a guest
May 22nd, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # ------------------------------------------------------------------------------------
  2. # PS PONG
  3. # By: Martin Engström (me222dv)
  4. #
  5. #
  6. # REQUIRES:
  7. # Powershell and .NET
  8. #
  9. # TODO:
  10. # Adjust the collision values so it looks nicer ..
  11. # Fix the bug where the computer for some reason jumps when the ball bounces
  12. # ------------------------------------------------------------------------------------
  13. #### CONSTANTS (not really constants but lets pretend they are)
  14. $PLAYER_MOVEMENT_INCREMENET = 5
  15. $RES_PLAYER = "E:\dev\pong\resources\player.bmp"
  16. $RES_BALL = "E:\dev\pong\resources\ball.bmp"
  17. $RES_WORLD = "E:\dev\pong\resources\background.bmp"
  18.  
  19. $MIN_TOP = 23
  20. $MAX_TOP = 257
  21. $MIN_LEFT = 50
  22. $MAX_LEFT = 320
  23.  
  24. ########################################################################
  25. [reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
  26.  
  27. function do_exit
  28. {
  29.      $form.close()
  30. }
  31.  
  32. function ShowSettingsDialog {
  33.     $settingsForm = New-Object Windows.Forms.Form
  34.     $settingsForm.Text = "Settings"
  35.     $speedSetting = New-Object Windows.Forms.TextBox
  36.     $speedSetting.Text = $timer.Interval
  37.     $speedSetting.Width = 150
  38.     $label = New-Object Windows.Forms.Label
  39.     $label.Text = "Tick-speed setting: (in ms)"
  40.     $okButton = New-Object Windows.Forms.Button
  41.     $okButton.Text = "Apply!"
  42.     $speedSetting.Top = $label.height + 5
  43.     $okButton.Top = $speedSetting.Top + $speedSetting.Height + 5
  44.     $timer.Stop()
  45.     $okButton.add_Click({$settingsForm.close()})
  46.     $settingsForm.Controls.Add($label)
  47.     $settingsForm.Controls.Add($speedSetting)
  48.     $settingsForm.Controls.Add($okButton)
  49.     $settingsForm.MaximizeBox = $false
  50.     $settingsForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
  51.     $settingsForm.ClientSize = New-Object System.Drawing.Size($speedSetting.Width, ($okButton.Top + $okButton.Height))
  52.     $settingsForm.ShowDialog()
  53.     $timer.Interval = $speedSetting.Text
  54.     $timer.Start()
  55.     $settingsForm.Dispose()
  56. }
  57.  
  58. $keyDown = {
  59.     ## This function will be called multiple times when a key is held
  60.     ## but thas fine, we dont do any calculations here. just set that variable
  61.     $keyPressedState = $true
  62.     $keyPressedCode = $_.KeyValue
  63.    
  64.     # Stops everything when 'Q' is pressed.
  65.     if ($_.KeyValue -eq 81) {
  66.         $form.close()
  67.     }
  68.    
  69.     # Show settings when 'C' is pressed.
  70.     if ($_.KeyValue -eq 67) {
  71.         ShowSettingsDialog
  72.     }
  73. }
  74.  
  75. $keyUp = {
  76.     $keyPressedState = $false
  77. }
  78.  
  79. function DrawWorld {
  80.     $worldResource = [System.Drawing.Image]::FromFile($RES_WORLD)
  81.     $form.BackgroundImage = $worldResource
  82.     $form.ClientSize = New-Object System.Drawing.Size($worldResource.Width, $worldResource.Height)
  83.     $form.Refresh()
  84. }
  85.  
  86. function IsInsideBounds {
  87.     param
  88.     (
  89.         $top,
  90.         $left,
  91.         $width,
  92.         $height
  93.     )
  94.    
  95.     if ($top -lt $MIN_TOP) {
  96.         return $false
  97.     }
  98.     elseif (($top + $height) -gt $MAX_TOP) {
  99.         return $false
  100.     }
  101.     elseif ($left -lt $MIN_LEFT) {
  102.         return $false
  103.     }
  104.     elseif (($left + $width) -gt $MAX_LEFT) {
  105.         return $false
  106.     }
  107.     else {
  108.         return $true
  109.     }
  110. }
  111.  
  112. function UpdateComputer {
  113.     # ITS ALIVEeeee
  114.    
  115.     ## TODO: Add some degree of random so its movement are not too accurate
  116.    
  117.     if (($ball.Top + ($ball.Height / 2)) -lt ($computer.Top)) {
  118.         # Its above us! Move up
  119.         $newTop = $computer.Top - ($PLAYER_MOVEMENT_INCREMENET - 2)
  120.     }
  121.     if (($ball.Top + ($ball.Height / 2)) -gt ($computer.Top + $computer.Height)) {
  122.         # Its below us! Move down
  123.         $newTop = $computer.Top + ($PLAYER_MOVEMENT_INCREMENET - 2)
  124.     }
  125.    
  126.     if (isInsideBounds -left $computer.Left -top $newTop -height $computer.Height -width $computer.Width) {
  127.         #Write-Host ([string]::format("OLD:{0} NEW:{1} INCREMENT:{2}", $computer.Top, $newTop, ($computer.Top - $newTop)))
  128.         $computer.Top = $newTop
  129.     }
  130. }
  131.  
  132. function UpdateBall {
  133.     $ball.Left += $ball.dx
  134.     $ball.Top += $ball.dy
  135.    
  136.     if (($ball.Top -lt $MIN_TOP) -or (($ball.Top + $ball.Height) -gt $MAX_TOP)) {
  137.         $ball.dy = -$ball.dy
  138.     }
  139.    
  140.     ## Make collision tests here
  141.    
  142.     if ($ball.Left -gt ($MAX_LEFT - $computer.Width)) {
  143.         # AI collision check
  144.         if ([System.Math]::Abs($ball.Top - ($computer.Top + 30 / 2)) -lt 30) {
  145.             $ball.dx = -$ball.dx
  146.         }
  147.         else {
  148.             if ($ball.Left -gt $MAX_LEFT) {
  149.             $timer.Stop()
  150.             $buttons = [Windows.Forms.MessageBoxButtons]::YesNo
  151.             $result = [Windows.Forms.MessageBox]::Show("You win!" + [Environment]::NewLine + "Would you like to restart?", "You lose!", $buttons)
  152.             if ($result -eq [Windows.Forms.DialogResult]::Yes) {
  153.                 ## Reset positions
  154.                 $timer.Stop()
  155.                 $ball.Left = 182
  156.                 $ball.top = 130
  157.                 $computer.Top = 130
  158.                 $player.Top = 130
  159.                 $timer.Start()
  160.             }
  161.             else {
  162.                 $form.Close()
  163.             }
  164.             }
  165.         }
  166.     }
  167.     elseif ($ball.Left -lt ($MIN_LEFT + $player.Width )) {
  168.         # Player collision check
  169.         Write-Host ([string]::Format("{0} ({1})", [System.Math]::Abs($ball.Top - ($computer.Top + 30 / 2)), $player.Top))
  170.         Write-Host "-"
  171.         if ([System.Math]::Abs($ball.Top - ($player.Top + 30 / 2)) -lt 30) {
  172.             $ball.dx = -$ball.dx
  173.         }
  174.         else {
  175.             if ($ball.Left -lt $MIN_LEFT) {
  176.             ## Add a score to the AI
  177.             ## and reset the ball.
  178.             $timer.Stop()
  179.             $buttons = [Windows.Forms.MessageBoxButtons]::YesNo
  180.             $result = [Windows.Forms.MessageBox]::Show("You lose!" + [Environment]::NewLine + "Would you like to restart?", "You lose!", $buttons)
  181.             if ($result -eq [Windows.Forms.DialogResult]::Yes) {
  182.                 ## Reset positions
  183.                 $timer.Stop()
  184.                 $ball.Left = 182
  185.                 $ball.top = 130
  186.                 $computer.Top = 130
  187.                 $player.Top = 130
  188.                 $timer.Start()
  189.             }
  190.             else {
  191.                 $form.Close()
  192.             }
  193.             }
  194.         }
  195.     }
  196. }
  197.  
  198. $gameTick = {
  199.     $timer.Stop()
  200.  
  201.     ## This is actually where everything happens.
  202.     ## In here it should draw every frame and look if a key is currently being pressed and
  203.     ## update the motion of the player accordingly. E.g. if KeyDown is held and player is not
  204.     ## at the bottom it should move him one step down
  205.  
  206.     # Keycode values
  207.     # Up: 38
  208.     # Down: 40
  209.  
  210.     #Write-Host "Tick!"
  211.  
  212.     if ($keyPressedState -eq $true) {
  213.         switch ($keyPressedCode) {
  214.             38 { $newTop = $player.Top - $PLAYER_MOVEMENT_INCREMENET }  # Arrow up
  215.             40 { $newTop = $player.Top + $PLAYER_MOVEMENT_INCREMENET }  # Arrow down
  216.         }
  217.        
  218.         # Verify that the new estimated position is within the bounds of the map
  219.         if (IsInsideBounds -left $player.Left -top $newTop -height $player.Height -width $player.Width) {
  220.                 $player.Top = $newTop
  221.         }
  222.     }
  223.    
  224.     #$timer.Stop()
  225.     UpdateBall
  226.     UpdateComputer
  227.    
  228.     $timer.Start()
  229. }
  230.  
  231.  
  232. ## Create the base window.
  233. ##########################################
  234. $form = New-Object Windows.Forms.Form
  235. $form.Text = "Martin Engström's PS PONG"
  236. $form.MaximizeBox = $false
  237. $form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
  238.  
  239. # Form event handlers
  240. $form.add_KeyDown($keyDown)
  241. $form.add_KeyUp($keyUp)
  242.  
  243.  
  244. # Set up the timer
  245. $timer = New-Object Windows.Forms.Timer
  246. $timer.interval = 300
  247. $timer.add_Tick($gameTick)
  248. $timer.Enabled = $true
  249.  
  250. # Draw the 'world'
  251. DrawWorld
  252.  
  253.  
  254. ### CREATE THE THREE INTERACTIVE RESOURCES ###
  255. ##############################################
  256. $playerResource = [System.Drawing.Image]::FromFile($RES_PLAYER)
  257. $player = New-Object Windows.Forms.PictureBox
  258. $player.Image = $playerResource
  259. $player.Size = New-Object System.Drawing.Size($playerResource.Width, $playerResource.Height)
  260. $player.Left = 50
  261. $player.Top = 130
  262. $form.Controls.Add($player)
  263.  
  264. $computer = New-Object Windows.Forms.PictureBox
  265. $computer.Image = $playerResource
  266. $computer.Size = New-Object System.Drawing.Size($playerResource.Width, $playerResource.Height)
  267. $computer.Left = 314
  268. $computer.Top = 130
  269. $form.Controls.Add($computer)
  270.  
  271. $ballResource = [System.Drawing.Image]::FromFile($RES_BALL)
  272. $ball = New-Object Windows.Forms.PictureBox
  273. $ball.Image = $ballResource
  274. $ball.Size = New-Object System.Drawing.Size($ballResource.Width, $ballResource.Height)
  275. $ball.Left = 182
  276. $ball.Top = 130
  277.  
  278. # Set ball deltas
  279. $ball | Add-Member -Name dx -MemberType NoteProperty -Value -3
  280. $ball | Add-Member -Name dy -MemberType NoteProperty -Value -3
  281.  
  282. $form.Controls.Add($ball)
  283.  
  284.  
  285. ## Show welcome messagebox! :)
  286. $timer.Stop()
  287. [Windows.Forms.MessageBox]::Show("Welcome to PS PONG!" + [Environment]::NewLine + `
  288.                                 "The controls are:" + [Environment]::NewLine + [Environment]::NewLine + `
  289.                                 "ArrowUp - Moves the paddle up" + [Environment]::NewLine + `
  290.                                 "ArrowDown - Moves the paddle down" + [Environment]::NewLine + `
  291.                                 "C - Opens the settings window" + [Environment]::NewLine + `
  292.                                 "Q - Quits the game")
  293. $timer.Start()
  294. $form.ShowDialog()
  295. # delete this before release?
  296. $form.Dispose()
Add Comment
Please, Sign In to add comment