zharry

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

Jun 1st, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.42 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 Ball class
  14. class Face (pygame.sprite.Sprite):
  15.  
  16.     # Create attributes for the Ball class
  17.     # image = the uploaded image or surface
  18.     # rect = rectangle of the picture
  19.     # xdir = x-direction
  20.     # ydir = y-direction
  21.     def __init__(self, xCoord, yCoord, xDirection, yDirection, rgb):
  22.         pygame.sprite.Sprite.__init__(self)
  23.  
  24.         # store the image for the sprite
  25.         face = pygame.Surface((100, 100))
  26.         pygame.draw.circle(face, rgb, (50, 50), 50)
  27.         pygame.draw.circle(face, THECOLORS["white"], (50, 64), 25)
  28.         pygame.draw.rect(face, rgb, (20, 30, 60, 36))
  29.         pygame.draw.line(face, THECOLORS["white"], (35, 20), (35, 40), 4)
  30.         pygame.draw.line(face, THECOLORS["white"], (65, 20), (65, 40), 4)
  31.         face.set_colorkey((0, 0, 0))
  32.         self.image = face
  33.        
  34.  
  35.         # Make our top-left corner the passed-in location.
  36.         self.rect = self.image.get_rect()
  37.         self.rect.top = yCoord
  38.         self.rect.left = xCoord
  39.  
  40.         # store the x and y direction of movement
  41.         self.xdir = xDirection
  42.         self.ydir = yDirection
  43.  
  44.     # Define movement for the Ball.
  45.     # This is a keyword that exists but does nothing unless we define this function
  46.     def update(self):
  47.         # Calculate the new position for the ball after moving
  48.         self.rect.left = self.rect.left + self.xdir * 8
  49.         self.rect.top = self.rect.top + self.ydir * 8
  50.  
  51.         # if the ball reaches the edge, bounce it back
  52.         if self.rect.left <= 0 or self.rect.left >= 510:
  53.             self.xdir = -self.xdir
  54.         if self.rect.top <= 0 or self.rect.top >= 390:
  55.             self.ydir = -self.ydir
  56.  
  57. pygame.init()
  58. clock = pygame.time.Clock()
  59. window = pygame.display.set_mode((600, 480))
  60. pygame.display.set_caption('The Bouncing Balls - Sprites')
  61. screen = pygame.display.get_surface()
  62. screen.fill(THECOLORS['white'])
  63.  
  64. # Define an initial single Ball staring at (1, 1) with x-direction 1 and y-direction 1
  65. fristFace = Face(1, 1, 1, 1, (128, 128, 128))
  66.  
  67. # a group of all the individual ball objects
  68. faceList = pygame.sprite.Group()
  69. faceList.add(fristFace)
  70.  
  71. # Event Handling #
  72. keepGoing = True
  73.  
  74. # Game Loop
  75. while keepGoing:
  76.     clock.tick(30)  # Frame rate 30
  77.  
  78.     # Clear the screen with white
  79.     screen.fill((255, 255, 255))
  80.  
  81.     # Use draw function to blit all the Ball objects to the screen
  82.     faceList.draw(screen)
  83.  
  84.     # Use the update function to calculate new coordinates for the Ball
  85.     faceList.update()
  86.  
  87.     # update the screen contents
  88.     pygame.display.flip()
  89.  
  90.     pygame.time.delay(20)
  91.  
  92.     events = pygame.event.get()
  93.  
  94.     # Event Loop
  95.     for ev in events:
  96.  
  97.         # If the mouse is clicked
  98.         if ev.type == MOUSEBUTTONDOWN:
  99.            
  100.             # x and y coordinates of the mouse
  101.             x, y = ev.pos
  102.            
  103.             # If the left mouse button is pressed add another Ball sprite
  104.             if ev.button == 3:
  105.                 for i in faceList:
  106.                     if i.rect.collidepoint(x,y):
  107.                         faceList.remove(i)
  108.             elif ev.button == 1:
  109.  
  110.                 # if the x coord will create and off screen ball, update the position to be on screen
  111.                 if x > 500:
  112.                     x = 500
  113.  
  114.                 # if the y coord will create and off screen ball, update the position to be on screen
  115.                 if y > 380:
  116.                     y = 380
  117.  
  118.                 # Based on which area of the screen is clicked, set the initial direction
  119.                 if x <= 300 and y <= 240:
  120.                     xdir = 1
  121.                     ydir = 1
  122.                 elif x <= 300 and y > 240:
  123.                     xdir = 1
  124.                     ydir = -1
  125.                 elif x > 300 and y <= 240:
  126.                     xdir = -1
  127.                     ydir = 1
  128.                 else:
  129.                     xdir = -1
  130.                     ydir = -1
  131.    
  132.                 # Create a new Ball sprite
  133.                 newBall = Face(x, y, xdir, ydir, (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255)))
  134.    
  135.                 # Add the new ball to the sprite group
  136.                 faceList.add(newBall)
  137.  
  138.         if ev.type == 12:
  139.             keepGoing = False  # Stop the program, it's detected quit...
  140.  
  141. pygame.quit()  # Keep this IDLE friendly
Add Comment
Please, Sign In to add comment