Advertisement
furas

Untitled

Jan 28th, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.41 KB | None | 0 0
  1. import pygame
  2.  
  3. BLACK = (0, 0, 0)
  4. WHITE = (255, 255, 255)
  5. GREEN = (0, 255, 0)
  6. RED = (255, 0, 0)
  7.  
  8. #----------------------------------------------
  9.  
  10. class Shield(pygame.sprite.Sprite):
  11.  
  12.     def __init__(self,color,width,height,location):
  13.         super().__init__()
  14.  
  15.         self.image = pygame.Surface([width,height])
  16.         self.image.fill(color)
  17.         self.rect = self.image.get_rect()
  18.         self.rect.topleft = location
  19.  
  20. #ball class        
  21. class Circle(pygame.sprite.Sprite):
  22.  
  23.     def __init__(self,location):
  24.         super().__init__()
  25.         self.image=pygame.Surface((45,45))
  26.         self.image.fill((0,0,0))
  27.         pygame.draw.circle(self.image,(255,0,0),(25,25),20,0)
  28.         self.rect=self.image.get_rect()
  29.         self.image.set_colorkey(BLACK)
  30.         self.rect.topleft = location
  31.  
  32.     def update(self):
  33.         self.rect.x +=1
  34.  
  35. class Pad(pygame.sprite.Sprite):
  36.  
  37.     def __init__(self,color,location,widht,height):
  38.         super().__init__()
  39.         self.image = pygame.Surface((widht,height))
  40.         self.image.fill(color)
  41.         self.rect = self.image.get_rect()
  42.         self.rect.topleft = location
  43.  
  44.     def update(self):
  45.         pass
  46.         #self.update()
  47.  
  48. #----------------------------------------------
  49.  
  50. size = (700, 500)
  51.  
  52. # --- init ---        
  53.  
  54. pygame.init()
  55.  
  56. screen = pygame.display.set_mode(size)
  57.  
  58. pygame.display.set_caption("PONG")
  59.  
  60. # --- other ---
  61.  
  62. collision_ball = False
  63.  
  64. color_shield = BLACK
  65.  
  66. l_shield = 15
  67. h_pads = 50
  68. l_pads = 20
  69.  
  70. color_pads = BLACK
  71.        
  72. pad_left = Pad(color_pads,(30,30),l_pads,h_pads)
  73. pad_right = Pad(color_pads,(650,30),l_pads,h_pads)
  74. pad_group = pygame.sprite.Group()
  75. pad_group.add(pad_left,pad_right)
  76.  
  77. ball = Circle((350,220))      
  78. #the edge of the screen
  79. balls = pygame.sprite.Group()
  80. balls.add(ball)
  81.  
  82. shield_up = Shield(color_shield,700,l_shield,(0,0))
  83. shield_down = Shield(color_shield,700,l_shield,(0,485))
  84. shield_right = Shield(color_shield,l_shield,500,(0,0))
  85. shield_left = Shield(color_shield,l_shield,500,(685,0))
  86. all_sprites = pygame.sprite.Group()
  87.  
  88. shield = pygame.sprite.Group()
  89. shield.add(shield_up,shield_down,shield_right,shield_left)
  90.  
  91. all_sprites.add(shield,ball,pad_group)
  92.  
  93. # --- mainloop ---
  94.  
  95. done = False
  96.  
  97. pygame.key.set_repeat(500,30)
  98. clock = pygame.time.Clock()
  99.  
  100. while not done:
  101.  
  102.     for event in pygame.event.get():
  103.         if event.type == pygame.QUIT:
  104.             done = True
  105.  
  106.         if event.type == pygame.KEYDOWN:
  107.             # --- pad movement ---
  108.             if event.key == pygame.K_DOWN:
  109.                 if (pad_right.rect.y<440):
  110.                     pad_right.rect.y +=10
  111.             if event.key == pygame.K_UP:
  112.                 if(pad_right.rect.y>10):
  113.                     pad_right.rect.y -=10
  114.             if event.key == pygame.K_s:
  115.                 if (pad_left.rect.y<440):
  116.                     pad_left.rect.y +=10
  117.             if event.key == pygame.K_w:
  118.                 if (pad_left.rect.y>10):
  119.                     pad_left.rect.y -=10
  120.  
  121.     # --- updates ---
  122.    
  123.     # smaller indentations and `=` instead of `==`
  124.     collision_ball = ball.rect.colliderect(pad_right.rect)
  125.  
  126.     # smaller indentations
  127.     if collision_ball == True:
  128.         print("ball hit the pad")
  129.    
  130.     ball.update()
  131.  
  132.     # --- draws ---
  133.    
  134.     screen.fill(WHITE)
  135.    
  136.     all_sprites.draw(screen)
  137.  
  138.     pygame.display.flip()
  139.  
  140.     clock.tick(60)
  141.  
  142. # --- the end ---
  143. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement