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 Shape class
- class Shape (pygame.sprite.Sprite):
- # Create attributes for the shape class
- # shapeImage = the uploaded image
- # xCoord = left coordinate
- # yCoord = top coordinate
- # xDirection = x-direction
- # yDirection = y-direction
- def __init__(self, shapeImage, xCoord, yCoord, xDirection, yDirection):
- pygame.sprite.Sprite.__init__(self)
- # store the image for the sprite
- self.image = pygame.Surface((90, 90))
- self.image.blit(shapeImage, (0, 0))
- self.image.set_colorkey((255, 255, 255))
- # Create a rectangle for the image and make our center the passed-in location.
- self.rect = self.image.get_rect()
- self.rect.centery = yCoord
- self.rect.centerx = xCoord
- # store the x and y direction of movement
- self.xdir = xDirection
- self.ydir = yDirection
- # Define movement for the Shape
- def update(self):
- # Calculate the new position for the shape after moving
- self.rect.centerx = self.rect.centerx + self.xdir * 4
- self.rect.centery = self.rect.centery + self.ydir * 4
- # if the shape reaches the edge, bounce it back
- if self.rect.centerx <= 45 or self.rect.centerx >= screen.get_width() - 45:
- self.xdir = -self.xdir
- if self.rect.centery <= 45 or self.rect.centery >= screen.get_height() - 45:
- self.ydir = -self.ydir
- # Still Shape Class
- class stillShape (pygame.sprite.Sprite):
- def __init__(self, xCoord, yCoord):
- pygame.sprite.Sprite.__init__(self)
- self.image = pygame.Surface((90, 90))
- shapeImage = pygame.image.load("x.png").convert()
- self.image.blit(shapeImage, (0, 0))
- self.image.set_colorkey((255, 255, 255))
- self.rect = self.image.get_rect()
- self.rect.centery = yCoord
- self.rect.centerx = xCoord
- pygame.init()
- clock = pygame.time.Clock()
- window = pygame.display.set_mode((800, 600))
- pygame.display.set_caption('The Bouncing Colliding Shape')
- screen = pygame.display.get_surface()
- screen.fill(THECOLORS['white'])
- # the images being used for all Shape objects
- oImage = pygame.image.load("o.png").convert()
- # a group of all the individual shape objects
- shapeList = pygame.sprite.Group()
- # a group of all the "O" shapes
- oShapeList = pygame.sprite.Group()
- # X Shape in the Center
- xShape = stillShape(400 - 45, 300 - 45)
- shapeList.add(xShape)
- # Make 10-20 random "O" shapes
- for i in range(random.randrange(10, 21)):
- # Generate random x and y coordinates for the top/left of the shape that will fit on the window
- xCoord = random.randrange(45, screen.get_width() - 45)
- yCoord = random.randrange(45, screen.get_height() - 45)
- # Generate random x and y directions
- xdir = random.choice([-1, 1])
- ydir = random.choice([-1, 1])
- # Create the shape
- newShape = Shape(oImage, xCoord, yCoord, xdir, ydir)
- # add the shape to the all shapes group
- shapeList.add(newShape)
- # add the shape to the "O" shape group
- oShapeList.add(newShape)
- # Event Handling #
- keepGoing = True
- # Font and Text Things
- count = 0
- ft = pygame.font.SysFont("comicsansms", 36)
- pygame.display.flip()
- # 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 Shape objects in the group to the screen
- shapeList.draw(screen)
- # Use the update function to calculate new coordinates for all the Shapes
- shapeList.update()
- # Update the Text counter
- text = ft.render(("Count: " + str(count)), True, (128, 128, 128))
- screen.blit(text, (10, 550))
- # update the screen contents
- pygame.display.flip()
- # Check if X and O are colliding
- # pygame.sprite.groupcollide(group1, group2, dokill1, dokill2)
- # dokill1 will remove the group1 sprites and dokill2 will remove the group2 sprites
- # False = do not delete
- # True = delete from all groups
- # notice in the example here, the X shapes will be removed and the O's will remain
- # hitShape = pygame.sprite.groupcollide(oShapeList, xShapeList, False, True)
- # Check through every shape in the oShapeList and see if they collided with xShapeList
- # spritecollideany(sprite, group) -> returns None if there is no collision
- # delete the "X" shape that collides with the "O"
- # Returns a single X sprite (or None) that collided with the O shape
- collideShape = pygame.sprite.spritecollideany(xShape, oShapeList)
- if collideShape != None:
- count += 1
- collideShape.kill()
- pygame.time.delay(20)
- events = pygame.event.get()
- # Event Loop
- for ev in events:
- 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