简体   繁体   English

使多个对象自动移动以在pygame中指定了多少个对象

[英]make multiple objects automatically move for how many objects have been specified in pygame

I am creating a tiled game that has NPC's in it. 我正在创建一个带有NPC的平铺游戏。 I can create one NPC successfully but when I draw more than one they share the same position after a few seconds of the code running. 我可以成功创建一个NPC,但是当我绘制多个NPC时,它们会在几秒钟的代码运行后共享相同的位置。 I created this sample to demonstrate what I mean. 我创建了这个样本来演示我的意思。

import pygame, random, math

screen = pygame.display.set_mode((800,600)) 

NPCP = {'Bob' : (2,6), 'John' : (4,4)} # 25, 19 max width and height
pygame.time.set_timer(pygame.USEREVENT, (100))
sMove = True

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.USEREVENT:
            sMove = True
    screen.fill((0,0,255))
    for name in NPCP:
        x,y = NPCP.get(name)
        pygame.draw.rect(screen, (255,0,0), (x*32,y*32,50,50))
        if sMove == True:
            move = random.randint(1,4)
            sMove = False
        if move == 1:
            if math.floor(y) > 2:
                y -= 2
        if move == 2:
            if math.floor(y) < 17:
                y += 2
        if move == 3:
            if math.floor(x) < 23:
                x += 2
        if move == 4:
            if math.floor(x) > 2:
                x -= 2
        print(x,y)
        NPCP[name] = (x,y)

    pygame.display.flip()

I use a dictionary to create these NPC's or rectangles in this case. 在这种情况下,我使用字典来创建这些NPC或矩形。 I move them around with a timer and a random number ranging from 1 to 4 to select which movement to do. 我用一个计时器和一个从1到4的随机数来移动它们,以选择要进行的移动。 I use a for loop to run for each NPC. 我使用for循环为每个NPC运行。 I would like to know how to allow these rectangles to not move around the same way and for the position not to eventually change to the same position and move differently from each other. 我想知道如何让这些矩形不以相同的方式移动,并且使位置最终不要更改为相同的位置并以彼此不同的方式移动。 I also want to have it that it uses the dictionary to do so. 我也希望它使用字典来做到这一点。

If you want to move then objects individually, then you've to generate a random direction for each object. 如果要单独移动对象,则必须为每个对象生成随机方向。

In your code only on direction is generated for all objects, because sMove is set False immediately after the direction for the first object was generated. 在您的代码中,仅为所有对象生成on direction,因为在生成第一个对象的方向后立即将sMove设置为False This direction is used for all the objects. 该方向用于所有对象。
Further the move direction ( move ) is never reset to 0. This causes that the last random direction is applied in all following frames, till the direction is changed again. 此外,移动方向( move )永远不会重置为0。这将导致在所有后续帧中应用最后的随机方向,直到再次更改方向为止。

 if sMove == True: move = random.randint(1,4) sMove = False 

Reset sMove reset move after the loop and, to solve the issue: 重置sMove重置后循环并move ,以解决问题:

for name in NPCP:
    x,y = NPCP.get(name)
    pygame.draw.rect(screen, (255,0,0), (x*32,y*32,50,50))
    if sMove == True:
        move = random.randint(1,4)
    if move == 1:
        if math.floor(y) > 2:
            y -= 2
    if move == 2:
        if math.floor(y) < 16:
            y += 2
    if move == 3:
        if math.floor(x) < 22:
            x += 2
    if move == 4:
        if math.floor(x) > 2:
            x -= 2
    print(x,y)
    NPCP[name] = (x,y)

sMove = False # wait for next timer
move = 0      # stop moving

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

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