簡體   English   中英

沒有按鍵時球一直振動

[英]Ball keeps vibrating when no key is pressed

當按鍵被按下一定距離 +- 給定的不確定性時,我正在嘗試讓球移動。 按下按鍵時,代碼似乎運行良好。 然而,當沒有按下任何鍵時,球會繼續在該位置移動並發出高斯噪音。 我希望它在沒有按下任何鍵時不移動。 我正在使用 pygame 並嘗試使用

if event.type ==  pygame.KEYDOWN is False:          
        rx +=  0
        ry +=  0        
    return rx[0] , ry[0]

但它沒有用。 關於如何移除運動有什么建議嗎?

import pygame 
import random 
import numpy as np

pygame.init()
map = pygame.image.load('occupancy1.png')
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
sigma_step = 0.5 
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_turn = np.radians(2)


def move_robot(rx , ry , fwd , side):
    # if nothing is pressed there shouldnt be any noise
    
    
    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)   
    if event.type ==  pygame.KEYDOWN is False:          
        rx +=  0
        ry +=  0        
    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)
      
    display_surface.fill(black)
   
    display_surface.blit(map, (0, 0)) 
    fwd , side  = get_input()
    rx, ry= move_robot(rx , ry , fwd , side) 
    avatar = pygame.draw.circle(display_surface, red, (rx , ry), 3)
   
     #surface , color , ceter , radius
    pygame.display.update()     
    pygame.time.delay(10)
     ```


閱讀How can I make a sprite move when key is held down並相應地更改get_inputmove_robot方法:

def get_input():   
    keys = pygame.key.get_pressed()
    fwd = (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * step 
    side = (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * step    
    return fwd, side 
   
def move_robot(rx , ry , fwd , side):
    if fwd != 0 or side != 0:
        fwd_noisy =  np.random.normal(fwd , sigma_step , 1)
        side_noisy =  np.random.normal(side , sigma_step , 1)
        rx +=  side_noisy[0]
        ry +=  fwd_noisy[0]
    return rx, ry

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM