Advertisement
diliupg

standard_object_factory a class to create objects for python

May 3rd, 2015
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. import pygame
  2. from math import cos, sin, pi
  3.  
  4. class object_factory(pygame.sprite.Sprite):
  5.     #----------------------------------------------------------------------
  6.     def __init__(self, imagelist, xpos = 0, ypos = 0, speedx = 0, speedy = 0, value = 0):
  7.         """Constructor"""
  8.         pygame.sprite.Sprite.__init__(self)
  9.         self.name = ""
  10.         self.frame = 0
  11.         self.imagelist = imagelist
  12.         self.image = imagelist[self.frame]
  13.         self.mask = pygame.mask.from_surface(self.image) # pixelmask
  14.         self.rect = self.image.get_rect()
  15.         self.rect.x = xpos
  16.         self.rect.y = ypos
  17.         self.speedx = speedx
  18.         self.speedy = speedy
  19.         self.timer = 0
  20.         self.timerlimit = 10
  21.  
  22.         #----------------------------------------------------------------------
  23.     def move(self):  # wallsprites, Herosprite, looptime
  24.         self.rect.x += self.speedx
  25.         self.rect.y += self.speedy
  26.    
  27.     #----------------------------------------------------------------------
  28.     def update(self):
  29.         """"""
  30.         self.image = self.imagelist[self.frame]
  31.         if self.timer >= self.timerlimit:
  32.             self.frame += 1
  33.             if self.frame >= len(self.imagelist):
  34.                 self.frame = 0
  35.             self.timer = 0
  36.         self.timer += 1
  37.  
  38.     #----------------------------------------------------------------------
  39.     def collide(self, colwith):
  40.         """"""
  41.         hit_list = pygame.sprite.spritecollide(self, colwith, False, collided = pygame.sprite.collide_rect)
  42.  
  43.         for colob in hit_list:
  44.  
  45.             if self.rect.bottom > colob.rect.top:
  46.                 self.rect.bottom = colob.rect.top
  47.        
  48.                 print self.rect.x, self.rect.y, colob.speedx, colob.speedy
  49.                 self.speedx = colob.speedx
  50.                
  51.                 #self.rect.y = colob.rect.y - self.rect.height
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement