Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

line drawing program python and pygame

Last updated on 7 months ago
C
caaSuper Admin
Posted 7 months ago
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()
Edited by caa on 03-05-2024 13:17, 7 months ago
C
caaSuper Admin
Posted 7 months ago
click on "download source" button to download complete code
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 2
Members Online 0

Total Members: 11
Newest Member: Jhilam