Advertisement
Guest User

Keyboard drawing

a guest
Feb 9th, 2017
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. import pygame, sys, random
  2. from pygame.locals import *
  3. def main():
  4.  
  5.     pygame.init()
  6.  
  7.     windowSurface = pygame.display.set_mode((750, 750), 0, 32)
  8.     pygame.display.set_caption('colourBox')
  9.     BLACK = (0, 0, 0)
  10.     WHITE = (255, 255, 255)
  11.     RED = (255, 0, 0)
  12.     GREEN = (0, 255, 0)
  13.     BLUE = (0, 0, 255)
  14.     YELLOW = (255, 255, 0)
  15.     COLOUR = WHITE
  16.     FILL = BLACK
  17.     paintStreak = False
  18.     offsetX = 0
  19.     offsetY = 0
  20.     SPEED = {-1 : -8, 0 : 0, 1: 8}
  21.     x = 325
  22.     y = 325
  23.     basicFont = pygame.font.SysFont(None, 48)
  24.     textcolour = 0
  25.     upperBound = 0
  26.     lowerBound = 700
  27.     leftBound =  0
  28.     rightBound = 700
  29.     windowSurface.fill(BLACK)
  30.     pygame.draw.rect(windowSurface, WHITE, (x, y, 50, 50))
  31.     pygame.display.update()
  32.  
  33.        
  34.  
  35.     def moveBox(x, y):
  36.         while True:
  37.             if paintStreak == False:
  38.                 pygame.draw.rect(windowSurface, FILL, (wipeX, wipeY, 50, 50))
  39.             pygame.draw.rect(windowSurface, COLOUR, (x, y, 50, 50))
  40.             pygame.display.update()
  41.             return
  42.    
  43.     while True:
  44.         if offsetX > 1:
  45.             offsetX = 1
  46.         if offsetX < -1:
  47.             offsetX = -1
  48.         if offsetY > 1:
  49.             offsetY = 1
  50.         if offsetY < -1:
  51.             offsetY = -1
  52.         wipeX = x
  53.         wipeY = y
  54.         x = x + SPEED[offsetX]
  55.         y = y + SPEED[offsetY]
  56.         if x > rightBound:
  57.             x = rightBound
  58.         if y > lowerBound:
  59.             y = lowerBound
  60.         if x < leftBound:
  61.             x = leftBound
  62.         if y < upperBound:
  63.             y = upperBound
  64.  
  65.         moveBox(x, y)
  66.  
  67.        
  68.        
  69.        
  70.         for event in pygame.event.get():
  71.             if event.type == QUIT:
  72.                 pygame.quit()
  73.                 sys.exit()
  74.             if event.type == KEYDOWN:
  75.                    
  76.                 if event.key == K_UP:
  77.                     offsetY -= 1                    
  78.                 if event.key == K_DOWN:
  79.                     offsetY += 1
  80.                 if event.key == K_LEFT:
  81.                     offsetX -= 1
  82.                 if event.key == K_RIGHT:
  83.                     offsetX += 1
  84.                 if event.key == K_RETURN:
  85.                     FILL = COLOUR
  86.                     windowSurface.fill(FILL)
  87.                 if event.key == K_g:
  88.                     COLOUR = GREEN
  89.                 if event.key == K_b:
  90.                     COLOUR = BLUE
  91.                 if event.key == K_r:
  92.                     COLOUR = RED
  93.                 if event.key == K_y:
  94.                     COLOUR = YELLOW
  95.                 if event.key == K_n:
  96.                     COLOUR = BLACK
  97.                 if event.key == K_w:
  98.                     COLOUR = WHITE
  99.                 if event.key == K_EQUALS:
  100.                     paintStreak = True
  101.                 if event.key == K_MINUS:
  102.                     paintStreak = False
  103.  
  104.  
  105.  
  106.             if event.type == KEYUP:
  107.                 if event.key == K_UP:
  108.                     offsetY += 1
  109.                 if event.key == K_DOWN:
  110.                     offsetY -= 1
  111.                 if event.key == K_LEFT:
  112.                     offsetX += 1
  113.                 if event.key == K_RIGHT:
  114.                     offsetX -= 1
  115.            
  116. if __name__ == "__main__":
  117.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement