《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

相关文章

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

GO语言短变量声明的实现示例

《GO语言短变量声明的实现示例》在Go语言中,短变量声明是一种简洁的变量声明方式,使用:=运算符,可以自动推断变量类型,下面就来具体介绍一下如何使用,感兴趣的可以了解一下... 目录基本语法功能特点与var的区别适用场景注意事项基本语法variableName := value功能特点1、自动类型推

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

python中的显式声明类型参数使用方式

《python中的显式声明类型参数使用方式》文章探讨了Python3.10+版本中类型注解的使用,指出FastAPI官方示例强调显式声明参数类型,通过|操作符替代Union/Optional,可提升代... 目录背景python函数显式声明的类型汇总基本类型集合类型Optional and Union(py