当前位置:网站首页>【plang 1.4.5】编写坦克(双人)游戏脚本
【plang 1.4.5】编写坦克(双人)游戏脚本
2022-08-02 03:29:00 【ChivenZhang】
最近,想着做一款双人玩的坦克游戏脚本示例,顺便尝试在plang编程语言上测出一波BUG,结果让人失望,程序正常运行。测试代码如下。
工程目录

Main.ps:程序执行入口
import std::IO;
import std::Type;
import std::OS;
import tank::TankClient;
class Main
{
public static main()
{
OS.system("cls");
client->Client = new TankClient();
client.onInit();
while(true)
{
OS.system("cls");
client.onUpdate();
co_yield null;
}
client.onUninit();
IO.printf("game over!");
}
}
TankClient.ps:坦克游戏场景
package tank;
import std::IO;
import std::Type;
import std::OS;
import std::IOEXT;
public class TankClient : Client
{
public TankClient(){}
public onInit()
{
tank1 = new Tank(0,0);
tank2 = new Tank(40,8);
}
public onUninit(){}
public onUpdate()
{
if(tank1 == null || tank2 == null) return;
// listen tank1's keyboard
{
a->int = IOEXT.key('A');
d->int = IOEXT.key('D');
w->int = IOEXT.key('W');
s->int = IOEXT.key('S');
f->int = IOEXT.key('E');
if(a!=0) tank1.move(-1,0);
if(d!=0) tank1.move(1,0);
if(w!=0) tank1.move(0,-1);
if(s!=0) tank1.move(0,1);
if(f!=0) tank1.shoot(tank2.getx(),tank2.gety());
tank1.update();
}
// listen tank2's keyboard
{
a->int = IOEXT.key('J');
d->int = IOEXT.key('L');
w->int = IOEXT.key('I');
s->int = IOEXT.key('K');
f->int = IOEXT.key('U');
if(a!=0) tank2.move(-1,0);
if(d!=0) tank2.move(1,0);
if(w!=0) tank2.move(0,-1);
if(s!=0) tank2.move(0,1);
if(f!=0) tank2.shoot(tank1.getx(),tank1.gety());
tank2.update();
}
// calculate tanks' life here
// but do nothing right now
}
tank1->Tank;
tank2->Tank;
}
Tank.ps:坦克模拟
package tank;
import gui::GUI;
public class Tank
{
public Tank()
{
bullet = new Bullet();
}
public Tank(x->int, y->int)
{
this.x = x;
this.y = y;
bullet = new Bullet(x, y);
}
public update()
{
GUI.getInstance().draw(x, y, '#');
if(bullet != null) bullet.update();
}
public move(offx->int, offy->int)
{
x += offx;
y += offy;
if(x<0 || x>50) x -= offx;
if(y<0 || y>10) y -= offy;
}
public shoot(x->int, y->int)
{
if(bullet != null) bullet.move(this.x, this.y, x, y);
}
public getx()->int{return x;}
public gety()->int{return y;}
x->int = 0;
y->int = 0;
bullet->Bullet;
}
Bullet.ps:弹药模拟
package tank;
import gui::GUI;
public class Bullet
{
public Bullet(){}
public Bullet(x->int, y->int)
{
this.x = this.cx = x;
this.y = this.cy = y;
}
public update()
{
if(cx<x)++cx;
else if(cx>x)--cx;
else;
if(cy<y)++cy;
else if(cy>y)--cy;
else;
if(cx==x && cy == y);
else GUI.getInstance().draw(cx, cy, '*');
}
public move(cx->int, cy->int, x->int, y->int)
{
if(x<0 || x>50);
else this.x = x;
if(y<0 || y>10);
else this.y = y;
if(cx<0 || cx>50);
else this.cx = cx;
if(cy<0 || cy>10);
else this.cy = cy;
}
x->int = 0;
y->int = 0;
cx->int = 0;
cy->int = 0;
}
运行截图(点击E键,上方坦克向下方发射弹药)

代码仓库链接
Gitee:plang-demo
GitHub:plang-demo (可能无法访问)
边栏推荐
- Transformer结构解析及常见问题
- 案例|工业物联网解决方案·智慧钢厂高性能安全数采
- 单火线开关设计详解
- Cadence allegro导出Gerber文件(制板文件)图文操作
- How to quickly build your own IoT platform?
- Spark数据读取和创建
- GM8775C MIPI转LVDS调试心得分享
- Industry where edge gateway strong?
- SyntaxError: unexpected character after line continuation character
- 【DS3231 RTC实时时钟模块与Arduino接口构建数字时钟】
猜你喜欢

MOS管开关原理及应用详解

基于阿里云OSS+PicGo的个人图床搭建

How to quickly build your own IoT platform?

uniCloud use

2019 - ICCV - 图像修复 Image Inpainting 论文导读《StructureFlow: Image Inpainting via Structure-aware ~~》

【NTC 热敏电阻与 Arduino 读取温度】

【科普贴】I2C接口详解——偏硬件解析

远程调试PLC,到底如何操作?

【Popular Science Post】Detailed explanation of MDIO interface

AD PCB导出Gerber文件(非常详细的步骤)
随机推荐
MQ-5 可燃气体传感器与 Arduino 接口
从Attention到Self-Attention和Multi-Head Attention
【Arduino使用旋转编码器模块】
网站开发方案研究
USB2.0一致性测试方法_高速示波器
Arduino点亮数码管
Scala,Spark依赖jar包冲突解决方法
将ORCAD原理图导入allegro中进行PCB设计
联阳IT66121FN提供SDI转HDMI方案分享
Type c PD 电路设计
Transformer结构解析及常见问题
【nRF24L01 与 Arduino 连接实现无线通信】
uniCloud通讯录实战
PCB设计思路
机器学习预备知识
【霍尔效应传感器模块与 Arduino】
回溯法 & 分支限界 - 2
Scala 中的集合(二):集合性能比较
博达工业云与阿里云对比
联阳IT6561|IT6561FN方案电路|替代IT6561方案设计DP转HDMI音视频转换器资料