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


推荐阅读
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Android JSON基础,音视频开发进阶指南目录
    Array里面的对象数据是有序的,json字符串最外层是方括号的,方括号:[]解析jsonArray代码try{json字符串最外层是 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 本文介绍了iOS数据库Sqlite的SQL语句分类和常见约束关键字。SQL语句分为DDL、DML和DQL三种类型,其中DDL语句用于定义、删除和修改数据表,关键字包括create、drop和alter。常见约束关键字包括if not exists、if exists、primary key、autoincrement、not null和default。此外,还介绍了常见的数据库数据类型,包括integer、text和real。 ... [详细]
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社区 版权所有