创建堆栈时出现问题!(街机游戏)
Problems creating Stack! (arcade game) in C++
我是C++的业余初学者,最近我有了重新制作流行街机游戏Stack的想法!在C++控制台中。当玩家必须放置垫子时,问题就会出现:这个pad放置得很好,但如果你犯了一个错误,它就不能正确地调整大小(当你运行代码时,你会更好地理解(。
不要在意有时会出错的图形,因为我可以自己修复。请帮帮我!
这是代码:
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <windows.h>
#include <string>
using namespace std;
bool bDirection = true; /* Bool for the direction:
true = dx, false = sx */
string sPad;
int nPadLenght = 6;
int x = 40, y =21; // Referement tile's position
int nSpeed = 200;
bool loop = true; // main loop
int nScore = 0; // score
int nPlaceX = 40;
int nTileX = 35, nTileY = 20; // Player's actual postition
int nEndTileX, nEndTileY;
void RenderLine(int *x, int *y);
int main();
// Void for the coordinates
void gotoxy (int x, int y){
COORD coord;
coord.X = x ;
coord.Y = y ;
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void Victory(){
x = 10;
y = 4;
Beep(698.5, 300);
Beep(698.5, 100);
Beep(1047, 500);
system("color a");
gotoxy(x,y); cout << "You win!nn Score = " << nScore;
system("pause >nul");
}
void PadLenght(int *x){
// Each number is equal to a possible pad lenght
switch (*x){
case 6:
sPad = "[][][]";
nEndTileX = nTileX + 5;
break;
case 5:
sPad = "[][]]";
nEndTileX = nTileX + 4;
break;
case 4:
sPad = "[][]";
nEndTileX = nTileX + 3;
break;
case 3:
sPad = "[][";
nEndTileX = nTileX + 2;
break;
case 2:
sPad = "[]";
nEndTileX = nTileX + 1;
break;
case 1:
sPad = "[";
nEndTileX = nTileX;
break;
}
}
void SwitchDirection(bool *x){
// Switches the bool of the direction
switch (*x){
case true:
*x = false;
break;
case false:
*x = true;
break;
}
}
void Speed(){
// For each line from 500ms to 20ms speed increments of 10ms
if (nSpeed > 20)
nSpeed -= 20;
}
// void for placing the pad
void Place() {
int i = nPlaceX - nTileX;
if (i < 0)
i * -1;
nPadLenght -= i;
}
void collision(){
// Collisions with the border
if (nTileX > 45 || nTileX < 35)
SwitchDirection(&bDirection);
}
void movement(){
int nLastX = nTileX;
// Place the pad if pressing down arrow
if(GetKeyState(VK_DOWN) & 0x8000){
nTileY--;
Place();
Speed();
Beep(698.5, 50);
Beep(880.0, 50);
Beep(1047, 50);
nScore += 10;
Sleep(60);
}
// Movement of the pad
switch (bDirection){
case true:
gotoxy (nLastX, nTileY); cout << " ";
nTileX++;
break;
case false:
gotoxy (nLastX - nPadLenght, nTileY); cout << " ";
nTileX--;
break;
}
}
int main(){
system("color 0");
while (loop = true){
char a = '"';
gotoxy(x,y); cout << a << a << a << a << a << a;
collision();
PadLenght(&nPadLenght);
movement();
gotoxy (nTileX, nTileY); cout << sPad;
Sleep (nSpeed);
if (nScore > 160) {
Victory();
break;
}
}
return 0;
}
以下是我发现的一些问题:
"使用命名空间std;">
这太糟糕了。这将从std
命名空间中引入所有标识符名称。优选使用std
前缀(例如std::cout
(或从std
命名空间(例如using std::cout;
(中进行选择。
全局变量
不希望有全局变量。在main
中创建它们,并将它们传递给函数
例如,您有全局x
和y
,并且在函数中使用x
和y
作为参数。这可能会导致您、读者和编译器之间的混淆,即您所指的变量。
通过指针
最好不要使用指针
传递可放入处理器寄存器的变量的值(不带指针(,如float, double, int, bool
和char
对于类和结构,通过引用传递。如果不修改参数,请通过常量引用传递。
不要为简单内容创建函数
函数需要执行开销,通常至少需要3条指令(保存参数、分支到函数、从函数返回(。一些操作可以用更少的语句执行:
// Switch directions:
direction = ! direction;
如果必须使用函数,请向编译器提示您需要它们inline
。这意味着编译器将把函数的内容粘贴到进行函数调用的位置。有些编译器可能会这样做以进行优化,但您必须告诉编译器进行优化。
布尔switch
语句
切换语句对于布尔变量来说有点过头了,因为只有两个结果。常见的编码准则是使用if
和else
。
条件表达式中的"=">
记住,一个=
用于分配,两个用于比较。这种语言允许在比较中进行分配,但很可能,你没有考虑分配,而是在测试平等性。
声明最接近用法的变量
通过声明最接近其使用位置的变量,使编译器和读者的生活更轻松。例如,在main
中,有一个loop
变量。作为一名读者,我必须滚动到源代码的顶部才能找到定义,而更好的方法是在使用它的main
函数中声明它。
每行一个变量声明
常见的编码准则是每行一个变量。多行对构建时间的影响可以忽略不计。然而,这使得修改更容易。在声明指针时,减少了注入的缺陷。让你的编译器优化代码,你的任务应该是写清楚(易读(和简洁的代码。
提高编译器的警告级别
强制编译器将警告级别打开到其最高级别
解决所有警告
干净的编译没有错误,也没有警告
有了完整的编译器警告,您会注意到我发现的一些问题。
- 从C++本机插件更新Vector3数组
- GCC本机矩阵运算库
- VC++本机单元测试,找不到调试符号
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- 在这种情况下,java对象是否可以调用本机函数
- Craps游戏问题,忽略if语句
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 无法将标准库导入本机 android 项目中的头文件
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 我的代码运行良好,但在游戏循环中中断
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 创建堆栈时出现问题!(街机游戏)