gpt4 book ai didi

c++ - 通过任意按键中断循环的功能

转载 作者:行者123 更新时间:2023-11-28 06:07:10 26 4
gpt4 key购买 nike

我是 C++ 新手。决定做我自己的游戏。我想为它制作一个启动屏幕。问题是我还没有找到在点继续时制作“按任意键继续”功能的方法。我让程序循环等待,直到按下任何按钮,但点不想显示。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <windows.h>

using namespace std;


int pressCheck(){
char c = 0;
c = getchar();
if (c == 0)
return 1;
return 0;
}

int main()
{
cout << "\t\t\t\t Hello" << endl;
Sleep(300);
cout << "\t\t Welcome to my new game BITHCES!" << endl << endl;
Sleep(700);
cout << "\t\t\tPress any key to proceed";
while(!pressCheck()){
Sleep(300);
cout << ".";
Sleep(300);
cout << ".";
Sleep(300);
cout << ".";
}
getchar();
system("cls");
Sleep(100);


return 0;
}

最佳答案

如果您正在创建基于文本的游戏,我建议您使用 ncurses (或 Windows 的 pdcurses):

[...] a toolkit for developing "GUI-like" application software that runs under a terminal emulator.

实现你上面的东西就像

#include <string>
#include <ncurses.h> // This header might be different on windows
#include <unistd.h> // for usleep, replace with Windows.h (?)

void DisplayCentre(int yy, const std::string& str)
{
// Get the screen size
int y, x;
getmaxyx(stdscr, y, x);

// Compute starting location for string (centre)
x = (x - str.size())/2;

// Write the string to the window
mvwprintw(stdscr, yy, x, str.c_str());

// Make sure the screen is updated
refresh();
}

void PromptForKey(void)
{
// Get the screen size
int y, x;
getmaxyx(stdscr, y, x);

// Write a message at the bottom left of the screen
mvwprintw(stdscr, y-1, 0, "Press any key to continue");

// Set a time-out for wgetch
wtimeout(stdscr, 300);

// While the user hasn't entered a character
while (wgetch(stdscr) == ERR)
{
// Add another dot to the screen
waddch(stdscr, '.');
refresh();
}

// Clear time-out
notimeout(stdscr, true);
}

int main(int argc, char** argv)
{
initscr(); // Initialize curses
cbreak(); // Make typed characters immediately available
noecho(); // Don't automatically print typed characters
curs_set(0); // Make the cursor invisible (where supported)

// Display `Hello' (at line 10)
DisplayCentre(10, "Hello");

// Delay (you might want to use Sleep())
sleep(1);

// Display `Welcome to my new game' (at line 15)
DisplayCentre(15, "Welcome to my new game");
sleep(1);

// Prompt user for key
PromptForKey();

// Close down curses
endwin();

return 0;
}

为了在 Linux 上编译这个程序,我使用了 g++ test.cpp -lncurses。在 Windows 上,您可能需要将 sleep 替换为 Windows Sleep 函数并使用适当的 header 。您可能还需要使用 ncurses 的替代品。

但是,如果您只是学习编程,我建议您尝试在 Python 中使用 ncurses。 Python 具有作为一种解释型语言的优势,因此您无需过多担心编译或链接可执行文件的问题。 Python 也主要是跨平台的。以上用 Python 实现:

#!/usr/bin/python

from curses import *
from time import sleep

def promptForKey(win):
""" Ask the user to press any key to continue. """
# Get screen size
y,x = win.getmaxyx()

# Display prompt
win.addstr(y-1, 0, "Press any key to continue")
win.refresh()

# Set time-out
win.timeout(300)

while (win.getch() == ERR):
win.addch('.')

# Disable time-out
win.notimeout(True)


def dispCentre(win, yy, string, delay):
""" Display string at line yy and wait for delay milliseconds. """
# Get screen size
y,x = win.getmaxyx()

# Display string in centre
x = (x - len(string))/2
win.addstr(yy, x, string)
win.refresh()

# Delay
sleep(delay)

if __name__ == '__main__':

# Initialize curses
win = initscr()
cbreak()
noecho()
curs_set(0)

# Display some stuff
dispCentre(win, 10, "Hello", 0.3)
dispCentre(win, 15, "Welcome to my new game", 0.7)
promptForKey(win)

# Close down curses
endwin()

关于c++ - 通过任意按键中断循环的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32172634/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com