没有按键时球一直振动
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_input
和 move_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
我正在尝试让球在按下特定距离 +- 给定的不确定性时移动。按下按键时,代码似乎运行良好。然而,当没有按下任何键时,球会继续在该位置移动并发出高斯噪音。我希望它在没有按下任何键时不移动。 我正在使用 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_input
和 move_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