我正在制作游戏,并在其中增加了生活并得分。一切都很好,但我想补充的是,如果分数是15的倍数,那么生活将增加1。 我在这里遇到的问题是,生命每帧而不是每秒增加。每秒60帧,因此可以使寿命增加60,而不是1。
正在运行的部分在“更新”中
public class RobotDodge
{
private Player _Player;
private Window _GameWindow;
private List<Robot> _Robots;
//private int _Lives;
private decimal _Score, _Lives;
private DateTime _Time, currentTime;
public bool Quit{
get{
return _Player.Quit;
}
}
// making our game constructor..
public RobotDodge(){
_GameWindow=new Window("Robot Dodge", 800, 800);
_Player=new Player(_GameWindow);
_Robots = new List<Robot>();
_Lives = _Player._Lives; // player lives are 5.
_Score = 0;
//_Counter = 0;
currentTime = DateTime.Now;
_Time = DateTime.Now;
}
// method that returns our new Robot object..
public Robot RandomRobot(){
float r = SplashKit.Rnd();
Boxy _Boxy=new Boxy(_GameWindow, _Player);
Roundy _Roundy=new Roundy(_GameWindow, _Player);
Boxy_v2 _Boxy_v2=new Boxy_v2(_GameWindow, _Player);
// setting up the probability to return the robot type..
if(r < 0.3){
return _Boxy;
}else if(r > 0.3 && r < 0.6){
return _Roundy;
}else{
return _Boxy_v2;
}
}
public void DrawGame(){
// clearing the window..
_GameWindow.Clear(Color.White);
//draw our robots..
for (int i = 0; i < _Robots.Count; i++){
_Robots[i].Draw();
SplashKit.DrawCircle(Color.Blue, _Robots[i].CollisionCircle);
}
// drawing our player..
_Player.DrawPlayer();
// Number of Lives..
SplashKit.DrawText($"Lives : {_Lives}", Color.Black, "Ariel", 14, 20, 20);
SplashKit.DrawText($"Score : {_Score}", Color.Black, "Ariel", 14, 20, 50);
_GameWindow.Refresh(60);
}
// update method..
public void Update(){
for (int i = 0; i < _Robots.Count; i++){
_Robots[i].UpdateRobot();
}
if (SplashKit.Rnd() < 0.02){
_Robots.Add(RandomRobot());
}
CheckCollisions();
// increase _Time by 1 second..
_Time = _Time.AddSeconds(1);
// increase score by 1 every second..
// the below condition will always be true as the difference between times is always 1 second..
if (_Time.Second - currentTime.Second == 1){
_Score += 1;
}
if (_Score % 15 == 0){
_Lives += 1;
}
}
}
答案 0 :(得分:0)
这应该解决。我必须在循环顶部为当前时间变量添加一个更新。我给您的印象是,您正在更新代码中除了Robot构造函数之外的其他地方,并且我不想歪曲您的活动。因此,将其添加到update方法的第一行。
protected DateTime currentTime = DateTime.Now;
// default to a minimum value
private DateTime _lastTime = DateTime.MinValue;
public void Update()
{
// UPDATE THE TIME HERE based on NOW
currentTime = DateTime.Now;
// just FYI, shortcut readability to loop vs indexing loop.
// An array is an enumerable you can loop through directly
foreach (var oneRobot in _Robots)
oneRobot.UpdateRobot();
// vs what you had
// for (int i = 0; i < _Robots.Count; i++)
//{
// _Robots[i].UpdateRobot();
//}
if (SplashKit.Rnd() < 0.02)
{
_Robots.Add(RandomRobot());
}
CheckCollisions();
// if the first time ever in, just set the last time to whatever current IS and get out.
// you would never increase as score or lives on the first instance as it is basically 0
if (_lastTime == DateTime.MinValue)
{
_lastTime = currentTime;
return;
}
// subtracting one date/time
// creates a "TimeSpan" object which represents the difference between two date/time fields.
// if no full second has completed yet, get out.
if ((currentTime - _lastTime).Seconds < 0)
return;
// a second HAS completed since the last measurement. Update Score and Lives
_Score++;
if (_Score % 15 == 0)
_Lives++;
// NOW, you can adjust, but if its a 1.3 second between cycles, you would be constantly
// getting farther and farther from actual seconds. So what I am proposing is to
// take the last second and just add an absolute one second to it, so when the next
// real Second cycle is complete, it should properly hit again.
_lastTime = _lastTime.AddSeconds(1);
}