How to stop bullets from going into hyper speed when ever I move a little? (Background moves not the player)

60 Views Asked by At

I'm new to python and I'm doing a top-view-shooter game where the player is actually stationary and the background moves giving it the illusion that the player is moving. My problem is with the bullets, the enemy bullets come out nice but if I move a little bit the bullet turns into a sonic going into that direction where I moved. How do I make it so the bullets are affected by the background, still move towards the player, and don't go into hyper speed? What I'm trying to go for is for the bullet to move towards the player and when the player moves, which would actually be the background, then the bullet moves at the same speed and in the same direction as the background.

These are parts of the full code:

Variables and classes:

import pygame, random, math

pygame.init()
pygame.display.set_caption("Vaenlane")
aken = pygame.display.set_mode([1920,1080])

#Background
taust = pygame.image.load("taust.png")
t_x = 0 
t_y = 0
t_x_muutus = 0
t_y_muutus = 0
linnu_baaskiirus = 400 #Base speed of the background

#Enemy
va = pygame.image.load("Cam_a.png")
v_x = random.randint(0,800)
v_y = random.randint(0,600)
v_x_muutus = 0
v_y_muutus = 0
v_baaskiirus = 180 #Base speed of the enemy


#Player
lind = "sinine_lind.png"
L_elud = 100 #Player's health

kell = pygame.time.Clock()

#Player class
class Lind:
    def __init__(self):
        self.x = 935
        self.y = 515
        self.position = [self.x, self.y]
        self.img = pygame.image.load(lind)
        self.kiirus_x = 0
        self.hitbox = pygame.Rect(self.x - self.img.get_width() / 2, self.y - self.img.get_height() / 2, self.img.get_width(), self.img.get_height())
    
    def joonista(self, window): #joonista - draw
        window.blit(self.img, [self.x - self.img.get_width() / 2, self.y - self.img.get_height() / 2])
        self.hitbox = pygame.Rect(self.x - self.img.get_width() / 2, self.y - self.img.get_height() / 2, self.img.get_width(), self.img.get_height())

#Enemy class
class Vaenlane:
    def __init__ (self):
        self.x = v_x
        self.y = v_y
        self.img = va
        self.position = [self.x, self.y]
        self.shoot_cooldown = 60 
        self.shoot_timer = 0


    #This is for the bullet           
    def update(self, lind):
        self.shoot_timer -= 0.2
        if self.shoot_timer <= 0:
            kuul = Kuul(self.position.copy(), lind.x - t_x, lind.y - t_y)
            kuulid.append(kuul)
            self.shoot_timer = self.shoot_cooldown

    def joonista(self,window):
        window.blit(self.img, [self.x - self.img.get_width() / 2, self.y - self.img.get_height() / 2])
#Bullet class 
class Kuul: #Kuul - Bullet, kuulid - bullets
    def __init__(self, position, player_x, player_y):
        self.position = position
        self.initial_position = position
        self.direction = self.get_direction(player_x, player_y)
        self.speed = 400
        self.radius = 5
        self.color = (255, 0, 0)
        self.distance_limit = 2000
        self.distance_traveled = 0
        self.hitbox = pygame.Rect(self.position[0] - self.radius, self.position[1] - self.radius, self.radius * 2, self.radius * 2)
    
    def get_direction(self, player_x, player_y):
        dx = player_x - self.position[0]
        dy = player_y - self.position[1]
        length = math.sqrt(dx ** 2 + dy ** 2)
        if length != 0:
            dx /= length
            dy /= length
        return [dx, dy]
    
    def update(self, dt, t_x, t_y):
        self.position[0] = self.initial_position[0] + (self.direction[0] * self.speed * dt) - t_x
        self.position[1] = self.initial_position[1] + (self.direction[1] * self.speed * dt) - t_y

        self.hitbox.x = self.position[0] - self.radius
        self.hitbox.y = self.position[1] - self.radius

        self.distance_traveled += self.speed * dt

        if self.distance_traveled >= self.distance_limit:
            kuulid.remove(self)
            
    def check_collision(self, lind):
        if self.hitbox.colliderect(lind.hitbox):
            return True
        return False

    def draw(self, screen):
        pygame.draw.circle(screen, self.color, (int(self.position[0]), int(self.position[1])), self.radius)

#Variables before main loop
kuulid = []
lind = Lind()
vaenlane = Vaenlane()

This is the main loop of the game:

töötab = True
while töötab:
    dt = kell.tick() / 1000        
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            töötab = False
            
        elif e.type == pygame.KEYDOWN:
            if e.key == pygame.K_w:
                t_y_muutus += linnu_baaskiirus
            if e.key == pygame.K_s:
                t_y_muutus += -linnu_baaskiirus
            if e.key == pygame.K_a:
                t_x_muutus += linnu_baaskiirus
            if e.key == pygame.K_d:
                t_x_muutus += -linnu_baaskiirus
                
            if e.key == pygame.K_w:
                v_y_muutus += v_baaskiirus
            if e.key == pygame.K_s:
                v_y_muutus += -v_baaskiirus
            if e.key == pygame.K_a:
                v_x_muutus += v_baaskiirus
            if e.key == pygame.K_d:
                v_x_muutus += -v_baaskiirus
                
        elif e.type == pygame.KEYUP:
            if e.key == pygame.K_w:
                t_y_muutus -= linnu_baaskiirus
            if e.key == pygame.K_s:
                t_y_muutus -= -linnu_baaskiirus
            if e.key == pygame.K_a:
                t_x_muutus -= linnu_baaskiirus
            if e.key == pygame.K_d:
                t_x_muutus -= -linnu_baaskiirus
                
            if e.key == pygame.K_w:
                v_y_muutus -= v_baaskiirus
            if e.key == pygame.K_s:
                v_y_muutus -= -v_baaskiirus
            if e.key == pygame.K_a:
                v_x_muutus -= v_baaskiirus
            if e.key == pygame.K_d:
                v_x_muutus -= -v_baaskiirus
                
    t_x += t_x_muutus *dt
    t_y += t_y_muutus *dt
    
    vaenlane.x += v_x_muutus *dt
    vaenlane.y += v_y_muutus *dt
    

    kuul = Kuul(vaenlane.position.copy(), lind.x, lind.y)
    
    vaenlane.update(lind)
    for kuul in kuulid:
        kuul.update(dt, t_x, t_y)
        if kuul.check_collision(lind):
            kuulid.remove(kuul)
            L_elud -= 20 
            
    if L_elud == 0:
        töötab = False
    
    aken.fill([255,255,255])
    aken.blit (taust,[t_x, t_y])
    lind.joonista(aken)
    vaenlane.joonista(aken)
    for kuul in kuulid:
        kuul.draw(aken)
    aken.blit(pygame.font.Font(None, 70).render("Elud:" + str(L_elud)+"/100", 1, [0,0,0]), (1600,1010))
    pygame.display.flip()
        
    pygame.time.delay(1)

pygame.quit()
0

There are 0 best solutions below