微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Python 中使用 Arcade 制作的现有窗口上制作更新计时器?

如何解决如何在 Python 中使用 Arcade 制作的现有窗口上制作更新计时器?

我一直在街机游戏中创建一个项目,该项目在屏幕上有一辆赛车,它可以躲避障碍物并尽可能快地到达终点线。我已经看到在创建带有计时器的新窗口时已回答的其他问题,但我似乎无法弄清楚如何将计时器添加到我现有的赛车屏幕中,该计时器会在赛车到达终点线时更新然后停止。任何帮助将不胜感激!

这是带有当前代码的主类:

""" 在屏幕上显示计时器。

""" from game.point 导入点 进口街机 导入日期时间

class Timer():
"""
Main application class.
"""
def __init__(self):
    self.output = str

def timer_draw(self,total_time):
    """ Use this function to draw everything to the screen. """

    # Calculate minutes
    minutes = int(total_time) // 60

    # Calculate seconds
    seconds = int(total_time) % 60

    # figure out our output
    self.output = f"Time: {minutes:02d}:{seconds:02d}"

单独上课:

def on_update(self,delta_time):
    self._cue_action("update")
    self.total_time += delta_time
    self.timer.timer_draw(self.total_time)

单独上课:

def timer(self,output):

    arcade.draw_text(f'Timer:',550,arcade.color.BLACK,30)

解决方法

您可以使用 time 库来测量比赛持续时间。 这是一个小例子(绿色圆圈是“汽车”,黄线是“终点线”):

代码:

import time
import arcade

class Game(arcade.Window):
    def __init__(self):
        super().__init__(400,300)
        self.time_start = time.time()
        self.time_elapsed = 0
        self.x = 0

    def on_draw(self):
        arcade.start_render()
        arcade.draw_text(str(self.time_elapsed),170,260,arcade.color.RED,28)
        arcade.draw_line(300,300,arcade.color.GOLD,5)
        arcade.draw_circle_filled(self.x,140,20,arcade.color.GREEN)

    def update(self,delta_time):
        if self.x < 320:
            self.x += 2
            self.time_elapsed = round(time.time()-self.time_start,1)
            
Game()
arcade.run()

输出:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。