Skip to content

@bradur /gist:5804625
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
import pygame
import random
pygame.init()
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 400
SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT)
SCREEN = pygame.display.set_mode(SIZE)
pygame.display.set_caption('Colour Birth')
class Ball (pygame.sprite.Sprite):
def __init__(self, colour, width, height, x=0, y=0):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width, height])
self.image.fill(colour)
self.rect = self.image.get_rect()
self.rect.x = random.randint(50, 200)
self.rect.y = random.randint(50, 200)
self.colour = colour
self.count = 0
self.screen_border() # DOESNT FUCKING WORK
self.collided = False
def movement(self):
directions = (-1, 1)
self.rect.x += random.choice(directions)
self.rect.y += random.choice(directions)
def collision(self, colour, obj1, obj2=None, obj3=None, obj4=None):
self.count = 0
if pygame.sprite.collide_rect(self, obj1) and self.collided is False:
c = Ball(colour, 20, 20, 50, 50)
self.count += 1
ball_list.add(c)
self.collided = True
if self.count > 1:
ball_list.remove(c)
def screen_border(self):
"""Making a border for the window, no sprites may pass through"""
print "It works!"
if self.rect.x >= SCREEN_WIDTH-20:
self.rect.x = SCREEN_WIDTH-40
if self.rect.x <= 0:
self.rect.x = 20
if self.rect.y >= SCREEN_HEIGHT-20:
self.rect.y = SCREEN_HEIGHT-40
if self.rect.y <= 0:
self.rect.y = 20
ball_list = pygame.sprite.Group()
a = Ball([255, 0, 0], 20, 20)
b = Ball([0, 0, 255], 20, 20)
e = Ball([0, 255, 255], 20, 20)
ball_list.add(a, b, e)
clock = pygame.time.Clock()
done = False
while done is False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
#Event processing
if event.type == pygame.MOUSEBUTTONDOWN:
pass
#game logic
for ball in ball_list:
ball.movement()
a.collision([255, 0, 255], b)
b.collision([0, 255, 0], e)
e.collision([255, 255, 0], a)
#a.screen_border()
#b.screen_border()
#e.screen_border()
#drawing commands
SCREEN.fill([255, 255, 255])
ball_list.draw(SCREEN)
pygame.display.flip()
clock.tick(30)
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.