Guest User

pong.py

a guest
Aug 22nd, 2010
275
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Módulos
  5. import sys, pygame
  6. from pygame.locals import *
  7.  
  8. # Constantes
  9.  
  10. WIDTH = 640
  11. HEIGHT = 480
  12.  
  13. # Funciones
  14. # ------------------------------------------------------------------------
  15.  
  16. def load_image(filename, transparent=False):
  17.     try: image = pygame.image.load(filename)
  18.     except pygame.error, message:
  19.         raise SystemExit, message
  20.     image = image.convert()
  21.     if transparent:
  22.         color = image.get_at((0,0))
  23.         image.set_colorkey(color, RLEACCEL)
  24.     return image
  25.  
  26. def texto(texto, posx, posy, color=(255,255,255)):
  27.     fuente = pygame.font.Font("images/DroidSans.ttf", 25)
  28.     salida = pygame.font.Font.render(fuente, texto, 1, color)
  29.     salida_rect = salida.get_rect()
  30.     salida_rect.centerx = posx
  31.     salida_rect.centery = posy
  32.     return salida, salida_rect
  33.  
  34. # ------------------------------------------------------------------------
  35.  
  36. # Clases
  37. # ------------------------------------------------------------------------
  38.  
  39. class Bola(pygame.sprite.Sprite):
  40.     def __init__(self):
  41.         pygame.sprite.Sprite.__init__(self)
  42.         self.image = load_image("images/ball.png", True)
  43.         self.rect = self.image.get_rect()
  44.         self.rect.centerx = WIDTH / 2
  45.         self.rect.centery = HEIGHT / 2
  46.         self.speed = [0.2, -0.2]
  47.     def actualizar(self, time, colisiones, puntos, jugador):
  48.         self.rect.centerx += self.speed[0] * time
  49.         self.rect.centery += self.speed[1] * time
  50.  
  51.         #chocar con la paleta
  52.         for elemento in colisiones:
  53.             if pygame.sprite.collide_rect(self, elemento):
  54.                 self.speed[0] = -self.speed[0]
  55.                 self.rect.centerx += self.speed[0] * time
  56.                 jugador = colisiones.index(elemento)
  57.  
  58.         #Sumar Punto
  59.         if self.rect.left <= 0 and jugador == 1:
  60.             puntos[1] += 1
  61.         if self.rect.right >= WIDTH and jugador == 0:
  62.             puntos[0] += 1
  63.  
  64.         #chocar con la pared
  65.         if self.rect.left <= 0 or self.rect.right >= WIDTH:
  66.             self.speed[0] = -self.speed[0]
  67.             self.rect.centerx += self.speed[0] * time
  68.         if self.rect.top <= 0 or self.rect.bottom >= HEIGHT:
  69.             self.speed[1] = -self.speed[1]
  70.             self.rect.centery += self.speed[1] * time
  71.  
  72.  
  73.         return puntos, jugador
  74.  
  75.        
  76.  
  77. class Pala(pygame.sprite.Sprite):
  78.     def __init__(self, x):
  79.         pygame.sprite.Sprite.__init__(self)
  80.         self.image = load_image("images/pala.png")
  81.         self.rect = self.image.get_rect()
  82.         self.rect.centerx = x
  83.         self.rect.centery = HEIGHT / 2
  84.         self.speed = 0.5
  85.     def mover (self, time, keys):
  86.         if self.rect.top >= 0:
  87.             if keys[K_UP]:
  88.                 self.rect.centery -= self.speed * time
  89.         if self.rect.bottom <= HEIGHT:
  90.             if keys [K_DOWN]:
  91.                 self.rect.centery += self.speed * time
  92.     def ia(self, time, ball):
  93.         if ball.speed[0] != 0 and ball.rect.centerx >= WIDTH/2:
  94.             if self.rect.centery < ball.rect.centery:
  95.                 self.rect.centery += self.speed * time
  96.             if self.rect.centery > ball.rect.centery:
  97.                 self.rect.centery -= self.speed * time
  98.  
  99. # ------------------------------------------------------------------------
  100.  
  101. def main():
  102.     screen = pygame.display.set_mode((WIDTH, HEIGHT))
  103.     pygame.display.set_caption("Pruebas Pygame")
  104.  
  105.     #carga de imágenes
  106.     background_image = load_image('images/fondo.jpg')
  107.     bola = Bola()
  108.     pala_jug = Pala(30)
  109.     pala_cpu = Pala(WIDTH - 30)
  110.  
  111.     #tiempo
  112.     clock = pygame.time.Clock()
  113.     time = clock.tick(60)
  114.  
  115.     puntos = [0,0]
  116.     jugador = 2
  117.  
  118.     while True:
  119.         for eventos in pygame.event.get():
  120.           if eventos.type == QUIT:
  121.             sys.exit(0)
  122.         keys = pygame.key.get_pressed()
  123.  
  124.         puntos, jugador = bola.actualizar(time, [pala_jug, pala_cpu], puntos, jugador)
  125.  
  126.         pala_jug.mover(time, keys)
  127.         pala_cpu.ia(time, bola)
  128.  
  129.         screen.blit(background_image, (0,0))
  130.         p_jug, p_jug_rect = texto(str(puntos[0]), WIDTH/4, 40)
  131.         p_cpu, p_cpu_rect = texto(str(puntos[1]), WIDTH-WIDTH/4, 40)
  132.         screen.blit(p_jug, p_jug_rect)
  133.         screen.blit(p_cpu, p_cpu_rect)
  134.         screen.blit(bola.image, bola.rect)
  135.         screen.blit(pala_jug.image, pala_jug.rect)
  136.         screen.blit(pala_cpu.image, pala_cpu.rect)
  137.         pygame.display.flip()
  138.  
  139.     return 0
  140.  
  141. if __name__ == '__main__':
  142.     pygame.init()
  143.     main()
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×