External Exam Download Resources Web Applications Games Recycle Bin

Shapes

shape1 rectangle.py

import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
while not done:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      done = True
      
  pygame.draw.rect(
    screen,          # surface to draw to
    (250, 128, 114), # (R, G, B) red green blue (tuple)
    (10, 10, 90, 25) # (topL_x, topL_y, width, height)
  )
  
  pygame.display.flip()
pygame.quit()

shapes2 lines circles polygons.py

# Colour constants to make our lives easier:
white = (255,255,255) #(R, G, B)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))

screen.fill(white) #change background colour of screen

done = False
while not done:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      done = True

  #-------- CIRCLES:
  pygame.draw.circle(
    screen,    # surface to draw to
    blue,      # colour
    (150,150), # (centerX, centerY)
    15         # radius
  )

  #-------- LINES:
  pygame.draw.line(
    screen,  # surface to draw to
    red,     # colour
    (10,10), # (startX, startY)
    (50,20), # (endX, endY)
    3        # width
  )

  #-------- POLYGONS:
  pygame.draw.polygon(
    screen,  # surface to draw to
    green,   # colour
    ( ####### POINTS IN POLYGON #######
      (90,90), # (x, y)
      (100,90),
      (90,110),
      (80, 100)
    ) #^^ COULD ADD OR REMOVE POINTS
  )

  pygame.display.flip()
pygame.quit()

shapes3 pixels.py

import pygame
pygame.init()
screen = pygame.display.set_mode((400, 300))
done = False
salmon = (250, 128, 114)
while not done:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      done = True

  pixel_array = pygame.PixelArray(screen)
  pixel_array[50][100] = salmon
  #^ will draw a salmon pixel at position X:50, Y:100
  
  pygame.display.flip()
pygame.quit()