Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ------------------------------------------------------------------------------------
- # PS PONG
- # By: Martin Engström (me222dv)
- #
- #
- # REQUIRES:
- # Powershell and .NET
- #
- # TODO:
- # Adjust the collision values so it looks nicer ..
- # Fix the bug where the computer for some reason jumps when the ball bounces
- # ------------------------------------------------------------------------------------
- #### CONSTANTS (not really constants but lets pretend they are)
- $PLAYER_MOVEMENT_INCREMENET = 5
- $RES_PLAYER = "E:\dev\pong\resources\player.bmp"
- $RES_BALL = "E:\dev\pong\resources\ball.bmp"
- $RES_WORLD = "E:\dev\pong\resources\background.bmp"
- $MIN_TOP = 23
- $MAX_TOP = 257
- $MIN_LEFT = 50
- $MAX_LEFT = 320
- ########################################################################
- [reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
- function do_exit
- {
- $form.close()
- }
- function ShowSettingsDialog {
- $settingsForm = New-Object Windows.Forms.Form
- $settingsForm.Text = "Settings"
- $speedSetting = New-Object Windows.Forms.TextBox
- $speedSetting.Text = $timer.Interval
- $speedSetting.Width = 150
- $label = New-Object Windows.Forms.Label
- $label.Text = "Tick-speed setting: (in ms)"
- $okButton = New-Object Windows.Forms.Button
- $okButton.Text = "Apply!"
- $speedSetting.Top = $label.height + 5
- $okButton.Top = $speedSetting.Top + $speedSetting.Height + 5
- $timer.Stop()
- $okButton.add_Click({$settingsForm.close()})
- $settingsForm.Controls.Add($label)
- $settingsForm.Controls.Add($speedSetting)
- $settingsForm.Controls.Add($okButton)
- $settingsForm.MaximizeBox = $false
- $settingsForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
- $settingsForm.ClientSize = New-Object System.Drawing.Size($speedSetting.Width, ($okButton.Top + $okButton.Height))
- $settingsForm.ShowDialog()
- $timer.Interval = $speedSetting.Text
- $timer.Start()
- $settingsForm.Dispose()
- }
- $keyDown = {
- ## This function will be called multiple times when a key is held
- ## but thas fine, we dont do any calculations here. just set that variable
- $keyPressedState = $true
- $keyPressedCode = $_.KeyValue
- # Stops everything when 'Q' is pressed.
- if ($_.KeyValue -eq 81) {
- $form.close()
- }
- # Show settings when 'C' is pressed.
- if ($_.KeyValue -eq 67) {
- ShowSettingsDialog
- }
- }
- $keyUp = {
- $keyPressedState = $false
- }
- function DrawWorld {
- $worldResource = [System.Drawing.Image]::FromFile($RES_WORLD)
- $form.BackgroundImage = $worldResource
- $form.ClientSize = New-Object System.Drawing.Size($worldResource.Width, $worldResource.Height)
- $form.Refresh()
- }
- function IsInsideBounds {
- param
- (
- $top,
- $left,
- $width,
- $height
- )
- if ($top -lt $MIN_TOP) {
- return $false
- }
- elseif (($top + $height) -gt $MAX_TOP) {
- return $false
- }
- elseif ($left -lt $MIN_LEFT) {
- return $false
- }
- elseif (($left + $width) -gt $MAX_LEFT) {
- return $false
- }
- else {
- return $true
- }
- }
- function UpdateComputer {
- # ITS ALIVEeeee
- ## TODO: Add some degree of random so its movement are not too accurate
- if (($ball.Top + ($ball.Height / 2)) -lt ($computer.Top)) {
- # Its above us! Move up
- $newTop = $computer.Top - ($PLAYER_MOVEMENT_INCREMENET - 2)
- }
- if (($ball.Top + ($ball.Height / 2)) -gt ($computer.Top + $computer.Height)) {
- # Its below us! Move down
- $newTop = $computer.Top + ($PLAYER_MOVEMENT_INCREMENET - 2)
- }
- if (isInsideBounds -left $computer.Left -top $newTop -height $computer.Height -width $computer.Width) {
- #Write-Host ([string]::format("OLD:{0} NEW:{1} INCREMENT:{2}", $computer.Top, $newTop, ($computer.Top - $newTop)))
- $computer.Top = $newTop
- }
- }
- function UpdateBall {
- $ball.Left += $ball.dx
- $ball.Top += $ball.dy
- if (($ball.Top -lt $MIN_TOP) -or (($ball.Top + $ball.Height) -gt $MAX_TOP)) {
- $ball.dy = -$ball.dy
- }
- ## Make collision tests here
- if ($ball.Left -gt ($MAX_LEFT - $computer.Width)) {
- # AI collision check
- if ([System.Math]::Abs($ball.Top - ($computer.Top + 30 / 2)) -lt 30) {
- $ball.dx = -$ball.dx
- }
- else {
- if ($ball.Left -gt $MAX_LEFT) {
- $timer.Stop()
- $buttons = [Windows.Forms.MessageBoxButtons]::YesNo
- $result = [Windows.Forms.MessageBox]::Show("You win!" + [Environment]::NewLine + "Would you like to restart?", "You lose!", $buttons)
- if ($result -eq [Windows.Forms.DialogResult]::Yes) {
- ## Reset positions
- $timer.Stop()
- $ball.Left = 182
- $ball.top = 130
- $computer.Top = 130
- $player.Top = 130
- $timer.Start()
- }
- else {
- $form.Close()
- }
- }
- }
- }
- elseif ($ball.Left -lt ($MIN_LEFT + $player.Width )) {
- # Player collision check
- Write-Host ([string]::Format("{0} ({1})", [System.Math]::Abs($ball.Top - ($computer.Top + 30 / 2)), $player.Top))
- Write-Host "-"
- if ([System.Math]::Abs($ball.Top - ($player.Top + 30 / 2)) -lt 30) {
- $ball.dx = -$ball.dx
- }
- else {
- if ($ball.Left -lt $MIN_LEFT) {
- ## Add a score to the AI
- ## and reset the ball.
- $timer.Stop()
- $buttons = [Windows.Forms.MessageBoxButtons]::YesNo
- $result = [Windows.Forms.MessageBox]::Show("You lose!" + [Environment]::NewLine + "Would you like to restart?", "You lose!", $buttons)
- if ($result -eq [Windows.Forms.DialogResult]::Yes) {
- ## Reset positions
- $timer.Stop()
- $ball.Left = 182
- $ball.top = 130
- $computer.Top = 130
- $player.Top = 130
- $timer.Start()
- }
- else {
- $form.Close()
- }
- }
- }
- }
- }
- $gameTick = {
- $timer.Stop()
- ## This is actually where everything happens.
- ## In here it should draw every frame and look if a key is currently being pressed and
- ## update the motion of the player accordingly. E.g. if KeyDown is held and player is not
- ## at the bottom it should move him one step down
- # Keycode values
- # Up: 38
- # Down: 40
- #Write-Host "Tick!"
- if ($keyPressedState -eq $true) {
- switch ($keyPressedCode) {
- 38 { $newTop = $player.Top - $PLAYER_MOVEMENT_INCREMENET } # Arrow up
- 40 { $newTop = $player.Top + $PLAYER_MOVEMENT_INCREMENET } # Arrow down
- }
- # Verify that the new estimated position is within the bounds of the map
- if (IsInsideBounds -left $player.Left -top $newTop -height $player.Height -width $player.Width) {
- $player.Top = $newTop
- }
- }
- #$timer.Stop()
- UpdateBall
- UpdateComputer
- $timer.Start()
- }
- ## Create the base window.
- ##########################################
- $form = New-Object Windows.Forms.Form
- $form.Text = "Martin Engström's PS PONG"
- $form.MaximizeBox = $false
- $form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
- # Form event handlers
- $form.add_KeyDown($keyDown)
- $form.add_KeyUp($keyUp)
- # Set up the timer
- $timer = New-Object Windows.Forms.Timer
- $timer.interval = 300
- $timer.add_Tick($gameTick)
- $timer.Enabled = $true
- # Draw the 'world'
- DrawWorld
- ### CREATE THE THREE INTERACTIVE RESOURCES ###
- ##############################################
- $playerResource = [System.Drawing.Image]::FromFile($RES_PLAYER)
- $player = New-Object Windows.Forms.PictureBox
- $player.Image = $playerResource
- $player.Size = New-Object System.Drawing.Size($playerResource.Width, $playerResource.Height)
- $player.Left = 50
- $player.Top = 130
- $form.Controls.Add($player)
- $computer = New-Object Windows.Forms.PictureBox
- $computer.Image = $playerResource
- $computer.Size = New-Object System.Drawing.Size($playerResource.Width, $playerResource.Height)
- $computer.Left = 314
- $computer.Top = 130
- $form.Controls.Add($computer)
- $ballResource = [System.Drawing.Image]::FromFile($RES_BALL)
- $ball = New-Object Windows.Forms.PictureBox
- $ball.Image = $ballResource
- $ball.Size = New-Object System.Drawing.Size($ballResource.Width, $ballResource.Height)
- $ball.Left = 182
- $ball.Top = 130
- # Set ball deltas
- $ball | Add-Member -Name dx -MemberType NoteProperty -Value -3
- $ball | Add-Member -Name dy -MemberType NoteProperty -Value -3
- $form.Controls.Add($ball)
- ## Show welcome messagebox! :)
- $timer.Stop()
- [Windows.Forms.MessageBox]::Show("Welcome to PS PONG!" + [Environment]::NewLine + `
- "The controls are:" + [Environment]::NewLine + [Environment]::NewLine + `
- "ArrowUp - Moves the paddle up" + [Environment]::NewLine + `
- "ArrowDown - Moves the paddle down" + [Environment]::NewLine + `
- "C - Opens the settings window" + [Environment]::NewLine + `
- "Q - Quits the game")
- $timer.Start()
- $form.ShowDialog()
- # delete this before release?
- $form.Dispose()
Add Comment
Please, Sign In to add comment