468,771 Members | 1,922 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

OMG please help

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/newsea...egory=shopping

Dec 22 '07 #1
5 1209
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.

On Sat, 22 Dec 2007 15:16:53 -0800, katie smith wrote:
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[i]) # or whatever the real command is
Does this help?

--
Steven
Dec 23 '07 #2
Steven D'Aprano wrote:
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.

On Sat, 22 Dec 2007 15:16:53 -0800, katie smith wrote:
>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[i]) # 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[i] = pygame.image.load(name)

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

--
mph
Dec 24 '07 #3
On Mon, 24 Dec 2007 17:14:58 +0100, Martin P. Hellwig wrote:
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[i] = 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?

--
Steven
Dec 26 '07 #4
Steven D'Aprano wrote:
On Mon, 24 Dec 2007 17:14:58 +0100, Martin P. Hellwig wrote:
>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[i] = 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 :-)

--
mph
Dec 26 '07 #5
On Dec 26, 1:09*am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
On Mon, 24 Dec 2007 17:14:58 +0100, Martin P. Hellwig wrote:
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[i] = 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()
Dec 26 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by tyler_durden | last post: by
23 posts views Thread by Jason | last post: by
5 posts views Thread by tabani | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.