0

Fishes are supposed to spawn from left to right. And also it seems to be something weird with spawning. Some fishes appear after 20 seconds even if I set up minimum and maximum time of spawning. Also I have doubts about the function add_fish() I'm not sure this is the right way to generate the fishes: 6 kind of fishes and all with the same chance to be generated. What am I doing wrong?

class SpawnTime():
    def __init__(self, previous, time):
        self.time_previous = previous
        self.time = time


class Fish():
    count = 0

    def __init__(self, txr, value, speed):
        self.txr = txr
        self.value = value
        self.speed = speed
        self.x = SCREEN_WIDTH
        self.y = random.randint(0, SCREEN_HEIGHT - self.txr.get_height())
        self.rect = pygame.Rect(
            self.x, self.y, self.txr.get_width(), self.txr.get_height())
        self.is_active = True
        Fish.count += 1

    def draw(self):
        SCREEN.blit(self.txr, self.rect)

    def update(self):
        self.rect.x += self.speed

        if self.rect.right <= 0:
            self.is_active = False


class Player():
    def __init__(self):
        self.txr = PLAYER_TXR
        self.x = 0
        self.y = 0
        self.width = self.txr.get_width()
        self.height = self.txr.get_height()
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
        self.score = 0
        self.reset()

    def draw(self):
        SCREEN.blit(self.txr, self.rect)

    def update(self):
        self.x, self.y = pygame.mouse.get_pos()
        self.rect.x = self.x
        self.rect.y = self.y

    def reset(self):
        self.x = self.width
        self.y = SCREEN_HEIGHT//2
        self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
        self.score = 0

    def clicked(self):
        for f in fish_list:
            if f.rect.collidepoint(self.x, self.y):
                self.score += f.value
                f.is_active = False
                SOUND_SLIP.play()
                break  # exit the for loop



def update():
    if playing:
        clock.tick(FPS)

    if playing and not game_over:

        game_time = pygame.time.get_ticks()

        if Fish.count < MAX_FISHES:
            if game_time - fish_spawn.time_previous >= fish_spawn.time:
                fish_spawn.time_previous = game_time
                fish_spawn.time = random.randint(
                    MIN_FISH_SPAWN_TIME, MAX_FISH_SPAWN_TIME) 

                add_fish()

        player.update()

        for fish in fish_list:
            fish.update()

        for fish in fish_list:
            if not fish.is_active:
                fish_list.remove(fish)


def draw():
    SCREEN.blit(TITLE_SCREEN, (0, 0))

    if not playing and game_over:

        if new_high_score:
            draw_text_centred_x(
                f'New High Score: {high_score}', FONT_20,  250, TEAL)
        else:
            draw_text_centred_x(
                f'High Score: {high_score}', FONT_20,  50, TEAL)
            draw_text_centred_x(
                f'SCORE: {player.score}', FONT_30, 170, TEAL)
            draw_text_centred_x('Press SPACE to Play',
                                FONT_30,  350, DARK_GREEN)

    if not playing and not game_over:

        draw_text_centred_x('Press SPACE to Play',
                            FONT_40,  250, DARK_GREEN)

    if playing and not game_over:

        draw_text(f'Score: {player.score}    High Score: {high_score}',
                  FONT_20,  5, 5, DARK_GREEN)


        for fish in fish_list:
            fish.draw()

        player.draw()

def draw_text_centred_x(text, font, y, colour):
    img = font.render(text, True, colour)
    x = SCREEN_WIDTH//2 - img.get_width()//2
    SCREEN.blit(img, (x, y))


def draw_text(text, font, x, y, colour):
    img = font.render(text, True, colour)
    SCREEN.blit(img, (x, y))


def reset_game():

    fish_list.clear()

    player.reset()


def add_fish():

    if random.randint(1, 6) == 1:
        rand = random.randint(0, len(FISHES_TXR)-1)
        txr = FISHES_TXR[rand]
        value = rand+1
        speed = rand*5

        if txr == FISHES_TXR[5]:
            value = -3
      
        f = Fish(txr, value, speed)
        fish_list.append(f)



if __name__ == "__main__":

    playing = False
    game_over = False

    high_score = 0
    new_high_score = False

    player = Player()
    fish = Fish

    fish_list = []

    fish_spawn = SpawnTime(0, MAX_FISH_SPAWN_TIME) 

    # main game loop
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:  
                quit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:  
                    quit()
                if event.key == pygame.K_SPACE and not game_over:
                    fish_spawn.time = MAX_FISH_SPAWN_TIME
                    playing = True
                    

                if event.key == pygame.K_SPACE and not playing:
                    # new game
                    playing = True
                    game_over = False
                    new_high_score = False
                    player.score = 0

                    fish_spawn.time = 700
                    Fish.count = 0

                    pygame.time.delay(500)  # pause for 500ms = 0.5s

            elif event.type == pygame.MOUSEBUTTONDOWN:
                player.clicked()

        if playing:
            update()
            if Fish.count >= MAX_FISHES and len(fish_list) == 0:
                game_over = True
                playing = False
                if player.score > high_score:
                    new_high_score = True
                    high_score = player.score

                draw()

                pygame.time.delay(500)  # pause for 500ms = 0.5s

        draw()

        pygame.display.update()
        clock.tick(FPS)
    
    pygame.quit()
Rabbid76
  • 177,135
  • 25
  • 101
  • 146
Zisca
  • 21
  • 1
  • 1
    Call `clock.tick(FPS)` just once at the end of the application loop. – Rabbid76 May 30 '21 at 17:07
  • Thank you. I still have a problem with spawning from left to right. With self.rect.x += self.speed it doesn't work. I don't understand why – Zisca May 30 '21 at 19:46

0 Answers0