Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame as pg
- from pygame.locals import *
- BLACK = (0,0,0)
- WHITE = (255,255,255)
- YELLOW = (255,255,0)
- BLUE = (0,0,255)
- SCREEN_SIZE = (500,500)
- class Dirty(pg.sprite.Sprite):
- def __init__(self, color, rect, *groups):
- pg.sprite.Sprite.__init__(self, groups)
- self.color = color
- self.rect = rect
- self.image = pg.Surface((rect.width, rect.height))
- self.image.fill(color)
- def change_color(self,color):
- self.color = color
- self.image.fill(color)
- def render(self, screen):
- screen.blit(self.image, self.rect)
- pg.init()
- screen = pg.display.set_mode(SCREEN_SIZE)
- clock = pg.time.Clock()
- background = pg.Surface(SCREEN_SIZE)
- background.fill(WHITE)
- dirty_a = Dirty(YELLOW, pg.Rect(100,250,32, 32))
- dirty_b = Dirty(BLUE, pg.Rect(400,250,32,32))
- rect_a = pg.Rect(100,250,64,64)
- rect_b = pg.Rect(400,250,64,64)
- update_rect = None
- while True:
- for event in pg.event.get():
- if event.type == pg.QUIT: exit()
- if event.type == pg.KEYDOWN:
- if event.key == pg.K_LEFT:
- background.fill(BLACK)
- if event.key == pg.K_RIGHT:
- background.fill(WHITE)
- if event.key == pg.K_UP:
- update_rect = rect_a
- if event.key == pg.K_DOWN:
- update_rect = rect_b
- screen.blit(background,(0,0))
- dirty_a.render(screen)
- dirty_b.render(screen)
- pg.display.update(update_rect)
Advertisement
Add Comment
Please, Sign In to add comment