Beruflich Dokumente
Kultur Dokumente
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
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.