0

So i have been trying to implement time delta in my shooter game so that on every pc it runs the same until now I just restricted it to 60fps. While trying to implement that my enemy sprites stoped moving and stoped at 0x 0y.

import pygame, sys, time, os
from random import randint
class Player(pygame.sprite.Sprite):
   def __init__(self):
       super().__init__()
       self.image = pygame.Surface((50, 50))
       self.image.fill((255, 255, 255))
       self.rect = self.image.get_rect(center=(screen_width / 2, screen_height / 2))

   def update(self):
       self.rect.center = pygame.mouse.get_pos()

   def create_bullet(self):
       return Bullet(pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1])

   def create_enemy(self):
       return Enemy(randint(50, screen_width - 50), -200)

   def get_damage(self, amount):
       if self.target_health > 0:
           self.target_health -= amount
       if self.target_health <= 0:
           self.target_health = 0

class Bullet(pygame.sprite.Sprite):
   def __init__(self, pos_x, pos_y):
       super().__init__()
       self.image = pygame.Surface((5, 25))
       self.image.fill((255, 0, 0))
       self.rect = self.image.get_rect(center=(pos_x, pos_y))
       self.pos = self.image.get_rect(center=(pos_x, pos_y))

   def update(self,dt):
       self.pos.y -= 5 * dt
       self.rect.y = round(self.pos.y)
       if self.rect.y <= 0:
           self.kill()


class Enemy(pygame.sprite.Sprite):

   def __init__(self, pos_x, pos_y):
       super().__init__()
       self.image = pygame.Surface((50, 50))
       self.image.fill((255, 100, 0))
       self.rect = self.image.get_rect(center=(pos_x, pos_y))
       self.pos = self.image.get_rect(center=(pos_x, pos_y))

somewhere over here is probably the problem when I multiplied the self.pos.y it stoped working

def update(self, dt):
        self.pos.y +=  5 * dt
        self.rect.y = round(self.pos.y)

        if self.rect.y >= screen_height + 200:
            self.kill()


pygame.init()
clock = pygame.time.Clock()

screen_width, screen_height = 800, 800
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.mouse.set_visible(False)

obstacle_timer = pygame.USEREVENT + 1
pygame.time.set_timer(obstacle_timer, 150)

player = Player()
player_group = pygame.sprite.Group()
player_group.add(player)

bullet_group = pygame.sprite.Group()

enemy_group = pygame.sprite.Group()

here I get dt

prev_time = time.time()
while True:
    dt = time.time() - prev_time
    prev_time = time.time()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:

            bullet_group.add(player.create_bullet())

        if event.type == obstacle_timer:
            if randint(0, 2):
                enemy_group.add(player.create_enemy())


    screen.fill((30, 30, 30))
    bullet_group.draw(screen)
    bullet_group.update(dt)

    
    collide_player_enemy = pygame.sprite.spritecollide(player, enemy_group, True)
    collide_enemy_bullet = pygame.sprite.groupcollide(bullet_group, enemy_group, True, True)

    for s in collide_player_enemy:
        pygame.draw.rect(screen, (255, 255, 255), s.rect, 5, 1)
        print('collide_player_enemy')

    for x in collide_enemy_bullet:
        pygame.draw.rect(screen, (255, 255, 255), x.rect, 5, 1)  
        print('collide_enemy_bullet')
    player_group.draw(screen)
    player_group.update()

 

over here I return the dt

    enemy_group.draw(screen)
    enemy_group.update(dt)

    pygame.display.flip()
    clock.tick(600)

Unix
  • 61
  • 4
  • 1
    "this is one whole program" - **please do not do this**. Instead, read [mre] and figure out what code needs to be shown **in order to understand the problem directly**. – Karl Knechtel Feb 15 '23 at 23:04
  • 1
    "its just that I dont know where the problem is " It is your responsibility to try to figure that out before posting. We do not provide a debugging service. Please read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ for some ideas about how to do this. – Karl Knechtel Feb 15 '23 at 23:09
  • 2
    You're storing your position as a direct integer. But `dt` is a float value less than 1. You even are rounding the result anyway, so you should be aware that you're throwing away tons of precision. Instead, store your position value as a float, and translate it to the appropriate `int` value for display reasons only. Even old NES games didn't store positions as pixel values; pixels are too big. They used sub-pixel values even way back then. – Random Davis Feb 15 '23 at 23:10
  • More to the point: the purpose of questions on Stack Overflow **is not** to get your code to work, but to **explain** something (i.e. **answer a question**) that can be expressed in a **searchable** way (so that other people who have the same problem, can find it with a search engine). Therefore: try to make a simpler example of the code - starting from scratch, if necessary - that has the same problem. Focus on the **specific functionality** you want to make work, that seem to be related to the problem. – Karl Knechtel Feb 15 '23 at 23:13
  • For example: does the player have to fire bullets, or display a health bar, or have an image for its sprite, or track hit points, in order to cause the problem? (If you don't know, then **try** to remove those implementations and see if the problem is still there.) – Karl Knechtel Feb 15 '23 at 23:14
  • tryied to make it as small as I could im sorry for before – Unix Feb 15 '23 at 23:16
  • I know im throwing away precision when I round it but Im rounding beacause I want to display it and because python throws away decimal values when trying to display rect(sprite) – Unix Feb 15 '23 at 23:21

0 Answers0