本文主要内容
- JavaSsript语法使用
- VScode编译环境
- Screeps Arena游戏规则
JavaSsript语法使用
基本数据类型
// String, Numker,Boolean,null, undefined
const username = "John";
const age = 30;
const rate = 4.5;
const iscool = true;
const x = null; #表示值为空指针
const y = undefined; #表示根本不存在定义
let z;
typeof age
字符串操作函数
const username = "John";
const age = 30;
#字符串拼接
console.log("My name is " + username + " and I am age");
const hello = `My name is ${username} and I am ${age}`;
console.log(hello);
const s = "Hello World!";
#字符串操作
console.log(s.substring(0, 5).toUpperCase());
s.split("") #不写东西的话就是切分最小
引用数据类型
//const 确保了我们不能将 arr 指向一个新的数组,但可以修改数组内容。
//let 可以将arr 指向一个新数组,也可以修改数组内容。
//声明数组
const fruits = ["apples", "oranges", "pears"];
fruits[3] = "grapes";
fruits.push("mangos");
fruits.unshift("strawberries");
fruits.pop();
console.log(Array.isArray("hello"));
console.log(fruits.indexof("oranges"));
console.log(fruits);
//声明对象
const person = {
firstName: "John",
lastName: "Doe",
age: 30,
hobbies: ["music", "movies", "sports"],
address: {
street: "50 main st",
city: "Boston",
state: "MA",
},
};
console.log(person.address.city);
//解构属性 用相同的属性名
const{
firstName,
lastName,
address: { city },
} = person;
console.log(city);
person.email = "john@gmail.com"; #直接增加属性
对象数组和JSON转化
const todos = [
{
id: 1,
text: "Take out trash",
isCompleted: true,
},
{
id: 2,
text: "Meeting with boss"
isCompleted: true,
},
{
id: 3,
text: "Dentist appt",
isCompleted: false,
},
];
const todoJSoN = JSoN.stringify(todos)
console.log(todoJsON);
[{"id":1,"text":"Take outtrash","isCompleted":true},
{"id":2,"text":"Meeting with boss","isCompleted":true},
{"id":3,"text":"Dentist appt","isCompleted":false}]
流程函数
const x = 4;
//if判断函数
if (x === 10) { #严格相等
console.log("x is 10");
} else if (x > 10) {
console.log("x is greater than 1o");
}else {
console.log("x is less than 10");
}
//三目运算符和switch函数
const color = x > 10 ? "red" : "blue";
switch (color) {
case "red":
console.log("color is red");
break;
case "blue":
console.log("color is blue");
break;
default:
console.log("color is Not red or blue");
}
//for循环
for (let i = 0; i <= 10; i++) {
console.log(`For Loop Number: ${i}`);
}
//while循环
let i = 0;
while (i < 10){
console.log(`While Loop Number: ${i}`);
i++;
}
总结内容
数据类型
一、基本数据类型
String(字符串)
字符串可以用单引号(')或双引号(")来定义。
Number(数字)
用于表示整数或浮点数。
Boolean(布尔值)
只有两个值,
true
(真)和false
(假)。Undefined
表示变量没有被赋值。例如:
let x;
,此时x
的值为undefined
。当尝试访问一个未初始化的变量时,也会得到undefined
。Null
表示一个空值,是一个有意的空值。例如:
let person = null;
,person
变量被赋予了null
值,表明目前它没有指向任何对象或值。需要注意的是,null
是一个单独的值,并且和undefined
不完全相同。BigInt(大整数)
用于表示非常大的整数。例如:
let bigNumber = 123456789012345678967890n;
二、引用数据类型(Object 对象)
包括对象、数组和函数等。
对象:是一个无序的键值对集合。例如:
let person = {name: "John", age: 30};
,person
是一个对象,它有两个属性,name
和age
。数组:是一种特殊类型的对象,用于存储有序的列表。例如:
let arr = [1, 2, 3, 4, 5];
,arr
是一个数组,包含五个元素,可以通过索引访问数组中的元素。函数:其实也是对象的一种,它是可执行的代码块。例如:
function add(a, b){return a + b;}
剩下的到Screeps Arena里面补充吧
VScode编译环境
VS Code 基础教程(一)—— VS Code 的基本使用入门-CSDN博客https://blog.csdn.net/weixin_46215588/article/details/110160065
Screeps Arena游戏规则
入门10个关卡
Screeps Arena 游戏基础教程_screeps: arena-CSDN博客https://blog.csdn.net/weixin_50216991/article/details/137058456
1.循环和导入 (Loop and Import)
2.简单移动(Simple move)
3.首次攻击 (First Attack)
4.爬虫的身体部分(Creeps Bodies)
5.存储和转移(Store and Transfer)
6.地形(Terrain)
7.生产爬虫虫(Spawn Creeps)
8.收割能源(Harvest Energy)
9.建设 (Construction)
10.最终测试 (Final Test)
通关代码
//主函数
export function loop() {
// Your code goes here
}
//移动农名
import { getObjectsByPrototype } from 'game/utils';
import { Creep, Flag } from 'game/prototypes';
export function loop() {
let creeps = getObjectsByPrototype(Creep);
let flags = getObjectsByPrototype(Flag);
creeps[0].moveTo(flags[0]);
}
//攻击敌方农名
import { getObjectsByPrototype } from 'game/utils';
import { Creep } from 'game/prototypes';
import { ERR_NOT_IN_RANGE } from 'game/constants';
export function loop() {
let myCreep = getObjectsByPrototype(Creep).find(creep => creep.my);
let enemyCreep = getObjectsByPrototype(Creep).find(creep => !creep.my);
if(myCreep.attack(enemyCreep) === ERR_NOT_IN_RANGE) {
myCreep.moveTo(enemyCreep);
}
}
//属性补充
MOVE:使爬虫移动; TOUGH :没有任何效果。
ATTACK:允许它近战范围内攻击; RANGED_ATTACK:允许它攻击3格外的目标。
HEAL:允许治疗它自己或另一个爬虫。
WORK:可以建造建筑或收集能量; CARRY:增加爬虫携带资源的能力,身上可以携带更多的资源。
//近战,远程,治疗
import { getObjectsByPrototype } from 'game/utils';
import { Creep } from 'game/prototypes';
import { ERR_NOT_IN_RANGE, ATTACK, RANGED_ATTACK, HEAL } from 'game/constants';
export function loop() {
let myCreeps = getObjectsByPrototype(Creep).filter(creep => creep.my);
let enemyCreep = getObjectsByPrototype(Creep).find(creep => !creep.my);
for(let creep of myCreeps) {
if(creep.body.some(bodyPart => bodyPart.type == ATTACK)) {
if(creep.attack(enemyCreep) == ERR_NOT_IN_RANGE) {
creep.moveTo(enemyCreep);
}
}
if(creep.body.some(bodyPart => bodyPart.type == RANGED_ATTACK)) {
if(creep.rangedAttack(enemyCreep) == ERR_NOT_IN_RANGE) {
creep.moveTo(enemyCreep);
}
}
if(creep.body.some(bodyPart => bodyPart.type == HEAL)) {
let myDamagedCreeps = myCreeps.filter(i => i.hits < i.hitsMax);
if(myDamagedCreeps.length > 0) {
if(creep.heal(myDamagedCreeps[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(myDamagedCreeps[0]);
}
}
}
}
}
//构造防御塔 这里container中立储能容器 和RESOURCE_ENERGY无尽能源
import { prototypes, utils, constants } from 'game';
export function loop() {
const tower = utils.getObjectsByPrototype(prototypes.StructureTower)[0];
if(tower.store[constants.RESOURCE_ENERGY] < 10) {
let myCreep = utils.getObjectsByPrototype(prototypes.Creep).find(creep => creep.my);
if(myCreep.store[constants.RESOURCE_ENERGY] == 0) {
let container = utils.getObjectsByPrototype(prototypes.StructureContainer)[0];
myCreep.withdraw(container, constants.RESOURCE_ENERGY);
} else {
myCreep.transfer(tower, constants.RESOURCE_ENERGY);
}
} else {
let target = utils.getObjectsByPrototype(prototypes.Creep).find(creep => !creep.my);
tower.attack(target);
}
}
//不同地形
import { getObjectsByPrototype } from 'game/utils';
import { Creep, Flag } from 'game/prototypes';
export function loop() {
let creeps = getObjectsByPrototype(Creep).filter(i => i.my);
let flags = getObjectsByPrototype(Flag);
for(let creep of creeps) {
let flag = creep.findClosestByPath(flags);
creep.moveTo(flag);
}
}
//构造兵种
`MOVE`:50能量;`ATTACK`:80能量;`RANGED_ATTACK`:150能量
`HEAL`:250能量;`WORK`:100能量;`CARRY`:50能量;`TOUGH` :10能量
//孵化兵种
import { getObjectsByPrototype } from 'game/utils';
import { Creep, Flag, StructureSpawn } from 'game/prototypes';
import { MOVE } from 'game/constants';
let creep1, creep2;
export function loop() {
let mySpawn = getObjectsByPrototype(StructureSpawn)[0];
let flags = getObjectsByPrototype(Flag);
if(!creep1) {
creep1 = mySpawn.spawnCreep([MOVE]).object;
} else {
creep1.moveTo(flags[0]);
if(!creep2) {
creep2 = mySpawn.spawnCreep([MOVE]).object;
} else {
creep2.moveTo(flags[1]);
}
}
}
//获取野外能量
import { prototypes, utils, constants } from 'game';
export function loop() {
let creep = utils.getObjectsByPrototype(prototypes.Creep).find(i => i.my);
let source = utils.getObjectsByPrototype(prototypes.Source)[0];
let spawn = utils.getObjectsByPrototype(prototypes.StructureSpawn).find(i => i.my);
if(creep.store.getFreeCapacity(constants.RESOURCE_ENERGY)) {
if(creep.harvest(source) == constants.ERR_NOT_IN_RANGE) {
creep.moveTo(source);
}
} else {
if(creep.transfer(spawn, constants.RESOURCE_ENERGY) == constants.ERR_NOT_IN_RANGE) {
creep.moveTo(spawn);
}
}
}
//构造建筑 一般只有塔
import { prototypes, utils } from 'game';
import { RESOURCE_ENERGY, ERR_NOT_IN_RANGE } from 'game/constants';
export function loop() {
const creep = utils.getObjectsByPrototype(prototypes.Creep).find(i => i.my);
if(!creep.store[RESOURCE_ENERGY]) {
const container = utils.findClosestByPath(creep, utils.getObjectsByPrototype(prototypes.StructureContainer));
if(creep.withdraw(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(container);
}
} else {
const constructionSite = utils.getObjectsByPrototype(prototypes.ConstructionSite).find(i => i.my);
if(!constructionSite) {
utils.createConstructionSite(50,55, prototypes.StructureTower);
} else {
if(creep.build(constructionSite) == ERR_NOT_IN_RANGE) {
creep.moveTo(constructionSite);
}
}
}
}
api文档
简介 | Screeps 中文文档https://screeps-cn.github.io/introduction.html