在我的游戏中,我添加了一个暂停功能,如果您已成功实现此功能,则可让您更换枪支。还有一个称为“转身”的功能,它可以与玩家一起旋转枪支,问题是有时切换枪支时,枪支最终会朝着玩家的枪尖指向,如果您改变枪支,枪支会朝着玩家的方向倾斜。再次指向玩家
我尝试过让它在每次切换到另一把枪时自动翻转,但这没有用。我看到的一种模式是,一旦我更换枪支,它将是正确的方法,而当我再次更换它时,将是错误的方法。
我不是100%确切地确定我真正需要哪些代码,但是我认为这是其中的一部分。只是为了快速地说出所有认为这是我的全部代码的人。 这不是我的全部代码!
class Gun():
def __init__(self, x, y, gun_pic_one, gun_pic_two, gun_pic_three, gun_pic_four, gun_pic_five, gun_pic_six):
self.direction_lock = False
self.current_indicator = 1
self.bullet_amount = 0
self.picture = gun_pic_one
self.gun_tag = "pistol"
self.gun_one = gun_pic_one
self.spare_gun_one = gun_pic_one
self.gun_one_tag = "pistol"
self.gun_one_permission = False
self.gun_one = pygame.transform.scale(self.gun_one, (30, 10))
self.direction = "right"
self.previous_direction = "right"
self.gun_two = gun_pic_two
self.gun_two_tag = "tatical shotgun"
self.gun_two_permission = False
self.gun_three = gun_pic_three
self.gun_three_tag = "machine gun"
self.gun_three_permission = False
self.gun_four = gun_pic_four
self.gun_four_tag = "AK47"
self.gun_four_permission = False
self.gun_five = gun_pic_five
self.gun_five_tag = "bazooka"
self.gun_five_permission = False
self.gun_six = gun_pic_six
self.gun_six_tag = "scar"
self.gun_six_permission = False
self.picture = pygame.transform.scale(self.picture, (70, 40))
if self.gun_one != None:
self.gun_one = pygame.transform.scale(self.gun_one, (70, 40))
self.spare_gun_one = pygame.transform.scale(self.spare_gun_one, (70, 40))
if self.gun_two != None:
self.gun_two = pygame.transform.scale(self.gun_two, (100, 100))
if self.gun_three != None:
self.gun_three = pygame.transform.scale(self.gun_three, (150, 150))
if self.gun_four != None:
self.gun_three = pygame.transform.scale(self.gun_four, (100, 50))
self.xpos = x
self.ypos = y
self.speed_x = 0
self.speed_y = 0
self.rect = self.picture.get_rect()
self.rect.x = self.xpos
self.rect.y = self.ypos
def change_costume(self, indicator):
if indicator == 1 and self.current_indicator != indicator:
if self.gun_one != None:
if self.gun_one_permission == True:
print(True)
self.picture = self.spare_gun_one
self.gun_tag = self.gun_one_tag
self.rect = self.picture.get_rect()
self.rect.x = self.xpos
self.rect.y = self.ypos
self.current_indicator = indicator
else:
print(False)
elif indicator == 2 and self.current_indicator != indicator:
if self.gun_two != None:
if self.gun_two_permission == True:
self.picture = self.gun_two
self.gun_tag = self.gun_two_tag
self.rect = self.picture.get_rect()
self.rect.x = self.xpos
self.rect.y = self.ypos
self.current_indicator = indicator
else:
self.picture = self.gun_one
self.gun_tag = self.gun_one_tag
self.rect = self.picture.get_rect()
self.rect.x = self.xpos
self.rect.y = self.ypos
elif indicator == 3 and self.current_indicator != indicator:
if self.gun_three != None:
if self.gun_three_permission == True:
self.picture = self.gun_three
self.gun_tag = self.gun_three_tag
self.rect = self.picture.get_rect()
self.rect.x = self.xpos
self.rect.y = self.ypos
self.current_indicator = indicator
else:
self.picture = self.gun_one
self.gun_tag = self.gun_one_tag
self.rect = self.picture.get_rect()
self.rect.x = self.xpos
self.rect.y = self.ypos
elif indicator == 4 and self.current_indicator != indicator:
if self.gun_four != None:
if self.gun_four_permission == True:
self.picture = self.gun_four
self.gun_tag = self.gun_four_tag
self.rect = self.picture.get_rect()
self.rect.x = self.xpos
self.rect.y = self.ypos
self.current_indicator = indicator
else:
self.picture = self.gun_one
self.gun_tag = self.gun_one_tag
self.rect = self.picture.get_rect()
self.rect.x = self.xpos
self.rect.y = self.ypos
elif indicator == 5 and self.current_indicator != indicator:
if self.gun_five != None:
if self.gun_five_permission == True:
self.picture = self.gun_five
self.gun_tag = self.gun_five_tag
self.rect = self.picture.get_rect()
self.rect.x = self.xpos
self.rect.y = self.ypos
self.current_indicator = indicator
else:
self.picture = self.gun_one
self.gun_tag = self.gun_one_tag
self.rect = self.picture.get_rect()
self.rect.x = self.xpos
self.rect.y = self.ypos
elif indicator == 6 and self.current_indicator != indicator:
if self.gun_six != None:
if self.gun_six_permission == True:
self.picture = self.gun_six
self.gun_tag = self.gun_six_tag
self.rect = self.picture.get_rect()
self.rect.x = self.xpos
self.rect.y = self.ypos
self.current_indicator = indicator
else:
self.picture = self.gun_one
self.gun_tag = self.gun_one_tag
self.rect = self.picture.get_rect()
self.rect.x = self.xpos
self.rect.y = self.ypos
"""if self.flipper_detector == True:
self.flipper_detector = False
elif self.flipper_detector == False:
self.flipper_detector = True"""
"""else:
self.picture = self.gun_one
self.gun_tag = self.gun_one_tag
self.rect = self.picture.get_rect()
self.rect.x = self.xpos
self.rect.y = self.ypos"""
def turn_around(self, direction):
if self.speed_x < 0:
self.direction = direction
elif self.speed_x > 0:
self.direction = direction
if self.direction != self.previous_direction:#meaning you can only flip to the opposite side when you direction is not equal to you last direction
self.picture = pygame.transform.flip(self.picture, True, False)
self.previous_direction = self.direction
while True:
if status_bar.show == False:
print(gun.direction)
else:
if cursor.clicked == True:
for icon in gun_icons:
if cursor.is_collided_with(icon):
if icon.tag == gun.gun_one_tag:
if gun.gun_one_permission == True:
gun.change_costume(1)
elif icon.tag == gun.gun_two_tag:
if gun.gun_two_permission == True:
gun.change_costume(2)
elif icon.tag == gun.gun_three_tag:
if gun.gun_three_permission == True:
gun.change_costume(3)
elif icon.tag == gun.gun_four_tag:
if gun.gun_four_permission == True:
gun.change_costume(4)
elif icon.tag == gun.gun_five_tag:
if gun.gun_five_permission == True:
gun.change_costume(5)
elif icon.tag == gun.gun_five_tag:
if gun.gun_six_permission == True:
gun.change_costume(6)
结果是,一旦玩家选择了一把新枪,它将在以前正确时翻转错误的方向,而在之前错误时翻转正确的方向。我想要尝试的是,一旦您获得一门新枪,它将自动更改为正确的方式。对于如何解决这个问题,我有一个小小的想法,但我不是100%确信它会起作用。我当时正在考虑添加一个布尔值,如果它是True,它将在您返回主循环之前快速翻转主炮。
在我们上面的图中,枪的朝向是右,但是游戏认为它的朝向是左。
一旦我去选择了同一把枪,它突然决定回到正确的位置。