public
Last active

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
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()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.