- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我必须用鼠标移动玩家。我会更好地解释:当我点击屏幕时,我有一个目标,我的英雄从他的实际位置移动到新位置。我必须划清界限,但我遇到了一些问题。我尝试了在这里发布的解决方案,但是当玩家触及边界时,他会卡在这些边界上。
英雄(玩家)类别:
def get_direction(self, target):
'''
Function:
takes total distance from sprite.center
to the sprites target
(gets direction to move)
Returns:
a normalized vector
Parameters:
- self
- target
x,y coordinates of the sprites target
can be any x,y coorinate pair in
brackets [x,y]
or parentheses (x,y)
'''
if self.target: # if the square has a target
position = Vector(self.rect.centerx, self.rect.centery) # create a vector from center x,y value
target = Vector(target[0], target[1]) # and one from the target x,y
self.dist = target - position # get total distance between target and position
direction = self.dist.normalize() # normalize so its constant in all directions
return direction
def distance_check(self, dist):
'''
Function:
tests if the total distance from the
sprite to the target is smaller than the
ammount of distance that would be normal
for the sprite to travel
(this lets the sprite know if it needs
to slow down. we want it to slow
down before it gets to it's target)
Returns:
bool
Parameters:
- self
- dist
this is the total distance from the
sprite to the target
can be any x,y value pair in
brackets [x,y]
or parentheses (x,y)
'''
dist_x = dist[0] ** 2 # gets absolute value of the x distance
dist_y = dist[1] ** 2 # gets absolute value of the y distance
t_dist = dist_x + dist_y # gets total absolute value distance
speed = self.speed ** 2 # gets aboslute value of the speed
if t_dist < (speed): # read function description above
return True
def Walking(self):
'''
Function:
gets direction to move then applies
the distance to the sprite.center
()
Parameters:
- self
'''
self.dir = self.get_direction(self.target) # get direction
if self.dir: # if there is a direction to move
if self.distance_check(self.dist): # if we need to stop
self.rect.center = self.target # center the sprite on the target
else: # if we need to move normal
self.x += (self.dir[0] * self.speed) # calculate speed from direction to move and speed constant
self.y += (self.dir[1] * self.speed)
self.rect.center = (round(self.x),round(self.y)) # apply values to sprite.center
1 级:
def ProcessInput(self, events):
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.__myHero.moveLeft()
if event.key == pygame.K_RIGHT:
self.__myHero.moveRight()
if event.type == pygame.MOUSEBUTTONDOWN:
self.__myHero.target = event.pos
#if event.type == pygame.MOUSEBUTTONDOWN:
#print(pygame.mouse.get_pos())
#self.__myHero.target = pygame.mouse.get_pos() # set the sprite.target to the mouse click position
def Update(self):
#Put your game logic in here for the scene.
#self.SwitchToScene(SecondScene())
if self.__myHero.x >= 740:
self.__myHero.target = None
else:
self.__myHero.Walking()
def Render(self, display_game):
display_game.blit(self.image_background, (0, 0))
display_game.blit(self.__myHero.getImage(), self.__myHero.rect.topleft)
pygame.display.update()
主要:
if not quit_attempt:
active_scene.ProcessInput(filtered_events)
active_scene.Update()
active_scene.Render(display_game)
active_scene = active_scene.next
在 Update()
函数中,我使用了以下代码:
if self.__myHero.x >= 740:
self.__myHero.target = None
如果我的英雄越过边界,我会更改他的目标并将其设置为无
因为我希望他停下脚步。他停了下来,但仍然完全被挡住了。我不知道为什么。你能帮忙吗?
最佳答案
如果你的英雄越过了边界,你可以将其目标更改为“无”以停止行走。这里的问题在于执行此操作的 if 语句。输入此语句后:
if self.__myHero.x >= 740:
self.__myHero.target = None
self.__myHero.x
的值仍然大于或等于 740。因此,您应该采取措施避免 self.__myHero.x
的值更大或 if 语句中等于 740,否则您将继续将 None
设置为 self.__myHero.target
关于python - 边界目标鼠标行走pygame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31408400/
我正在尝试让 Sprite 在 Canvas 上走过背景图像。理想情况下,我会在一张 Canvas 上完成这一切,但使用两张 Canvas 似乎更高效、更容易。 到目前为止我所拥有的: Fiddle
哦,嗨。我是一名初级 Java 开发人员,在空闲时间从事一些基于 2D 图 block 的游戏。现在我正在尝试实现游戏模型中非常基本的东西 - 各种类型的对象如何彼此交互。我希望有一天添加网络支持,所
我们如何使用 CoreMotion 数据检测用户正在驾驶/步行/运行/静止。我们可以使用 CMMotionActivityManager 获取 iPhone 5s 中的用户事件。但是如何进入低版本设备
我是一名优秀的程序员,十分优秀!