Untitled

mail@pastecode.io avatar
unknown
python
a year ago
2.7 kB
2
Indexable
Never
# import
import pygame, sys, random

pygame.init()  # initializing
screen = pygame.display.set_mode((800, 600))  # display surface

# hide the mouse
pygame.mouse.set_visible(False)

clock = pygame.time.Clock()  # determine speed / frame rate of the game
bg = pygame.image.load('wood_bg.png')  # same folder
land_bg = pygame.image.load('land_bg.png')  # land image
water = pygame.image.load('water_bg.png')  # water image
cloud_1 = pygame.image.load('cloud1.png')  # cloud img 1
cloud_2 = pygame.image.load('cloud2.png')  # cloud img 2
crosshair = pygame.image.load('crosshair.png')
duck_surface = pygame.image.load('duck.png')

duck_list = []  # empty list
for duck in range(10):  # for loop to show 10 ducks
    duck_position_x = random.randrange(50,750)
    duck_position_y = random.randrange(100,550)
    duck_rect = duck_surface.get_rect(center=(duck_position_x, duck_position_y))
    duck_list.append(duck_rect)

land_pos_y = 500
land_speed = 1
water_pos_y = 550
water_speed = 1.5

# text
game_font = pygame.font.Font(None, 70)
text = game_font.render('You Won!!', True , (255,255,255))
text_rect = text.get_rect(center = (400, 300))


while True:  # game loop
    for event in pygame.event.get():  # when the while loop starts
        # pygame starts looking for all the events
        if event.type == pygame.QUIT:  # closing
            pygame.quit()  # close pygame
            sys.exit()  # close all the program

        if event.type == pygame.MOUSEMOTION: # mouse pointer position
            pygame.mouse.get_pos()
            cross_rect = crosshair.get_rect(center=pygame.mouse.get_pos())

        if event.type == pygame.MOUSEBUTTONDOWN: # mouse click event
            for index, duck_rect in enumerate (duck_list):
                if duck_rect.collidepoint(event.pos):
                    del duck_list[index]

    # bg screen
    screen.blit(bg, (0, 0))

    # animation
    land_pos_y = land_pos_y + land_speed
    if land_pos_y <= 500 or land_pos_y >= 550:
        land_speed *= -1
    screen.blit(land_bg, (0, land_pos_y))

    water_pos_y += water_speed
    if water_pos_y <= 510 or water_pos_y >= 560:
        water_speed *= -1

    screen.blit(water, (0, water_pos_y))

    # show all the ducks
    for duck_rect in duck_list:    # display
        screen.blit(duck_surface, duck_rect)

    # check the duck list
    if len(duck_list) == 0:
        screen.blit(text, text_rect)

    # crosshair show
    screen.blit(crosshair, cross_rect)

    # cloud show
    screen.blit(cloud_1, (30, 30))
    screen.blit(cloud_2, (150, 70))
    screen.blit(cloud_1, (350, 40))
    screen.blit(cloud_2, (550, 65))
    screen.blit(cloud_1, (700, 50))

    pygame.display.update()
    clock.tick(60)  # frame rate