Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 5th, 2013  |  syntax: Python  |  size: 3.95 KB  |  views: 217  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #
  2. import pygame
  3. from pygame.locals import *
  4. from random import randint
  5. import math
  6.  
  7. class Paddle(pygame.sprite.Sprite):
  8.     WIDTH, HEIGHT = 15, 300
  9.     SPEED = 20.
  10.     def __init__(self, pos):
  11.         pygame.sprite.Sprite.__init__(self)
  12.         self.rect = Rect(0,0, Paddle.WIDTH, Paddle.HEIGHT)
  13.         self.rect.center = pos
  14.         self.image = pygame.Surface(self.rect.size)
  15.         self.image.fill(Color("gray80"))
  16.     def handle_keystate(self, keys):
  17.         if keys[pygame.K_w]:
  18.             self.rect.top -= self.SPEED
  19.         elif keys[pygame.K_s]:
  20.             self.rect.top += self.SPEED
  21.  
  22. class Ball(pygame.sprite.Sprite):
  23.     SPEED = 10.
  24.     def __init__(self, game):
  25.         pygame.sprite.Sprite.__init__(self)
  26.         self.game = game
  27.         self.rect = Rect(0,0,20,20)
  28.         self.image = pygame.Surface(self.rect.size)
  29.         self.image.fill(Color("gray20"))
  30.         self.velx, self.vely = self.SPEED, self.SPEED
  31.     def draw(self):
  32.         self.game.screen.blit(self.image, self.rect)
  33.     def reset(self):
  34.         # to center
  35.         self.rect.center = self.game.screen.get_rect().center
  36.        
  37.         # random direction, with same initial speed
  38.         angle = math.radians(randint(0,360))
  39.         self.velx = math.cos(angle) * self.SPEED
  40.         self.vely = math.sin(angle) * self.SPEED        
  41.  
  42.     def update(self):    
  43.         # move
  44.         self.rect.x += self.velx
  45.         self.rect.y += self.vely
  46.         # bounce walls
  47.         if self.rect.top <= 0:            
  48.             self.vely *= -1
  49.         if self.rect.bottom >= self.game.screen.get_rect().bottom:
  50.             self.vely *= -1
  51.  
  52.         # offscreen
  53.         if not self.rect.colliderect(self.game.screen.get_rect()):
  54.             if self.rect.left <= 0:
  55.                 print "ai scores"
  56.             else:
  57.                 print "human scores"
  58.             self.reset()        
  59.  
  60.     def handle_collision(self, other):
  61.         self.velx *= -1
  62.         self.vely *= -1
  63.  
  64.  
  65.  
  66. class GameMain():
  67.     """game Main. entry point. handles intialization of game and graphics, as well as game loop."""    
  68.     done = False
  69.     color_bg = Color('gray30') # or also: Color(50,50,50) , or: Color('#fefefe')
  70.    
  71.     def __init__(self, width=800, height=600):
  72.         """
  73.        """
  74.         pygame.init()
  75.  
  76.         self.width, self.height = width, height
  77.         self.screen = pygame.display.set_mode(( self.width, self.height ))
  78.         self.clock = pygame.time.Clock()      
  79.  
  80.         self.player = Paddle(( 50 ,300))
  81.         self.ai = Paddle(( self.screen.get_rect().right - 50,300))
  82.         self.paddles = pygame.sprite.Group()
  83.         self.ball = Ball(self)
  84.         self.paddles.add( self.player, self.ai)
  85.    
  86.     def main_loop(self):
  87.         """Game() main loop."""
  88.         while not self.done:
  89.             self.handle_events()        
  90.             # move ball
  91.             self.ball.update()
  92.  
  93.             for p in self.paddles:
  94.                 if self.ball.rect.colliderect(p.rect):
  95.                     self.ball.handle_collision(p)
  96.  
  97.             self.draw()
  98.             self.clock.tick(60)
  99.                
  100.     def draw(self):
  101.         """draw screen"""
  102.  
  103.         self.screen.fill( self.color_bg )
  104.  
  105.         self.paddles.draw(self.screen)
  106.         self.ball.draw()
  107.  
  108.         pygame.display.flip()
  109.        
  110.     def handle_events(self):
  111.         """handle events: keyboard, mouse, etc."""
  112.         events = pygame.event.get()
  113.  
  114.         # keystates
  115.         keys = pygame.key.get_pressed()
  116.         self.player.handle_keystate(keys)
  117.        
  118.         # events
  119.         for event in events:
  120.             if event.type == pygame.QUIT: self.done = True
  121.             # event: keydown
  122.             elif event.type == KEYDOWN:
  123.                 if event.key == K_ESCAPE: self.done = True                
  124.                 elif event.key == K_SPACE:
  125.                     self.ball.reset()
  126.  
  127. if __name__ == "__main__":        
  128.     game = GameMain()
  129.     game.main_loop()
clone this paste RAW Paste Data