954,116 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

fileIO & pygame help

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

Attachments shapes.txt (0.04KB)
CurtisEClark
Newbie Poster
12 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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
fallopiano
Junior Poster in Training
68 posts since Jun 2009
Reputation Points: 10
Solved Threads: 6
 
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
C:\Users\Curtie\Desktop\HomeWork\CSET\codes\assignment_11\assignment_11.py 2
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.

CurtisEClark
Newbie Poster
12 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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

fallopiano
Junior Poster in Training
68 posts since Jun 2009
Reputation Points: 10
Solved Threads: 6
 

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

fallopiano
Junior Poster in Training
68 posts since Jun 2009
Reputation Points: 10
Solved Threads: 6
 

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 )
pythopian
Junior Poster in Training
81 posts since Nov 2009
Reputation Points: 20
Solved Threads: 25
 

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

CurtisEClark
Newbie Poster
12 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
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?

CurtisEClark
Newbie Poster
12 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
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 "", line 244, in run_nodebug
File "C:\Users\Curtie\Desktop\HomeWork\CSET\codes\assignment_11\assignment_112.py", line 17, in
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 "", line 244, in run_nodebug
File "C:\Users\Curtie\Desktop\HomeWork\CSET\codes\assignment_11\assignment_112.py", line 19, in
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

CurtisEClark
Newbie Poster
12 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
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. Aline 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.

vegaseat
DaniWeb's Hypocrite
Moderator
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
 

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
vegaseat
DaniWeb's Hypocrite
Moderator
5,976 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,416
 
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!

CurtisEClark
Newbie Poster
12 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
... and last not least an event loop at the end


lol

fallopiano
Junior Poster in Training
68 posts since Jun 2009
Reputation Points: 10
Solved Threads: 6
 

ooops fixed thanks lol

CurtisEClark
Newbie Poster
12 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You