简体   繁体   English

如何在 Pygame 中创建自动化 object

[英]How to create an automated object in Pygame

I want to create an automated object that moves to a random location on the screen.我想创建一个自动 object 移动到屏幕上的随机位置。 The image is being shown but it is not moving to any location.图像正在显示,但它没有移动到任何位置。

After importing pygame, setting up the display and loading the desired image, I defined the following parameters:导入 pygame 后,设置显示并加载所需的图像,我定义了以下参数:

object_start_x = 200
object_start_y = -600
object_width = 60
object_height = 160
object_speed = 1

In the main game_loop , using the object() function I had previously defined, I took random integers as the x and y coordinates:在主游戏循环中,使用我之前定义的 object() game_loop ,我将随机整数作为 x 和 y 坐标:

gameExit = False

while not gameExit:
    object(object_start_x, object_start_y, object_width, object_height)
    object_start_y += object_speed
        if object_start_y >= 50:
            object_start_y = 50
            random_locationx = random.randint(50, display_width - object_width)
            random_locationy = random.randint(50, display_height - object_height)

Then, I tried to change the object's position until it was the same as the random integer generated.然后,我尝试更改对象的 position 直到它与生成的随机 integer 相同。

            if random_locationx < object_start_x:
                object_start_x - object_speed
                if object_start_x == random_locationx:
                    object_start_x == random_locationx
            elif random_locationx > object_start_x:
                object_start_x + object_speed
                if object_start_x == random_locationx:
                    object_start_x == random_locationx
            elif random_locationx == object_start_x:
                object_start_x == random_locationx

I repeated the same for object_start_y and random_locationy for the y coordinate.我对 y 坐标的object_start_yrandom_locationy重复了相同的操作。 Although the image does appear, the object is not moving.虽然图像确实出现了,但 object 没有移动。 Any ideas on what I can do to fix this?关于我能做些什么来解决这个问题的任何想法?

Edit: In response to @Sal 's comment, Pygame moving an object does not address my problem as I want to automate an object randomly, not be able to move it myself.编辑:针对@Sal 的评论, Pygame 移动 object并不能解决我的问题,因为我想随机自动化 object,不能自己移动它。

There's not much code here, but there's a few issues, so I'll document those:这里没有太多代码,但有一些问题,所以我将记录这些:

The random location is only generated once the object passes 50 pixels in y .仅当 object 在y中通过 50 个像素时,才会生成随机位置。 Maybe this is by design?也许这是设计使然?

if object_start_y >= 50:
   object_start_y = 50
   random_locationx = random.randint(50, display_width - object_width)
   random_locationy = random.randint(50, display_height - object_height)

It's not clear to me what the purpose of this code-section is, but it looks like the comparison operator == is being used, where the assignment operator = is intended.我不清楚这个代码部分的目的是什么,但看起来比较运算符==正在使用,而赋值运算符=的目的是。

if random_locationx < object_start_x:
    object_start_x - object_speed
    if object_start_x == random_locationx:
        object_start_x == random_locationx    # <-- HERE

Furthermore, even if this was an assignment, it's saying the equivalent of if my_fruit == 'apple', then my_fruit = 'apple' .此外,即使这是一个作业,它也相当于if my_fruit == 'apple', then my_fruit = 'apple' It doesn't achieve a change.它没有实现改变。

I would pack the "Object" data into a structure.我会将“对象”数据打包成一个结构。 A python Rect object would be best, as it already supports x, y, width, height and comes with a great set of utility functions (for collisions etc.) python Rect object 是最好的,因为它已经支持 x、y、宽度、高度,并带有一组很棒的实用功能(用于碰撞等)

object_start_x = 200
object_start_y = -600
object_width   = 60
object_height  = 160

my_object_rect  = pygame.Rect( object_start_x, object_start_y, object_width, object_height )
my_object_speed = 1   # in Y

...

while not gameExit:

    # Move the object
    my_object_rect.y += my_object_speed
    if ( my_object_rect.y > 50 ):
        rand_x = random.randint(50, display_width - object_width)
        rand_y = random.randint(50, display_height - object_height)
        my_object_rect.topleft = ( rand_x, rand_y )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM