472,958 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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 1376
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kurt Watson | last post by:
Iím having a different kind of problem with Hotmail when I sign in it says, "Web Browser Software Limitations Your Current Software Will Limit Your Ability to Use Hotmail You are using a web...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
23
by: Jason | last post by:
Hi, I was wondering if any could point me to an example or give me ideas on how to dynamically create a form based on a database table? So, I would have a table designed to tell my application...
13
by: Joner | last post by:
Hello, I'm having trouble with a little programme of mine where I connect to an access database. It seems to connect fine, and disconnect fine, but then after it won't reconnect, I get the error...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
1
PEB
by: PEB | last post by:
POSTING GUIDELINES Please follow these guidelines when posting questions Post your question in a relevant forum Do NOT PM questions to individual experts - This is not fair on them and...
0
by: 2Barter.net | last post by:
newsmail@reuters.uk.ed10.net Fwd: Money for New Orleans, AL & GA Inbox Reply Reply to all Forward Print Add 2Barter.net to Contacts list Delete this message Report phishing Show original
6
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID,...
5
by: tabani | last post by:
I wrote the program and its not giving me correct answer can any one help me with that please and specify my mistake please it will be highly appreciable... The error arrives from option 'a' it asks...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.