Pygame Draw Dashed Line

среда 12 июняadmin
Pygame Draw Dashed Line Rating: 7,8/10 7025 reviews

This is a drawing program for the raspberry pi that uses pygame. It's designed to be compatable with the Adafruit TFT.

You can make a game without making classes, but since pygame use objects like Surface you'd probably want some understanding about them. To answer your question however, the pygame.transform module do offer functions to manipulate (resize, flip, rotate and more) images. Pygame.transform.rotate(image, angle) will return the image rotated by the. This function takes two coordinates and draws a colored dashed line from the first to the second coordinate. Line a = abs(x2 - x1) to line dy = dl. b / c calculates the amount x and y change for dl (dashlength). Because dx and dy are floats, I had to use numpy.arange (built-in.

Step by Step Guide

  1. Open Idle (2.7, not 3)

  2. Justin bieber acoustic songs. Click File > New Window or hit Ctrl + N

  3. Import the pygame, sys, os and time modules

  4. Initiate pygame

  5. Next we'll set up the mouse and the frames per second clock.

  6. Now we'll set up our two displays:

    • window
    • and canvas
  7. 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 tool

    note: you'll need to use the american spelling of color..unfortunately it will break if you spell it the corect way with a u

  8. Now we'll build our window. Set up a forever loop and udate the display and add in an exit loop

    Save your code using ctrl + s and then press F5, a small black window should apear. Press the x to close it.

  9. 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()

  10. Save your code and run it again, you should now see that your screen is filled with your background color.

  11. 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 the while True after the window.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.

  12. 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 our for 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 previous if statement

    And you'll also need to blit the canvas ontp the window, so after window.fill(WHITE) add in the following line:

    And before the while 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!

  13. 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.

  1. You could add the ability to change colors
  2. Or use the arrow keys to make the brush change size
  3. You could add in a clear button
  4. Or introduce the ability to save the image you have created
  5. 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.