1

我正在尝试使用pygame制作一个红点在迷宫中移动的游戏,但是当它移动时会留下痕迹。我读到我应该使用 get_rect 函数来制作它,这样它就不会留下痕迹,但是我没有成功实现它。我究竟做错了什么?

dimx , dimy = 800 , 600
display_surface = pygame.display.set_mode((dimx, dimy))
red = (255, 0, 0)
black = (0 ,0 ,0)
white =(255,255,255)
green = (0 , 255, 0)

#generate initial position on a valid coordinate
rx , ry = (random.randint(0, map.get_width()), random.randint(0, map.get_height()))
while pygame.Surface.get_at(map , (rx, ry)) == black:
    rx = random.randint(0, map.get_width())
    ry = random.randint(0, map.get_height())
    if rx and ry == white:
        break 
rtheta = 0 
step = 2

t = np.radians(25)
event = None
def get_input():   
        fwd = 0 
        turn = 0 
        side = 0 
        if event is not None:            
            if event.type == pygame.KEYDOWN: 
                if event.key == pygame.K_UP:
                   fwd = -step 
                elif event.key == pygame.K_DOWN:
                    fwd = step 
                elif event.key == pygame.K_LEFT:
                    side = -step            
                elif event.key == pygame.K_RIGHT:
                    side = step                     
        return fwd , side 
    
sigma_step = 0.5 
#sigma_turn = np.radians(2)


def move_robot(rx , ry , fwd , side):
    fwd_noisy = np.random.normal(fwd , sigma_step , 1)
    side_noisy = np.random.normal(side , sigma_step , 1)
    rx +=  side_noisy #* np.cos(rtheta)
    ry +=  fwd_noisy #* np.sin(rtheta)    
    print('fwd noisy' , fwd_noisy)   
 
    return rx[0] , ry[0]
                        



while True : 
    for event in pygame.event.get() : 
        if event.type == pygame.QUIT :    
            # deactivates the pygame library
            pygame.quit()
            # quit the program.
            quit()
            
    pygame.time.delay(10)
    fwd , side  = get_input() 
   
     #surface , color , ceter , radius
    pygame.display.update()  
    
    pygame.time.delay(10)
    display_surface.fill(black)
    display_surface.blit(map, (0, 0))
    pygame.draw.circle(map, red, (rx , ry), 2)
    rx, ry = pygame.get_rect.move_robot(rx , ry , fwd , side)``` 
4

1 回答 1

0

您需要在显示屏上而不是在地图上绘制圆圈:

pygame.draw.circle(map, red, (rx , ry), 2)

pygame.draw.circle(display_surface, red, (rx , ry), 2)
于 2022-02-16T18:07:41.723 回答