热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

powershellSnake更新

篇首语:本文由编程笔记#小编为大家整理,主要介绍了powershellSnake更新相关的知识,希望对你有一定的参考价值。

篇首语:本文由编程笔记#小编为大家整理,主要介绍了powershell Snake更新相关的知识,希望对你有一定的参考价值。




#requires -version 2
#
# Powershell Snake Game
# Author : Kurt Jaegers
#
function SetEmptySquare($x, $y)
{
$matrix[$x, $y] = $emptysquare
[console]::SetCursorPosition($x + 1, $y + 1)
Write-Host -ForegroundColor White -BackgroundColor Black -NoNewline " "
}
function SetBodySquare($x, $y)
{
$matrix[$x, $y] = $bodysquare
[console]::SetCursorPosition($x + 1, $y + 1)
Write-Host -ForegroundColor White -BackgroundColor White -NoNewline " "
}
#
# Draws the snake to the screen, including cleaning up the last segment of the tail
#
function DrawTheSnake($x, $y)
{
$newPoint = New-Object System.Drawing.Point($x, $y)
$tail.Enqueue($newPoint)
SetBodySquare -x $x -y $y
if ($tail.Count -gt $script:maxTailLength)
{
$oldPoint = $tail.Dequeue()
SetEmptySquare -x $oldPoint.X -y $oldPoint.Y
}
}
#
# Generate a random location for the apple, making sure it isnt inside the snake
#
function MoveTheApple
{
do
{
$x = get-random -min 2 -max ($width - 2)
$y = get-random -min 2 -max ($height - 2)
}
until ($matrix[$x, $y] -eq $emptysquare )
$matrix[$x, $y] = $applesquare
DrawTheApple -x $x -y $y
}
#
# Draw the apple to the screen
#
function DrawTheApple($x, $y)
{
[console]::SetCursorPosition($x + 1, $y + 1)
Write-Host -foregroundcolor red -backgroundcolor black "@"
}
#
# Check to see if the snake hits the apple
#
function CheckAppleHit($x, $y)
{
if ($matrix[$x, $y] -eq $applesquare)
{
# relocate the apple
MoveTheApple
SetEmptySquare -x $x -y $y

$script:score += 500

# Add to the snake's length
$script:maxTailLength++
}
}
#
# Check to see if the snake's head hits the walls of the screen
#
function CheckWallHits($x, $y)
{
if ($matrix[$x, $y] -eq $wallsquare)
{
cls
write-host -foregroundcolor red "You lost! Score was $script:score"
exit
}
}
function SetBorderSquare($x, $y)
{
[console]::SetCursorPosition($x + 1, $y + 1)
Write-Host -ForegroundColor Black -BackgroundColor White '#' -NoNewline
$matrix[$x, $y] = $wallsquare
}
#
# Draw a fence around the edges of the screen
#
function DrawScreenBorders
{
for ($x = 0; $x -lt $width; $x++)
{
SetBorderSquare -x $x -y 0
SetBorderSquare -x $x -y ($height - 1)
}
for ($y = 0; $y -lt $height; $y++)
{
SetBorderSquare -x 0 -y $y
SetBorderSquare -x ($width - 1) -y $y
}
}
function CheckSnakeBodyHits($x, $y)
{
if ($matrix[$x, $y] -eq $bodysquare)
{
cls
write-host -foregroundcolor red "You lost! Score was $script:score"
exit
}
}
function DrawScore($score)
{
$string = "Score: $score"
$xPos = [int](($script:width - $string.Length) / 2)
[console]::SetCursorPosition($xPos, 0)
Write-Host -ForegroundColor Red -BackgroundColor Black $string
}
# ---------------------------------
# ---------------------------------
# Main script block starts here
# ---------------------------------
# ---------------------------------
if ($host.name -ne "ConsoleHost")
{
write-host "This script should only be run in a ConsoleHost window (outside of the ISE)"
exit
$dOne=$true
}
Add-Type -AssemblyName System.Drawing
# Grab UI objects and set some colors
$ui=(get-host).ui
$rui=$ui.rawui
$rui.BackgroundColor="Black"
$rui.ForegroundColor="Red"
cls
# write out lines to make sure the buffer is big enough to cover the screen
for ($i=0; $i -lt $rui.screensize.height; $i++)
{
write-host ""
}
$cs = $rui.cursorsize
$rui.cursorsize=0
$script:score = 0
$width = $rui.WindowSize.Width - 2
$height = $rui.WindowSize.Height - 2
$emptysquare = 0
$bodysquare = 1
$applesquare = 2
$wallsquare = 3
$currentX = [int]($width / 2)
$currentY = [int]($height / 2)
$matrix = New-Object 'int[,]' -ArgumentList ($width, $height)
$tail = New-Object System.Collections.Queue
$script:maxTailLength = 5
$dOne= $false
$before = 0
$after = 15
$dir = 0
DrawScreenBorders;
DrawTheSnake -x $currentX -y $currentY
MoveTheApple;
while (!$done)
{
if ($rui.KeyAvailable)
{
$key = $rui.ReadKey()
if ($key.virtualkeycode -eq -27)
{
$dOne=$true
}
if ($key.keydown)
{
# Left
if ($key.virtualkeycode -eq 37)
{
$dir=0
}
# Up
if ($key.virtualkeycode -eq 38)
{
$dir=1
}
# Right
if ($key.virtualkeycode -eq 39)
{
$dir=2
}
# Down
if ($key.virtualkeycode -eq 40)
{
$dir=3
}
}
}

if ($dir -eq 0)
{
$currentX--;
}

if ($dir -eq 1)
{
$currentY--;
}

if ($dir -eq 2)
{
$currentX++;
}

if ($dir -eq 3)
{
$currentY++;
}
CheckWallHits -x $currentX -y $currentY
CheckSnakeBodyHits -x $currentX -y $currentY
CheckAppleHit -x $currentX -y $currentY
DrawTheSnake -x $currentX -y $currentY

$script:score += $script:maxTailLength
DrawScore -score $script:score
start-sleep -mil 100
}
$rui.cursorsize=$cs


推荐阅读
author-avatar
xsf9507
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有