Re: line drawing program python and pygame
[code]import pygame
import sys
# Initialize pygame
pygame.init()
# Set the window size
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600
WINDOW_SIZE = (WINDOW_WIDTH, WINDOW_HEIGHT)
# Set up the window
window = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption('Line Drawing Program')
# Set up colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Set up variables for line drawing
drawing = False
start_pos = None
end_pos = None
# Main loop
while True:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Left mouse button
drawing = True
start_pos = event.pos
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1: # Left mouse button
drawing = False
end_pos = event.pos
pygame.draw.line(window, BLACK, start_pos, end_pos, 2)
pygame.display.flip()
elif event.type == pygame.MOUSEMOTION:
if drawing:
end_pos = event.pos
window.fill(WHITE) # Clear the window
pygame.draw.line(window, BLACK, start_pos, end_pos, 2)
pygame.display.flip()
# Update the display
pygame.display.update()[/code]