This is a drawing program for the raspberry pi that uses pygame. It's designed to be compatable with the Adafruit TFT.
Image.stroke(paint.line(10, 10, 90, 70).dash(0, (10, 5)), paint.rgb(0xff,0,0), 10) Returns. Green = 0, blue = 0, alpha = 0xff) which is used to draw the dashed line. Graphics Primitives. Phillip Burgess. Each device-specific display library will have its own constructors and initialization functions. These are documented in the individual tutorials for each display type, or oftentimes are evident in the specific library header file. You can also draw lines, with a starting and end point and color.
Step by Step Guide
- Open Idle (2.7, not 3)
- Click
File > New Window
or hitCtrl + N
- Import the
pygame
,sys
,os
andtime
modules - Initiate pygame
- Next we'll set up the mouse and the frames per second clock.
- Now we'll set up our two displays:
- window
- and canvas
- Next we'll set up some colors, for now we'll just use black and white but feel free to add more, you can do this by using an RGB color code, you can find these by using this toolnote: you'll need to use the american spelling of color...unfortunately it will break if you spell it the corect way with a
u
- Now we'll build our window. Set up a forever loop and udate the display and add in an exit loopSave your code using
ctrl + s
and then pressF5
, a small black window should apear. Press the x to close it. - Now we're going to fill in our window with a background colour, in this case it will be the white color we set up earlier. In the while True we created earlier enter the following line of code before
python pygame.display.update()
- Save your code and run it again, you should now see that your screen is filled with your background color.
- Next we're going to make a circle follow your mouse so as you can see where you'll draw. So we're going to find out where your mouse is using
pygame.mouse.get_pos()
. Inside thewhile True
after thewindow.fill(WHITE)
line add in the code that draws the circle at the cursor's position.Now if you save and run your code you should see a small black circle following your cursor around. - Now we need to give your program the ablility to check that your mouse is held down or not. To do this we going to add the following lines at the top of our
while True
loop.and in order to draw a circle when your mouse click is detected you'll have to add this code on to the end of ourfor event in pygame.event.get()
loop, which can otherwize be known as an event handler.Note: you must ensure it has the same level of indentation as your previousif
statementAnd you'll also need to blit the canvas ontp the window, so afterwindow.fill(WHITE)
add in the following line:And before thewhile True
loop add a line that fills in the background of the the canvas in white.If you save and run now you should be able to draw! - Finally we're going to give your window a title. Below
pygame.init()
add in the following line of code, you can replace paintme with whatever title for your game you want!
How to Extend your project.
- You could add the ability to change colors
- Or use the arrow keys to make the brush change size
- You could add in a clear button
- Or introduce the ability to save the image you have created
- Allow the user to import a picture, draw on top of it, and resave the image
And much much more!You can find the codes for different key presses here and other pygame functionality here.