Guest User

Untitled

a guest
Nov 30th, 2017
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1.  
  2. import pygame as pg
  3. from pygame.locals import *
  4.  
  5. BLACK = (0,0,0)
  6. WHITE = (255,255,255)
  7. YELLOW = (255,255,0)
  8. BLUE = (0,0,255)
  9.  
  10. SCREEN_SIZE = (500,500)
  11.  
  12. class Dirty(pg.sprite.Sprite):
  13.    
  14.     def __init__(self, color, rect, *groups):
  15.         pg.sprite.Sprite.__init__(self, groups)
  16.        
  17.         self.color = color
  18.         self.rect = rect
  19.        
  20.         self.image = pg.Surface((rect.width, rect.height))
  21.         self.image.fill(color)
  22.    
  23.     def change_color(self,color):
  24.         self.color = color
  25.         self.image.fill(color)
  26.    
  27.     def render(self, screen):
  28.         screen.blit(self.image, self.rect)
  29.        
  30.  
  31. pg.init()
  32. screen = pg.display.set_mode(SCREEN_SIZE)
  33. clock = pg.time.Clock()
  34.  
  35. background = pg.Surface(SCREEN_SIZE)
  36. background.fill(WHITE)
  37.  
  38. dirty_a = Dirty(YELLOW, pg.Rect(100,250,32, 32))
  39. dirty_b = Dirty(BLUE, pg.Rect(400,250,32,32))
  40.  
  41. rect_a = pg.Rect(100,250,64,64)
  42. rect_b = pg.Rect(400,250,64,64)
  43.  
  44. update_rect = None
  45.  
  46. while True:
  47.    
  48.     for event in pg.event.get():
  49.         if event.type == pg.QUIT: exit()
  50.        
  51.         if event.type == pg.KEYDOWN:
  52.             if event.key == pg.K_LEFT:
  53.                 background.fill(BLACK)
  54.             if event.key == pg.K_RIGHT:
  55.                 background.fill(WHITE)
  56.             if event.key == pg.K_UP:
  57.                 update_rect = rect_a
  58.             if event.key == pg.K_DOWN:
  59.                 update_rect = rect_b
  60.                
  61.  
  62.    
  63.  
  64.     screen.blit(background,(0,0))
  65.    
  66.     dirty_a.render(screen)
  67.     dirty_b.render(screen)
  68.    
  69.     pg.display.update(update_rect)
Advertisement
Add Comment
Please, Sign In to add comment