fine吧 关注:373贴子:1,746
  • 0回复贴,共1

fine语言编写的贪吃蛇游戏

只看楼主收藏回复

# 创建 GUI 对象
gui = GUI()
gui.HideConsoleWindow() # 隐藏控制台窗口
math = MATH()
time = TIME()
os = OS()
# 定义窗口和游戏区域大小
WINDOW_WIDTH = 48
WINDOW_HEIGHT = 31
GAME_WIDTH = 30 # 窗口宽度减去滚动条宽度
GAME_HEIGHT = 18
global board_str = "" # 显示缓冲区
# 初始化蛇和食物
point0 = [2,3]
point1 = [1,3]
point2 = [0,3]
global snake_x = [point0[0], point1[0], point2[0]]
global snake_y = [point0[1], point1[1], point2[1]]
global food_x = math.rand(1, GAME_WIDTH-2)
global food_y = math.rand(1, GAME_HEIGHT-2)
global direction = "right"
# 窗口标题
title = "贪食蛇游戏"
# 窗口大小
winSize = [30, 5, WINDOW_WIDTH, WINDOW_HEIGHT]
# 四个方向控制按钮
button_up = ["上", "button", 2, 26, 5, 1]
button_down = ["下", "button", 9, 26, 5, 1]
button_left = ["左", "button", 16, 26, 5, 1]
button_right = ["右", "button", 23, 26, 5, 1]
# textbox 控件用于显示游戏画面
textbox = ["游戏画面|新宋体", "textbox", "N", 0, 0, GAME_WIDTH, GAME_HEIGHT]
# 打包窗口元素
element_list = [title, winSize, button_up, button_down, button_left, button_right, textbox]
# 创建窗口并获取窗口资源 ID
window_id = gui.Fine(element_list)
# 绘制游戏画面到 textbox
def draw_game()
{
# 食物坐标是:[food_x,food_y]
board_str = "" # board_str是显示缓冲区,将二维数组转换为字符串
for j in range(0,GAME_HEIGHT,1) # textbox显示区的行循环
{
for i in range(0,GAME_WIDTH,1) # textbox显示区的列循环
{
# food
if i == food_x and j == food_y # food是一个包含两项的列表,描述食物的位置[x,y],第x行、y列
{
board_str = board_str + "*"
}
# snake body
if i in snake_x and j in snake_y
{
board_str = board_str + "o"
}
else
{
board_str = board_str + " "
}
}
board_str = board_str + "\n"
}
gui.SendTextbox(window_id, [board_str])
}
# 移动蛇
def move_snake()
{
head_x = snake_x[0]
head_y = snake_y[0]
if direction == "right"
{
head_x = head_x + 1
}
elif direction == "left"
{
head_x = head_x - 1
}
elif direction == "up"
{
head_y = head_y - 1
}
elif direction == "down"
{
head_y = head_y + 1
}
# 边界检测
if head_x < 0 or head_x >= GAME_WIDTH or head_y < 0 or head_y >= GAME_HEIGHT
{
box = gui.MessageBox("游戏结束", "确定")
while gui.MessageBoxClosed(box) != -1 # 等待关闭窗口消息
{
PowerDown(3) # 低功耗设置(分1、2、3级,基数越大、功耗越低)
}
return False
}
snake_x.insert(0, head_x)
snake_y.insert(0, head_y)
# 判断是否吃到食物
if (head_x >= food_x-1 and head_x <= food_x+1) and (head_y >= food_y-1 and head_y <= food_y+1 )
{
# 如果吃到食物,重新随机放置食物
food_x = math.rand(1, GAME_WIDTH - 2)
food_y = math.rand(1, GAME_HEIGHT - 2)
}
else
{
# 如果没有吃到食物,snake恢复原来长度(抛弃最后一个字符)
snake_x.pop()
snake_y.pop()
}
return True
}
# 主循环
while gui.FineClosed(window_id) != -1
{
PowerDown(3)
if gui.FineReady(window_id) == -1
{
gui.SendTextbox(window_id, ["FINECLEAR"]) # 清屏
draw_game()
time.sleep(800) # 控制蛇的移动速度
if not move_snake() # 按既定方向移动一位,如果越界game over,没有越界继续
{
gui.CloseFine(window_id) # 关闭窗口
}
continue
}
input_data = gui.FineRead(window_id)
button_action = input_data[0]
if button_action == "button-0" # 上
{
direction = "up"
}
elif button_action == "button-1" # 下
{
direction = "down"
}
elif button_action == "button-2" # 左
{
direction = "left"
}
elif button_action == "button-3" # 右
{
direction = "right"
}
}


IP属地:河北1楼2025-02-06 17:16回复