I am trying to make pygame draw a shape from a list of shapes imported with fileIO but i get a pygame import error please help

import random
import pygame
w = 640
h = 480

x = open("shapes.txt")
z = x.readlines()[n]
n = random.randrange(6)

screen = pygame.display.set_mode((w, h))
pygame.draw.z(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )

for event in pygame.event.get():
        if event.type == QUIT:
            exit()

this is my my "shapes.txt"

circle
rect
line
line
rect
circle

say if n was circle, this is what python interprets:

pygame.draw."circle"(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )

now thats not code; its a string. you could add if statements like so:

if z == 'circle':
    pygame.draw.circle(arguements_go_here)
elif z == 'line':
    # code for drawing line here
import random
import pygame
w = 640
h = 480

n = random.randrange(6)
x = open("shapes.txt")
z = x.readlines()[n]

screen = pygame.display.set_mode((w, h))
if z == "circle":
    pygame.draw.circle(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
elif z == "line":
    pygame.draw.line(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
elif z == "rect":
    pygame.draw.rect(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )



for event in pygame.event.get():
        if event.type == QUIT:
            exit()

my new code but i get this error now

Message File Name Line Position
Traceback
<module> C:\Users\Curtie\Desktop\HomeWork\CSET\codes\assignment_11\assignment_11.py 2
<module> L:\PortableApps\PortablePython_1.1_py3.0.1\App\lib\site-packages\pygame\__init__.py 95
ImportError: DLL load failed: The specified module could not be found.

try

import pygame
from pygame.locals import *

if that doesn't work, make sure you have the pygame that matches up with your version of python

also, if that fails, try reinstalling your version of pygame

Quick hint: Instead of the condition cascade

if z == "circle": ... elif ...

consider a more pythonically succinct approach:

assert n in ('circle', 'line', 'rect')
draw = getattr(pygame.draw, n)
draw(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )

ive reinstalled python and pygame ive added the

import pygame
from pygame.locals import *

with no results....i am using 3.01. it works in 2.61 but i am required to use 3.01 does anyone else no what might be causing this?
also its in just the import pygame function as i have tested just running that and get the same error code

import random
import pygame

w = 640
h = 480

n = random.randrange(6)
x = open("shapes.txt")
z = x.readlines()[n]



screen = pygame.display.set_mode((w, h))
if z == "circle":
    pygame.draw.circle(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 100 )
elif z == "line":
    pygame.draw.line(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 100 )
elif z == "rect":
    pygame.draw.rect(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 100 )

This code works in 2.6.1 but when there are no shapes just a black screen, is there a way to fix this?

import random
import pygame

w = 640
h = 480

n = random.randrange(6)
x = open("shapes.txt")
z = x.readlines()[n]



screen = pygame.display.set_mode((w, h))
if "circle" in z:
    pygame.draw.circle(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
elif "line" in z:
    pygame.draw.line(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
elif "rect" in z:
    pygame.draw.rect(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )

I've fixed the main problem but now i get

Traceback (most recent call last):
File "<string>", line 244, in run_nodebug
File "C:\Users\Curtie\Desktop\HomeWork\CSET\codes\assignment_11\assignment_112.py", line 17, in <module>
pygame.draw.line(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
TypeError: Invalid end position argument
>>>
Traceback (most recent call last):
File "<string>", line 244, in run_nodebug
File "C:\Users\Curtie\Desktop\HomeWork\CSET\codes\assignment_11\assignment_112.py", line 19, in <module>
pygame.draw.rect(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
TypeError: Rect argument is invalid

import random
import pygame

w = 640
h = 480

n = random.randrange(6)
x = open("shapes.txt")
z = x.readlines()[n]



screen = pygame.display.set_mode((w, h))
if z == "circle":
    pygame.draw.circle(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 100 )
elif z == "line":
    pygame.draw.line(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 100 )
elif z == "rect":
    pygame.draw.rect(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 100 )

This code works in 2.6.1 but when there are no shapes just a black screen, is there a way to fix this?

You need to read the pygame manual!

Drawing a line, circle or rectangle takes different arguments. They all have screen and color in common, but then they diverge. A line takes 2 coordinate points for start and end of line. A circle takes one coordinate point for the center and a radius value. The rectangle takes diagonal corner coordinates.

You also need to call
pygame.display.flip()
for anything to show

... and last not least an event loop at the end.

When you have a project like this, it is best to test each shape you want to draw before you combine it all. Let's start out with a pygame template that you can use for each shape ...

import pygame
import random

pygame.init()
w = 640
h = 480
screen = pygame.display.set_mode((w, h))

# set (r, g, b) color tuple
color = (random.randrange(255), random.randrange(255), 
    random.randrange(255))


# ... your shape code goes here ...


# update display
pygame.display.flip()

# event loop ...
running = True
while running:
    for event in pygame.event.get():
        # quit when window corner x is clicked
        if event.type == pygame.QUIT:
            running = False

Now let's test the rectangle shape ...

import pygame
import random

pygame.init()
w = 640
h = 480
screen = pygame.display.set_mode((w, h))

# set (r, g, b) color tuple
color = (random.randrange(255), random.randrange(255), 
    random.randrange(255))


# set rect corner coordiates to draw a rectangle
# (x1, y1, x2, y2) upper left and lower right corner coordinates
rect = (random.randrange(w), random.randrange(h), 
    random.randrange(w), random.randrange(h))
# pygame.draw.rect(Surface, color, Rect, width=0)
# if width=0 (or not given) the rectangle is filled with color
pygame.draw.rect(screen, color, rect)


# update display
pygame.display.flip()

# event loop ...
running = True
while running:
    for event in pygame.event.get():
        # quit when window corner x is clicked
        if event.type == pygame.QUIT:
            running = False

... next test the line ...

import pygame
import random

pygame.init()
w = 640
h = 480
screen = pygame.display.set_mode((w, h))

# set (r, g, b) color tuple
color = (random.randrange(255), random.randrange(255), 
    random.randrange(255))


# set start and end position of line
start_pos = random.randrange(w), random.randrange(h) 
end_pos = random.randrange(w), random.randrange(h)
# pygame.draw.line(Surface, color, start_pos, end_pos, width=1)
# width is thickness of line (default is 1)
pygame.draw.line(screen, color, start_pos, end_pos)


# update display
pygame.display.flip()

# event loop ...
running = True
while running:
    for event in pygame.event.get():
        # quit when window corner x is clicked
        if event.type == pygame.QUIT:
            running = False

... lastly test the circle ...

import pygame
import random

pygame.init()
w = 640
h = 480
screen = pygame.display.set_mode((w, h))

# set (r, g, b) color tuple
color = (random.randrange(255), random.randrange(255), 
    random.randrange(255))


# set center_pos for circle
center_pos = random.randrange(w), random.randrange(h)
# pygame.draw.circle(Surface, color, center_pos, radius, width=0)
# if width=0 (or not given) the circle is filled with color
radius = 50
pygame.draw.circle(screen, color, center_pos, radius)


# update display
pygame.display.flip()

# event loop ...
running = True
while running:
    for event in pygame.event.get():
        # quit when window corner x is clicked
        if event.type == pygame.QUIT:
            running = False
import random
import pygame

w = 640
h = 480

n = random.randrange(6)
x = open("shapes.txt")
z = x.readlines()[n]

lines = len(z)
str(lines)
l = str(('The file shapes.txt has',lines,'lines.'))
output = open('output.txt','w')
output.write(l)
output.close()

screen = pygame.display.set_mode((w, h))
if "circle" in z:
    pygame.draw.circle(screen, (random.randrange(255), random.randrange(255), random.randrange(255)), (random.randrange(640), random.randrange(480)), 50 )
elif "line" in z:
    pygame.draw.line(screen, (random.randrange(255), random.randrange(255), random.randrange(255)),(random.randrange(w), random.randrange(h)), (random.randrange(w), random.randrange(h)))
elif "rect" in z:
    pygame.draw.rect(screen, (random.randrange(255), random.randrange(255), random.randrange(255)),(random.randrange(w), random.randrange(h), random.randrange(w), random.randrange(h)))

pygame.display.flip()


running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

Solved thanks to everyone for the help!

... and last not least an event loop at the end

lol

ooops fixed thanks lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.