Advertisement
diliupg

A standard object creator class for pygame

May 4th, 2015
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. """ This code can be used as a starting pointto create an object using pygame. Related to the code an object falls onto another object moving in a circular path and stays without falling off"""
  2. import pygame
  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 update(self):
  24.         """"""
  25.         self.image = self.imagelist[self.frame]
  26.         if self.timer >= self.timerlimit:
  27.             self.frame += 1
  28.             if self.frame >= len(self.imagelist):
  29.                 self.frame = 0
  30.             self.timer = 0
  31.         self.timer += 1
  32.  
  33.     #----------------------------------------------------------------------
  34.     def collide(self, colwith):
  35.         """"""
  36.         hit_list = pygame.sprite.spritecollide(self, colwith, False, collided = pygame.sprite.collide_rect)
  37.  
  38.         for colob in hit_list:
  39.  
  40.             if self.rect.bottom > colob.rect.top:
  41.                 self.rect.bottom = colob.rect.top
  42.        
  43.                 self.speedx = colob.speedx
  44.                
  45.                 self.speedy += colob.speedy
  46.                
  47.                 """when there was no change to self.speedy with regard to the platform the box fell off the left edge.
  48.                
  49.                when self.speedy = colob.speedy
  50.                
  51.                the box moved to the right and fell of the right edge after 36 rotations.
  52.                
  53.                But when self.speedy += colob.speedy
  54.                
  55.                and the box speed was restricted by the code below the box stayed on the platform even after 570 rotations and there was no visible displacement."""
  56.                
  57.                 if self.speedy >= 4 or self.speedy <= 2:
  58.                     self.speedy = 4
  59.  
  60.     def move(self):  # wallsprites, Herosprite, looptime
  61.         self.rect.x += self.speedx
  62.         self.rect.y += self.speedy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement