gpt4 book ai didi

python - 边界目标鼠标行走pygame

转载 作者:太空宇宙 更新时间:2023-11-03 17:34:26 25 4
gpt4 key购买 nike

我必须用鼠标移动玩家。我会更好地解释:当我点击屏幕时,我有一个目标,我的英雄从他的实际位置移动到新位置。我必须划清界限,但我遇到了一些问题。我尝试了在这里发布的解决方案,但是当玩家触及边界时,他会卡在这些边界上。

英雄(玩家)类别:

    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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com