我正在为课堂制作一个打鼹鼠的游戏,我试图让我的每次3秒钟出现我的精灵,但是我无法弄清楚如何让它发挥作用。现在我让游戏运行5秒钟,最后它将是60.这是该项目的主要内容。我需要将mole1.visible更改为true,以便显示出来。在我得到这个之后,我将在每个洞中添加另外5个痣。
EDIT 出于某种原因,我无法计算时间,但我想出了如何首先使痣出现,但我不能在之后消失。我使用modulo使它变得虚假,我认为相反的doig会让它消失,但它并没有
if((60-now)%4==3){
mole1.visible=true;
mole1.paint_sprite(myscreen);
}
if ((60-now)%4!=3){
mole1.visible=false;
mole1.paint_sprite(myscreen);
}
其余代码:
using namespace std; // allows us to avoid std::cout
#include <iostream> // standard C++ include
#include <curses.h> // this is required to use the Unix curses libraries
#include "screen.cpp" // screen class
#include "sprite2.cpp" // generic sprite class
#include "nonblocking.h" // facilitates non-blocking keyboard events
#include <unistd.h> // used by sleep
#include <time.h>
long start_time, now;
int i;
main() // main function
{
char c; // used to get character input from keyboard
screen myscreen; // screen data structure declaration
char aimage[80][24]={' '}; // fills in entire array with spaces
long start_time, now;
int i; // used for counters
int loop=0;
aimage[1][0]='_';
aimage[2][0]='_';
aimage[0][1]='(';
aimage[1][1]='_';
aimage[2][1]='(';
aimage[3][1]=')';
aimage[1][2]='|';
aimage[2][2]='|';
char bgimage[80][24]={' '}; // fills in entire array with spaces
bgimage[3][0]='"';
bgimage[4][0]='"';
bgimage[5][0]='"';
bgimage[2][0]='-';
bgimage[6][0]='-';
bgimage[1][0]='.';
bgimage[7][0]='.';
bgimage[0][1]='/';
bgimage[8][1]='\\';
bgimage[0][2]='|';
bgimage[8][2]='|';
bgimage[0][3]='\\';
bgimage[8][3]='/';
bgimage[1][4]='"';
bgimage[2][4]='-';
bgimage[3][4]='.';
bgimage[4][4]='.';
bgimage[5][4]='.';
bgimage[6][4]='-';
bgimage[7][4]='"';
char cimage[80][24]={' '}; // fills in entire array with spaces
cimage[1][0]='c';
cimage[2][0]='.';
cimage[3][0]='_';
cimage[4][1]='\'';
cimage[5][1]='-';
cimage[6][1]='.';
cimage[0][1]='C';
cimage[3][1]='o';
cimage[5][2]='\'';
cimage[4][2]='.';
cimage[3][2]='.';
cimage[2][3]='-';
cimage[1][3]='-';
cimage[0][2]='(';
char dimage[80][24]={' '}; // fills in entire array with spaces
dimage[0][0]='6';
dimage[1][0]='0';
sprite hammer(22,10,3,4,aimage,&myscreen);
sprite hole1(20,3,5,9,bgimage,&myscreen);
sprite hole2(40,3,5,9,bgimage,&myscreen);
sprite hole3(60,3,5,9,bgimage,&myscreen);
sprite hole4(20,15,5,9,bgimage,&myscreen);
sprite hole5(40,15,5,9,bgimage,&myscreen);
sprite hole6(60,15,5,9,bgimage,&myscreen);
sprite mole1(21,4,4,7,cimage,&myscreen);
sprite timer(5,10,1,2,dimage, &myscreen);
mole1.visible=false; // bullet should be false until the player shoots
hole1.paint_sprite(myscreen);
hole2.paint_sprite(myscreen);
hole3.paint_sprite(myscreen);
hole4.paint_sprite(myscreen);
hole5.paint_sprite(myscreen);
hole6.paint_sprite(myscreen);
hammer.paint_sprite(myscreen);
mole1.paint_sprite(myscreen);
timer.paint_sprite(myscreen);
myscreen.display(); // cause the screen to paint for the first time
start_time=(unsigned)time(NULL);
for(;;) // infinite loop
{
now = (unsigned)time(NULL)-start_time;
if((5-now)<=0) //ends game after 60 seconds
{
endwin(); // clean up curses (really never executed)
return(1);
}
loop++;
if (kbhit())
{
c=getchar(); // get one character from the keyboard
tcflush(0, TCIFLUSH); // system call to flush the keyboard buffer
if (c=='a') // if z, move ship left
{
hammer.move_sprite(-20,0,myscreen);
}
if (c=='d') // if a, move ship right
{
hammer.move_sprite(20,0,myscreen);
}
if (c=='s') // if z, move ship down
{
hammer.move_sprite(0,10,myscreen);
}
if (c=='w') // if z, move ship up
{
hammer.move_sprite(0,-10,myscreen);
}
}
myscreen.display(); // refresh the screen
}
endwin(); // clean up curses (really never executed)
return(1); // end program (also, never executed)
}
答案 0 :(得分:0)
您可以使用全局循环计算时差,然后在经过3.0秒后设置visible=true;
。
像这里:
#include <iostream>
#include <chrono>
#include <unistd.h>
const float TIME_TO_SHOW = 3.0f;
//Function to update all objects
void Update( float dt )
{
static float DeltaCounter = 0.0f;
DeltaCounter+= dt;
if ( DeltaCounter > TIME_TO_SHOW )
{
DeltaCounter -= TIME_TO_SHOW; //Keep overflow
//Set object visible here. For example your mole1.visible=true;
}
}
int main()
{
typedef std::chrono::duration<float> FloatSeconds;
auto OldMs = std::chrono::system_clock::now().time_since_epoch();
const uint32_t SleepMicroseconds = 100;
//Global loop
while (true)
{
auto CurMs = std::chrono::system_clock::now().time_since_epoch();
auto DeltaMs = CurMs - OldMs;
OldMs = CurMs;
//Cast delta time to float seconds
auto DeltaFloat = std::chrono::duration_cast<FloatSeconds>(DeltaMs);
std::cout << "Seconds passed since last update: " << DeltaFloat.count() << " seconds" << std::endl;
//Update all object by time as float value.
Update( DeltaFloat.count() );
// Sleep to give time for system interaction
usleep(SleepMicroseconds);
// Any other actions to calculate can be here
//...
}
return 0;
}
如果你有持续的行为,你可以使用sleep
函数的简单循环。它让你的过程在给定的几秒钟内休眠:
const int32_t CountObjectToShow = 10;
const unsigned int TIME_TO_SHOW = 3;
for ( int32_t i = 0; i < CountObjectToShow; i++ )
{
sleep(TIME_TO_SHOW);
//Set object visible here. For example your mole1.visible=true;
std::cout << "Object showed" << std::endl;
}
具有全局循环的代码更灵活,允许执行许多其他有用的操作。
答案 1 :(得分:0)
好吧,为了在每一定时间内显示某些内容,您需要有一个引用开始时间的变量。然后,检查当前时间和存储时间之间的差值是否大于某个数量。
一个很好的工具来完成这项任务将是一个时钟类。
<强> Clock.h 强>
#ifndef CLOCK_H
#define CLOCK_H
#include <chrono>
template<typename Clock_t = std::chrono::steady_clock>
class Clock
{
public:
using TimePoint = decltype(Clock_t::now());
private:
TimePoint m_start;
public:
Clock() : m_start(Clock_t::now()) {
}
~Clock() {
}
void reset() {
m_start = Clock_t::now():
}
float getSeconds() const {
return std::chrono::duration_cast<std::chrono::duration<float>>(Clock_t::now() - m_start).count();
}
long long getMilliseconds() const {
return std::chrono::duration_cast<std::chrono::milliseconds>(ClockType::now() - m_start).count();
}
};
#endif
示例强>
#include <iostream>
#include "Clock.h"
int main() {
Clock<> clock;
constexpr long long spawnRate = 3000;
while (true) {
if (clock.getMilliseconds() >= spawnRate) {
std::cout << "SPAWN\n";
clock.reset();
}
}
}
因此,对于你的情况,你会有一个游戏时钟,鼹鼠产生的时钟等。
在游戏过程中,您只需检查时钟的当前经过时间是否大于某个增量。如果是这种情况,请做其他事情。
此外,请确保正确重置时钟,例如重置鼹鼠生成计时器以及开始游戏时。
这应该处理事情的时间安排。如果您有其他问题,那么您应该询问这些问题。