Pygame

Reply

Join Date: Sep 2006
Posts: 109
Reputation: chris99 is an unknown quantity at this point 
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Pygame

 
0
  #1
Feb 15th, 2007
I am trying to follow the tutorial on making the whack-a-monkey game, but it keeps giving me this error message:

Traceback (most recent call last):
File "C:/Documents and Settings/Christopher/My Documents/pygame/examples/examples/monkeystuff.py", line 128, in -toplevel-
chimp = Chimp()
File "C:/Documents and Settings/Christopher/My Documents/pygame/examples/examples/monkeystuff.py", line 66, in __init__
screen = pygame.display.get_suface()
AttributeError: 'module' object has no attribute 'get_suface'
Here's the code:

[PHP]
import pygame, sys,os
from pygame.locals import *
if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'
pygame.init()
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:", name
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()
def load_sound(name):
class NoneSound:
def play(self): pass
if not pygame.mixer:
return NoneSound()
fullname = os.path.join('data', name)
try:
sound = pygame.mixer.Sound(fullname)
except pygame.error, message:
print "Cannot load sound:", wav
raise SystemExit, message
return sound
class Fist(pygame.sprite.Sprite):
"""Moves a clenched fist on the screen, following the mouse"""
def __init__(self):
pygame.sprite.Sprite.__init__(self) #call sprite initializer
self.image, self.rect = load_image(fist.bmp, -1)
self.punching = 0
def update(self):
"Move the fist based on the mouse position"
pos = pygame.mouse.get_pos()
self.rect.midtop = pos
if self.punching:
self.rect.move_ip(5, 10)
def punch(self, target):
"Returns true if the fist collides with the target"
if not self.punching:
self.punching = 1
hitbox = self.rect.inflate(-5, -5)
return hitbox.colliderect(target.rect)
def unpunch(self):
"Called to pull the fist back"
self.punching = 0
class Chimp(pygame.sprite.Sprite):
"""Moves a monkey across the screen. It can spin the monkey when it is
punched"""
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('chimp.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):
"Walk or spin, depending on the monkey's state"
if self.dizzy:
self._spin()
else:
self._walk()
def _walk(self):
"Move the monkey across the screen, and turn at the ends"
newpos = self.rect.move((self.move, 0))
if self.rect.left < self.area.left or \
self.rect.right > self.area.right:
self.move = -self.move
newpos = self.rect.move((self.move, 0))
self.image = pygame.transform.flip(self.image, 1, 0)
self.rect = newpos
def _spin(self):
"Spin the monkey image"
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, self.dizzy)
self.image.get_rect()
self.rect.center = center
def punched(self):
"This will cause the monkey to start spinning"
if not self.dizzy:
self.dizzy = 1
self.original = self.image
pygame.init()
screen = pygame.display.set_mode((468, 60))
pygame.display.set_caption('Monkey Fever')
pygame.mouse.set_visible(0)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
if pygame.font:
font = pygame.font.Font(None, 36)
text = font.render("Pummel the Chimp, and win £££", 1, (10, 10, 10))
textpos = text.get_rect()
textpos.centerx = background.get_rect().centerx
background.blit(text, textpos)
screen.blit(background, (0,0))
pygame.display.flip()
whiff_sound = load_sound('whiff.wav')
punch_sound = load_sound('punch.wav')
chimp = Chimp()
fist = Fist()
allsprites = pygame.sprite.RenderPlain((fist, chimp))
clock = pygame.time.Clock()
while 1:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
sys.exit(0)
elif event.type == KEYDOWN and event.key == K_ESCAPE:
sys.exit(0)
elif event.type == MOUSEBUTTONDOWN:
if fist.punch(chimp):
punch_sound.play()
chimp.punched()
else:
whiff_sound.play()
elif event.type == MOUSEBUTTONUP:
fist.unpunch()
allsprites.update()
screen.blit(background, (0, 0))
allsprites.draw(screen)
pygame.display.flip()

[/PHP]

How do I fix it?
I am just like Mandark. I am calm, constantly annoyed and consider everyone insects. And my laugh...

HAhahahaHAhahahahahaHAhahahaaaHAhahaahaa</mandark clone>
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Pygame

 
0
  #2
Feb 15th, 2007
You have really butchered the original example code! I would suggest to reload the original code and it will work fine!
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 109
Reputation: chris99 is an unknown quantity at this point 
Solved Threads: 0
chris99's Avatar
chris99 chris99 is offline Offline
Junior Poster

Re: Pygame

 
0
  #3
Feb 15th, 2007
Well, I can't just copy and paste, as the tutorial doesn't have a section where the entire code is printed at once.
I am just like Mandark. I am calm, constantly annoyed and consider everyone insects. And my laugh...

HAhahahaHAhahahahahaHAhahahaaaHAhahaahaa</mandark clone>
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Pygame

 
0
  #4
Feb 16th, 2007
Here is the code from the pyame docs:
  1. #/usr/bin/env python
  2. """
  3. This simple example is used for the line-by-line tutorial
  4. that comes with pygame. It is based on a 'popular' web banner.
  5. Note there are comments here, but for the full explanation,
  6. follow along in the tutorial.
  7. If you downloaded the Pygame Docs this file is in for instance
  8. C:\Python24\Doc\Pygame-Docs\examples\chimp.py
  9. images are in
  10. C:\Python24\Doc\Pygame-Docs\examples\data\
  11. """
  12.  
  13.  
  14. #Import Modules
  15. import os, pygame
  16. from pygame.locals import *
  17.  
  18. if not pygame.font: print 'Warning, fonts disabled'
  19. if not pygame.mixer: print 'Warning, sound disabled'
  20.  
  21.  
  22. #functions to create our resources
  23. def load_image(name, colorkey=None):
  24. fullname = os.path.join('data', name)
  25. try:
  26. image = pygame.image.load(fullname)
  27. except pygame.error, message:
  28. print 'Cannot load image:', fullname
  29. raise SystemExit, message
  30. image = image.convert()
  31. if colorkey is not None:
  32. if colorkey is -1:
  33. colorkey = image.get_at((0,0))
  34. image.set_colorkey(colorkey, RLEACCEL)
  35. return image, image.get_rect()
  36.  
  37. def load_sound(name):
  38. class NoneSound:
  39. def play(self): pass
  40. if not pygame.mixer or not pygame.mixer.get_init():
  41. return NoneSound()
  42. fullname = os.path.join('data', name)
  43. try:
  44. sound = pygame.mixer.Sound(fullname)
  45. except pygame.error, message:
  46. print 'Cannot load sound:', fullname
  47. raise SystemExit, message
  48. return sound
  49.  
  50.  
  51. #classes for our game objects
  52. class Fist(pygame.sprite.Sprite):
  53. """moves a clenched fist on the screen, following the mouse"""
  54. def __init__(self):
  55. pygame.sprite.Sprite.__init__(self) #call Sprite initializer
  56. self.image, self.rect = load_image('fist.bmp', -1)
  57. self.punching = 0
  58.  
  59. def update(self):
  60. "move the fist based on the mouse position"
  61. pos = pygame.mouse.get_pos()
  62. self.rect.midtop = pos
  63. if self.punching:
  64. self.rect.move_ip(5, 10)
  65.  
  66. def punch(self, target):
  67. "returns true if the fist collides with the target"
  68. if not self.punching:
  69. self.punching = 1
  70. hitbox = self.rect.inflate(-5, -5)
  71. return hitbox.colliderect(target.rect)
  72.  
  73. def unpunch(self):
  74. "called to pull the fist back"
  75. self.punching = 0
  76.  
  77.  
  78. class Chimp(pygame.sprite.Sprite):
  79. """moves a monkey critter across the screen. it can spin the
  80. monkey when it is punched."""
  81. def __init__(self):
  82. pygame.sprite.Sprite.__init__(self) #call Sprite intializer
  83. self.image, self.rect = load_image('chimp.bmp', -1)
  84. screen = pygame.display.get_surface()
  85. self.area = screen.get_rect()
  86. self.rect.topleft = 10, 10
  87. self.move = 9
  88. self.dizzy = 0
  89.  
  90. def update(self):
  91. "walk or spin, depending on the monkeys state"
  92. if self.dizzy:
  93. self._spin()
  94. else:
  95. self._walk()
  96.  
  97. def _walk(self):
  98. "move the monkey across the screen, and turn at the ends"
  99. newpos = self.rect.move((self.move, 0))
  100. if self.rect.left < self.area.left or \
  101. self.rect.right > self.area.right:
  102. self.move = -self.move
  103. newpos = self.rect.move((self.move, 0))
  104. self.image = pygame.transform.flip(self.image, 1, 0)
  105. self.rect = newpos
  106.  
  107. def _spin(self):
  108. "spin the monkey image"
  109. center = self.rect.center
  110. self.dizzy = self.dizzy + 12
  111. if self.dizzy >= 360:
  112. self.dizzy = 0
  113. self.image = self.original
  114. else:
  115. rotate = pygame.transform.rotate
  116. self.image = rotate(self.original, self.dizzy)
  117. self.rect = self.image.get_rect()
  118. self.rect.center = center
  119.  
  120. def punched(self):
  121. "this will cause the monkey to start spinning"
  122. if not self.dizzy:
  123. self.dizzy = 1
  124. self.original = self.image
  125.  
  126.  
  127. def main():
  128. """this function is called when the program starts.
  129. it initializes everything it needs, then runs in
  130. a loop until the function returns."""
  131. #Initialize Everything
  132. pygame.init()
  133. screen = pygame.display.set_mode((468, 60))
  134. pygame.display.set_caption('Monkey Fever')
  135. pygame.mouse.set_visible(0)
  136.  
  137. #Create The Backgound
  138. background = pygame.Surface(screen.get_size())
  139. background = background.convert()
  140. background.fill((250, 250, 250))
  141.  
  142. #Put Text On The Background, Centered
  143. if pygame.font:
  144. font = pygame.font.Font(None, 36)
  145. text = font.render("Pummel The Chimp, And Win $$$", 1, (10, 10, 10))
  146. textpos = text.get_rect()
  147. textpos.centerx = background.get_rect().centerx
  148. background.blit(text, textpos)
  149.  
  150. #Display The Background
  151. screen.blit(background, (0, 0))
  152. pygame.display.flip()
  153.  
  154. #Prepare Game Objects
  155. clock = pygame.time.Clock()
  156. whiff_sound = load_sound('whiff.wav')
  157. punch_sound = load_sound('punch.wav')
  158. chimp = Chimp()
  159. fist = Fist()
  160. allsprites = pygame.sprite.RenderPlain((fist, chimp))
  161.  
  162. #Main Loop
  163. while 1:
  164. clock.tick(60)
  165.  
  166. #Handle Input Events
  167. for event in pygame.event.get():
  168. if event.type == QUIT:
  169. return
  170. elif event.type == KEYDOWN and event.key == K_ESCAPE:
  171. return
  172. elif event.type == MOUSEBUTTONDOWN:
  173. if fist.punch(chimp):
  174. punch_sound.play() #punch
  175. chimp.punched()
  176. else:
  177. whiff_sound.play() #miss
  178. elif event.type == MOUSEBUTTONUP:
  179. fist.unpunch()
  180.  
  181. allsprites.update()
  182.  
  183. #Draw Everything
  184. screen.blit(background, (0, 0))
  185. allsprites.draw(screen)
  186. pygame.display.flip()
  187.  
  188. #Game Over
  189.  
  190.  
  191. #this calls the 'main' function when this script is executed
  192. if __name__ == '__main__': main()
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC