为什么我的物品以不同的速度移动?

发布于 2025-02-07 07:20:20 字数 2056 浏览 0 评论 0原文

我正在用Pygame在Python制作游戏。

我与4个孩子一起做了一个父母课程。 据我所知,它们都应该按照项目类别中定义的速度“ DX”移动。 但是,当我运行该程序时,其中一些比其他程序快得多。对于哪些颜色项目也更快,似乎不一致。

import pygame
from random import randint

WIDTH, HEIGHT = 800, 600
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
FPS = 60
clock = pygame.time.Clock()

gravity = 15

# Colours
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)


def draw_window():
    WINDOW.fill(WHITE)
    for item in items:
        item.draw_item()
    pygame.display.update()


def main():
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        clock.tick(FPS)

        for item in items:
            item.movement()

        draw_window()
    pygame.quit()


class Item:

    def __init__(self, x):
        self.x = x
        self.radius = 10
        self.y = randint(100, 500) + self.radius
        self.dx = -2

    def perform_action(self):
        pass

    def movement(self):
        self.x += self.dx
        if self.x < 0 - WIDTH:
            self.x = WIDTH + randint(0, 300)
            self.y = HEIGHT - 40 - randint(0, 400)

    def draw_item(self, colour):
        pygame.draw.circle(WINDOW, colour, (self.x, self.y), self.radius)


class GravUp(Item):

    def __init__(self, x):
        super().__init__(x)

    def draw_item(self):
        super().draw_item(RED)

    def perform_action(self):
        global gravity
        gravity += 3


class GravDown(Item):

    def __init__(self, x):
        super().__init__(x)

    def draw_item(self):
        super().draw_item(GREEN)


class AgilUp(Item):

    def __init__(self, x):
        super().__init__(x)

    def draw_item(self):
        super().draw_item(CYAN)


item_types = {0: GravUp(WIDTH + randint(0, 500)),
              1: GravDown(WIDTH + randint(0, 500)),
              2: AgilUp(WIDTH + randint(0, 500))}
items = []

for i in range(10):
    items.append(item_types[randint(0, 2)])

if __name__ == '__main__':
    main()

I'm making a game in python with pygame.

I've made a parent class Items with 4 children.
As far as I can see, they should all be moving at speed 'dx' as defined in the Items class.
However, when I run the program, some of them are much faster than others. It seems to be inconsistent as to which colour items are faster, too.

import pygame
from random import randint

WIDTH, HEIGHT = 800, 600
WINDOW = pygame.display.set_mode((WIDTH, HEIGHT))
FPS = 60
clock = pygame.time.Clock()

gravity = 15

# Colours
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)


def draw_window():
    WINDOW.fill(WHITE)
    for item in items:
        item.draw_item()
    pygame.display.update()


def main():
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        clock.tick(FPS)

        for item in items:
            item.movement()

        draw_window()
    pygame.quit()


class Item:

    def __init__(self, x):
        self.x = x
        self.radius = 10
        self.y = randint(100, 500) + self.radius
        self.dx = -2

    def perform_action(self):
        pass

    def movement(self):
        self.x += self.dx
        if self.x < 0 - WIDTH:
            self.x = WIDTH + randint(0, 300)
            self.y = HEIGHT - 40 - randint(0, 400)

    def draw_item(self, colour):
        pygame.draw.circle(WINDOW, colour, (self.x, self.y), self.radius)


class GravUp(Item):

    def __init__(self, x):
        super().__init__(x)

    def draw_item(self):
        super().draw_item(RED)

    def perform_action(self):
        global gravity
        gravity += 3


class GravDown(Item):

    def __init__(self, x):
        super().__init__(x)

    def draw_item(self):
        super().draw_item(GREEN)


class AgilUp(Item):

    def __init__(self, x):
        super().__init__(x)

    def draw_item(self):
        super().draw_item(CYAN)


item_types = {0: GravUp(WIDTH + randint(0, 500)),
              1: GravDown(WIDTH + randint(0, 500)),
              2: AgilUp(WIDTH + randint(0, 500))}
items = []

for i in range(10):
    items.append(item_types[randint(0, 2)])

if __name__ == '__main__':
    main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小忆控 2025-02-14 07:20:21

打印出创建对象的地址时,您会看到为什么有不同的速度:

<__main__.AgilUp object at 0x0BD30E70>
<__main__.AgilUp object at 0x0BD30E70>
<__main__.GravUp object at 0x0BD30550>
<__main__.AgilUp object at 0x0BD30E70>
<__main__.GravUp object at 0x0BD30550>
<__main__.GravUp object at 0x0BD30550>
<__main__.GravDown object at 0x0BD306F0>
<__main__.GravDown object at 0x0BD306F0>
<__main__.AgilUp object at 0x0BD30E70>
<__main__.AgilUp object at 0x0BD30E70>

因此,您没有10个对象,而只有3个和10个随机指针。在此示例中,运动说明将调用5次Agilup,3次升压和2倍的Grevdown。

重新设计对象创建过程应解决问题。

When printing out the addresses of the created objects you will see why there are different speeds:

<__main__.AgilUp object at 0x0BD30E70>
<__main__.AgilUp object at 0x0BD30E70>
<__main__.GravUp object at 0x0BD30550>
<__main__.AgilUp object at 0x0BD30E70>
<__main__.GravUp object at 0x0BD30550>
<__main__.GravUp object at 0x0BD30550>
<__main__.GravDown object at 0x0BD306F0>
<__main__.GravDown object at 0x0BD306F0>
<__main__.AgilUp object at 0x0BD30E70>
<__main__.AgilUp object at 0x0BD30E70>

So you don't have 10 objects, but only three, and 10 random pointers to one of these. In this example, the movement instructions will call 5 times AgilUp, 3 times GravUp and 2 times GravDown.

Redesigning the object creation process should fix the issue.

时光瘦了 2025-02-14 07:20:21

您的随机速度行为的答案位于程序中的以下代码中。

item_types = {0: GravUp(WIDTH + randint(0, 500)),
              1: GravDown(WIDTH + randint(0, 500)),
              2: AgilUp(WIDTH + randint(0, 500))}

items = []

for i in range(10):
    items.append(item_types[randint(0, 2)])

这些代码正在做的事情确实定义了您的三个彩色圆圈。然后,它将随机分配多次(十分之一)每个圆圈并重新绘制。

当我在“ draw_item”函数中添加打印操作并运行程序时,我能够通过打印物品的RGB颜色值来确定每个圆圈所获得的绘图/重新呼叫的数量。

(0, 255, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 255)
(255, 0, 0)
(0, 255, 0)
(0, 255, 255)

如果您增加数量,绿色圆圈会接到四个呼叫,并重新绘制重新拨动,红色圆圈将获得四个呼叫和重新绘制的呼叫,而青色圆圈将获得两个呼叫,并重新播放。再次运行它可能会为每种颜色产生不同的调用。

回顾您的代码,我猜想您最终想做的是在随机位置创建带有随机颜色的十个圆圈。如果这实际上是您想做的,则需要修改代码块,以先得出一个随机数,然后基于该数字,用随机项目类型填充项目组。

我希望这阐明了事情。

问候。

The answer to your random speed behavior resides within the following lines of code in your program.

item_types = {0: GravUp(WIDTH + randint(0, 500)),
              1: GravDown(WIDTH + randint(0, 500)),
              2: AgilUp(WIDTH + randint(0, 500))}

items = []

for i in range(10):
    items.append(item_types[randint(0, 2)])

What this bit of code is doing is indeed defining your three colored circles. Then, it is randomly assigning a number of times (one out of ten ) each circle will be moved and redrawn.

When I added in a print operation within the "draw_item" function and ran the program, I was able to determine the number of drawing/redrawing calls each circle was getting by printing out the item's RGB color value.

(0, 255, 0)
(255, 0, 0)
(255, 0, 0)
(255, 0, 0)
(0, 255, 0)
(0, 255, 0)
(0, 255, 255)
(255, 0, 0)
(0, 255, 0)
(0, 255, 255)

If you tally up the quantities, the green circle gets four calls to move and redraw, the red circle gets four calls to move and redraw, and the cyan circle gets two calls to move and redraw. Running this again would probably produce a different mix of calls for each color.

Reviewing your code, I am guessing what you ultimately wanted to do was create ten circles with random colors at random positions. If that is actually what you want to do, you will need to revise the block of code to derive a random number first, and then based upon that number, populate the item group with the random item type.

I hope that clarifies things.

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文