C#游戏编程入门

Loop

03-2循环案例 (逐字弹出文本)

按住alt框选可以矩形框选

1.Console.Write();   won't return

2.string表示字符串 char表示字符串中的一个字符

 3.use  ' ' for character   " " for string

4.' ' character literal can only have one character       (for example '\n')

meanwhile,  " " can have many characters

5. \n means new line character, is also one character

6.循环体只有一条语句不用加{} (如图高亮语句)

for (int k = 0; k < 10000 * 10000; k++) ;

//for循环后面是空语句用分号直接结束,否则用花括号不加分号

03-3二重循环

\t 制表符(Tab 或者 Tabulator Character)是一种控制字符,用于在文本中插入水平间距

            for (int i =0; i <= 9; i++)//i代表行
            {
                for (int j = 0;j <= i; j++)//j代表列  
                {
                    Console.Write("{0}*{1}={2}\t",j,i,j*i);
                }
                Console.WriteLine();

            }

03-4 break_continue

break;//跳出循环(跳出break所在的那层for循环)

continue;//结束本次循环,但不退出循环,而是开始下一次的循环(今天不活了,明天继续)

03-5交互案例(hp交互掉血)

1.列行

Console.SetWindowSize(100, 20); //(列,行)

2.Console.ReadLine();

也可以是等待用户输入回车键的一个交互 (已读)

pwd = Console.ReadLine(); 用于从控制台读取用户输入的一行文本,并将其赋值给变量 pwd

3.Console.Write();

用于输出文本,但不会自动添加换行符。

4.初始化的hp值不能放在for循环里面

正确做法

错误做法

03-6 Blackjack

1.rnd

Console.WriteLine("Your current point total is {0}\n Hit?", player);

03-07 last digits lottery

       1.Next方法

           int lastDigits = rnd.Next(0,10);//定义变量(前半段还没有被创建出来),并分配空间

2.自制彩票机

using System;

class Program
{
    static void Main(string[] args)
    {
        #region last digits lottery

        Random rnd = new Random();

        int luckyNumber = rnd.Next(0,10);
        int money = 5000;
        Console.WriteLine("the lucky number is  {0}", luckyNumber);

        for (; ;)
        {                    
            Console.WriteLine("your current money is  {0}", money);
            Console.WriteLine("press enter to get a lottery ball");
            Console.ReadLine();
            Console.WriteLine("-50!");
            money -= 50;
            int ballNumber = rnd.Next(100, 1000);
            Console.WriteLine("the lucky ball is{0}",ballNumber);

            if (ballNumber %10 ==luckyNumber)
            {
                Console.WriteLine("you win!\n +400");//win 400
                money += 400;
            }
            else
            {
                Console.WriteLine("Better luck next time.");
            }

            Console.WriteLine("press n to quit, any other key to continue");
            string s = Console.ReadLine();
            if (s == "n")
            {
                break;
            }
            else
            {
                continue;
            }

        }
        Console.ReadLine();

        #endregion
    }
}

Array

1.定义

由简单数字构成的数据结合,如背包物品集合(arrays),技能集合

2.语句

int[]a;//定义数组 (还未被创建)

a=new int[5];//分配数组空间, 创建一个长度为 5 的整数数组。

[]里可以不写:a=int[5]{2,4,6,8,10}----------a=int[]{2,4,6,8,10}

a.Length访问长度

3.数组与内存

1)内存单位

1Byte = 8 bit (一字节为八位)  B/字节/Byte

1 int= 4byte (一个整数占4个字节)

2)Script
        int[] a = new int[5] { 1, 2, 3, 4, 5 };
        int[] b = a;
        b[0]++;

i) 在像C和C++这样的低级编程语言中,数组变量存储的是数组在内存中的首元素的地址,即数组的起始地址。这意味着数组名可以用作指向数组第一个元素的指针。

图中存储的是这20B在内存中的起始地址。(如1001)

ii)将数组a赋值给数组b时,b将引用同一个数组对象,而不是创建一个数组的副本

iii)   32位二进制数  可以表示32位系统中任何有效内存地址

无符号32位整数(unsigned int),因为它的范围从0到4,294,967,295,正好覆盖了所有可能的内存地址。

在一个32位系统中,内存地址(也称为指针)是用32位的数字表示的。每一个内存地址都是一个32位(0或1)的数值,它指向内存中的一个字节。32位表示可以表示的最大数字是2^32 - 1,也就是4,294,967,295。这意味着32位系统能够寻址的最大内存空间是4GB(因为每个地址指向一个字节,总共有4GB的地址空间)。

3)32位系统和64位系统

32位系统和64位系统主要指计算机处理器和操作系统的数据处理能力和内存寻址能力。

2^32B=4GB

2^64B= 2^34 GB。

4)练习
i)输入输出数组

int[] a = new int[n];//声明一个名为a的整数数组,并初始化这个数组,使其可以容纳n个整数。

代码 Console.WriteLine(a); 在C#中,直接打印一个数组对象时,只会输出其类型信息(例如System.Int32[],System.Int32[] 是 C# 中数组类型的完整类型名称。它表示一个由 System.Int32 类型(即 int)元素组成的数组。),而不是数组的元素内容。如果想打印内容,需要遍历数组来格式化数组的输出。如下:

for (int i = 0; i < a.Length; i++)
{
    Console.WriteLine("Element {0}: {1}", i, a[i]);
}
ii)统计数组内容

char 是表示单个字符的基本数据类型。它是 System.Char 类型的别名。char 类型用于存储一个 16 位的 Unicode 字符。char[]== string  ,string可以当作是一组char(不全相同)

空格键也算是特殊字符。在编程和字符处理的上下文中,空格( )是一个有效的字符,尽管它看起来是空白的。它在许多情况下都被视为特殊字符,原因如下:

、1. 空格的字符编码 在ASCII编码中,空格字符的值是32(十进制)或0x20(十六进制)。 在Unicode中,空格也有相同的编码,表示为空白字符。

2. 编程中的空格 空格字符:在字符串处理中,空格是一个有效的字符,可以与其他字符一样处理。 字符串中的空格:字符串可以包含空格,这些空格在字符串的比较、分割、拼接等操作中都会被考虑。

iii)对话字幕

1.使用 new string[] { ... } 来初始化字符串数组。

2.dialogs[i][j]    //表示第i行的第j个字符

4.二维数组

1)表达

二维数组中的一行是一个一维数组

[1,2] //[行, 列]

        int[] a = new int[] { 1, 2, 3 };

        int[,] b = new int[5, 3] {
            { 1, 2, 3 }, { 2, 3, 4 }, { 3, 4, 5 }, { 4, 5, 6 }, { 5, 6, 7 }
        };

        for (int i = 0; i < b.GetLength(0); i++)
        {
            for (int j = 0; j < b.GetLength(1); j++)
            {
                Console.Write("{0}\t", b[i, j]);
            };
            Console.WriteLine();
        }

2)语法

b.Length 属性   b.GetLength()方法

例如 int[,] b = new int[3, 4];

  • b.Length 的结果是 12,因为这个数组包含 3 * 4 = 12 个元素。

如果你想分别获取数组的行数和列数,可以使用 GetLength 方法:

  • b.GetLength(0) 获取数组的行数。// b = new int[3, 4];//3的index是0,4的index是1。b.GetLength(0)会得到行数3.
  • b.GetLength(1) 获取数组的列数。

3)输出

一维数组中,一个元素是string的话,string可以看作也是一个数组,里面的char是数组里的元素。这时候遍历一个char是使用b[i][j]输出。

二维数组中 遍历元素是用b[i,j]输出。

4)制表符 \t

Console.Write("{0}\t",b[i,j]);

\t是制表位,会自动对齐。要放在“”中 不然会报错。

5.冒泡排序和选择排序

  1. 冒泡排序:Bubble Sort (在每次迭代中,冒泡排序通过相邻元素的比较和交换,将较大的元素逐步向右移动,就像气泡上升一样。)
  2. 选择排序:Selection Sort
冒泡排序
using System;

class Program
{
    static void Main(string[] args)
    {
        #region 选出一个数

        //int[] a =new int[] { 1, 2, 3 };

        //Console.WriteLine("pls type in a number: ");

        //int n = int.Parse(Console.ReadLine());
        //bool found = false;

        //for (int i = 0; i < a.Length; i++)
        //{
        //    if (a[i] == n)
        //    {
        //        Console.WriteLine(i);
        //        found = true;
        //    }

        //}
        //if (!found)
        //{
        //    Console.WriteLine("no element");
        //}

        //Console.ReadLine();


        #endregion

        #region 冒泡排序练习,用冒泡排序找五个数的最大者,将其调换到数组的最后一位

        int[] a = new int[] { 9,8,7,6,5,11,12,12,13 };

        for (int j=1; j<a.Length; j++)
        {

            for (int i = 0; i < a.Length - j; i++)
            {
                if (a[i] > a[i + 1])
                {
                    int t = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = t;
                }
            }

        }

        Console.WriteLine("_____________________");
        for (int i = 0;i < a.Length; i++)
        {
            Console.WriteLine(a[i]);
        }

        #endregion
    }
}

选择排序

debug​​​​​​:

  1. 内层循环的起始条件错误:内层循环for (int i = 1; i < a.Length; i++)应该从当前未排序部分的第一个元素开始,而不是总是从1开始。
  2. 交换元素位置错误:交换最小元素和第一个元素的位置始终是a[0],而不是当前未排序部分的第一个元素位置。

        #region 选择排序

        int[] a = new int[] { 9, 8, 7, 6, 5, 14, 10, 12, 13,102,0,2,4,8 };


        for(int j=1; j<a.Length; j++)
        {

            int min_index = j-1;
            for (int i = j; i < a.Length; i++)
            {
                
                if (a[i] < a[min_index])
                {
                    min_index = i;
                }
            }
            Console.WriteLine("minimal number index is a[{0}]\nminimal number is {1}", min_index, a[min_index]);

            int t = a[j - 1];
            a[j - 1] = a[min_index];
            a[min_index] = t;
        }

        Console.WriteLine("_____________________");
        for (int i = 0; i < a.Length; i++)
        {
            Console.WriteLine(a[i]);
        }

        #endregion
Binary Search

二分搜索必须是经过从小到大排序的

速度快

方法

05.1方法的使用

static  返回值类型  方法名(参数列表)

{

}

1)如果没有返回值(不需要返回结果)就填void

2)方法名(returnType)是自定义的

方法是为了功能复用

string[] args 是C#程序中的一个参数,它表示一个字符串数组,用来接收命令行参数。

让我们详细解释一下:

1. 什么是字符串数组 string[]

  • string[] 表示一个字符串数组,也就是一个可以存储多个字符串的集合。
  • 在C#中,数组是一种数据结构,用来存储相同类型的多个值。
  • 例如,string[] fruits = new string[] { "apple", "banana", "cherry" }; 定义了一个字符串数组,包含了三个字符串:"apple"、"banana" 和 "cherry"。

2. args 参数的作用

  • args 是字符串数组的名称。你可以将它命名为其他名字,但 args 是约定俗成的名字。
  • 当你运行C#程序时,可以在命令行中传递参数,这些参数会被存储在 args 数组中。
  • 例如,如果你在命令行中运行 MyProgram.exe arg1 arg2 arg3,那么 args 数组就会包含这些字符串:args[0] 是 "arg1",args[1] 是 "arg2",args[2] 是 "arg3"。
  • 分大小写
  • F11(Step Into):逐步执行 且 进入方法内部。  Shift+F11(Step Out)
  • F10(Step Over):逐步执行 (但不进入方法内部)。

  • step out:退出当前方法并返回调用该方法的上一层。 (会把当前层后面的语句执行完)

F5再ctrl alt C

调用堆栈(Call Stack)是一个数据结构,用于跟踪程序执行过程中方法或函数调用的顺序。它记录了程序当前执行路径上的所有活动方法,帮助开发者理解程序的执行流程和定位错误。

F10、F11下面都会有 call stack窗口。正常运行没有。

3.构建方法说明

///

    /// <summary>
    /// 计算1~n的和
    /// </summary>
    /// <param name="n">传入n</param>
    /// <returns>1~n的和</returns>

4.方法的用途必须专一

比如上面的求和方法,return完了如果要再输出去static void Main(string[] args)里输出

05.2方法练习

1)报错 

报错1 The left hand side must be a variable, property or indexer

if (n % i = 0)
if (n % i == 0)

简直瞎了

报错2 Argument cannot convert from int to char[]

 格式化输出的弱智报错

在 C# 中,Console.WriteLine 方法不能直接接受多个参数来输出。需要通过格式化字符串或字符串插值来实现。这是因为 Console.WriteLine 方法的重载没有接受多个普通变量的方法。 

报错3 not all code paths return a value

 缺少所有路径的返回值

if有返回值 else没有。

if (n == a * c + b * c + c * c)
{
    return true;
}
return false;//没有这行会出现如上报错

2)代码 判断素数

using System;

class Program
{
    /// <summary>
    /// 判断一个数是否为素数
    /// </summary>
    /// <param name="n">待判断的正整数</param>
    /// <returns>是或者不是</returns>
    static bool IsPrime(int n)//2  n-1
    {
        for (int i = 2; i < n;  i++) //好像弱智
        {
            if (n % i == 0)
            {
                return false;
            }               
        }
        return true;

    }

    static void Main(string[] args)
    {
        int n = int.Parse(Console.ReadLine());

        bool b = IsPrime(n);
        Console.WriteLine(b);
        Console.WriteLine("{0}{1} a prime number",n,b?" is":" is not");

    }
}

3)四则运算和基础表达式 以及我是弱智

a^3  就是 a*a*a 立方!!!!

报错4:Operator:'^' cannot be applied to operands of type 'bool' and 'int'

^不能用在运算中 因为这是亦或符

整数除法&&浮点数除法
  • Python

    • 如果使用浮点数(默认的除法运算):153 / 10 会得到 15.3
    • 如果使用整数除法(使用双斜杠运算符):153 // 10 会得到 15
  • Java

    • 如果变量是浮点型(如 doublefloat):153 / 10 会得到 15.3
    • 如果变量是整型(如 int):153 / 10 会得到 15
  • JavaScript

    • 默认除法运算会返回浮点数:153 / 10 会得到 15.3
  • C/C++

    • 如果变量是浮点型(如 floatdouble):153 / 10 会得到 15.3
    • 如果变量是整型(如 int):153 / 10 会得到 15

4.Narcissistic number

using System;

class Program
{
    /// <summary>
    /// 判断一个数是否为素数
    /// </summary>
    /// <param name="n">待判断的正整数</param>
    /// <returns>是或者不是</returns>
    static bool IsFlower(int n)
    {
        int a = n / 100;
        int b = n / 10 % 10;//n / 10-a*10;弱智
        int c = n % 10;

        if (n == a * a*a + b*b*b + c * c * c)
        {
            return true;
        }
        return false;

    }

    static void Main(string[] args)
    {
        int n = int.Parse(Console.ReadLine());
        bool f = IsFlower(n);
        //int n = 153;
        //int a = n / 100;
        //int b = n / 10 %10;//n / 10-a*10;
        //int c = n % 10;
        //Console.WriteLine("a: {0}, b: {1}, c: {2}", a, b, c);
        Console.WriteLine("{0}{1} a flower number",n,f?" is":" is not");


    }
}

05.3方法练习2

自增

int n = 5;
int result = ++n; // n 变成 6,result 也等于 6
int n = 5;
int result = n++; // result 是 5,n 变成 6

// 后缀自增 n++

// 1. 先使用当前值 5 赋给 result

// 2. 然后 n 增加 1

方法传参

static int AddOne(int n)
{
    int a = n + 1;
    return a;
}

static void Main(string[] args)
{
    int n = int.Parse(Console.ReadLine());
    int result = AddOne(n);
    Console.WriteLine(result);

}

方法重载和可变长参数列表

06 类和对象 

Modifiers: ctrl alt shift

第 1 章 游戏开发和托管代码 .... 3 1.1 什么是.NET? ...... 3 1.2 什么是托管代码 ...... 5 1.3 使用 Microsoft Visual Studio .NET 2003 IDE 编写代码 ...... 5 1.3.1 C#代码 ..... 6 1.3.2 VB .NET 代码 ..... 8 1.4 在命令行中编译.NET 代码 ...... 9 1.5 游戏开发简述 ...... 10 1.6 开发人员 ...... 10 1.7 游戏开发过程 ...... 11 1.8 工具 ...... 12 1.9 小结 ...... 16 第 II 部分 图形和游戏 1 的介绍 第 2 章 策划第一个游戏 .... 19 2.1 提出游戏构想 ...... 19 2.2 理解一个 3D 游戏的需求 ...... 21 2.3 游戏规范 ...... 24 2.4 小结 ...... 26 第 3 章 理解示例框架 .... 27 3.1 创建项目 ...... 27 3.2 枚举所有设备选项 ...... 32 3.3 小结 ...... 39 第 4 章 在屏幕上显示 .... 40 4.1 创建设备 ...... 40 4.2 开始绘图 ...... 47 4.3 加载并绘制网格 ...... 48 4.4 在场景中添加照相机 ...... 51 4.5 小结 ...... 54 第 5 章 完成代码 .... 55 5.1 理解高分辨率计时器 ...... 55 5.2 处理丢失的设备 ...... 60 5.3 添加帧速率输出 ...... 63 5.4 设计 UI 界面 ...... 65 5.5 设计按钮 ...... 72 5.6 小结 ...... 75 第 6 章 实现用户界面 .... 76 6.1 设计主菜单 ...... 76 6.2 插入到游戏引擎中 ...... 81 6.3 选择人物(Loopy) .. 84 6.4 利用新界面更新游戏引擎 ...... 91 6.5 小结 ...... 95 第 7 章 实现玩家和块 .... 96 7.1 编写 Player 对象 ...... 96 7.2 设计块 ...... 104 7.3 小结 ...... 110 第 8 章 实现级别对象 .... 111 8.1 实现级别 ...... 111 8.2 控制玩家的移动 ...... 116 8.3 处理级别的更新 ...... 119 8.4 小结 ...... 123 第 9 章 综合应用 .... 124 9.1 包含玩家 ...... 124 9.2 挂钩级别 ...... 128 9.3 实现退出界面 ...... 132 9.4 结束工作 ...... 135 9.5 小结 ...... 140 第 III 部分 基本的数学规则 第 10 章 3D 数学快速入门 .... 145 10.1 2D 与 3D .... 145 10.2 使用 3D 点 ...... 147 10.3 操作 3D 对象 ...... 148 10.3.1 平移(移动)对象 ..... 149 10.3.2 缩放 ..... 149 10.3.3 旋转 ..... 150 10.3.4 坐标系 ..... 150 10.4 数学结构 ...... 151 10.5 向量 ...... 151 10.6 矩阵 ...... 154 10.7 小结 ...... 157 第 IV 部分 间接图形、对等网、游戏 2 第 11 章 开始创建游戏 .... 161 11.1 Tankers—— 下一个游戏构想 ...... 161 11.2 创建 Tankers 项目 ...... 163 11.3 项目的图形绘制 ...... 169 11.4 为纹理构建对象池 ...... 171 11.5 小结 ...... 173 第 12 章 开发更先进的用户界面 .... 174 12.1 使用 Blockers 的基类(Base 类) .. 174 12.2 添加新的基类 ...... 179 12.3 实现主界面 ...... 182 12.4 利用用户界面绘制 3D 模型 ...... 189 12.5 小结 ...... 193 第 13 章 绘制真实的坦克 .... 194 13.1 理解网格层次结构 ...... 194 13.2 加载坦克层次结构 ...... 197 13.3 绘制网格层次 ...... 199 13.4 操作坦克 ...... 201 13.5 坦克的属性 ...... 204 13.6 创建照相机类 ...... 207 13.7 小结 ...... 210 第 14 章 天空?级别?玩家! .... 211 14.1 没有天空的世界将是黑色的世界 ...... 211 14.2 有了一个天空,但坦克不能驱动到那里 ...... 214 14.3 控制坦克 ...... 216 14.4 IMoveableObject 接口 ...... 225 14.5 基本碰撞检测 ...... 231 14.6 小结 ...... 234 第 15 章 准备,瞄准,开火! .... 235 15.1 实现 Ammunition 类 ...... 235 15.2 Bullets 集合 ...... 242 15.3 完成玩家 ...... 243 15.4 添加声音 ...... 245 15.5 小结 ...... 248 第 16 章 避免单人游戏的枯燥 .... 249 16.1 使用 DirectPlay ... 249 16.2 创建会话 ...... 255 16.3 加入会话 ...... 257 16.4 事件处理程序 ...... 258 16.5 发送及接收数据 ...... 260 16.6 小结 ...... 265 第 17 章 完成 Tankers 游戏 .... 266 17.1 插入到游戏引擎中 ...... 266 17.2 绘制游戏 ...... 272 17.3 小结 ...... 275 第 V 部分 高级绘图、客户/服务器网络和游戏 3 第 18 章 添加特殊效果 .... 279 18.1 实现基本粒子系统 ...... 279 18.2 绘制粒子系统 ...... 287 18.3 将各部分连接到一起 ...... 290 18.4 小结 ...... 292 第 19 章 构建自己的游戏 .... 293 19.1 阐明思想 ...... 293 19.2 创建自己的项目 ...... 294 19.3 设计用户界面 ...... 300 19.4 小结 ...... 305 第 20 章 可编程流水线 .... 306 20.1 定义可编程流水线 ...... 306 20.2 使用 HLSL ... 307 20.3 编写 Vertex Shader .. 309 20.4 使用着色增加逼真度 ...... 314 20.5 添加 Pixel shader .. 315 20.6 小结 ...... 317 第 21 章 控制细节的级别 .... 318 21.1 简化网格 ...... 318 21.2 使用简化的网格 ...... 322 21.3 使用渐进网格控制细节的级别 ...... 323 21.4 小结 ...... 324 第 22 章 使用绘图目标创建特效 .... 325 22.1 绘制跑道和多辆卡丁车 ...... 325 22.2 创建绘图目标和表面 ...... 331 22.3 将场景绘制到绘图目标 ...... 333 22.4 演示后视镜 ...... 334 22.5 小结 ...... 335 第 23 章 理解高级渲染语言 .... 336 23.1 理解老的 shader 模型的限制 ...... 336 23.2 添加卡丁车镜面高亮 ...... 337 23.3 逐 pixel 镜面高亮 ...... 340 23.4 小结 ...... 343 第 24 章 关于性能的注意事项 .... 344 24.1 事件模型和 Managed DirectX .... 344 24.2 生成本机程序集 ...... 345 24.3 Boxing 恶梦 ...... 346 24.4 Managed DirectX 的速度 ...... 348 24.5 理解方法的开销 ...... 349 24.6 小结 ...... 350 第 VI 部分 附 录 附录 A 开发级别创建器 .... 353
Course Technology PTR, 2010 Even experienced game developers sometimes have a hard time making their vision for a great game a reality. The number of available programming languages, libraries, and production methods can make the development process overwhelming and result in complicated, unreliable game code. C# Game Programming: For Serious Game Creation shows programmers how to write simple, clean, and reliable code step-by-step through the creation of a basic game. The game is built using C#, a high-level programming langua ge, and OpenGL, an industry favorite for graphics display. You'll get an overview of the methods and libraries used to build good games, learn how to use those libraries and create your own, and finally build your own scrolling shooter game. You'll even find tips and information on how to develop your own game ideas and you'll have an excellent code base to work with. C# Game Programming: For Serious Game Creation provides you with all the information you need to take your game ideas from concept to completion. Aboit author Daniel Schuller is a British-born computer game developer who has worked and lived in the United States, Singapore, Japan, and is currently working in the United Kingdom. He has released games on the PC as well as the Xbox 360 and PlayStation 3. Schuller has developed games for Sony, Ubisoft, Naughty Dog, RedBull, and Wizards of the Coast, and maintains a game development website at http://www.godpatterns.com. In addition to developing computer games, Schuller also studies Japanese and is interested in Artificial Intelligence, cognition, and the use of games in education. amazon link:http://www.amazon.com/exec/obidos/ASIN/1435455568/buythisbooks-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值