Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pygame.surface
#1
The idea is that stars fall from top of screen to bottom.
I have this
1
2
3
4
5
6
7
starPosX = [50,100,200,300,400]
starPosY = [-10,-20,-40,-500]
imagelist = [filename for filename in cd.iterdir() if filename.suffix in ['.png', '.jpg', '.bmp']]
 
starx = random.choice(starPosX)
stary = random.choice(starPosY)
star = random.choice(imagelist)
in the loop I have
1
screen.blit(star, (starx, stary))
The error is
1
2
3
4
Traceback (most recent call last):
  File "C:\Users\anybl\Python_Progs\shapes\Shapes.py", line 140, in <module>
    screen.blit(star, (starx, stary))
TypeError: argument 1 must be pygame.Surface, not WindowsPath
My questions are
1)How can I use range 0,500 step 50 instead of starPosX = [50,100,200,300,400] I have tried various thing but nothing works.
2)How do I strip the path from the star.png so that variable star ends up just being = to red_star.png, or whichever colored star is chosen at random.
Reply
#2
1.
1
startPosX = list(range(50, 450, 50))
2.
1
2
3
4
5
import os
starpath = 'path/to/star.png'
print(os.path.dirname(starpath))
# or
print(os.path.split(starpath))
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Thanks windspar,
The
1
startPosX = list(range(50, 450, 50))
worked.

But can't get the starpath to work.
This is fine a star falls from top of screen to bottom
1
2
starColor = pygame.image.load('purple_star.png')
screen.blit(starColor, (starX, starY))
This works
prints a random star to screen
1
2
imagelist = [filename for filename in cd.iterdir() if filename.suffix in ['.png', '.jpg', '.bmp']]
print (random.choice(imagelist))
What I want is to get the random star to fall
1
2
starColor = random.choice(imagelist)
screen.blit(starColor, (starX, starY))
Reply
#4
Let see if this works. Since I believe you are using pathlib.
Need to load images
1
imagelist = [pygame.image.load(str(filename)) for filename in cd.iterdir() if filename.suffix in ['.png', '.jpg', '.bmp']]
or
1
star_images = [pygame.image.load(str(filename)) for filename in cd.iterdir() if filename.name.endswith('star.png')]
You also should convert images. Converting image to match pygame format will make it run faster.
Otherwise pygame will do it on the fly. It will convert it every loop.
1
2
3
4
5
6
star_images = []
for filename in cd.iterdir():
    if filename.name.endswith('star.png'):
        image = pygame.load(str(filename))
        # if is alpha then use convert_alpha()
        star_images.append(image.convert())

Before converting image. You need main pygame surface first.
99 percent of computer problems exists between chair and keyboard.
Reply
#5
Windspar, thanks for the help.
The error code I get now is
1
2
3
4
Traceback (most recent call last):
  File "C:\Users\anybl\Python_Progs\shapes\Shapes.py", line 41, in <module>
    image = pygame.load(str(filename))
AttributeError: module 'pygame' has no attribute 'load'
Below is the full code that I have, maybe it will help to figure where I'm going wrong.
It is nothing more than me trying various things with python as an introduction to my learning.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import random, pygame, sys
from pygame.locals import *
from pathlib import Path
 
pygame.init()
 
# Define the path
currentDirectory = Path('.')
cd = Path('.')
 
# 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)
 
PI = 3.141592653
 
# Set the height and width of the screen
size = (500, 600)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Shapes")
  
# Loop until the user clicks the close button.
done = False
clock = pygame.time.Clock()
     
starPosX = list(range(50, 450, 50))
starPosY = list(range(10, 50, 5))
imagelist = [filename for filename in cd.iterdir() if filename.suffix in ['.png', '.jpg', '.bmp']]
star_images = []
for filename in cd.iterdir():
    if filename.name.endswith('.png'):
        image = pygame.load(str(filename))
        # If is alpha then use convert_alpha()
        star_images.append(image.convert())
         
starX = (random.choice(starPosX))
starY = (random.choice(starPosY))
# StarColor = pygame.image.load('green_star.png')
 
while not done:
    starY += 2
    if starY > 650:
        starX = (random.choice(starPosX))
        starY = (random.choice(starPosY)-50) # StarPosY above screen
        starColor = random.choice(star_images)# choose random star image
    for event in pygame.event.get():  # User did something
        if event.type == pygame.QUIT:  # If user clicked close
            done = True  # Flag that we are done so we exit this loop
     
    # All drawing code happens after the for loop but
    # inside the main while not done loop.
     
    # Clear the screen and set the screen background
    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)
     
    # Select the font to use, size, bold, italics
    font = pygame.font.SysFont('Calibri', 25, True, False)
      
    # Render the text. "True" means anti-aliased text.
    # Black is the color. This creates an image of the
    # Letters, but does not put it on the screen
    text = font.render("Shapes Demo", True, BLUE)
    text1 = font.render("Arcs x 4", True, GREEN)
         
    # Put the image of the text on the screen at x , y
    screen.blit(text, [150, 60])
    screen.blit(text1, [60, 330])
    screen.blit(star_images, (starX, starY))# Place star on screen
    # Go ahead and update the screen with what we've drawn.
    # This MUST happen after all the other drawing commands.
    pygame.display.flip()
      
    # This limits the while loop to a max of 60 times per second.
    # Leave this out and we will use all CPU we can.
    clock.tick(60)
 
# Be IDLE friendly
pygame.quit()
Reply
#6
Yep I forgot image.
1
pygame.image.load

Only grab stars. If you have other images in folder.
1
2
3
4
5
6
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.
1
2
3
4
5
6
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())

1
2
3
4
5
6
7
8
9
10
11
12
# 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
1
PI = 3.141592653
just import pi
1
from math import pi
or
1
from math import pi as PI

Since this draw the same every time. Remove it from loop and apply it to another surface.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
99 percent of computer problems exists between chair and keyboard.
Reply
#7
Still the same problem
1
2
3
4
Traceback (most recent call last):
  File "C:\Users\anybl\Python_Progs\shapes\Shapes.py", line 143, in <module>
    screen.blit(star_images, (starX, starY))# Place star on screen
TypeError: argument 1 must be pygame.Surface, not list
Tried this in another window,
1
2
3
4
5
6
import random, pygame, sys
from pathlib import Path
cd = Path('.')
pygame.init()
imagelist = [filename for filename in cd.iterdir() if filename.suffix in ['.png', '.jpg', '.bmp']]
print(random.choice(imagelist))
Which results in this.
1
2
3
4
5
6
7
8
9
C:\Users\anybl\Python_Progs\shapes>image.py
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
purple_star.png #Random color selected
 
C:\Users\anybl\Python_Progs\shapes>image.py
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
green_star.png # Random color selected
This works if you only want the one image.
1
StarColor = pygame.image.load('green_star.png')
Tried this, thinking th variable starChoice would be passed to the image.load function
1
2
3
4
5
6
imagelist = [filename for filename in cd.iterdir() if filename.suffix in ['.png', '.jpg', '.bmp']]
starChoice = (random.choice(imagelist))
         
starX = (random.choice(starPosX))
starY = (random.choice(starPosY))
StarColor = pygame.image.load(starChoice)
Just get this
1
2
3
4
Traceback (most recent call last):
  File "C:\Users\anybl\Python_Progs\shapes\Shapes.py", line 42, in <module>
    StarColor = pygame.image.load(starChoice)
pygame.error: Can't seek in this data source
The trick seems to be getting the result of the random selection passed in to,
1
screen.blit(starColor, (starX, starY))# Place star on screen
Reply
#8
add function before while loop and this after function.
1
background = create_background(size)
Line 62 replace screen.fill(WHITE).
1
screen.blit(background, (0,0))

1
2
imagelist = [filename for filename in cd.iterdir() if filename.suffix in ['.png', '.jpg', '.bmp']]
starChoice = (random.choice(imagelist))
imagelist filename is a pathlib class. It is not a string.
try.
1
StarColor = pygame.image.load(str(starChoice))
99 percent of computer problems exists between chair and keyboard.
Reply
#9
I read somewhere that programming with Python was easy!
Glad I'm not here on my own or computer would be in canal!
1
2
3
4
Traceback (most recent call last):
  File "C:\Users\anybl\Python_Progs\shapes\Shapes.py", line 42, in <module>
    background = create_background(size)
NameError: name 'create_background' is not defined
Reply
#10
Programming is a tool set. Can you go in your garage and make a couch. You probably could make a simple bookshelf. It takes time and practice. One simple rule. KISS (keep it simple stupid).

Here my example. This is how I would write it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import pygame
from math import pi as PI
from random import choice
 
class Star:
    @classmethod
    def allowed_colors(cls):
        cls.colors = []
        for key in pygame.color.THECOLORS.keys():
            color = pygame.Color(key)
            if key.startswith('gray') or key.startswith('grey'):
                continue
            elif color[0] > 230 and color[1] > 230 and color[2] > 230:
                continue
            else:
                cls.colors.append(color)
 
    def __init__(self):
        self.color = choice(self.colors)
        self.x = choice(list(range(20, 500, 20)))
        self.y = 20
        self.fall_speed = choice(list(range(17, 37))) / 10
        self.create_image()
 
    def create_image(self):
        self.image = pygame.Surface((20,20))
        self.image = self.image.convert_alpha()
        self.image.fill((0,0,0,0))
 
        pygame.draw.polygon(self.image, self.color, [(0,19), (9,0), (19,19), (0,7), (19,7)])
 
    def draw(self, surface, delta):
        pos = int(self.x), int(self.y)
        surface.blit(self.image, pos)
 
        self.y += self.fall_speed * delta * 0.0625
 
class Game:
    def __init__(self):
        self.rect = pygame.Rect(0, 0, 500, 600)
        pygame.display.set_caption('Star Drop Example')
        self.surface = pygame.display.set_mode(self.rect.size)
        self.background = self.create_background(self.rect.size)
        self.clock = pygame.time.Clock()
 
        Star.allowed_colors()
        self.star = Star()
        self.star2 = Star()
 
    def create_background(self, 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
 
    def loop(self):
        self.running = True
        while self.running:
            delta = self.clock.tick(30)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False
 
            self.surface.blit(self.background, (0,0))
 
            if self.star.y < self.rect.bottom - 20:
                self.star.draw(self.surface, delta)
            else:
                # create a new star
                self.star = Star()
 
            if self.star2.y < self.rect.bottom - 20:
                self.star2.draw(self.surface, delta)
            else:
                # create a new star
                self.star2 = Star()
 
            pygame.display.flip()
 
 
if __name__ == '__main__':
    pygame.init()
    game = Game()
    game.loop()
    pygame.quit()
99 percent of computer problems exists between chair and keyboard.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Surface and rectangle in pygame Fabrizio_fg 7 6,615 Apr-15-2025, 05:04 PM
Last Post: jassonadder
  [PyGame] Pygame is treating blob_group as a surface, when I need it to treat it as a Sprite. Swagford 1 2,366 Jan-24-2023, 09:58 PM
Last Post: metulburr
  Coloring a surface with transparency Sandor 4 3,419 Jan-02-2022, 08:11 AM
Last Post: Sandor
  Drawn line shift when that surface is copied to another in pygame Leo_Red 4 4,510 Feb-11-2021, 06:33 AM
Last Post: Leo_Red
  [PyGame] pygame.Surface.fill help Shemira 3 7,754 Nov-29-2019, 12:01 AM
Last Post: Shemira
  [PyGame] PLEASE HELP! TypeError: unsupported operand type(s) for +: 'pygame.Surface' and 'int' keyfive 1 6,411 Jun-19-2018, 01:20 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020