So I'm trying to make a duck hunt style game and ive gotten to the point where when i click on a duck I'm trying to make it disappear. I have all the ducks in a list and when i click on them they disappear, but the problem is for some reason it deletes all the ducks in the list instead of just the one i clicked on.
hopefully i wont doxx myself with the file locations
Here is my code:
import math
import pygame.locals
pygame.init()
# Setting a fps
fps = pygame.time.Clock()
# Making a color
transparent = (0, 0, 0, 0)
# Windowing screen width and height
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Makes the mouse invisible
pygame.mouse.set_visible(False)
# Loading all images I'm using
gun = pygame.image.load(r"C:\Users\Ben\Downloads\download (3).png")
crosshair_image = pygame.image.load(r"C:\Users\Ben\Downloads\2783540c8c81da4.png")
background_image = pygame.image.load(r"C:\Users\Ben\Downloads\pixel-1000x600 (1).png")
duck_image = pygame.image.load(r"C:\Users\Ben\Downloads\pixel-20x20.png")
# Scaling the background to fit dimensions
background = pygame.transform.scale(background_image, (5000, 3000))
screen.blit(background, (-1930, -1200))
# Rotating and putting the gun in the correct spot
gun = pygame.transform.rotate(gun, 90)
gun_position = (375, 400)
pygame.transform.rotate(gun, 180)
# Scaling the crosshair image
crosshair = pygame.transform.scale(crosshair_image, (100, 100))
# Scaling the ducks image
duck = pygame.transform.scale(duck_image, (100, 100))
duck1 = pygame.transform.scale(duck_image, (100, 100))
duck2 = pygame.transform.scale(duck_image, (100, 100))
duck3 = pygame.transform.scale(duck_image, (100, 100))
duck4 = pygame.transform.scale(duck_image, (100, 100))
ducks = [
duck,
duck1,
duck2,
duck3,
duck4,
]
# 0 - image is looking to the right
# 90 - image is looking up
# 180 - image is looking to the left
# 270 - image is looking down
correction_angle = 90
# Defining the ducks position
x = -120
y = 20
x1 = -120
x2 = -120
x3 = -120
x4 = -120
run = True
while run:
# Displaying fps
fps.tick(60)
clicking = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# Making the ducks move across the screen
if x == 1000:
x = -120
if run:
x += 1
if x == 100:
x1 = -120
else:
x1 += 1
if x1 == 100:
x2 = -120
else:
x2 += 1
if x2 == 100:
x3 = -120
else:
x3 += 1
if x3 == 100:
x4 = -120
else:
x4 += 1
# Gets Mouse Position
mouse_position = pygame.mouse.get_pos()
if event.type == pygame.MOUSEMOTION:
# Putting the background in the right place
screen.blit(background, (-1930, -1200))
# Putting the ducks on the screen
for duck in ducks:
screen.blit(duck, (x, y))
for duck1 in ducks:
screen.blit(duck1, (x1, y))
for duck2 in ducks:
screen.blit(duck2, (x2, y))
for duck3 in ducks:
screen.blit(duck3, (x3, y))
for duck4 in ducks:
screen.blit(duck4, (x4, y))
if x == -120:
ducks.append(duck)
if x1 == -120:
ducks.append(duck1)
if x2 == -120:
ducks.append(duck2)
if x3 == -120:
ducks.append(duck3)
if x4 == -120:
ducks.append(duck4)
# Putting the crosshair on the mouse position
screen.blit(crosshair, mouse_position)
# Makes the gun rotate towards the mouse(crosshair)
player_pos = screen.get_rect().center
player_rect = gun.get_rect(center=player_pos)
mx, my = pygame.mouse.get_pos()
dx, dy = mx - player_rect.centerx, my - player_rect.centery
angle = math.degrees(math.atan2(-dy, dx)) - correction_angle
rot_image = pygame.transform.rotate(gun, angle)
rot_image_rect = rot_image.get_rect(center=player_rect.center)
screen.blit(rot_image, rot_image_rect.topleft,)
if event.type == pygame.MOUSEBUTTONDOWN:
for duck in ducks[:]:
if duck.get_rect().collidepoint(pygame.mouse.get_pos()):
ducks.remove(duck)
if event.type == pygame.MOUSEBUTTONDOWN:
for duck1 in ducks[:]:
if duck1.get_rect().collidepoint(pygame.mouse.get_pos()):
ducks.remove(duck1)
if event.type == pygame.MOUSEBUTTONDOWN:
for duck2 in ducks[:]:
if duck2.get_rect().collidepoint(pygame.mouse.get_pos()):
ducks.remove(duck2)
if event.type == pygame.MOUSEBUTTONDOWN:
for duck3 in ducks[:]:
if duck3.get_rect().collidepoint(pygame.mouse.get_pos()):
ducks.remove(duck3)
if event.type == pygame.MOUSEBUTTONDOWN:
for duck4 in ducks[:]:
if duck4.get_rect().collidepoint(pygame.mouse.get_pos()):
ducks.remove(duck4)
pygame.display.flip()
pygame.display.update()
pygame.quit()