473,376 Members | 1,043 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,376 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 1394
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.