Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, sys, os, time, random
- from pygame.locals import *
- from pygame.color import THECOLORS
- # # If you get the no available video device error, copy and paste the below code ##
- import platform
- if platform.system() == "Windows":
- os.environ['SDL_VIDEODRIVER'] = 'windib'
- ### If you get the no available video device error, copy and paste the above code ###
- # Define Ball class
- class Face (pygame.sprite.Sprite):
- # Create attributes for the Ball class
- # image = the uploaded image or surface
- # rect = rectangle of the picture
- # xdir = x-direction
- # ydir = y-direction
- def __init__(self, xCoord, yCoord, xDirection, yDirection, rgb):
- pygame.sprite.Sprite.__init__(self)
- # store the image for the sprite
- face = pygame.Surface((100, 100))
- pygame.draw.circle(face, rgb, (50, 50), 50)
- pygame.draw.circle(face, THECOLORS["white"], (50, 64), 25)
- pygame.draw.rect(face, rgb, (20, 30, 60, 36))
- pygame.draw.line(face, THECOLORS["white"], (35, 20), (35, 40), 4)
- pygame.draw.line(face, THECOLORS["white"], (65, 20), (65, 40), 4)
- face.set_colorkey((0, 0, 0))
- self.image = face
- # Make our top-left corner the passed-in location.
- self.rect = self.image.get_rect()
- self.rect.top = yCoord
- self.rect.left = xCoord
- # store the x and y direction of movement
- self.xdir = xDirection
- self.ydir = yDirection
- # Define movement for the Ball.
- # This is a keyword that exists but does nothing unless we define this function
- def update(self):
- # Calculate the new position for the ball after moving
- self.rect.left = self.rect.left + self.xdir * 8
- self.rect.top = self.rect.top + self.ydir * 8
- # if the ball reaches the edge, bounce it back
- if self.rect.left <= 0 or self.rect.left >= 510:
- self.xdir = -self.xdir
- if self.rect.top <= 0 or self.rect.top >= 390:
- self.ydir = -self.ydir
- pygame.init()
- clock = pygame.time.Clock()
- window = pygame.display.set_mode((600, 480))
- pygame.display.set_caption('The Bouncing Balls - Sprites')
- screen = pygame.display.get_surface()
- screen.fill(THECOLORS['white'])
- # Define an initial single Ball staring at (1, 1) with x-direction 1 and y-direction 1
- fristFace = Face(1, 1, 1, 1, (128, 128, 128))
- # a group of all the individual ball objects
- faceList = pygame.sprite.Group()
- faceList.add(fristFace)
- # Event Handling #
- keepGoing = True
- # Game Loop
- while keepGoing:
- clock.tick(30) # Frame rate 30
- # Clear the screen with white
- screen.fill((255, 255, 255))
- # Use draw function to blit all the Ball objects to the screen
- faceList.draw(screen)
- # Use the update function to calculate new coordinates for the Ball
- faceList.update()
- # update the screen contents
- pygame.display.flip()
- pygame.time.delay(20)
- events = pygame.event.get()
- # Event Loop
- for ev in events:
- # If the mouse is clicked
- if ev.type == MOUSEBUTTONDOWN:
- # x and y coordinates of the mouse
- x, y = ev.pos
- # If the left mouse button is pressed add another Ball sprite
- if ev.button == 3:
- for i in faceList:
- if i.rect.collidepoint(x,y):
- faceList.remove(i)
- elif ev.button == 1:
- # if the x coord will create and off screen ball, update the position to be on screen
- if x > 500:
- x = 500
- # if the y coord will create and off screen ball, update the position to be on screen
- if y > 380:
- y = 380
- # Based on which area of the screen is clicked, set the initial direction
- if x <= 300 and y <= 240:
- xdir = 1
- ydir = 1
- elif x <= 300 and y > 240:
- xdir = 1
- ydir = -1
- elif x > 300 and y <= 240:
- xdir = -1
- ydir = 1
- else:
- xdir = -1
- ydir = -1
- # Create a new Ball sprite
- newBall = Face(x, y, xdir, ydir, (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255)))
- # Add the new ball to the sprite group
- faceList.add(newBall)
- if ev.type == 12:
- keepGoing = False # Stop the program, it's detected quit...
- pygame.quit() # Keep this IDLE friendly
Add Comment
Please, Sign In to add comment