Pythoenixx / TembaKetik

0 stars 0 forks source link

empty targetting bug #17

Closed Pythoenixx closed 9 months ago

Pythoenixx commented 9 months ago

image

Pythoenixx commented 9 months ago

if still happened, this might help:

if self.nearest_enemy is not None:
            #rotate self to enemy
            jrk_x = self.nearest_enemy.rect.centerx - self.rect.centerx
            jrk_y = -(self.nearest_enemy.rect.centery - self.rect.centery)
            sudut = math.degrees(math.atan2(jrk_y, jrk_x))
            self.image = pygame.transform.rotate(self.image, sudut - 90)
            self.rect = self.image.get_rect(center = (self.x, self.y)) #this make it rotate at its center for some reason #maybe cos when the image rotate, it create new image and new image has its own rect size but since new rect size is not created, it still use the old rect and its coords so it makes other images cant grow past this old rect coord point(which is at topleft) but it still allow other corner to grow
            #highlight enemy
            self.nearest_enemy.text_color = 'Gold'
            pygame.draw.rect(screen, 'Gold', self.nearest_enemy.rect, 1)
            self.nearest_enemy.targeted = True
            if char_updated:
                #update enemy
                if self.nearest_enemy.word[0] == char_typed[-1]: # cane ai tau??
                    tembak = Bullet(self.x, self.y, self.nearest_enemy)#buat mcm ztype, musuh akan explode bila sume bullet sampai kat dia
                    self.group_bullets.add(tembak)

                    self.nearest_enemy.word = self.nearest_enemy.word[1:] #dia phm aku nk delete word tu ke?
                    self.typed_word_count += 1
                else:
                    self.miss += 1
                    self.miss_word.append(self.nearest_enemy.ori_word)

                if self.nearest_enemy.word == '':
                            self.enemy_killed += 1
                            print(self.nearest_enemy.ori_word, "killed")
                            self.nearest_enemy = None
                            print("enemy set to :", self.nearest_enemy)

                            if len(enemy_group.sprites()) == 1: #last musuh blm mati lagi sbb update dia lepas pemain and dia akan mati sbb enemy.word == '' so kene cek klo tinggal 1 bukan 0
                                self.end_time = pygame.time.get_ticks()
                                self.elapsed_time = ((self.end_time - self.start_time) / 1000) / self.enemy_killed
                                self.elapsed_time_list.append(self.elapsed_time)
                                self.enemy_killed = 0
                                self.start_time = 0 #utk benarkan timer start balik
Pythoenixx commented 9 months ago

this will check if enemy word is ' ' everytime player type, unlike before where it only check that everytime user successfully type the right character from the enemy word

Pythoenixx commented 9 months ago

this wouldnt work since the even the enemy is "killed" (self.kill) the enemy object still exist and the enemy.word is also exist. What the self.kill do is remove itself from the enemy group sprites so the enemy will not be updated and rendered(draw). Since the player only target new enemy if the enemy word is ' ', it will target the same target even if the enemy is "killed". So the reason why player will not switching target is because it only switch the target when the last enemy.word is empty and not when the enemy is "killed"

Pythoenixx commented 9 months ago

`if self.nearest_enemy is not None:

rotate self to enemy

        jrk_x = self.nearest_enemy.rect.centerx - self.rect.centerx
        jrk_y = -(self.nearest_enemy.rect.centery - self.rect.centery)
        sudut = math.degrees(math.atan2(jrk_y, jrk_x))
        self.image = pygame.transform.rotate(self.image, sudut - 90)
        self.rect = self.image.get_rect(center = (self.x, self.y)) #this make it rotate at its center for some reason #maybe cos when the image rotate, it create new image and new image has its own rect size but since new rect size is not created, it still use the old rect and its coords so it makes other images cant grow past this old rect coord point(which is at topleft) but it still allow other corner to grow
        #highlight enemy
        self.nearest_enemy.text_color = 'Gold'
        pygame.draw.rect(screen, 'Gold', self.nearest_enemy.rect, 1)
        self.nearest_enemy.targeted = True

        if self.nearest_enemy.word == '' or self.nearest_enemy.dying:
            self.enemy_killed += 1
            print(self.nearest_enemy.ori_word, "killed, coords:", self.nearest_enemy.rect.center)
            self.nearest_enemy = None
            print("enemy set to :", self.nearest_enemy)

            if len(enemy_group.sprites()) == 1: #last musuh blm mati lagi sbb update dia lepas pemain and dia akan mati sbb enemy.word == '' so kene cek klo tinggal 1 bukan 0
                self.end_time = pygame.time.get_ticks()
                self.elapsed_time = ((self.end_time - self.start_time) / 1000) / self.enemy_killed
                self.elapsed_time_list.append(self.elapsed_time)
                self.enemy_killed = 0
                self.start_time = 0 #utk benarkan timer start balik

        elif char_updated:
            print("player type:", char_typed[-1])
            #update enemy
            if self.nearest_enemy.word[0] == char_typed[-1]: # cane ai tau??
                tembak = Bullet(self.x, self.y, self.nearest_enemy)
                self.group_bullets.add(tembak)

                self.nearest_enemy.word = self.nearest_enemy.word[1:] #dia phm aku nk delete word tu ke?
                print("remaining word:", self.nearest_enemy.word)
                self.typed_word_count += 1
            else:
                self.miss += 1
                self.miss_word.append(self.nearest_enemy.ori_word)`
Pythoenixx commented 9 months ago

this code snippets should now be less prone to the bug