OMG please help

K

katie smith

Here is the program I just started, The problem i am having is I'm trying to get it to load the image file Sand1 with eval(loader) = pygame.image.load(loader)
because Loader is euqual to "Sand1" but It wont load it. If I set it as loader = pygame.image.load(loader) then it sets the image to the variable loader. So I'm basically trying to set a string equal to a surface variable. I dont want to have to go Sand1 = pygame.image.load("Sand1.bmp") for every image because I'm expecting there to be a lot of them when I am done.

So hard to explain if you don't understand what I'm trying to get from it please let me know.






import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode( (150,150) )
background = pygame.Surface( screen.get_size() )
pygame.display.set_caption("Empire Strategy")
pygame.key.set_repeat(1, 1)
def LoadMaterial():
loader = loading + "1"
eval(loader) = pygame.image.load(loader)
loader = loading + "2"
eval(loader) = pygame.image.load(loader)
loader = loading + "3"
eval(loader) = pygame.image.load(loader)
loader = loading + "4"
eval(loader) = pygame.image.load(loader)
loader = loading + "R"
eval(loader) = pygame.image.load(loader)
loader = loading + "L"
eval(loader) = pygame.image.load(loader)
loader = loading + "T"
eval(loader) = pygame.image.load(loader)
loader = loading + "D"
eval(loader) = pygame.image.load(loader)
loader = loading + "TR"
eval(loader) = pygame.image.load(loader)
loader = loading + "TL"
eval(loader) = pygame.image.load(loader)
loader = loading + "BR"
eval(loader) = pygame.image.load(loader)
loader = loading + "BL"
eval(loader) = pygame.image.load(loader)
loading = "Sand"
LoadMaterial()
pygame.display.update()
repeat = True

while repeat:
for event in pygame.event.get():
if event.type == (QUIT):
pygame.quit()
if (event.type == KEYDOWN):
if (event.key == K_ESCAPE):
pygame.quit()
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
position = pygame.mouse.get_pos()


____________________________________________________________________________________
Looking for last minute shopping deals?
Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping
 
D

Dennis Lee Bieber

So hard to explain if you don't understand what I'm trying to get from it please let me know.
"eval" is sort of like running the argument AS A PROGRAM... It is
not a translation that returns an object that can be used as a
destination to an assignment.

Read the manuals on "dictionary" and use a dictionary to store
things.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
S

Steven D'Aprano

Hi Katie,

Please try to write more descriptive subject lines. "OMG please help"
makes you sound like a 14 y.o. breathless school girl who has just broken
a nail. Probably 3/4th of the regulars who *could* help haven't even read
your post because of the subject line.

Here is the program I just started, The problem i am having is I'm
trying to get it to load the image file Sand1 with eval(loader) =
pygame.image.load(loader) because Loader is euqual to "Sand1" but It
wont load it. If I set it as loader = pygame.image.load(loader) then it
sets the image to the variable loader. So I'm basically trying to set a
string equal to a surface variable. I dont want to have to go Sand1 =
pygame.image.load("Sand1.bmp") for every image because I'm expecting
there to be a lot of them when I am done.

99% of the time, when you find yourself wanting to write things like:

sand1 = pygame.image.load("Sand1.bmp")
sand2 = pygame.image.load("Sand2.bmp")
sand3 = pygame.image.load("Sand3.bmp")
....
sand99 = pygame.image.load("Sand99.bmp")

(or similar) you are going about it the wrong way.

The better way is to do something like this:

sands = [None]
filename = "Sand%d.bmp" # template for the file names
for i in range(1, 100): # start at 1 instead of 0
name = filename % i
sands.append(pygame.image.load(name))


Once you've run that code, sands is a list holding all the images you
need.

(Note: The first item of the sands list is None, because lists are
numbered from 0 but your sands are numbered from 1. So we need to make an
adjustment.)

The second half is, how do you use the images?

Instead of writing something like this:


draw(sand1) # I don't actually know how to draw bitmaps in PyGame...
draw(sand2)
draw(sand3)
....
draw(sand99)


you would do something like this:


for i in range(1, 100):
draw(sands) # or whatever the real command is


Does this help?
 
M

Martin P. Hellwig

Steven said:
Hi Katie,

Please try to write more descriptive subject lines. "OMG please help"
makes you sound like a 14 y.o. breathless school girl who has just broken
a nail. Probably 3/4th of the regulars who *could* help haven't even read
your post because of the subject line.

Here is the program I just started, The problem i am having is I'm
trying to get it to load the image file Sand1 with eval(loader) =
pygame.image.load(loader) because Loader is euqual to "Sand1" but It
wont load it. If I set it as loader = pygame.image.load(loader) then it
sets the image to the variable loader. So I'm basically trying to set a
string equal to a surface variable. I dont want to have to go Sand1 =
pygame.image.load("Sand1.bmp") for every image because I'm expecting
there to be a lot of them when I am done.

99% of the time, when you find yourself wanting to write things like:

sand1 = pygame.image.load("Sand1.bmp")
sand2 = pygame.image.load("Sand2.bmp")
sand3 = pygame.image.load("Sand3.bmp")
...
sand99 = pygame.image.load("Sand99.bmp")

(or similar) you are going about it the wrong way.

The better way is to do something like this:

sands = [None]
filename = "Sand%d.bmp" # template for the file names
for i in range(1, 100): # start at 1 instead of 0
name = filename % i
sands.append(pygame.image.load(name))


Once you've run that code, sands is a list holding all the images you
need.

(Note: The first item of the sands list is None, because lists are
numbered from 0 but your sands are numbered from 1. So we need to make an
adjustment.)

The second half is, how do you use the images?

Instead of writing something like this:


draw(sand1) # I don't actually know how to draw bitmaps in PyGame...
draw(sand2)
draw(sand3)
...
draw(sand99)


you would do something like this:


for i in range(1, 100):
draw(sands) # or whatever the real command is


Does this help?

As Dennis already pointed out I like to use dictionaries in these cases,
so I would use sand = dict() instead of sands = list()
and would do sand = pygame.image.load(name)

Then you can retrieve the content by doing sand[your_number].
 
S

Steven D'Aprano

As Dennis already pointed out I like to use dictionaries in these cases,
so I would use sand = dict() instead of sands = list() and would do
sand = pygame.image.load(name)

Then you can retrieve the content by doing sand[your_number].


If the keys are just the integers 0...n inclusive, then why bother with
the extra overhead of a dict when you get all the functionality you need
from a list?
 
M

Martin P. Hellwig

Steven said:
As Dennis already pointed out I like to use dictionaries in these cases,
so I would use sand = dict() instead of sands = list() and would do
sand = pygame.image.load(name)

Then you can retrieve the content by doing sand[your_number].


If the keys are just the integers 0...n inclusive, then why bother with
the extra overhead of a dict when you get all the functionality you need
from a list?

Just a matter of preference in my case no other good reason. Although I
do have a tendency to misuse dict all over the place, but on the other
hand it keeps my stuff readable for others :)
 
P

Paul Hankin

As Dennis already pointed out I like to use dictionaries in these cases,
so I would use sand = dict() instead of sands = list() and would do
sand = pygame.image.load(name)

Then you can retrieve the content by doing sand[your_number].

If the keys are just the integers 0...n inclusive, then why bother with
the extra overhead of a dict when you get all the functionality you need
from a list?


The keys aren't integers 0...n here, they're 1, 2, 3, 4, L, R, T, D,
TL, TR, BL, BR in the code, so a dict is preferable to a list.

Incidentally Katie: is 'D' a typo? It should be 'B' for consistency.

Also, functions can be passed arguments, and doing so is preferable to
passing information via global variables.

So
def LoadMaterial(loader):
...
sand = LoadMaterial('Sand')

Is a lot better than

def LoadMaterial():
... code using 'loader'
loader = 'Sand'
sand = LoadMaterial()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top