Advertisement
metalx1000

Circle Game in PyGame - POP the Bubbles

Jan 20th, 2012
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #Created by Kris Occhipinti
  3. #http://FilmsByKris.com December 29th 2010
  4. #GPLv3 - http://www.gnu.org/licenses/gpl-3.0.html
  5.  
  6. import pygame, sys, random          #load pygame module
  7. from pygame.locals import *
  8.  
  9. w = 800                 #set width of screen
  10. h = 600                 #set height
  11.  
  12. miss = 0
  13.  
  14. screen = pygame.display.set_mode((w, h)) #make screen
  15.  
  16. pygame.display.flip()
  17.  
  18.  
  19. class Ball:
  20.    def __init__(self, radius, speed, x, y, color, size):
  21.        if speed == 0:
  22.           speed=2
  23.        self.d = 15
  24.        self.y=y
  25.        self.x=x
  26.        self.size=size
  27.        #speed
  28.        self.yy=speed
  29.        self.xx=speed
  30.        self.radius=radius
  31.        self.color=color
  32.        pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius )
  33.  
  34.  
  35.    def cir_mov(self):
  36.        global h, w
  37.        self.d-=1
  38.        self.x+=self.xx
  39.        self.y+=self.yy
  40.        if self.x > w - self.radius:
  41.           self.xx = self.xx*-1
  42.        elif self.x < 0 + self.radius:
  43.           self.xx = self.xx*-1
  44.        elif self.y > h - self.radius:
  45.           self.yy = self.yy*-1
  46.        elif self.y < 0 + self.radius:
  47.           self.yy = self.yy*-1
  48.        pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius )
  49.  
  50.    def cir_click(self, x, y):
  51.        global miss
  52.        if self.d < 0:
  53.            if x > self.x - self.radius and x < self.x + self.radius:
  54.                if y > self.y - self.radius and y < self.y + self.radius:
  55.                    miss = 1
  56.                    ball.remove(self)
  57.                    if self.size == "L":
  58.                        ball.append(Ball(25, 2, self.x, self.y, (random.randint(1,255),random.randint(1,255),random.randint(1,255)), "S"))
  59.                        ball.append(Ball(25, -2, self.x, self.y, (random.randint(1,255),random.randint(1,255),random.randint(1,255)), "S"))
  60.                    
  61. clock = pygame.time.Clock()
  62. ball = []
  63. ball.append(Ball(50, random.randint(-3,3), 250, 250, (random.randint(1,255),random.randint(1,255),random.randint(1,255)),"L"))
  64. ball.append(Ball(50, random.randint(-3,3), 150, 150, (random.randint(1,255),random.randint(1,255),random.randint(1,255)),"L"))
  65.  
  66.  
  67. while 1:
  68.    clock.tick(60)
  69.    for event in pygame.event.get():
  70.        if event.type == pygame.QUIT:
  71.            sys.exit()
  72.        elif event.type == MOUSEBUTTONDOWN:
  73.            print "click"
  74.            x,y = pygame.mouse.get_pos()
  75.            for i in ball:
  76.                i.cir_click(x,y)
  77.        elif event.type == MOUSEBUTTONUP:
  78.            if miss == 0:
  79.                ball.append(Ball(50, random.randint(-3,3), x, y, (random.randint(1,255),random.randint(1,255),random.randint(1,255)),"L"))
  80.            miss = 0
  81.  
  82.    screen.fill((0,0,0))
  83.    for i in ball:
  84.        i.cir_mov()
  85.  
  86.    pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement