热门标签 | 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


推荐阅读
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板
    本文介绍了在Xamarin XAML语言中如何在页面级别构建ControlTemplate控件模板的方法和步骤,包括将ResourceDictionary添加到页面中以及在ResourceDictionary中实现模板的构建。通过本文的阅读,读者可以了解到在Xamarin XAML语言中构建控件模板的具体操作步骤和语法形式。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 本文讨论了如何使用Web.Config进行自定义配置节的配置转换。作者提到,他将msbuild设置为详细模式,但转换却忽略了带有替换转换的自定义部分的存在。 ... [详细]
  • 图像因存在错误而无法显示 ... [详细]
  • Android自定义控件绘图篇之Paint函数大汇总
    本文介绍了Android自定义控件绘图篇中的Paint函数大汇总,包括重置画笔、设置颜色、设置透明度、设置样式、设置宽度、设置抗锯齿等功能。通过学习这些函数,可以更好地掌握Paint的用法。 ... [详细]
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社区 版权所有