为了做好游戏学C#!
- ■学习是为了什么?
-
Building a Legacy of Kindness and Joy. One can never give too much kindness.It‘s a currency which never runs out and creates more abundance when it is given away.
* ■学习进度条————>
------------------------------------------------------------------------------~~202409.30~~ ①会写函数式 ②创建一个新的CLass(类/族群) ③熟悉变量和数组和流程控制之间的发生关系和概念 ④熟悉Unity和脚本的运行关系,脚本通过挂件挂在一个对象身上。改变脚本的代码,可以改变对象和其他对象的互动反应。 ⑤熟悉掌握Unity一些关键模块的库,比如文案库TextMeshPro/剧本导入库Ink和inputManager ⑥熟悉掌握如何设定规则和tileset,随机生成rougelike关卡
<
在声明变量时,访问修饰符 - - Private 和 Public 让编译器知道哪些类应该有权访问该字段:
private - 只有当前类才能访问字段或方法。public - 任何类都可以引用字段或调用方法。
实用而言,选择public可以在Unity面板做实验操控它,但是这个内容很紧要的话,就要在打包游戏时把它改为private,这样信息就不会泄露出去
2024.02.01 开始学编程
2024.02.07 学会写键盘与鼠标的操作代码完成有bug的游戏碰撞功能
2024.04.02-04 Unity篇:1.UI登录页+血量条 2.游戏架构基础:生成、存储、遍历的代码演示操作
2024.04.06 Unity篇:UI背包+音乐音效
2024.06.20 C# 8.0 跟书学
* ■如何跟一个游戏项目Say HelloWorld!————>
0.去一个空间宽阔的硬盘大草原
在命令行敲
cd d:
1.在命令行敲
dotnet new console --name LIHAOTIAN
2.安装必须的环境
dotnet add package Raylib-cs
3.写心爱的代码,写完敲
dotnet run
4.编译运行代码donnet publish
* ■快捷键in Code Page
删除行 - Ctrl del
注释行 - Ctrl /
搜索文件 - Ctrl P
搜索文档 - Ctrl f
中断命令行的程序运行 - Ctrl C
保存代码 - Ctrl S
侧面页面展示当前画面 Ctrl +I
进入一个类画面 Ctrl +鼠标左键 or F12
omishsharp.json 格式化代码 AIT+SHIFT+F
修改全部代码名字 ?
规范代码排版 ctrl ait a
游戏准则须知
------------------------------------------------------------------------------
①有void 不需要return,无就强制return
②代码的语法包括变量声明(Var)、类型(type)定义、函数(function)定义、字段(string)定义、调用函数。语言:if/while/for
* ■Level 0的游戏开发框架
using System;
using System.Numerics;
using Raylib_cs;
public class LOVEPLAY(项目名){
public static void Main{
(此处省略若干行代码)
}
}
搞定!一个朴实无华毫无功能不好玩的游戏项目就开发完成!
* ■Level 1的游戏开发
输入与输出
System.Console.Write("让玩家输入一段文字回答A")
//输入
A= System.Console.ReadLine();
//把A记录起来
System.Console.WriteLine($"Move: {ctx.input.moveDir} Fire:{ctx.input.isFire}");
//输出信息给玩家看,显示Move的方向和是否发射了子弹。
-
{行动模块的布尔代数使用}
void CheckPlayerinput()
{
bool input_left =Input.GetKey(keyCode.LeftArrow);
bool input_right =Input.GetKey(keyCode.RightArrow);
bool input_space =Input.GetKey(keyCode.Space);
walk = input_left | |input_right;
jump = input_space
}
//Unity 的 GetKey 方法是當我們按下鍵盤輸入鍵(左/右/空格),會返回一個`TURE`信號,代碼將input_xxx也定義爲bool,然后命名和定义walk和jump状态。
定位与交叉碰撞检测算法
------------------------------------------------------------------------------ public class Program {
public static void Main() {
//定义飞机/子弹/敌人的坐标位置
Vector2 vec = new Vector2(640, 480);
//定义子弹的数量+子弹的速度+子弹的坐标+子弹的大小
int[] circles = new int[5];
circles[0] = 50;
//定义敌人的数量+敌人的速度+敌人的坐标+敌人的大小
//敌人有五个,移动方向是左右横向移动,速度30dt坐标是在画面顶部(i,100),大小是2020
int[] circles = new int[5];
circles[0] = 50;
Raylib.InitWindow(640, 480, "Game");while (!Raylib.WindowShouldClose()) { Raylib.BeginDrawing(); //画游戏背景 //画飞机在画面底部,玩家可以通过键盘WSAD控制飞机的移动 //画子弹在飞机的坐标位置,子弹的数量是5,子弹的速度是100*dt,子弹的大小是10*10,颜色为红色 //画五个敌人在画面顶部 Raylib.EndDrawing(); } Raylib.CloseWindow();
}
}
【开发语言和模块】
------------------------------------------------------------------------------[]()-
{计算机思维:状态机}
图灵构造出一台假想的机器,由几个部分组成:(1)一条无限长的纸带。它被划分
为方格,每个方格内有一个来自有限字母表的符号,字母表中有一个特殊符号表示空
格。纸带上的方格从左到右依次被编号为 0,1,2,...,纸带的右端可以无限延伸。
(2)一个读写头。它可以在纸带上左右移动,能读出当前方格里的符号,并能改变该符号。
(3)一个状态存储器。它用来保存图灵机当前所处的状态。图灵机的所有可能状态的数目是有限的,且有一个特殊的状态,称为“停机状态”。
(4)一套控制规则。它根据当前机器所处的状态以及当前读写头所指方格内的符号来确定读写头下一步动作,并改变状态存储器的值,令机器进入一个新状态。 -
Player控制的逻辑
static Vector2 GetMoveAxis() {Vector2 axis = new Vector2(0, 0);
if (Raylib.IsKeyDown(KeyboardKey.W)) {
axis.Y = -1;
} else if (Raylib.IsKeyDown(KeyboardKey.S)) {
axis.Y = 1;
}if (Raylib.IsKeyDown(KeyboardKey.A)) {
axis.X = -1;
} else if (Raylib.IsKeyDown(KeyboardKey.D)) {
axis.X = 1;
}return axis;
}
-
鼓捣数组
------------------------------------------------------------------------------ (var)//int[]bullet=new int[6];//定义有6个子弹
bullet[0]=5;//赋值
int a =bullet[0];//从数值里取出值
子弹
Vector2 point
Vector2 dir
point =point +dir 点向移动方向移动
- 子弹自动移动模块 - Bullet Move
for (int i = 0; i < bulletCount; i += 1) {
Vector2 bullet = bullets[i];
bullet.Y -= 100 * dt;
bullets[i] = bullet;
}
执行子弹的动作,每个子弹都有画面
新子弹的坐标是子弹夹的坐标
新子弹朝y轴往上飞,速度100dt
将子弹夹的数值赋给新子弹
- UI的坐标系布局尺寸和交互
Button用于点击,归属下层。它不知道高层Panel的存在,要知道高层的信息只能通过1.委托2.interface 3.底层的值(字段)
Dropdown下拉菜单
Slider滑轮条
Toggle 切换是否{ 多选 or 单选 }
ScrollBar
坐标系:Screen Space Camera相机空间坐标 /Screen Space Overlay屏幕空间坐标/World Space 世界空间坐标
Pivot支点中心点位置的百分比
TIPS:Canvas和EventSystem是Unity UI必备的GameObject
* ■Level 2的游戏开发:架构篇:生成/存储/遍历
------------------------------------------------------------------------------* ■Level 3的游戏开发:
------------------------------------------------------------------------------- 背包系统
- UI的架构和上下级委托沟通方式
func这个东东就是委托
public void logplaylevel 这个代码行为就在找委托
</div>
<div class="toc-container">
<ul class="markdownIt-TOC">
</div>
</div>
</article>
</div>
<div class="next-post">
<div class="next">下一篇</div>
<a href="https://bacusio.github.io/post/er-0-er-er-mu-du-zhi-guai-xian-zhuang/">
<h3 class="post-title">
二0二二目睹之怪现状
</h3>
</a>
</div>
<div class="site-footer">
</div>
</div>
<script>
hljs.initHighlightingOnLoad()
let mainNavLinks = document.querySelectorAll(".markdownIt-TOC a");
// This should probably be throttled.
// Especially because it triggers during smooth scrolling.
// https://lodash.com/docs/4.17.10#throttle
// You could do like...
// window.addEventListener("scroll", () => {
// _.throttle(doThatStuff, 100);
// });
// Only not doing it here to keep this Pen dependency-free.
window.addEventListener("scroll", event => {
let fromTop = window.scrollY;
mainNavLinks.forEach((link, index) => {
let section = document.getElementById(decodeURI(link.hash).substring(1));
let nextSection = null
if (mainNavLinks[index + 1]) {
nextSection = document.getElementById(decodeURI(mainNavLinks[index + 1].hash).substring(1));
}
if (section.offsetTop <= fromTop) {
if (nextSection) {
if (nextSection.offsetTop > fromTop) {
link.classList.add("current");
} else {
link.classList.remove("current");
}
} else {
link.classList.add("current");
}
} else {
link.classList.remove("current");
}
});
});
</script>