Introduction
Installation of Pygame with Pycharm
Basics of Pygame
Running simple Pygame programs which do almost nothing.
A very simple program
A demonstration of a simple program showing the structure of Pygame programming.
import pygame #initialization pygame.init() #create the screen screen=pygame.display.set_mode((300,300)) windowrunning = True while windowrunning: #main loop for event in pygame.event.get(): #event loop if event.type == pygame.QUIT: windowrunning=False if event.type == pygame.MOUSEBUTTONDOWN: mousex, mousey = pygame.mouse.get_pos() print("ButtonDown" + " " + str(mousex) + " " + str(mousey)) pygame.draw.circle(screen, (255, 0, 0), (mousex, mousey), 5, 1) pygame.display.update() if event.type == pygame.MOUSEMOTION: mousex, mousey = pygame.mouse.get_pos() pygame.draw.circle(screen, (255, 255, 255), (mousex, mousey), 1, 1) pygame.display.update() pygame.display.quit() pygame.quit()
Pygame Simple Drawing Application
If the line is a sequence of dots then this is a drawing application. Pretty useless but a stepping stone.
import pygame #initialization pygame.init() #create the screen screen=pygame.display.set_mode((1280,720)) windowrunning = True img=1 write=0 while windowrunning: #main loop for event in pygame.event.get(): #event loop if event.type == pygame.QUIT: windowrunning=False if event.type == pygame.MOUSEBUTTONDOWN: mousex, mousey = pygame.mouse.get_pos() write=1 if event.type == pygame.MOUSEBUTTONUP: write=0 if event.type == pygame.MOUSEMOTION and write==1: mousex, mousey = pygame.mouse.get_pos() if pygame.mouse.get_pressed()[0]: #Red for Left pygame.draw.circle(screen, (255, 0, 0), (mousex, mousey), 1, 1) if pygame.mouse.get_pressed()[2]: #Green for Right pygame.draw.circle(screen, (0, 255, 0), (mousex, mousey), 1, 1) if pygame.mouse.get_pressed()[1]: #Blue for Middle pygame.draw.circle(screen, (0, 0, 255), (mousex, mousey), 1, 1) pygame.display.update() if event.type == pygame.KEYDOWN: print ("Key" + str(event.key)) if event.key== 115: imgname="image"+"{0:03}".format(img)+".jpg" pygame.image.save(screen, imgname) img=img+1 pygame.display.quit() pygame.quit()
Blackboard Application
We iron out the kinks in the basic drawing app to build a blackboard app.
import pygame #initialization pygame.init() #create the screen screen=pygame.display.set_mode((1280,720)) windowrunning = True #main loop variable img=1 #saved photo counter write=0 #flag for writing lines r=255 #red color g=0 #green color b=0 #blue color thickness=1 #thickness of line rect=0 #flag for writing rectangle while windowrunning: #main loop for event in pygame.event.get(): #event loop if event.type == pygame.QUIT: windowrunning=False if event.type == pygame.MOUSEBUTTONDOWN: mousex, mousey = pygame.mouse.get_pos() write=1 if event.type == pygame.MOUSEBUTTONUP: mousex1, mousey1 = pygame.mouse.get_pos() if(rect==1): pygame.draw.rect(screen, (r, g, b), (mousex, mousey, (mousex1-mousex), (mousey1-mousey)) , 1) write=0 rect=0 pygame.display.update() if event.type == pygame.MOUSEMOTION and write==1: mousex1, mousey1 = pygame.mouse.get_pos() if pygame.mouse.get_pressed()[0]: pygame.draw.line(screen, (r, g, b), (mousex, mousey), (mousex1, mousey1) , thickness) mousex=mousex1 mousey=mousey1 if pygame.mouse.get_pressed()[2]: rect=1 pygame.display.update() if event.type == pygame.KEYDOWN: print ("Key" + str(event.key)) if event.key== 115: imgname="image"+"{0:03}".format(img)+".jpg" pygame.image.save(screen, imgname) img=img+1 if event.key== pygame.K_DELETE: screen.fill((0,0,0)) pygame.display.update() if event.key== pygame.K_y: r,g,b=255,255,0 if event.key== pygame.K_r: r,g,b=255,0,0 if event.key== pygame.K_w: r,g,b=255,255,255 if event.key== pygame.K_b: r,g,b=0,0,255 if event.key== pygame.K_g: r,g,b=0,255,0 if event.key== 61: #plus key if thickness < 10: thickness = thickness +1 if event.key == pygame.K_MINUS: if thickness>1: thickness = thickness - 1 pygame.display.quit() pygame.quit()