RootOfTheNull

Statix SC's Corrected Code 2

Apr 11th, 2015
288
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2.  
  3. '''
  4. !!!!!!!!!!!!!!!!!
  5. So here is a major issue here.
  6.  
  7. All of your _init_ constructor declarations have only ONE underscore _
  8. on the edges rather than two. So all of your constructors are not being run.
  9.  
  10.  
  11. I have fixed all the errors I've seen and left some notes. Hopefully the code
  12. runs perfectly fine for you now.
  13. '''
  14.  
  15.  
  16.  
  17. ###################################################################
  18. pygame.init()
  19. ###################################################################
  20. WHITE=(255,255,255)
  21. RED=(255,0,0)
  22. BLUE=(0,0,255)
  23.  
  24. screenWidth=800
  25. screenHeight=600
  26. BlockSize=50
  27. FPS=30
  28. # !!!!!!!!!!!!!!!!!!
  29. # This FPS is low, I would have expected 60. Is this what you would like?
  30.  
  31. screen=pygame.display.set_mode((screenWidth,screenHeight))
  32. Clock=pygame.time.Clock()
  33.  
  34. class Player(pygame.sprite.Sprite):
  35.     def __init__(self, color=RED, width=64, height=64):
  36.         super(Player,self).__init__()
  37.         self.image=pygame.Surface((width,height))
  38.         self.image.fill(color)
  39.         self.rect=self.image.get_rect()
  40.        
  41.         self.hs=0
  42.         self.vs=0
  43.         self.speed=20
  44.  
  45.         self.level=0
  46.  
  47.     def applyGravity(self, gravity=.9):
  48.         if self.vs==0: self.vs=1
  49.         else: self.vs+=gravity
  50.  
  51.     def set_position(self,x,y):
  52.         self.rect.x=x
  53.         self.rect.y=y
  54.  
  55.     def set_image(self,filename=None):
  56.         if (filename!=None):
  57.             self.image=pygame.image.load(filename)
  58.             self.rect=self.image.get_rect()
  59.  
  60.     def change_speed(self, fhs, fvs):
  61.         self.hs+= fhs
  62.         self.vs+= fvs
  63.  
  64.     def update(self, collidable=pygame.sprite.Group(), event=None):
  65.  
  66.         self.applyGravity()
  67.  
  68.         self.rect.x += self.hs
  69.         collisionList=pygame.sprite.spritecollide(self,collidable,False)
  70.  
  71.         for collided_object in collisionList:
  72.             if self.hs > 0:
  73.                self.rect.right=collided_object.rect.left
  74.             elif self.hs<0:
  75.                 self.rect.left=collided_object.rect.right
  76.  
  77.         self.rect.y += self.vs
  78.         collisionList=pygame.sprite.spritecollide(self,collidable,False)
  79.  
  80.         for collided_object in collisionList:
  81.             if self.vs > 0:
  82.                self.rect.bottom=collided_object.rect.top
  83.                self.vs=0
  84.             elif self.vs<0:
  85.                 self.rect.top=collided_object.rect.bottom
  86.                 self.vs=0
  87.  
  88.         if not (event==None):
  89.             if event.type==pygame.KEYDOWN:
  90.                 print self.speed
  91.                 if event.key==pygame.K_RIGHT:
  92.                     print("key pressed")
  93.                    
  94.                     self.hs=self.speed
  95.                 if event.key==pygame.K_LEFT:
  96.                     self.hs= -self.speed
  97.                 if event.key==pygame.K_UP:
  98.                     if self.vs==0:
  99.                         self.vs=-self.speed
  100.                 if event.key==pygame.K_DOWN:
  101.                     pass
  102.                     #self.change_speed(0,SPEED)
  103.             if event.type==pygame.KEYUP:
  104.                 if event.key==pygame.K_RIGHT:
  105.                     if self.hs!=0: self.hs=0
  106.                 if event.key==pygame.K_LEFT:
  107.                     if self.hs!=0: self.hs=0
  108.                 if event.key==pygame.K_UP:
  109.                     pass
  110.                     #if self.vs!=0: self.vs=0
  111.                 if event.key==pygame.K_DOWN:
  112.                     pass
  113.                     #if self.vs!=0: self.vs=0
  114.         #copy above for vert
  115. class cBlock(pygame.sprite.Sprite):
  116.     def __init__ (self, x, y, width, height, color=RED):
  117.         super(cBlock,self).__init__()
  118.         self.image=pygame.Surface((width,height))
  119.         self.image.fill(color)
  120.         self.rect=self.image.get_rect()
  121.         self.rect.x=x
  122.         self.rect.y=y
  123.  
  124. '''
  125. !!!!!!!!!!!!!
  126. I have changed the object class names to be capitalized, so to avoid
  127. variable name confusion when you actually use a 'level' list in that
  128. object. I recommend always using a capitalized class name.
  129. '''
  130.  
  131.  
  132. class Level(object):
  133.     def __init__(self, player_object):
  134.         self.object_list=pygame.sprite.Group()
  135.         self.player_object=player_object
  136.     def update(self):
  137.         self.object_list.update()
  138.  
  139.     def draw(self, screen):
  140.         screen.fill(BLUE)
  141.         self.object_list.draw(screen)
  142.  
  143. class Level_01(Level):
  144.     def __init__(self, player_object):
  145.         super(Level_01, self).__init__(player_object)
  146.         level = [
  147.             #[x,y,width,height,color]
  148.             [100,500,400,10,WHITE],
  149.         ]
  150.         for block in level:
  151.             block=cBlock(block[0],block[1],block[2],block[3],block[4])
  152.             self.object_list.add(block)
  153.  
  154. ###################################################################
  155. ###################################################################
  156.  
  157. '''
  158. !!!!!!!!!!!!!!!!!!!!!!!!!
  159. Remember to take all of these initialization functions
  160. OUTSIDE of the while loop! That way everything is not overwrote every
  161. single frame -- that's a big unnecessary thing and even takes away from
  162. everything we try to do -- it would just revert it back to the original
  163. state.
  164.  
  165. This was the exact same problem you had in the code that you sent me,
  166. and it's the same problem here. I've fixed it, but it seems to be a
  167. reoccuring issue.
  168.  
  169. I just recommend taking all of the initialization things and the functions
  170. that should only be called ONCE should come BEFORE and OUTSIDE the while loop.
  171.  
  172. '''
  173.  
  174. active_object_list=pygame.sprite.Group()
  175. player=Player()
  176. player.set_position(100,100)
  177. active_object_list.add(player)
  178.  
  179. level_list=[]
  180. level_list.append(Level_01(player))
  181.  
  182. current_level_number=0
  183. current_level=level_list[current_level_number]
  184.  
  185. player.level=current_level
  186.  
  187.  
  188. GameExit=False
  189. while not GameExit:
  190.  
  191.     #screen.fill(WHITE)
  192.     # You don't particularly need this line, since you make the background screen blue inside your level object.
  193.  
  194.     for event in pygame.event.get():
  195.         if event.type == pygame.QUIT:
  196.             GameExit=True
  197.             pygame.quit()
  198.  
  199.     #update functions
  200.     player.update(current_level.object_list, event)
  201.     event=None
  202.     current_level.update()
  203.     #logic
  204.  
  205.     #Draw
  206.     current_level.draw(screen)
  207.     active_object_list.draw(screen)
  208.  
  209.     #delay Frames
  210.     Clock.tick(FPS)
  211.  
  212.     #update screen
  213.     pygame.display.update()
  214.  
  215. ##################################################################
  216. pygame.quit()
  217. ##################################################################
RAW Paste Data