《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

相关文章

Python38个游戏开发库整理汇总

《Python38个游戏开发库整理汇总》文章介绍了多种Python游戏开发库,涵盖2D/3D游戏开发、多人游戏框架及视觉小说引擎,适合不同需求的开发者入门,强调跨平台支持与易用性,并鼓励读者交流反馈以... 目录PyGameCocos2dPySoyPyOgrepygletPanda3DBlenderFife

Python标准库datetime模块日期和时间数据类型解读

《Python标准库datetime模块日期和时间数据类型解读》文章介绍Python中datetime模块的date、time、datetime类,用于处理日期、时间及日期时间结合体,通过属性获取时间... 目录Datetime常用类日期date类型使用时间 time 类型使用日期和时间的结合体–日期时间(

Python标准库之数据压缩和存档的应用详解

《Python标准库之数据压缩和存档的应用详解》在数据处理与存储领域,压缩和存档是提升效率的关键技术,Python标准库提供了一套完整的工具链,下面小编就来和大家简单介绍一下吧... 目录一、核心模块架构与设计哲学二、关键模块深度解析1.tarfile:专业级归档工具2.zipfile:跨平台归档首选3.

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

C++11范围for初始化列表auto decltype详解

《C++11范围for初始化列表autodecltype详解》C++11引入auto类型推导、decltype类型推断、统一列表初始化、范围for循环及智能指针,提升代码简洁性、类型安全与资源管理效... 目录C++11新特性1. 自动类型推导auto1.1 基本语法2. decltype3. 列表初始化3

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

Java获取当前时间String类型和Date类型方式

《Java获取当前时间String类型和Date类型方式》:本文主要介绍Java获取当前时间String类型和Date类型方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录Java获取当前时间String和Date类型String类型和Date类型输出结果总结Java获取

游戏闪退弹窗提示找不到storm.dll文件怎么办? Stormdll文件损坏修复技巧

《游戏闪退弹窗提示找不到storm.dll文件怎么办?Stormdll文件损坏修复技巧》DLL文件丢失或损坏会导致软件无法正常运行,例如我们在电脑上运行软件或游戏时会得到以下提示:storm.dll... 很多玩家在打开游戏时,突然弹出“找不到storm.dll文件”的提示框,随后游戏直接闪退,这通常是由于

C++中detach的作用、使用场景及注意事项

《C++中detach的作用、使用场景及注意事项》关于C++中的detach,它主要涉及多线程编程中的线程管理,理解detach的作用、使用场景以及注意事项,对于写出高效、安全的多线程程序至关重要,下... 目录一、什么是join()?它的作用是什么?类比一下:二、join()的作用总结三、join()怎么

Spring Boot 与微服务入门实战详细总结

《SpringBoot与微服务入门实战详细总结》本文讲解SpringBoot框架的核心特性如快速构建、自动配置、零XML与微服务架构的定义、演进及优缺点,涵盖开发环境准备和HelloWorld实战... 目录一、Spring Boot 核心概述二、微服务架构详解1. 微服务的定义与演进2. 微服务的优缺点三