[Tutor] Help with a game (fwd)
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Tue Nov 11 14:38:21 EST 2003
Forwarding to tutor at python.org.
Ok, good, the error message gives us something to look for. The error
message is saying that the system can't find the 'main()' function: where
is it?
Please continue to reply to the address 'tutor at python.org'; I might be a
little busy today, but the others on the Python-Tutor list can help you.
---------- Forwarded message ----------
Date: Tue, 11 Nov 2003 11:13:46 -0800 (PST)
From: j j <jeffq16 at yahoo.com>
To: Danny Yoo <dyoo at hkn.eecs.berkeley.edu>
Subject: Re: [Tutor] Help with a game
I'm really sorry about the mistake of not including such information.
An exception is raised when i attempt to run my program, this is what the interactive window says...(it also
Traceback (most recent call last):
File "C:\PYTHON23\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "A:\game folder\game.py", line 129, in ?
main()
NameError: name 'main' is not defined
Again, i've included my code and i'll add in the bitmap files i've obtained, any help is apprieciated and i thank you so much for helping me out. Here is the code and agian, Thank you.
import os, pygame
from pygame.locals import *
if not pygame.font:print 'Warning, fonts disabled'
def load_image(name, colorkey=None):
fullname = os.path.join('data', name)
try:
image = pygame.image.load(fullname)
except pygame.error, message:
print 'Cannot load image:', fullname
raise SystemExit, message
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()
class Gun(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('gun.bmp', -1)
self.punching = 0
def update(self):
pos = pygame.mouse.get_pos()
self.rect.midtop = pos
if self.punching:
self.rect.move_ip(5, 10)
def shoot(self, target):
if not self.punching:
self.punching = 1
hitbox = self.rect.inflate(-5,-5)
return hitbox.colliderect(target.rect)
def unshoot(self):
self.punching = 0
class ship(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('new.bmp',-1)
screen = pygame.display.get_suface()
self.area = screen.get_rect()
self.rect.topleft = 10, 10
self.move = 9
self.dizzy = 0
def update(self):
if self.dizzy:
self._spin()
else:
self._walk()
def _walk(self):
change = self.rect.move((self.move, 0))
if self.rect.left< self.area.left or \
self.rect.right > self.area.right:
self.move = -self.move
change = self.rect.move((self.move, 0))
self.image = pygame.transform.flip(self.image, 1, 0)
self.rect = change
def _change(self):
center = self.rect.center
self.dizzy = self.dizzy + 12
if self.dizzy >= 360:
self.dizzy = 0
self.image = self.original
else:
rotate = pygame.transform.rotate
self.image = rotate(self.original, slef.dizzy)
self.rect = self.image.get_rect()
self.rect.center = center
def done(self):
if not self.dizzy:
self.dizzy = 1
self.original = self.image
def main():
pygame.init()
screen = pygame.display.set_mode((480, 60))
pygame.display.set_caption('Battleship')
pygame.mouse.set_visible(0)
backround = pygame.Surface(screen.get_size())
backround = backround.convert()
backround.fill((250, 250, 250))
screen.blit(backround, (0, 0))
pygame.display.flip()
clock = pygame.time.Clock()
new = New()
gun = Gun()
allsprites = pygame.sprite.RenderPlain((gun, new))
while 1:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN and event.key == K_ESCAPE:
return
elif event.type == MOUSEBUTTONDOWN:
if fist.punch(chimp):
chimp.punched()
elif event.type == MOUSEBUTTONUP:
fist.unpunch()
allsprites.update()
screen.blit(backround, (0, 0))
allsprites.draw(screen)
pygame.display.flip()
if __name__ == '__main__':
main()
>>>
Danny Yoo <dyoo at hkn.eecs.berkeley.edu> wrote:
On Mon, 10 Nov 2003, j j wrote:
> Hey guys, i'm in a programmng class and i need some help. I made a game
> based off of the pygame example "Chimp" and it won't run.
Hello!
Can you be more specific by what you mean by "won't run"? This question
may sound silly, but we're actually a little serious here: does Python
give out an exception or traceback? Do you see anything at all on the
screen when you run the program? And have you been able to get other
pygame examples to work yet?
Many of us have already seen the Chimp example from pygame's web page:
http://pygame.org/docs/tut/chimp/ChimpLineByLine.html
so I'm pretty sure we can get this straightened out quickly. But we
really do need more information about how the program is failing;
otherwise, we can't effectively debug the situation.
Good luck to you!
---------------------------------
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
More information about the Tutor
mailing list