当前位置:网站首页>【plang 1.4.4】编写贪吃蛇脚本
【plang 1.4.4】编写贪吃蛇脚本
2022-08-02 03:29:00 【ChivenZhang】
最近打算用plang编程语言编写一些小游戏demo,顺便测测语言工具的健壮性。贪吃蛇是小时候喜欢玩的,就决定是它了。
先设计Snake类:
package snake;
import gui::GUI;
import std::collection::ArrayList;
public class Snake : GameObject
{
public Snake()
{
super();
i->int = 0;
while(i < s.length)
{
s[i][0] = i;
s[i][1] = 0;
++i;
}
}
// 绘制
public update()
{
i->int = 0;
while(i<s.length)
{
gui.draw(s[i][0], s[i][1], '6');
++i;
}
}
// 更新位置
public update(x->int, y->int)->boolean
{
i->int=0;
while(i+2<s.length)
{
if(x == s[i][0] && y == s[i][1])
{
return false;
}
++i;
}
if(x == s[s.length-2][0] && y == s[s.length-2][1])
{
return true;
}
i=0;
while(i+1<s.length)
{
s[i][0] = s[i+1][0];
s[i][1] = s[i+1][1];
++i;
}
s[s.length-1][0] = x;
s[s.length-1][1] = y;
return true;
}
public getX()->int
{
return s[s.length-1][0];
}
public getY()->int
{
return s[s.length-1][1];
}
s->int[][] = new int[10][2];
}
食物类:
package snake;
public class Food : GameObject
{
public Food()
{
super();
}
// 绘制
public update()
{
gui.draw(x, y, '@');
}
// 更新位置
public update(x->int, y->int)->boolean
{
if(x==this.x && y==this.y)
{
return true;
}
this.x = x;
this.y = y;
return true;
}
public getX()->int {return x;}
public getY()->int {return y;}
protected x->int = 0;
protected y->int = 0;
}
再写一个场景类测试一下,这条代码蛇是否能动!
import std::IO;
import std::Type;
import std::OS;
import std::TIME;
import std::IOEXT;
import snake::Snake;
import snake::Food;
class Main
{
public static main()
{
OS.system("cls");
OS.srand(TIME.time());
snake->Snake = new Snake();
food->Food = new Food();
L->int = 0;
R->int = 50;
T->int = 0;
B->int = 20;
food.update((L+R)/2, (T+B)/2);
times->int = 0;
offX->int = 0;
offY->int = 0;
while(true)
{
OS.system("cls");
a->int = IOEXT.key('A');
d->int = IOEXT.key('D');
w->int = IOEXT.key('W');
s->int = IOEXT.key('S');
if(a != 0 && d != 0)
{
offX = 0;
}
else if(a != 0)
{
offX = -1;
}
else if(d != 0)
{
offX = 1;
}
else
{
offX = 0;
}
if(w != 0 && s != 0)
{
offY = 0;
}
else if(w != 0)
{
offY = -1;
}
else if(s != 0)
{
offY = 1;
}
else
{
offY = 0;
}
if(offX != 0 || offY != 0)
{
x->int = snake.getX() + offX;
y->int = snake.getY() + offY;
if(L <= x && x<= R && T <= y && y <= B)
{
if(snake.update(x, y) == false)
{
break;
}
if(snake.getX() == food.getX() && snake.getY() == food.getY())
{
food.update(OS.rand() % (L+R), OS.rand() % (T+B));
}
}
}
food.update();
snake.update();
co_yield null;
}
IO.printf("game over!");
}
}
整个demo是在命令行环境下运行的,实在过于简陋,但本着测试的初衷,勉强给个及格。
运行截图:
源代码仓库:https://github.com/chivenzhang/plang-demo
自研产品介绍:Plang高级编程语言
边栏推荐
猜你喜欢
随机推荐
Go中的一些优化笔记,简约而不简单
【Arduino连接GPS 模块 (NEO-6M)读取定位数据】
联阳IT66121FN提供SDI转HDMI方案分享
AD8361检波器
How to remotely debug PLC?
【萌新解题】斐波那契数列
MIPI解决方案 ICN6202:MIPI DSI转LVDS转换芯片
Anaconda(Jupyter)里发现不能识别自己的GPU该怎么办?
R语言 —— 多元线性回归
OneNET Studio与IoT Studio对比分析
TC358860XBG BGA65 东芝桥接芯片 HDMI转MIPI
n皇后问题(回溯法)
倍福ET2000侦听器使用
兼容C51与STM32的Keil5安装方法
同时求最大值与最小值(看似简单却值得思考~)
阿里云华为云对比分析
MC1496乘法器
回溯法 & 分支限界 - 2
博达工业云与阿里云对比
Arduino lights up nixie tubes