问题描述
我在尝试实现此功能时遇到问题,我的播放器精灵在单击时会冲到或简单地移动到当前鼠标位置。我试过使用向量和计算斜率来实现它,但我无法让它工作。到目前为止,这是我的代码。
https://github.com/nimapourjafar/zip
class Player(pygame.sprite.Sprite):
def __init__(self,x,y,scale,ammo,weapon):
pygame.sprite.Sprite.__init__(self)
self.alive = True
self.ammo = ammo
self.speed = 10
self.start_ammo = ammo
self.shoot_cooldown = 0
self.health = 100
self.max_health = self.health
self.direction = 0
self.vel_y = 0
self.jump = False
self.in_air = True
self.flip = False
self.animation_list = []
self.frame_index = 0
self.action = 0
self.update_time = pygame.time.get_ticks()
animation_types = ['idle','run','jump']
for animation in animation_types:
temp_list = []
num_frame = len(os.listdir(f'img/player/{animation}'))
for i in range(num_frame):
img = pygame.image.load(f'img/player/{animation}/{i}.png').convert_alpha()
img = pygame.transform.scale(img,(int(img.get_width() * scale),int(img.get_height() * scale)))
temp_list.append(img)
self.animation_list.append(temp_list)
self.image = self.animation_list[self.action][self.frame_index]
self.rect = self.image.get_rect()
self.rect.center = (x,y)
self.width = self.image.get_width()
self.height = self.image.get_height()
def move(self,moving_left,moving_right):
screen_scroll = 0
dx = 0
dy = 0
if moving_left:
dx = -self.speed
self.flip = True
self.direction = -1
if moving_right:
dx = self.speed
self.flip = False
self.direction = 1
if self.jump is True and self.in_air is False:
self.vel_y = -11
self.jump = False
self.in_air = True
self.vel_y += GraviTY
if self.vel_y >10:
self.vel_y
dy += self.vel_y
for tile in world.obstacle_list:
#check collision in the x direction
if tile[1].colliderect(self.rect.x + dx,self.rect.y,self.width,self.height):
dx = 0
#check for collision in the y direction
if tile[1].colliderect(self.rect.x,self.rect.y + dy,self.height):
#check if below the ground,i.e. jumping
if self.vel_y < 0:
self.vel_y = 0
dy = tile[1].bottom - self.rect.top
#check if above the ground,i.e. falling
elif self.vel_y >= 0:
self.vel_y = 0
self.in_air = False
dy = tile[1].top - self.rect.bottom
if self.rect.left + dx < 0 or self.rect.right + dx > SCREEN_WIDTH:
dx = 0
self.rect.x +=dx
self.rect.y +=dy
#scroll
if (self.rect.right > SCREEN_WIDTH - SCROLL_THRESH and bg_scroll < (world.level_length * TILE_SIZE) - SCREEN_WIDTH) or (self.rect.left < SCROLL_THRESH and bg_scroll > abs(dx)):
self.rect.x-=dx
screen_scroll = -dx
return screen_scroll
def update_animation(self):
ANIMATION_COOLDOWN = 240
self.image = self.animation_list[self.action][self.frame_index]
if pygame.time.get_ticks() - self.update_time> ANIMATION_COOLDOWN:
self.update_time = pygame.time.get_ticks()
self.frame_index += 1
if self.frame_index >= len(self.animation_list[self.action]):
self.frame_index =0
def update_action(self,new_action):
if new_action != self.action:
self.action = new_action
self.frame_index = 0
self.update_time = pygame.time.get_ticks()
def draw(self):
screen.blit(pygame.transform.flip(self.image,self.flip,False),self.rect)
def update(self):
self.update_animation()
if self.shoot_cooldown > 0:
self.shoot_cooldown -= 1
以下是我的播放器对象。我正在考虑添加一个函数,使其破折号并将鼠标的位置作为参数,但我不太确定在那之后如何更新精灵。我的另一个想法是在我的更新函数中实现一些东西,并有一个条件语句来检查鼠标点击是否被按下,但我无法让它正常工作。有人可以帮忙吗?
(抱歉格式不正确,第一次在这里问)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)