简易vbs贪吃蛇游戏代码

时间: 2023-09-18 17:03:52 浏览: 132
下面是一个简单的VBScript贪吃蛇游戏代码例子: ```vbscript Option Explicit Const LEFT = -1 Const RIGHT = 1 Const UP = -2 Const DOWN = 2 Dim snake(50, 2), direction, speed, xFruit, yFruit, score, gameover Sub InitGame() ReDim snake(50, 2) direction = RIGHT speed = 100 xFruit = 10 yFruit = 10 score = 0 gameover = False snake(0, 0) = 5 snake(0, 1) = 5 Randomize PlaceFruit End Sub Sub PlaceFruit() xFruit = Int((Console.WindowWidth - 1) * Rnd) + 1 yFruit = Int((Console.WindowHeight - 1) * Rnd) + 1 End Sub Sub DrawSnake() For i = 0 To score Console.SetCursorPosition(snake(i, 0), snake(i, 1)) If i = 0 Then Console.Write("o") Else Console.Write("#") End If Next End Sub Sub MoveSnake() Dim headX, headY, tailX, tailY headX = snake(0, 0) headY = snake(0, 1) Select Case direction Case LEFT headX = headX - 1 Case RIGHT headX = headX + 1 Case UP headY = headY - 1 Case DOWN headY = headY + 1 End Select tailX = snake(score, 0) tailY = snake(score, 1) For i = score To 1 Step -1 snake(i, 0) = snake(i - 1, 0) snake(i, 1) = snake(i - 1, 1) Next snake(0, 0) = headX snake(0, 1) = headY If headX = xFruit And headY = yFruit Then score = score + 1 PlaceFruit End If If headX <= 0 Or headX >= Console.WindowWidth Or headY <= 0 Or headY >= Console.WindowHeight - 1 Then gameover = True Else For i = 1 To score If headX = snake(i, 0) And headY = snake(i, 1) Then gameover = True End If Next End If If gameover Then Console.Clear Console.SetCursorPosition((Console.WindowWidth - 10) / 2, Console.WindowHeight / 2) Console.Write("Game Over") End If End Sub InitGame Do Until gameover Console.Clear DrawSnake Console.SetCursorPosition(xFruit, yFruit) Console.Write("*") WScript.Sleep(speed) MoveSnake If Console.KeyAvailable Then Select Case Asc(Console.ReadKey(True).KeyChar) Case 97, 65 ' a or A If direction <> RIGHT Then direction = LEFT Case 100, 68 ' d or D If direction <> LEFT Then direction = RIGHT Case 119, 87 ' w or W If direction <> DOWN Then direction = UP Case 115, 83 ' s or S If direction <> UP Then direction = DOWN Case 27 ' Esc gameover = True End Select End If Loop ``` 这个贪吃蛇游戏使用了控制台窗口,并且通过键盘输入“a”、“d”、“w”、“s”来控制贪吃蛇的方向。贪吃蛇的身体由“#”符号组成,头部用“o”符号表示,食物由“*”符号表示。通过吃掉食物来增加得分,如果贪吃蛇触碰到边界或者自己的身体,则游戏结束。

相关推荐

最新推荐

recommend-type

VBS基础篇 vbscript Sendkeys模拟键盘操作

在vbscript中我们可以object.SendKeys模拟键盘操作,将一个或多个按键指令发送到指定Windows窗口来控制应用程序运行,,需要的朋友可以参考下
recommend-type

wincc使用VBS操作趋势曲线.docx

wincc使用VBS脚本操作趋势曲线,此文件对1月份的所发布趋势曲线增加删除的补充,添加一个个人例子
recommend-type

wincc用vbs脚本写入sqlserver数据库.docx

wincc用VBS代码向数据库内写入数据图文教程,附带vbs代码样例环境win764+winccv7.2+sqlserver2008
recommend-type

wincc 趋势曲线VBS脚本

博途V15 wincc使用VBS脚本添加删除趋势曲线,文件为添加删除曲线脚本
recommend-type

Vbs脚本经典教材(最全的资料还是MSDN)

主要介绍了Vbs脚本经典教材,其实最全的资料还是MSDN,不过脚本之家已经为大家整理了一些,也可以查看
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。