《C++游戏编程入门》第1章 类型、变量与标准I/O: Lost Fortune

2024-03-12 01:36

本文主要是介绍《C++游戏编程入门》第1章 类型、变量与标准I/O: Lost Fortune,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《C++游戏编程入门》第1章 类型、变量与标准I/O: Lost Fortune

      • 1.1.1 使用C++编写游戏
      • 1.1.2 生成可执行文件
      • 1.1.3 错误处理
    • 1.2 第一个C++程序
        • 01.game_over.cpp
        • 01.game_over2.cpp
        • 01.game_over3.cpp
    • 1.4 使用算术运算符
        • 01.expensive_calculator.cpp
    • 1.5 声明和初始化变量
        • 01.game_stats.cpp
    • 1.6 使用变量进行算术运算
        • 01.game_stats2.cpp
    • 1.7 使用常量
        • 01.game_stats3.cpp
    • 1.8 Lost Fortune
        • 01.lost_fortune.cpp

1.1.1 使用C++编写游戏

原因:
一、高速。高性能。
二、灵活。包括面向对象编程的多范型语言。
三、良好支持。大量资源库可使用,图像API、2D、3D、物理以及声音引擎。

1.1.2 生成可执行文件

编辑器->源代码->编译器->目标代码->链接器->可执行文件

1.1.3 错误处理

类型:
一、编译错误。语法错误,应修复警告。
二、链接错误。无法找到外部引用,调整引用关系,重新编译/链接。
三、运行错误。非法操作,逻辑错误等。

1.2 第一个C++程序

01.game_over.cpp
// 预处理器指令以#符号开头
// 包含头文件
#include <iostream>int main() // 主函数
{// std名称空间//::作用域解析运算符// 所有语句以;结尾std::cout << "Game Over!" << std::endl; // 标准输出return 0;								// 0表示程序正常结束
}// 注释
/*
注释
注释
*/// 空白字符(空格、制表符、换行符)用于分隔代码块,会被编译器忽略
01.game_over2.cpp
#include <iostream>
using namespace std;//使用using指令int main()
{cout << "Game Over!" << endl;return 0;
}
01.game_over3.cpp
#include <iostream>
using std::cout;//使用using声明
using std::endl;int main()
{cout << "Game Over!" << endl;return 0;
}

1.4 使用算术运算符

01.expensive_calculator.cpp
#include <iostream>
using namespace std;int main()
{
//加法、加法与乘法cout << "7 + 3 = " << 7 + 3 << endl;cout << "7 - 3 = " << 7 - 3 << endl;cout << "7 * 3 = " << 7 * 3 << endl;cout << "7 / 3 = " << 7 / 3 << endl;//整数除法取整cout << "7.0 / 3.0 = " << 7.0 / 3.0 << endl;//除法,至少一个浮点数,保留小数位cout << "7 % 3 = " << 7 % 3 << endl;//余数cout << "7 + 3 * 5 = " << 7 + 3 * 5 << endl;cout << "(7 + 3) * 5 = " << (7 + 3) * 5 << endl;//运算符优先级return 0;
}

1.5 声明和初始化变量

01.game_stats.cpp
#include <iostream>
using namespace std;int main()
{int score; // 变量声明double distance;char playAgain;bool shieldsUp;short lives, aliensKilled; // 变量声明score = 0;distance = 1200.76;playAgain = 'y';shieldsUp = true;lives = 3;aliensKilled = 10; // 变量赋值double engineTemp = 6572.89; // 变量初始化cout << "\nscore: " << score << endl; // 显示变量值cout << "distance: " << distance << endl;cout << "playAgain: " << playAgain << endl;// skipping shieldsUp since you don't generally print Boolean valuescout << "lives: " << lives << endl;cout << "aliensKilled: " << aliensKilled << endl;cout << "engineTemp: " << engineTemp << endl;int fuel;cout << "\nHow much fuel? ";cin >> fuel; // 获取用户输入cout << "fuel: " << fuel << endl;typedef unsigned short int ushort; // 定义新变量名ushort bonus = 10;cout << "\nbonus: " << bonus << endl;return 0;
}// 基本类型
// bool, char, int, float, double// 类型修饰符
// short, long, signed, unsigned// 变量命名
// 字母,数字,下划线(非数字开头,非关键字)
// 命名准则:
// 描述性名称、前后一致、语言传统、短变量名// 根据数据使用范围选择数据类型

1.6 使用变量进行算术运算

01.game_stats2.cpp
#include <iostream>
using namespace std;int main()
{unsigned int score = 5000;cout << "score: " << score << endl;// altering the value of a variablescore = score + 100; // 修改变量值cout << "score: " << score << endl;// combined assignment operatorscore += 100; // 使用组合赋值运算符(+=,-=,*=,/=,%=)cout << "score: " << score << endl;// increment operatorsint lives = 3;++lives; // 前置递增运算符,--livescout << "lives: " << lives << endl;lives = 3;lives++; // 后置递增运算符,lives--cout << "lives: " << lives << endl;lives = 3;int bonus = ++lives * 10;cout << "lives, bonus = " << lives << ", " << bonus << endl;lives = 3;bonus = lives++ * 10;cout << "lives, bonus = " << lives << ", " << bonus << endl;// integer wrap aroundscore = 4294967295;cout << "\nscore: " << score << endl;++score; // 溢出,变为0cout << "score: " << score << endl;return 0;
}

1.7 使用常量

01.game_stats3.cpp
#include <iostream>
using namespace std;int main()
{const int ALIEN_POINTS = 150; // 常量,经过命名的无法修改的值int aliensKilled = 10;int score = aliensKilled * ALIEN_POINTS;cout << "score: " << score << endl;enum difficulty // 枚举类型{NOVICE,EASY,NORMAL,HARD,UNBEATABLE};difficulty myDifficulty = EASY;enum shipCost{FIGHTER_COST = 25,BOMBER_COST,CRUISER_COST = 50};shipCost myShipCost = BOMBER_COST;cout << "\nTo upgrade my ship to a Cruiser will cost "<< (CRUISER_COST - myShipCost) << " Resource Points.\n";return 0;
}

1.8 Lost Fortune

01.lost_fortune.cpp
#include <iostream>
#include <string>using std::cin;
using std::cout;
using std::endl;
using std::string;int main()
{const int GOLD_PIECES = 900;int adventurers, killed, survivors;string leader;// get the informationcout << "Welcome to Lost Fortune\n\n";cout << "Please enter the following for your personalized adventure\n";cout << "Enter a number: ";cin >> adventurers;cout << "Enter a number, smaller than the first: ";cin >> killed;survivors = adventurers - killed;cout << "Enter your last name: ";cin >> leader;// tell the storycout << "\nA brave group of " << adventurers << " set out on a quest ";cout << "-- in search of the lost treasure of the Ancient Dwarves. ";cout << "The group was led by that legendary rogue, " << leader << ".\n";cout << "\nAlong the way, a band of marauding ogres ambushed the party. ";cout << "All fought bravely under the command of " << leader;cout << ", and the ogres were defeated, but at a cost. ";cout << "Of the adventurers, " << killed << " were vanquished, ";cout << "leaving just " << survivors << " in the group.\n";cout << "\nThe party was about to give up all hope. ";cout << "But while laying the deceased to rest, ";cout << "they stumbled upon the buried fortune. ";cout << "So the adventurers split " << GOLD_PIECES << " gold pieces.";cout << leader << " held on to the extra " << (GOLD_PIECES % survivors);cout << " pieces to keep things fair of course.\n";return 0;
}

这篇关于《C++游戏编程入门》第1章 类型、变量与标准I/O: Lost Fortune的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/799737

相关文章

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

c++中的set容器介绍及操作大全

《c++中的set容器介绍及操作大全》:本文主要介绍c++中的set容器介绍及操作大全,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录​​一、核心特性​​️ ​​二、基本操作​​​​1. 初始化与赋值​​​​2. 增删查操作​​​​3. 遍历方

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决