Initial commit: Oraset B-5 with C++ style features
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
# 贪吃蛇游戏
|
||||
|
||||
这是一个用 Oraset B-5 编写的控制台贪吃蛇游戏!
|
||||
|
||||
## 游戏说明
|
||||
|
||||
使用方向键控制蛇的移动方向,吃食物得分。
|
||||
|
||||
## 运行方法
|
||||
|
||||
```bash
|
||||
oraset examples/snake.ora
|
||||
```
|
||||
|
||||
## 代码
|
||||
|
||||
```oraset
|
||||
// snake.ora - 贪吃蛇游戏
|
||||
// 使用 w/a/s/d 或方向键控制
|
||||
// Oraset B-5 版本 - 使用数组存储蛇的位置
|
||||
|
||||
var width = 40;
|
||||
var height = 20;
|
||||
var speed = 150;
|
||||
var game_over = 0;
|
||||
var score = 0;
|
||||
|
||||
var snake_x = [10, 9, 8];
|
||||
var snake_y = [10, 10, 10];
|
||||
var snake_len = 3;
|
||||
|
||||
var food_x = 25;
|
||||
var food_y = 10;
|
||||
|
||||
var direction = 1;
|
||||
var next_direction = 1;
|
||||
|
||||
function generate_food() {
|
||||
food_x = int(rand() * (width - 4)) + 2;
|
||||
food_y = int(rand() * (height - 4)) + 2;
|
||||
}
|
||||
|
||||
generate_food();
|
||||
|
||||
while (game_over == 0) {
|
||||
direction = next_direction;
|
||||
|
||||
var new_head_x = snake_x[0];
|
||||
var new_head_y = snake_y[0];
|
||||
|
||||
if (direction == 0) new_head_y--;
|
||||
if (direction == 1) new_head_x++;
|
||||
if (direction == 2) new_head_y++;
|
||||
if (direction == 3) new_head_x--;
|
||||
|
||||
if (new_head_x <= 0 || new_head_x >= width - 1 ||
|
||||
new_head_y <= 0 || new_head_y >= height - 1) {
|
||||
game_over = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
var i = 0;
|
||||
while (i < snake_len) {
|
||||
if (new_head_x == snake_x[i] && new_head_y == snake_y[i]) {
|
||||
game_over = 1;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (game_over == 1) break;
|
||||
|
||||
if (new_head_x == food_x && new_head_y == food_y) {
|
||||
score++;
|
||||
snake_len++;
|
||||
generate_food();
|
||||
}
|
||||
|
||||
i = snake_len - 1;
|
||||
while (i > 0) {
|
||||
snake_x[i] = snake_x[i - 1];
|
||||
snake_y[i] = snake_y[i - 1];
|
||||
i--;
|
||||
}
|
||||
|
||||
snake_x[0] = new_head_x;
|
||||
snake_y[0] = new_head_y;
|
||||
|
||||
var y = 0;
|
||||
while (y < height) {
|
||||
var x = 0;
|
||||
while (x < width) {
|
||||
var is_snake = 0;
|
||||
var is_food = 0;
|
||||
|
||||
i = 0;
|
||||
while (i < snake_len) {
|
||||
if (x == snake_x[i] && y == snake_y[i]) {
|
||||
is_snake = 1;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if (x == food_x && y == food_y) {
|
||||
is_food = 1;
|
||||
}
|
||||
|
||||
if (is_snake == 1) {
|
||||
print("O", "nel");
|
||||
} else if (is_food == 1) {
|
||||
print("*", "nel");
|
||||
} else if (x == 0 || x == width - 1 || y == 0 || y == height - 1) {
|
||||
print("-", "nel");
|
||||
} else {
|
||||
print(" ", "nel");
|
||||
}
|
||||
x++;
|
||||
}
|
||||
print("");
|
||||
y++;
|
||||
}
|
||||
|
||||
print("分数:", score, " | 长度:", snake_len);
|
||||
print("w=上 a=左 s=下 d=右");
|
||||
}
|
||||
|
||||
print("游戏结束! 最终分数:", score);
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
这个版本使用了 Oraset B-5 的数组功能,代码更加简洁高效。
|
||||
蛇的身体现在存储在数组中,可以无限增长。
|
||||
Reference in New Issue
Block a user