views:

22

answers:

1

Hello all, I'm playing with pygame for the first time (and am kind of a newb about python in general), and wondering if anyone could help me with this...

I'm making a little shootem-up game and want to be able to create a class for bad guys. My thought was that the class should inherit from pygame.Surface, but that's giving me all kinds of problems (def, could be me screwing up basic inheritance/class stuff). For example, why doesn't this work(pygame, screen, etc all work fine and are used in other parts of the code, I'm just trying to move functions which I already have working into a class):

class Zombie(pygame.Surface):
  x_pos = y_pos = 0
  def __init__(self, x, y):
    #create zombie
    self = pygame.image.load('zombie_1.png')
    self = pygame.transform.scale(self,(50, 50))

    x_pos = x
    y_pos = y

zombie = Zombie(screen.get_width()/3, screen.get_height()/3)
screen.blit(zombie, (zombie.x_pos, zombie.y_pos))

The above produces an error: "pygame.error: display Surface quit" edit: apparently this is a result of calling a Surface after pygame.display.quit() has been called. Anyone with any experience in pygame want to take a swing at this?

Entire code:

#Initialize
import pygame, sys
pygame.init()

#classes
class Zombie(pygame.Surface):
  x_pos = y_pos = 0
  def __init__(self, x, y):
    #create zombie
    self = pygame.image.load('zombie_1.png')
    self = pygame.transform.scale(self,(50, 50))

    x_pos = x
    y_pos = y

  def is_hit(mouse_pos):
    #grab variables
    (mouseX, mouseY) = mouse_pos
    print "\nboxW_x, y = " + str(self.x) + ", " + str(self.y) + "\nmouseX, Y = " + str(mouseX) + ", " + str(mouseY) 
    headshot_x = self.x + (.5 * zombie.get_width())
    headshot_y = self.y + (.25 * zombie.get_height())
    margin_of_error_x = (zombie.get_width()/float(1000)) * zombie.get_width()
    margin_of_error_y = (zombie.get_height()/float(1000)) * zombie.get_height()
    print "Headshot_x: " + str(headshot_x) + ", " + str(headshot_y)
    print "diff in headshot and actual shot: " + str(mouseX - headshot_x) + ", " + str(mouseY - headshot_y)
    print "margin of error x = " + str(margin_of_error_x) + " y = " + str(margin_of_error_y)
    print "zombie size: " + str(zombie.get_size())

    valid_x = valid_y = False

    if abs(mouseX-headshot_x) < margin_of_error_x:
      valid_x = True
      print "valid x"
    if abs(mouseY-headshot_y) < margin_of_error_y:
      valid_y = True
      print "valid y"

    return (valid_x and valid_y)

#list of bad guys
zombie_list = []

#functions (which should later be moved into classes)
def is_hit():
  #grab variables
  (mouseX, mouseY) = pygame.mouse.get_pos()
  print "\nboxW_x, y = " + str(boxW_x) + ", " + str(boxW_y) + "\nmouseX, Y = " + str(mouseX) + ", " + str(mouseY) 
  headshot_x = boxW_x + (.5 * boxW.get_width())
  headshot_y = boxW_y + (.25 * boxW.get_height())
  margin_of_error_x = (boxW.get_width()/float(1000)) * boxW.get_width()
  margin_of_error_y = (boxW.get_height()/float(1000)) * boxW.get_height()
  print "Headshot_x: " + str(headshot_x) + ", " + str(headshot_y)
  print "diff in headshot and actual shot: " + str(mouseX - headshot_x) + ", " + str(mouseY - headshot_y)
  print "margin of error x = " + str(margin_of_error_x) + " y = " + str(margin_of_error_y)
  print "zombie size: " + str(boxW.get_size())

  valid_x = valid_y = False

  if abs(mouseX-headshot_x) < margin_of_error_x:
    valid_x = True
    print "valid x"
  if abs(mouseY-headshot_y) < margin_of_error_y:
    valid_y = True
    print "valid y"

  return (valid_x and valid_y)

pygame.mouse.set_visible(True)
pygame.mouse.set_cursor(*pygame.cursors.diamond)

#Display
screen = pygame.display.set_mode((640, 640))
pygame.display.set_caption("Zombie Massacre")

#Entities
#background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((0, 0, 0))

#make a zombie
boxW = pygame.image.load('zombie_1.png')
boxW = pygame.transform.scale(boxW,(50, 50))

#set up some box variables
boxW_x = screen.get_width()/3
boxW_y = screen.get_height()/3

#testing zombie class
zombie = Zombie(screen.get_width()/3, screen.get_height()/3)

#Action

#Assign
clock = pygame.time.Clock()
keepGoing = True

#Loop
count = 0;
rotation_vect = 1.01
while keepGoing:

  #setup rotation_vect for this pass
  if (count % 3) == 0:
    rotation_vect = 0 - rotation_vect

  #Time
  clock.tick(30)

  #Events
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      keepGoing = False
    elif event.type is pygame.MOUSEBUTTONDOWN:
      #loop through zombie list, using each one's "is_hit()" function
      keepGoing = not(is_hit())

#Refresh screen
  screen.blit(background, (0,0))
  boxW = pygame.transform.rotozoom(pygame.image.load('zombie_1.png'), rotation_vect, 1.01)
  boxW = pygame.transform.scale(boxW,(count+50, count+100))
#for zombie in zombies
  screen.blit(boxW,(boxW_x+(boxW_x * .1), boxW_y+(boxW_y * .1)))
# error is result of following line
  screen.blit(zombie, (zombie.x_pos, zombie.y_pos))
  pygame.display.flip()

#increment count
  count += 1
+2  A: 

You didn't call the inherited constructor:

class Zombie(pygame.Surface):
    def __init__(self, x, y):
        pygame.Surface.__init__(self, size=(w,h))

And you're assigning to self. That's not going to work.

adw
What should I assign it too?
danwoods