C++ C++;使精灵每3秒出现一次
我正在做一个打鼹鼠的游戏,我想让我的鼹鼠精灵每3秒出现一次,但我不知道如何让它工作。现在我的游戏只需要运行5秒,最后是60秒。这是该项目的主要内容。我需要把mole1.visible改为true,这样他就会出现了。在我得到这个之后,我会在每个孔中加上另外5摩尔 编辑 出于某种原因,我无法让chrono编译,但我知道了如何首先让鼹鼠出现,但我不能让它消失。我用模使其为假,我认为相反的做法会使其消失,但事实并非如此C++ C++;使精灵每3秒出现一次,c++,intervals,C++,Intervals,我正在做一个打鼹鼠的游戏,我想让我的鼹鼠精灵每3秒出现一次,但我不知道如何让它工作。现在我的游戏只需要运行5秒,最后是60秒。这是该项目的主要内容。我需要把mole1.visible改为true,这样他就会出现了。在我得到这个之后,我会在每个孔中加上另外5摩尔 编辑 出于某种原因,我无法让chrono编译,但我知道了如何首先让鼹鼠出现,但我不能让它消失。我用模使其为假,我认为相反的做法会使其消失,但事实并非如此 if((60-now)%4==3){ mole1.visible=true;
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)
}
使用命名空间std;//允许我们避免性病::cout
包含/ /标准C++包括
#include//这是使用Unix curses库所必需的
#包括“screen.cpp”//screen类
#包括“sprite2.cpp”//通用sprite类
#包括“nonblocking.h”//有助于非阻塞键盘事件
#包含//用于睡眠
#包括
漫长的开始时间,现在;
int i;
main()//主函数
{
char c;//用于从键盘获取字符输入
screen myscreen;//屏幕数据结构声明
char aimage[80][24]={''};//用空格填充整个数组
漫长的开始时间,现在;
int i;//用于计数器
int循环=0;
目标[1][0]='';
目标[2][0]='';
目标[0][1]='(';
目的[1][1]='';
目的[2][1]='(';
目的[3][1]=')';
目的[1][2]=“|”;
目的[2][2]=“|”;
char bgimage[80][24]={''};//用空格填充整个数组
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]={''};//用空格填充整个数组
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]={''};//用空格填充整个数组
dimage[0][0]='6';
dimage[1][0]='0';
雪碧锤(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);
精灵定时器(5、10、1、2、dimage和myscreen);
mole1.visible=false;//在玩家射击之前,子弹应该是假的
孔1.绘制雪碧(myscreen);
孔2.绘制雪碧(myscreen);
孔3.油漆雪碧(myscreen);
孔4.油漆精灵(myscreen);
孔5.油漆精灵(myscreen);
孔6.油漆精灵(myscreen);
锤子、油漆和雪碧(myscreen);
mole1.油漆雪碧(myscreen);
定时器。绘制精灵(myscreen);
myscreen.display();//使屏幕第一次绘制
开始时间=(无符号)时间(空);
for(;;)//无限循环
{
现在=(无符号)时间(空)-开始时间;
如果((5-now)您可以使用全局循环计算时间差,然后在经过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;
}
带有全局循环的代码更灵活,可以做许多其他有用的事情。为了每隔一定的秒数显示一些东西,您需要有一个引用开始时间的变量。然后,检查当前时间和存储时间之间的增量是否大于一定的量
一个很好的工具,使这类任务将是一个时钟类
时钟.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
\ifndef时钟
#定义时钟
#包括
样板
上课钟
{
公众:
使用TimePoint=decltype(Clock_t::now());
私人:
时间点m_开始;
公众:
Clock():m_start(Clock_t::now()){
}
~Clock(){
}
无效重置(){
m_start=Clock_t::now():
}
float getSeconds()常量{
返回std::chrono::duration_cast(Clock_t::now()-m_start).count();
}
long getmillizes()常量{
返回std::chrono::duration_cast(ClockType::now()-m_start).count();
}
};
#恩迪夫
示例
#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();
}
}
}
#包括
#包括“Clock.h”
int main(){
时钟;
constexpr长产卵率=3000;
while(true){
if(clock.getmillizes()>=繁殖速率){
std::cout对于特定于时间的需求,您通常会发现提供您正在使用的系统非常有用。--在linux中,#包括和睡眠(3)
或usleep(3000000000)
将执行此操作,而不是连续检查差异。--现在,时间可能不会返回秒,如果是微秒,则两次迭代之间可能永远不会有精确的3微秒。--此外,在检查if之前,您将now2设置为Now,因此在本例中永远不会为真。--显示真实的代码如果您需要更进一步、更精确的帮助,请提供帮助。;)我怀疑第一行中的注释。抱歉,我编辑了它并提供了完整的代码,不知道是否应该导致它与计时器无关,而且我是linux上的。提供一些。也许您想在linux或POSIX上使用if。请阅读
#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();
}
}
}