Home > OS >  Why does calculating my enemy's movement doesnt make it move in that direction?
Why does calculating my enemy's movement doesnt make it move in that direction?

Time:10-22

I have a game, where there is an enemy and its ai only moves it northwest. It should move in all directions, but it doesnt and i cant figure it out. Can someone help me? The enemy code:

import pygame
import inventory_essentials
import variables
import level
import os
import projectiles
import math
import random


class Enemy(pygame.sprite.Sprite):
    def __init__(self, pos, type, damage=50, health=100):
        super().__init__()
        self.image = type
        self.pos = pos
        self.damage = damage
        self.state_stage = 0
        self.direction = 0
        self.health = health
        self.fireballs = []
        self.rect = self.image.get_rect(topleft=(pos[0]   10, pos[1]   10))
        self.fireballs = pygame.sprite.Group()

    def update(self, x_shift, do_damage=False):
        self.ai()
        self.rect.x  = x_shift
        self.rect.x  = math.cos(self.direction)
        self.rect.y  = math.sin(self.direction)
        if random.randint(0, 100) > 99:
            destination = pygame.math.Vector2(variables.current_coords[0] - self.rect.x, variables.current_coords[1] - self.rect.y)
            destination.normalize()
            destination.scale_to_length(10)
            new_fireball = projectiles.Fireball((self.rect.x   10, self.rect.y   10), variables.fireball_icon, self.damage, destination)
            variables.level.create_fireball(new_fireball)

    def ai(self):
        dd = random.randint(-int(math.pi/2)*100, int(math.pi/2)*100)/100
        self.direction  = dd
        print(math.cos(self.direction), math.sin(self.direction))
        pass

i have calculated the sin and cos for the movement and it still doesnt work. Any help is appriciated!

CodePudding user response:

I do not speak python but one thing stands out at me anyway:

int(math.pi/2)

I am not sure what you are trying to do with this line but I'm sure you're not getting the expected result. That's going to evaluate to 1.

CodePudding user response:

I think you are trying to generate the range [-pi/2, pi/2] with your line dd = random.randint(-int(math.pi/2)*100, int(math.pi/2)*100)/100, however because you are casting to int, you are only getting range [-1, 1]

Try doing something like this: dd = random.random() * math.pi - math.pi/2

  • Related