Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. import pygame
  2. pygame.init()
  3.  
  4. #Constants
  5. #Colors
  6. BLACK = (0,0,0)
  7. RED = (255, 0, 0)
  8. WHITE = (0,0,0)
  9. screen = pygame.display.set_mode((700,500))
  10.  
  11. #variables
  12. clock = pygame.time.Clock()
  13. running = True
  14. count = 0
  15.  
  16. #classes
  17. class Block:
  18. def __init__(self, color, x, y, width, height):
  19. self.color = color
  20. self.x = x
  21. self.y = y
  22. self.width = width
  23. self.height = height
  24.  
  25. def display(self):
  26. pygame.draw.rect(screen, self.color, [self.x,self.y,self.width,self.height])
  27.  
  28. def move(self,direction):
  29. if direction == "LEFT":
  30. self.x = self.x - 1
  31. elif direction == "RIGHT":
  32. self.x = self.x + 1
  33. elif direction == "DOWN":
  34. self.y = self.y + 1
  35. elif direction == "UP":
  36. self.y = self.y - 1
  37.  
  38. def UpdateDisplay():
  39. screen.fill(WHITE)
  40. A.display()
  41. pygame.display.flip()
  42.  
  43. A = Block(RED, 10, 10, 20, 20)
  44.  
  45. while running:
  46. for event in pygame.event.get():
  47. if event.type == pygame.QUIT:
  48. running = False
  49. elif event.type == pygame.KEYDOWN:
  50. print(event.key)
  51.  
  52. keys = pygame.key.get_pressed()
  53. if keys[pygame.K_a]:
  54. A.move("LEFT")
  55. elif keys[pygame.K_d]:
  56. A.move("RIGHT")
  57. elif keys[pygame.K_w]:
  58. A.move("UP")
  59. elif keys[pygame.K_s]:
  60. A.move("DOWN")
  61. clock.tick(60)
  62. UpdateDisplay()
  63.  
  64. pygame.QUIT()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement