Yep I forgot image.
pygame.image.load
Only grab stars. If you have other images in folder.
star_images = []
for filename in cd.iterdir():
if filename.name.endswith('star.png'):
image = pygame.image.load(str(filename))
# if is alpha then use convert_alpha()
star_images.append(image.convert())
If you just want suffix.
star_images = []
for filename in cd.iterdir():
if filename.suffix == '.png':
image = pygame.image.load(str(filename))
# if is alpha then use convert_alpha()
star_images.append(image.convert())
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
PURPLE = (173,11,144)
CYAN =(0, 255, 255)
YELLOW =(255, 255, 0)
ORANGE =(255, 128, 0)
GRAY =(100, 100, 100)
NAVYBLUE=(60, 60, 100)
Can be replace with pygame.Color(color_name)
Output:
>>> pygame.Color('navyblue')
(0, 0, 128, 255)
No need to define PI
PI = 3.141592653
just import pi
from math import pi
or
from math import pi as PI
Since this draw the same every time. Remove it from loop and apply it to another surface.
screen.fill(WHITE)
# Draw on the screen a line from (0,50) to (190,150)
# 5 pixels wide.
pygame.draw.line(screen, GREEN, [0, 150], [500, 150], 5)
pygame.draw.line(screen, GREEN, [0, 260], [500, 260], 5)
# Draw on the screen several lines from (0,160) to (100,160)
# 5 pixels wide using a loop
for y_offset in range(0, 100, 10):
pygame.draw.line(screen, PURPLE, [0, 160 + y_offset], [240 - y_offset, 160 + y_offset], 5)
pygame.draw.line(screen, PURPLE, [262 + y_offset, 160 + y_offset], [500, 160 + y_offset], 5)
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Draw crosses top
for x_offset in range(95, 370, 10):
pygame.draw.line(screen,BLUE,[x_offset,15],[x_offset-10,5],2)
pygame.draw.line(screen,RED,[x_offset,5],[x_offset-10,15],2)
# Draw crosses bottom
for x_offset in range(95, 370, 10):
pygame.draw.line(screen,BLUE,[x_offset,135],[x_offset-10,125],2)
pygame.draw.line(screen,RED,[x_offset,125],[x_offset-10,135],2)
# Draw crosses left
for y_offset in range(24, 130, 10):
pygame.draw.line(screen,BLUE,[95, y_offset],[85,y_offset-10],2)
pygame.draw.line(screen,RED,[85,y_offset],[95, y_offset-10],2)
# Draw crosses right
for y_offset in range(24, 130, 10):
pygame.draw.line(screen,BLUE,[365, y_offset],[355,y_offset-10],2)
pygame.draw.line(screen,RED,[355,y_offset],[365, y_offset-10],2)
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Draw an ellipse, using a rectangle as the outside boundaries
pygame.draw.rect(screen, RED, [100, 20, 250, 100], 4)
pygame.draw.ellipse(screen, PURPLE, [100, 20, 250, 100], 3)
# Draw an ellipse with nner ellipses
pygame.draw.ellipse(screen, BLUE, [390, 295, 100, 150], 3)
pygame.draw.ellipse(screen, RED, [400, 305, 80, 130], 3)
pygame.draw.ellipse(screen, CYAN, [425, 330, 30, 80], 3)
# Draw an arc as part of an ellipse.
# Use radians to determine what angle to draw.
pygame.draw.arc(screen, PURPLE, [20, 295, 150, 100], 0, PI / 2, 2)
pygame.draw.arc(screen, GREEN, [20, 295, 150, 100], PI / 2, PI, 2)
pygame.draw.arc(screen, BLUE, [20, 295, 150, 100], PI, 3 * PI / 2, 2)
pygame.draw.arc(screen, RED, [20, 295, 150, 100], 3 * PI / 2, 2 * PI, 2)
# This draws triangles using the polygon command
pygame.draw.polygon(screen, BLACK, [[250, 150], [145, 255], [355, 255]], 2)
pygame.draw.polygon(screen, RED, [[250, 255], [197, 203], [303, 203]], 2)
# Draw a star using polygon command
pygame.draw.polygon(screen, BLUE, [[270, 270], [290, 340], [370, 340],[310, 380],[335, 455],[270, 405],[210, 455],[235, 380],[180, 340],[250, 340]], 4)
# Draw a square with a cross
pygame.draw.rect(screen, RED, [20, 450, 100, 100], 4)
pygame.draw.line(screen, BLUE, (20, 450), (120, 550), 4)
pygame.draw.line(screen, BLUE, (120, 450), (20, 550), 4)
# Draw a colored square with a circle
pygame.draw.line(screen, BLUE, (350, 480), (450, 480), 4)
pygame.draw.line(screen, PURPLE, (450, 480), (450, 580), 4)
pygame.draw.line(screen, GREEN, (450, 580), (350, 580), 4)
pygame.draw.line(screen, RED, (350, 580), (350, 480), 4)
pygame.draw.ellipse(screen, ORANGE, [350, 480, 100, 100], 3)
Background
def create_background(size):
surface = pygame.Surface(size)
surface.fill(pygame.Color('white'))
# Draw on the surface a line from (0,50) to (190,150)
# 5 pixels wide.
pygame.draw.line(surface, pygame.Color('green'), [0, 150], [500, 150], 5)
pygame.draw.line(surface, pygame.Color('green'), [0, 260], [500, 260], 5)
# Draw on the surface several lines from (0,160) to (100,160)
# 5 pixels wide using a loop
for y_offset in range(0, 100, 10):
pygame.draw.line(surface, pygame.Color('purple'), [0, 160 + y_offset], [240 - y_offset, 160 + y_offset], 5)
pygame.draw.line(surface, pygame.Color('purple'), [262 + y_offset, 160 + y_offset], [500, 160 + y_offset], 5)
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Draw crosses top
for x_offset in range(95, 370, 10):
pygame.draw.line(surface,pygame.Color('blue'),[x_offset,15],[x_offset-10,5],2)
pygame.draw.line(surface,pygame.Color('red'),[x_offset,5],[x_offset-10,15],2)
# Draw crosses bottom
for x_offset in range(95, 370, 10):
pygame.draw.line(surface,pygame.Color('blue'),[x_offset,135],[x_offset-10,125],2)
pygame.draw.line(surface,pygame.Color('red'),[x_offset,125],[x_offset-10,135],2)
# Draw crosses left
for y_offset in range(24, 130, 10):
pygame.draw.line(surface,pygame.Color('blue'),[95, y_offset],[85,y_offset-10],2)
pygame.draw.line(surface,pygame.Color('red'),[85,y_offset],[95, y_offset-10],2)
# Draw crosses right
for y_offset in range(24, 130, 10):
pygame.draw.line(surface,pygame.Color('blue'),[365, y_offset],[355,y_offset-10],2)
pygame.draw.line(surface,pygame.Color('red'),[355,y_offset],[365, y_offset-10],2)
#xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# Draw an ellipse, using a rectangle as the outside boundaries
pygame.draw.rect(surface, pygame.Color('red'), [100, 20, 250, 100], 4)
pygame.draw.ellipse(surface, pygame.Color('purple'), [100, 20, 250, 100], 3)
# Draw an ellipse with nner ellipses
pygame.draw.ellipse(surface, pygame.Color('blue'), [390, 295, 100, 150], 3)
pygame.draw.ellipse(surface, pygame.Color('red'), [400, 305, 80, 130], 3)
pygame.draw.ellipse(surface, pygame.Color('cyan'), [425, 330, 30, 80], 3)
# Draw an arc as part of an ellipse.
# Use radians to determine what angle to draw.
pygame.draw.arc(surface, pygame.Color('purple'), [20, 295, 150, 100], 0, PI / 2, 2)
pygame.draw.arc(surface, pygame.Color('green'), [20, 295, 150, 100], PI / 2, PI, 2)
pygame.draw.arc(surface, pygame.Color('blue'), [20, 295, 150, 100], PI, 3 * PI / 2, 2)
pygame.draw.arc(surface, pygame.Color('red'), [20, 295, 150, 100], 3 * PI / 2, 2 * PI, 2)
# This draws triangles using the polygon command
pygame.draw.polygon(surface, pygame.Color('black'), [[250, 150], [145, 255], [355, 255]], 2)
pygame.draw.polygon(surface, pygame.Color('red'), [[250, 255], [197, 203], [303, 203]], 2)
# Draw a star using polygon command
pygame.draw.polygon(surface, pygame.Color('blue'), [[270, 270], [290, 340], [370, 340],[310, 380],[335, 455],[270, 405],[210, 455],[235, 380],[180, 340],[250, 340]], 4)
# Draw a square with a cross
pygame.draw.rect(surface, pygame.Color('red'), [20, 450, 100, 100], 4)
pygame.draw.line(surface, pygame.Color('blue'), (20, 450), (120, 550), 4)
pygame.draw.line(surface, pygame.Color('blue'), (120, 450), (20, 550), 4)
# Draw a colored square with a circle
pygame.draw.line(surface, pygame.Color('blue'), (350, 480), (450, 480), 4)
pygame.draw.line(surface, pygame.Color('purple'), (450, 480), (450, 580), 4)
pygame.draw.line(surface, pygame.Color('green'), (450, 580), (350, 580), 4)
pygame.draw.line(surface, pygame.Color('red'), (350, 580), (350, 480), 4)
pygame.draw.ellipse(surface, pygame.Color('orange'), [350, 480, 100, 100], 3)
return surface