RootOfTheNull

Statix SC's Code Corrected 3

Apr 13th, 2015
262
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. pygame.init()
  3. ##########################################################################################
  4. BLACK=(0,0,0)
  5. WHITE=(220,220,220)
  6. BLUE=(0,0,155)
  7. RED=(155,0,0)
  8. YELLOW=(255,255,0)
  9. FPS=30
  10.  
  11. screenwidth=800
  12. screenheight=800
  13.  
  14. padding=50
  15. screen=pygame.display.set_mode( (screenwidth,screenheight) )
  16. clock=pygame.time.Clock()
  17.  
  18. ##########################################################################################
  19. class Ball(pygame.sprite.Sprite):
  20.     def __init__(self,x,y,width=20,height=20, color=WHITE):
  21.         super(Ball,self).__init__()
  22.         self.image=pygame.Surface((width,height))
  23.         self.image.fill(color)
  24.         self.rect=self.image.get_rect()
  25.         self.rect.x=x
  26.         self.rect.y=y
  27.         self.dx=1
  28.         self.dy=1
  29.  
  30.     def move(self):
  31.  
  32.         self.rect.x += self.dx
  33.         self.rect.y += self.dy
  34.  
  35.     def update(self, collide=pygame.sprite.Group()):
  36.  
  37.         self.move()
  38.  
  39.         if self.rect.right > screenwidth:
  40.             self.dx *= -1
  41.         elif self.rect.bottom > screenheight:
  42.             self.dy *= -1
  43.         if self.rect.left <= 0:
  44.             self.dx *= -1
  45.         elif self.rect.top < 0:
  46.             self.dy *= -1
  47.  
  48.         collision_list=pygame.sprite.spritecollide(self,collide,False)
  49.  
  50.         for collide in collision_list:
  51.  
  52.             # !!!!!!!!!!!!!!!!!
  53.             # So since all you really do when you collide with a paddle is just
  54.             # bounce off it, like reverse the horizontal direction it is going in,
  55.             # you don't really need to test anything, like which direction you are
  56.             # going in or how you are moving. All you need to do is reverse your dx.
  57.  
  58.             #self.dx *= -1
  59.  
  60.             # If you want more precise collisions, you can test for the direction you
  61.             # are moving in ( if your horizontal direction is positive or negative )
  62.             # and then match the edge you are on with the edge you are hitting it with.
  63.             # Like this...
  64.  
  65.             if self.dx > 0: # MOVING TO THE RIGHT, RIGHT SIDE IS WHAT HITS
  66.                 self.rect.right = collide.rect.left # Jump to the edge, just for that moment
  67.                
  68.                 self.dx *= -1 # Still bounce
  69.                 continue # Get out of this iteration so we don't test for any other edges
  70.  
  71.                
  72.             if self.dx < 0: # MOVING TO THE LEFT, LEFT SIDE IS WHAT HITS
  73.                 self.rect.left = collide.rect.right # Jump to the edge, just for that moment
  74.                
  75.                 self.dx *= -1 # Still bounce
  76.                 continue# Get out of this iteration so we don't test for any other edges
  77.  
  78.  
  79. class Paddle(pygame.sprite.Sprite):
  80.     def __init__(self,x,y,width=20,height=150, color=WHITE):
  81.         super(Paddle,self).__init__()
  82.         self.image=pygame.Surface((width,height))
  83.         self.image.fill(color)
  84.         self.rect=self.image.get_rect()
  85.         self.rect.x=x
  86.         self.rect.y=y
  87.         self.movespeed=2
  88.         self.pos=0
  89.  
  90.     def update(self):
  91.         pos=pygame.mouse.get_pos()
  92.         self.rect.y = pos[1]
  93.  
  94. ##########################################################################################
  95. #PRE LOOP STUFF
  96.  
  97. ball=Ball(screenwidth/2,screenheight/2-100)
  98. player=Paddle(0+padding, screenheight/2)
  99.  
  100. active_sprites=pygame.sprite.Group()
  101. active_sprites.add(ball, player)
  102.  
  103. collision_list=pygame.sprite.Group()
  104. collision_list.add(player)
  105.  
  106.  
  107. ##########################################################################################
  108. gameexit=False
  109. while not gameexit:
  110.  
  111.     screen.fill(BLACK)
  112.  
  113.     for event in pygame.event.get():
  114.         if event.type==pygame.QUIT:
  115.             gameexit=True
  116.             pygame.quit()
  117.     #update functions
  118.     ball.update(collision_list)
  119.     player.update()
  120.     #logic
  121.     #Draw
  122.     active_sprites.draw(screen)
  123.     #delay Frames
  124.     #update screen
  125.     pygame.display.update()
  126.  
  127. pygame.quit()
RAW Paste Data