-1

The code:

import pygame
from sys import exit

pygame.init()
screen = pygame.display.set_mode((2000, 1350))
pygame.display.set_caption('Fish Hunt')
clock = pygame.time.Clock()

def sky_animation():
    global sky_surface, sky_index

    sky_index += 0.3
    if sky_index >= len(sky):
       sky_index = 0
    sky_surface = sky[int(sky_index)]

def ocean_animation():
    global ocean_surface, ocean_index

    ocean_index += 0.5
    if ocean_index >= len(ocean):
        ocean_index = 0
    ocean_surface = ocean[int(ocean_index)]

def start_page_animation():
    global fish_surface, fish_index, hunt_surface, hunt_index, play_surface, play_index

    fish_index += 0.5
    if fish_index >= len(fish):
        fish_index = 0
    fish_surface = fish[int(fish_index)]
    hunt_index += 0.5
    if hunt_index >= len(hunt):
        hunt_index = 0
    hunt_surface = hunt[int(hunt_index)]
    play_index += 0.5
    if play_index >= len(play):
        play_index = 0
    play_surface = play[int(play_index)]

def gold_fish_animation():
    global gold_fish_surface, gold_fish_index

    gold_fish_index += 1.6
    if gold_fish_index >= len(gold_fish):
        gold_fish_index = 0
    gold_fish_surface = gold_fish[int(gold_fish_index)]

def hook_down_animation():
   global hook_down_surface, hook_down_index

   hook_down_index += 1
   if hook_down_index >= len(hook_down):
       hook_down_index = 0
   hook_down_surface = hook_down[int(hook_down_index)]

def hook_caught_animation():
    global hook_caught_surface, hook_caught_index, hook_swing_surface, hook_swing_index

    hook_caught_index += 1.5
    if hook_caught_index >= len(hook_caught):
        hook_caught_index = 0
    hook_caught_surface = hook_caught[int(hook_caught_index)]

# font
font = pygame.font.Font('KindlyRewind-BOon.ttf', 64)

# colors
black = (0, 0, 0)
white = (255, 255, 255)

# score
score = 0
score_surface = font.render(f'score: {score}', False, white)
score_rect = score_surface.get_rect(bottomright=(1970, 100))

# sky
sky_f1 = pygame.image.load('skyf1.png')
sky_f2 = pygame.image.load('skyf2.png')
sky_f3 = pygame.image.load('skyf3.png')
sky = [sky_f1, sky_f2, sky_f3]
sky_index = 0

# ocean
ocean_f1 = pygame.image.load('oceanf1.png')
ocean_f2 = pygame.image.load('oceanf2.png')
ocean_f3 = pygame.image.load('oceanf3.png')
ocean = [ocean_f1, ocean_f2, ocean_f3]
ocean_index = 0

# start page
fish_f1 = pygame.image.load('fishf1.png')
fish_f2 = pygame.image.load('fishf2.png')
hunt_f1 = pygame.image.load('huntf1.png')
hunt_f2 = pygame.image.load('huntf2.png')
play_f1 = pygame.image.load('playf1.png')
play_f2 = pygame.image.load('playf2.png')

fish = [fish_f1, fish_f2]
hunt = [hunt_f1, hunt_f2]
play = [play_f1, play_f2]

fish_index = 0
hunt_index = 0
play_index = 0

# gold fish 1
goldf_f1 = pygame.image.load('goldf_f1.png').convert_alpha()
goldf_f1_rect = goldf_f1.get_rect(bottomright=(1200, 1250))
goldf_f2 = pygame.image.load('goldf_f2.png').convert_alpha()

gold_fish = [goldf_f1, goldf_f2]
gold_fish_index = 0

goldf_x_pos = 1200
gold_fish_animation()
gold_fish_rect = gold_fish_surface.get_rect(bottomright=(goldf_x_pos, 1250))

# hook down
hookf = pygame.image.load('hookf.png')
hookf_rect = hookf.get_rect(bottomright=(1950, 1350))

hookf_f1_down = pygame.image.load('hookf_f1_down.png')

hookf_f2_down = pygame.image.load('hookf_f2_down.png')
hookf_f2_rect = hookf_f2_down.get_rect(bottomright=(1950, 1350))

hook_down = [hookf, hookf_f1_down, hookf_f2_down, hookf_f2_down]
hook_down_index = 0

hookf_x_pos = 1950
hook_down_animation()
hook_down_rect = hook_down_surface.get_rect(bottomright=(hookf_x_pos, 1350))

# hook caught and swing
hookf_f1_caught = pygame.image.load('hookf_f1_caught.png')
hookf_f2_caught = pygame.image.load('hookf_f2_caught.png')
hookf_f3_caught = pygame.image.load('hookf_f3_caught.png')
hookf_f4_caught = pygame.image.load('hookf_f4_caught.png')

hookf_f1_swing = pygame.image.load('hookf_f1_swing.png')
hookf_f2_swing = pygame.image.load('hookf_f2_swing.png')
hookf_f3_swing = pygame.image.load('hookf_f3_swing.png')
hookf_f4_swing = pygame.image.load('hookf_f4_swing.png')
hookf_f5_swing = pygame.image.load('hookf_f5_swing.png')

hook_caught = [hookf_f1_caught, hookf_f2_caught, hookf_f3_caught, hookf_f4_caught,
           hookf_f1_swing, hookf_f2_swing, hookf_f3_swing, hookf_f4_swing, hookf_f5_swing]
hook_caught_index = 0


hook_caught_x_pos = 1950
hook_caught_animation()
hook_caught_rect = hook_caught_surface.get_rect(bottomright=(hook_caught_x_pos, 1351))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
           pygame.quit()
           exit()

sky_animation()
screen.blit(sky_surface, (0, 0))
ocean_animation()
screen.blit(ocean_surface, (0, 0))
#start_page_animation()
#screen.blit(fish_surface, (0, 0))
#screen.blit(hunt_surface, (0, 0))
#screen.blit(play_surface, (0, 0))
gold_fish_animation()
gold_fish_rect.right += 150
screen.blit(gold_fish_surface, gold_fish_rect)
if gold_fish_rect.right > 3500:
    gold_fish_rect.right = 1200
screen.blit(hookf, hookf_rect)
screen.blit(score_surface, score_rect)
keys = pygame.key.get_pressed()
if keys[pygame.K_DOWN]:
    screen.blit(sky_surface, (0, 0))
    screen.blit(ocean_surface, (0, 0))
    screen.blit(gold_fish_surface, gold_fish_rect)
    hook_down_animation()
    screen.blit(hook_down_surface, hook_down_rect)
    screen.blit(score_surface, score_rect)
if keys[pygame.K_RIGHT]:
    screen.blit(sky_surface, (0, 0))
    screen.blit(ocean_surface, (0, 0))
    screen.blit(gold_fish_surface, gold_fish_rect)
    screen.blit(hookf, hookf_rect)
    screen.blit(score_surface, score_rect)
    hookf_rect.x += 100
if keys[pygame.K_DOWN] and hookf_f2_rect.colliderect(goldf_f1_rect):
    print('collide')
    hook_caught_animation()
    screen.blit(sky_surface, (0, 0))
    screen.blit(ocean_surface, (0, 0))
    hook_caught_rect = hookf_rect
    screen.blit(hook_caught_surface, hook_caught_rect)
    screen.blit(score_surface, score_rect)
    gold_fish_rect.right = -100

pygame.display.update()
clock.tick(60)

The problem I'm having is that even when the gold fish aren't touching, it would print collide in the terminal when I press the down arrow and it would display the goldfish being caught. And even when it is caught, if i don't let go of the down arrow button it keeps on displaying the animation. I tried using hook_down_rect.colliderect(gold_fish_rect) but that didn't work. Assuming the condition is if keys[pygame.K_DOWN] and hookf_f2_rectt.colliderect(goldf_f1_rect) then that should mean it prints collide only if both conditions are satisfied but I'm not sure where I went wrong. Any tips would be helpful!! Go easy on me I'm a beginner :') and sorry for the long code I just wanted everything to be clear.

avery
  • 1
  • 3

0 Answers0