首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建稳定的图像Python Arcade

创建稳定的图像Python Arcade
EN

Stack Overflow用户
提问于 2018-11-10 01:57:04
回答 2查看 185关注 0票数 1

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

代码语言:javascript
复制
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()
EN

回答 2

Stack Overflow用户

发布于 2021-07-09 23:29:51

在我使用Arcade的经验中,画圆的地方是def on_draw(self):。您将其放入MyGame类中,并在arcade.start_render()之后绘制所需的内容(这就是您的self.ball.draw()所在的位置)。由于您希望使对象Ball出现在屏幕上,因此可以创建一个触发器变量来使其出现。

下面是一个基于您的代码的示例:

代码语言:javascript
复制
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()

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

代码语言:javascript
复制
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()

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

票数 2
EN

Stack Overflow用户

发布于 2018-11-10 02:05:16

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

代码语言:javascript
复制
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 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53231022

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档