Home API Notes
import pygame 
import sys

# Initialize Pygame
pygame.init()

# Set up the window 
window_size = (800, 600)
window = pygame.display.set_mode(window_size)
pygame.display.set_caption("Catch the Fish!")

# define colors
BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
BLUE  = (  0,   0, 255)
GREEN = (  0, 255,   0)
RED   = (255,   0,   0)

# Set up the variables needed 
clock = pygame.time.Clock()
FPS = 30
score = 0

# The player
player_size = 50
player_position = [350, 550]

# The fish
fish_size = 50
fish_position = [random.randint(0, window_size[0]-fish_size), 0]
fish_speed = 5

# The game loop 
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
   
    # Move the player
    # Get keys pressed
    keys = pygame.key.get_pressed() 
    # Check if left or right arrow is pressed and move the player
    if keys[pygame.K_LEFT] and player_position[0] > 0:
        player_position[0] -= 5
    if keys[pygame.K_RIGHT] and player_position[0] < window_size[0] - player_size:
        player_position[0] += 5
    
    # Move the fish
    if fish_position[1] >= window_size[1]:
        fish_position[0] = random.randint(0, window_size[0] - fish_size)
        fish_position[1] = 0
    else:
        fish_position[1] += fish_speed
    
    # Check if the player caught the fish
    if fish_position[1] + fish_size >= player_position[1]:
        # Check if the fish is in the same x position as the player
        if fish_position[0] > player_position[0] and fish_position[0] < player_position[0] + player_size or \
            fish_position[0] + fish_size > player_position[0] and fish_position[0] + fish_size < player_position[0] + player_size:
            score += 1
            fish_position[0] = random.randint(0, window_size[0] - fish_size)
            fish_position[1] = 0
    
    # Draw the window 
    window.fill(BLACK)
    pygame.draw.rect(window, WHITE, [player_position[0], player_position[1], player_size, player_size])
    pygame.draw.rect(window, GREEN, [fish_position[0], fish_position[1], fish_size, fish_size])
    # Display the score
    font = pygame.font.SysFont("Arial", 25)
    text = font.render("Score: " + str(score), True, WHITE)
    window.blit(text, [window_size[0]-100, 20])
    
    # Update the screen
    pygame.display.update()
    # Tick the clock
    clock.tick(FPS)
pygame 2.1.2 (SDL 2.0.18, Python 3.10.6)
Hello from the pygame community. https://www.pygame.org/contribute.html
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
c:\Users\etran\.vscode\fastpages_EthanT\_notebooks\python_game.ipynb Cell 2 in <cell line: 30>()
     <a href='vscode-notebook-cell:/c%3A/Users/etran/.vscode/fastpages_EthanT/_notebooks/python_game.ipynb#W1sZmlsZQ%3D%3D?line=27'>28</a> # The fish
     <a href='vscode-notebook-cell:/c%3A/Users/etran/.vscode/fastpages_EthanT/_notebooks/python_game.ipynb#W1sZmlsZQ%3D%3D?line=28'>29</a> fish_size = 50
---> <a href='vscode-notebook-cell:/c%3A/Users/etran/.vscode/fastpages_EthanT/_notebooks/python_game.ipynb#W1sZmlsZQ%3D%3D?line=29'>30</a> fish_position = [random.randint(0, window_size[0]-fish_size), 0]
     <a href='vscode-notebook-cell:/c%3A/Users/etran/.vscode/fastpages_EthanT/_notebooks/python_game.ipynb#W1sZmlsZQ%3D%3D?line=30'>31</a> fish_speed = 5
     <a href='vscode-notebook-cell:/c%3A/Users/etran/.vscode/fastpages_EthanT/_notebooks/python_game.ipynb#W1sZmlsZQ%3D%3D?line=32'>33</a> # The game loop 

NameError: name 'random' is not defined
The Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details.