Pygame live code: how to draw on the screen

How to draw on the screen with the mouse in a very simple app using pygame? Let’s start diving into the code with a very basic window made with pygame. In the next posts we will add new features. I made other posts about this, but I didn’t made a video of the live coding and now you can find it below, after the code.

from PIL import Image
# pip install pillow
import pygame
import glob
import os

pygame.init()
# create the window
screen = pygame.display.set_mode((600,400))
# give the name to the window
pygame.display.set_caption("Trace")
clock = pygame.time.Clock()

loop = 1
while loop:
	try:
		for event in pygame.event.get():
			if event.type== pygame.QUIT:
				loop = False
		px, py = pygame.mouse.get_pos()
		if pygame.mouse.get_pressed() == (1,0,0):
			pygame.draw.ellipse(screen, (255,255,255), (px,py,10,10))
		pygame.display.update()
		clock.tick(100)

	except Exception as e:
		print(e)
		pygame.quit()

pygame.quit()

Live conding video: draw, part 1

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.