Posts: 14
Threads: 4
Joined: Nov 2018
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.
Posts: 544
Threads: 15
Joined: Oct 2016
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))
print (os.path.split(starpath))
|
99 percent of computer problems exists between chair and keyboard.
Posts: 14
Threads: 4
Joined: Nov 2018
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))
|
Posts: 544
Threads: 15
Joined: Oct 2016
Nov-29-2018, 02:02 AM
(This post was last modified: Nov-29-2018, 02:05 AM by Windspar.)
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))
star_images.append(image.convert())
|
Before converting image. You need main pygame surface first.
99 percent of computer problems exists between chair and keyboard.
Posts: 14
Threads: 4
Joined: Nov 2018
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()
currentDirectory = Path( '.' )
cd = Path( '.' )
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
size = ( 500 , 600 )
screen = pygame.display.set_mode(size)
pygame.display.set_caption( "Shapes" )
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))
star_images.append(image.convert())
starX = (random.choice(starPosX))
starY = (random.choice(starPosY))
while not done:
starY + = 2
if starY > 650 :
starX = (random.choice(starPosX))
starY = (random.choice(starPosY) - 50 )
starColor = random.choice(star_images)
for event in pygame.event.get():
if event. type = = pygame.QUIT:
done = True
screen.fill(WHITE)
pygame.draw.line(screen, GREEN, [ 0 , 150 ], [ 500 , 150 ], 5 )
pygame.draw.line(screen, GREEN, [ 0 , 260 ], [ 500 , 260 ], 5 )
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 )
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 )
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 )
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 )
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 )
pygame.draw.rect(screen, RED, [ 100 , 20 , 250 , 100 ], 4 )
pygame.draw.ellipse(screen, PURPLE, [ 100 , 20 , 250 , 100 ], 3 )
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 )
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 )
pygame.draw.polygon(screen, BLACK, [[ 250 , 150 ], [ 145 , 255 ], [ 355 , 255 ]], 2 )
pygame.draw.polygon(screen, RED, [[ 250 , 255 ], [ 197 , 203 ], [ 303 , 203 ]], 2 )
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 )
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 )
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 )
font = pygame.font.SysFont( 'Calibri' , 25 , True , False )
text = font.render( "Shapes Demo" , True , BLUE)
text1 = font.render( "Arcs x 4" , True , GREEN)
screen.blit(text, [ 150 , 60 ])
screen.blit(text1, [ 60 , 330 ])
screen.blit(star_images, (starX, starY))
pygame.display.flip()
clock.tick( 60 )
pygame.quit()
|
Posts: 544
Threads: 15
Joined: Oct 2016
Nov-29-2018, 02:33 PM
(This post was last modified: Nov-29-2018, 03:14 PM by Windspar.)
Yep I forgot image.
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))
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))
star_images.append(image.convert())
|
1 2 3 4 5 6 7 8 9 10 11 12 |
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
just 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)
pygame.draw.line(screen, GREEN, [ 0 , 150 ], [ 500 , 150 ], 5 )
pygame.draw.line(screen, GREEN, [ 0 , 260 ], [ 500 , 260 ], 5 )
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 )
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 )
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 )
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 )
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 )
pygame.draw.rect(screen, RED, [ 100 , 20 , 250 , 100 ], 4 )
pygame.draw.ellipse(screen, PURPLE, [ 100 , 20 , 250 , 100 ], 3 )
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 )
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 )
pygame.draw.polygon(screen, BLACK, [[ 250 , 150 ], [ 145 , 255 ], [ 355 , 255 ]], 2 )
pygame.draw.polygon(screen, RED, [[ 250 , 255 ], [ 197 , 203 ], [ 303 , 203 ]], 2 )
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 )
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 )
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' ))
pygame.draw.line(surface, pygame.Color( 'green' ), [ 0 , 150 ], [ 500 , 150 ], 5 )
pygame.draw.line(surface, pygame.Color( 'green' ), [ 0 , 260 ], [ 500 , 260 ], 5 )
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 )
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 )
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 )
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 )
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 )
pygame.draw.rect(surface, pygame.Color( 'red' ), [ 100 , 20 , 250 , 100 ], 4 )
pygame.draw.ellipse(surface, pygame.Color( 'purple' ), [ 100 , 20 , 250 , 100 ], 3 )
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 )
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 )
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 )
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 )
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 )
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.
Posts: 14
Threads: 4
Joined: Nov 2018
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))
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
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
|
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))
|
Posts: 544
Threads: 15
Joined: Oct 2016
Nov-29-2018, 03:22 PM
(This post was last modified: Nov-29-2018, 03:26 PM by Windspar.)
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.
Posts: 14
Threads: 4
Joined: Nov 2018
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
|
Posts: 544
Threads: 15
Joined: Oct 2016
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' ))
pygame.draw.line(surface, pygame.Color( 'green' ), [ 0 , 150 ], [ 500 , 150 ], 5 )
pygame.draw.line(surface, pygame.Color( 'green' ), [ 0 , 260 ], [ 500 , 260 ], 5 )
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 )
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 )
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 )
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 )
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 )
pygame.draw.rect(surface, pygame.Color( 'red' ), [ 100 , 20 , 250 , 100 ], 4 )
pygame.draw.ellipse(surface, pygame.Color( 'purple' ), [ 100 , 20 , 250 , 100 ], 3 )
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 )
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 )
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 )
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 )
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 )
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 :
self .star = Star()
if self .star2.y < self .rect.bottom - 20 :
self .star2.draw( self .surface, delta)
else :
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.
|