# Arup Guha
# 6/16/2025
# Solution to Sample Quiz Problem Moving Sprite

import pygame, sys
from pygame.locals import *
import random

pygame.init()
DISPLAYSURF = pygame.display.set_mode((1000, 600))
sprite = pygame.image.load("watermelon.jfif")
sprite = pygame.transform.scale(sprite, (64, 64))

width = 1000
height = 600
posX = 500
posY = 300

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        # Looks for a key being pressed down.
        if event.type == KEYDOWN:

            # Down arrow.
            if event.key == K_DOWN:

                # If we're not all the way at the bottom move down randomly.
                if posY < 400:
                    posY = posY + random.randint(1, 400-posY)

            # Up arrow.
            if event.key == K_UP:

                # If we're not at the top, move up randomly.
                if posY > 0:
                    posY = posY - random.randint(1, posY)

            # Left arrow.
            if event.key == K_LEFT:

                # If we're not all the way left, randomly move left.
                if posX > 0:
                    posX = posX - random.randint(1, posX)

            # Right arrow.
            if event.key == K_RIGHT:

                # If we're not all the way right, move right.
                if posX < 800:
                    posX = posX + random.randint(1, 800-posX)

    DISPLAYSURF.fill(pygame.Color(0, 0, 0))
    DISPLAYSURF.blit(sprite, (posX, posY))
    pygame.display.update()
