具有不同“速度"的pygame元素 [英] pygame elements with different "speed"

查看:55
本文介绍了具有不同“速度"的pygame元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚制作了一个太空入侵游戏,里面的东西掉到地上,你必须避免崩溃等.

I just made a space-invadish game, where things fall to the ground and you have to avoid crashing, etc.

我成功地创建了 2 个同时下落的对象,但我无法让它们以不同的速度这样做.

I succeeded in creating 2 objects falling down simultaneously but I cannot make them doing this with different speed.

这是第一个对象的属性.

This the first object's attributes.

thing_startx = random.randrange(0, display_width-100)
thing_starty = -700
thing_speed = 4

现在它掉了

thing_starty += thing_speed 

在每次 while 循环迭代中.

in each while loop iteration.

对于下一个对象,我只是在原始 X 和 Y 坐标上添加了随机数,因此它的位置不同.(如果mult == True,请参见下面的函数以创建两个矩形对象)

For the next object I just added random numbers to the original X and Y coordinates so it gets it different position. (cf function below to create two rect objects if mult == True)

def things_mult(thingx, thingy, thingw, thingh, color, mult, xopt, yopt, wopt, col2):
    if mult == False:
        pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
    else:
        pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw , thingh])
        pygame.draw.rect(gameDisplay, col2, [thingx + xopt, thingy + yopt, thingw + wopt, thingh])

现在,我假设我只需要定义

Now, I assume I just need to define

thingy_new = thing_starty + thing_yopt
thingy_new = thingy_new + thing_speed* someconstant #(to make it faster or slower)

不幸的是,它不是那样工作的.有人可以向我解释为什么我以某种方式在这个简单的逻辑上有缺陷吗?

Unfortunately, it does not work out like that. Can someone please explain to me why I am somehow shortcoming that simple logic?

推荐答案

最简单的解决方案是将代表你的游戏对象的列表中的 rect、speed 和其他数据组合起来,然后将这些对象放入另一个列表中并使用 for 循环更新位置并绘制它们.

The simplest solution is to combine the rect, speed and other data in lists which represent your game objects, and then put these objects into another list and use for loops to update the positions and to draw them.

您也可以使用字典而不是列表来使代码更具可读性.

You could also use dictionaries instead of lists to make the code more readable.

import pygame as pg


pg.init()
screen = pg.display.set_mode((640, 480))
clock = pg.time.Clock()
BG_COLOR = pg.Color('gray12')
# The objects consist of a pygame.Rect, the y-speed and a color.
objects = [
    [pygame.Rect(150, -20, 64, 30), 5, pg.Color('dodgerblue')],
    [pygame.Rect(350, -20, 64, 30), 3, pg.Color('sienna1')],
    ]

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True

    for obj in objects:
        # [0] is the rect, [1] is the y-speed.
        # Move the objects by adding the speed to the rect.y coord.
        obj[0].y += obj[1]

    screen.fill(BG_COLOR)
    # Draw the rects.
    for obj in objects:
        pg.draw.rect(screen, obj[2], obj[0])
    pg.display.flip()
    clock.tick(60)

pg.quit()

如果您知道类的工作原理并且您的对象也需要特殊的行为,那么最好为您的对象定义一个类.

If you know how classes work and your objects also need special behavior, then better define a class for your objects.

这篇关于具有不同“速度"的pygame元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆