1

我正在尝试使用 Python Arcade 库编写一个程序,其中用户按下空格键会导致屏幕上出现一个红色圆圈。但是,每当我按空格键时,图像就会出现,但它会非常迅速地闪烁,就好像它每秒被绘制和擦除和重绘几次一样。我想让它在按空格键后圆圈将保留在屏幕上而不会闪烁。下面是我使用的代码。

import arcade

SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600

class Ball:
    def __init__(self, x , y, radius, color):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color

    def draw(self):
        arcade.draw_circle_filled(self.x, self.y, self.radius, self.color)


class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)

        arcade.set_background_color(arcade.color.ASH_GREY)

        self.ball=Ball(300, 300, 50, arcade.color.AUBURN)

        arcade.start_render()

    def on_key_press(self, key, modifiers):
        if key == arcade.key.SPACE:
            self.ball.draw()

def main():
    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, "Press spacebar")
    arcade.run()

if __name__ == "__main__":
    main()
4

2 回答 2

2

根据我在 Arcade 的经验,你画圆圈的地方是def on_draw(self):. 你把它放在你的MyGame班级里,然后在里面画你想要的东西arcade.start_render()(所以这就是你要去的地方self.ball.draw())。由于您想让对象Ball出现在屏幕上,您可以创建一个触发变量来使其出现。

以下是基于您的代码的示例:

import arcade

SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600


class Ball:
    def __init__(self, x, y, radius, color):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color

    def draw(self):
        arcade.draw_circle_filled(self.x, self.y, self.radius, self.color)


class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)

        arcade.set_background_color(arcade.color.ASH_GREY)

        self.ball = Ball(300, 300, 50, arcade.color.AUBURN)
        self.show_ball = False

        arcade.start_render()

    def on_draw(self):
        arcade.start_render()

        # will draw the ball if self.show_ball is True
        if self.show_ball:
            self.ball.draw()

    def on_key_press(self, key, modifiers):
        if key == arcade.key.SPACE:
            self.show_ball = True


def main():
    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, "Press spacebar")
    arcade.run()


if __name__ == "__main__":
    main()

在我看来,a 的使用dataclass可能很有趣,因为球基本上只是数据,我认为它会更容易阅读(我不认为这是一个标准,这只是我在学校学习的方式)。根据我的建议,它会给你:

import arcade
from dataclasses import dataclass

SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600

@dataclass
class Ball:
    x: int
    y: int
    radius: int
    color: (int, int, int)

    def draw(self):
        arcade.draw_circle_filled(self.x, self.y, self.radius, self.color)


class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)

        arcade.set_background_color(arcade.color.ASH_GREY)

        self.ball = Ball(300, 300, 50, arcade.color.AUBURN)
        self.show_ball = False

        arcade.start_render()

    def on_draw(self):
        arcade.start_render()

        # will draw the ball if self.show_ball is True
        if self.show_ball:
            self.ball.draw()

    def on_key_press(self, key, modifiers):
        if key == arcade.key.SPACE:
            self.show_ball = True


def main():
    window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, "Press spacebar")
    arcade.run()


if __name__ == "__main__":
    main()

如果您对街机库有任何其他问题,请随时询问不和谐服务器 ( https://discord.gg/s4ctsYbC ),他们将很乐意为您提供帮助。

于 2021-07-09T15:29:50.593 回答
0

我没有安装该arcade库,但在我看来,您可以简单地使用跟踪变量来检查您是否已经按下了空格键。

class MyGame(arcade.Window):
    def __init__(self, width, height, title):
        super().__init__(width, height, title)
        self.first_press = False # added tracking variable

        arcade.set_background_color(arcade.color.ASH_GREY)

        self.ball=Ball(300, 300, 50, arcade.color.AUBURN)

        arcade.start_render()

    def on_key_press(self, key, modifiers):
        # added an "and" clause to the if statement to check tracking variable
        if key == arcade.key.SPACE and self.first_press == False:
            self.ball.draw()
            # with this change this if statement will no longer trigger after first press.
            self.first_press = True 
于 2018-11-09T18:05:15.707 回答