zharry

ICS2O1-05 6/2/2016 Pygame Question 1 Answer

Jun 2nd, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.26 KB | None | 0 0
  1. import pygame, sys, os, time, random
  2. from pygame.locals import *
  3. from pygame.color import THECOLORS
  4.  
  5. # # If you get the no available video device error, copy and paste the below code ##
  6. import platform
  7.  
  8. if platform.system() == "Windows":
  9.     os.environ['SDL_VIDEODRIVER'] = 'windib'
  10. ### If you get the no available video device error, copy and paste the above code ###
  11.  
  12.  
  13. # Define Shape class
  14. class Shape (pygame.sprite.Sprite):
  15.  
  16.     # Create attributes for the shape class
  17.     # shapeImage = the uploaded image
  18.     # xCoord = left coordinate
  19.     # yCoord = top coordinate
  20.     # xDirection = x-direction
  21.     # yDirection = y-direction
  22.     def __init__(self, shapeImage, xCoord, yCoord, xDirection, yDirection):
  23.         pygame.sprite.Sprite.__init__(self)
  24.  
  25.         # store the image for the sprite
  26.         self.image = pygame.Surface((90, 90))
  27.         self.image.blit(shapeImage, (0, 0))
  28.         self.image.set_colorkey((255, 255, 255))
  29.  
  30.         # Create a rectangle for the image and make our center the passed-in location.
  31.         self.rect = self.image.get_rect()
  32.         self.rect.centery = yCoord
  33.         self.rect.centerx = xCoord
  34.  
  35.         # store the x and y direction of movement
  36.         self.xdir = xDirection
  37.         self.ydir = yDirection
  38.  
  39.     # Define movement for the Shape
  40.     def update(self):
  41.         # Calculate the new position for the shape after moving
  42.         self.rect.centerx = self.rect.centerx + self.xdir * 4
  43.         self.rect.centery = self.rect.centery + self.ydir * 4
  44.  
  45.         # if the shape reaches the edge, bounce it back
  46.         if self.rect.centerx <= 45 or self.rect.centerx >= screen.get_width() - 45:
  47.             self.xdir = -self.xdir
  48.         if self.rect.centery <= 45 or self.rect.centery >= screen.get_height() - 45:
  49.             self.ydir = -self.ydir
  50.            
  51. # Still Shape Class
  52. class stillShape (pygame.sprite.Sprite):
  53.     def __init__(self, xCoord, yCoord):
  54.         pygame.sprite.Sprite.__init__(self)
  55.         self.image = pygame.Surface((90, 90))
  56.         shapeImage = pygame.image.load("x.png").convert()
  57.         self.image.blit(shapeImage, (0, 0))
  58.         self.image.set_colorkey((255, 255, 255))
  59.         self.rect = self.image.get_rect()
  60.         self.rect.centery = yCoord
  61.         self.rect.centerx = xCoord
  62.  
  63. pygame.init()
  64. clock = pygame.time.Clock()
  65. window = pygame.display.set_mode((800, 600))
  66. pygame.display.set_caption('The Bouncing Colliding Shape')
  67. screen = pygame.display.get_surface()
  68. screen.fill(THECOLORS['white'])
  69.  
  70. # the images being used for all Shape objects
  71. oImage = pygame.image.load("o.png").convert()
  72.  
  73. # a group of all the individual shape objects
  74. shapeList = pygame.sprite.Group()
  75.  
  76. # a group of all the "O" shapes
  77. oShapeList = pygame.sprite.Group()
  78.  
  79. # X Shape in the Center
  80. xShape = stillShape(400 - 45, 300 - 45)
  81. shapeList.add(xShape)
  82.  
  83. # Make 10-20 random "O" shapes
  84. for i in range(random.randrange(10, 21)):
  85.     # Generate random x and y coordinates for the top/left of the shape that will fit on the window
  86.     xCoord = random.randrange(45, screen.get_width() - 45)
  87.     yCoord = random.randrange(45, screen.get_height() - 45)
  88.  
  89.     # Generate random x and y directions
  90.     xdir = random.choice([-1, 1])
  91.     ydir = random.choice([-1, 1])
  92.  
  93.     # Create the shape
  94.     newShape = Shape(oImage, xCoord, yCoord, xdir, ydir)
  95.  
  96.     # add the shape to the all shapes group
  97.     shapeList.add(newShape)
  98.  
  99.     # add the shape to the "O" shape group
  100.     oShapeList.add(newShape)
  101.  
  102. # Event Handling #
  103. keepGoing = True
  104.  
  105. # Font and Text Things
  106. count = 0
  107. ft = pygame.font.SysFont("comicsansms", 36)
  108.  
  109. pygame.display.flip()
  110.  
  111. # Game Loop
  112. while keepGoing:
  113.     clock.tick(30)  # Frame rate 30
  114.  
  115.     # Clear the screen with white
  116.     screen.fill((255, 255, 255))
  117.  
  118.     # Use draw function to blit all the Shape objects in the group to the screen
  119.     shapeList.draw(screen)
  120.  
  121.     # Use the update function to calculate new coordinates for all the Shapes
  122.     shapeList.update()
  123.    
  124.     # Update the Text counter
  125.     text = ft.render(("Count: " + str(count)), True, (128, 128, 128))
  126.     screen.blit(text, (10, 550))
  127.  
  128.     # update the screen contents
  129.     pygame.display.flip()
  130.  
  131.     # Check if X and O are colliding
  132.     # pygame.sprite.groupcollide(group1, group2, dokill1, dokill2)
  133.     # dokill1 will remove the group1 sprites and dokill2 will remove the group2 sprites
  134.     # False = do not delete
  135.     # True = delete from all groups
  136.     # notice in the example here, the X shapes will be removed and the O's will remain
  137.     # hitShape = pygame.sprite.groupcollide(oShapeList, xShapeList, False, True)
  138.  
  139.     # Check through every shape in the oShapeList and see if they collided with xShapeList
  140.     # spritecollideany(sprite, group) -> returns None if there is no collision
  141.     # delete the "X" shape that collides with the "O"
  142.         # Returns a single X sprite (or None) that collided with the O shape
  143.     collideShape = pygame.sprite.spritecollideany(xShape, oShapeList)
  144.  
  145.     if collideShape != None:
  146.         count += 1
  147.         collideShape.kill()
  148.  
  149.     pygame.time.delay(20)
  150.  
  151.     events = pygame.event.get()
  152.  
  153.     # Event Loop
  154.     for ev in events:
  155.         if ev.type == 12:
  156.             keepGoing = False  # Stop the program, it's detected quit...
  157.  
  158. pygame.quit()  # Keep this IDLE friendly
Add Comment
Please, Sign In to add comment