Sie sind auf Seite 1von 2

Pygame Reference Sheet Drawing with Pygame

Setting things up
In order to use Pygame you need to do a little bit of setup work. import pygame pygame.init() size = (800, 600) screen = pygame.display.set_mode(size) This imports all of the pygame "stuff" into your program, starts the pygame engine and opens a window. You don't need to have size as a separate variable (you could just add that tuple to your set_mode) but by having it as a variable you can more easily change the size of your program.

Screen Set-up
The screen is set up as a grid of pixels. Each pixel has an (x, y) location, with (0, 0) being the top left corner. X increases to the right and Y increases as it goes down.

(0,0)

x Y

(3, 12)

Understanding Colours
In Pygame, all colours are represented by a tuple of their RGB values, with each value being given a number from 0 to 255 to indicate its intensity. The most basic colours are: BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) To get more advances colours you often need to use a paint program, or look them up in a table, Like: STEEL_BLUE = (70, 130, 180) SALMON = 250, 128, 114

Page 1 of 2

Drawing Basic Shapes


All of the basic drawing commands require a Surface as their first parameter, colour as the second parameter, and width as the last. Surface - For the most part, the Surface that we want to draw to is the window we opened, or the variable "screen" from above. Colour - As above, this is a RGB tuple. Width This is the thickness of the pen that is used to draw the shape. If the thickness is zero the shape will be filled in. In all of the basic drawing commands width is an optional parameter. So if you do not specify the width it will be 0 (and thus filled.)

pygame.draw.rect
pygame.draw.rect(Surface, colour, Rect, width=0) e.g. pygame.draw.rect(screen, (0, 155, 0), pygame.Rect(50, 100, 150, 200), 2)

Draws a rectangular shape on the Surface. The given Rect is the area of the rectangle. The width argument is the thickness to draw the outer edge. If width is zero or not specified then the rectangle will be filled.

pygame.draw.circle
pygame.draw.circle(Surface, colour, pos, radius, width=0) e.g. pygame.draw.circle(screen, (0, 155, 0), (150, 200), 50, 2)

Draws a circular shape on the Surface. The pos argument is the center of the circle, and radius is the size. The width argument is the thickness to draw the outer edge. If width is zero then the circle will be filled.

pygame.draw.ellipse
pygame.draw.ellipse(Surface, colour, Rect, width=0) e.g. pygame.draw.ellipse(screen, (0, 155, 0), pygame.Rect(50, 100, 150, 200))

Draws an elliptical shape on the Surface. The given rectangle is the area that the circle will fill. The width argument is the thickness to draw the outer edge. If width is zero then the ellipse will be filled

pygame.draw.line
pygame.draw.line(Surface, color, start_pos, end_pos, width=1): return Rect

Draw a straight line segment on a Surface. There are no endcaps, the ends are squared off for thick lines.

Other Important Commands


time.wait(miliseconds) pauses the program for the number of milliseconds specified. Important to achieve animation effects. display.flip() - When you draw things in Pygame, they are not drawn to the screen. Instead they are drawn to an "offscreen buffer" and not copied to the screen until it runs display.flip() quit() - ALWAYS quit() at the end of your program. This shuts down the Pygame engine and frees up resources. Page 2 of 2

Das könnte Ihnen auch gefallen