Advertisement
snowden_web

Untitled

Aug 8th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. import pygame
  2.  
  3. # Define some colors
  4. BLACK = (0, 0, 0)
  5. WHITE = (255, 255, 255)
  6. GREEN = (0, 255, 0)
  7. RED = (255, 0, 0)
  8.  
  9.  
  10. def draw_item(screen, x, y):
  11.     # Head
  12.     pygame.draw.ellipse(screen, BLACK, [1 + x, y, 10, 10], 0)
  13.  
  14.     # Legs
  15.     pygame.draw.line(screen, BLACK, [5 + x, 17 + y], [10 + x, 27 + y], 2)
  16.     pygame.draw.line(screen, BLACK, [5 + x, 17 + y], [x, 27 + y], 2)
  17.  
  18.     # Body
  19.     pygame.draw.line(screen, RED, [5 + x, 17 + y], [5 + x, 7 + y], 2)
  20.  
  21.     # Arms
  22.     pygame.draw.line(screen, RED, [5 + x, 7 + y], [9 + x, 17 + y], 2)
  23.     pygame.draw.line(screen, RED, [5 + x, 7 + y], [1 + x, 17 + y], 2)
  24.  
  25. # Setup
  26. pygame.init()
  27.  
  28. # Set the width and height of the screen [width,height]
  29. size = [700, 500]
  30. screen = pygame.display.set_mode(size)
  31.  
  32. pygame.display.set_caption("My Game")
  33.  
  34. # Loop until the user clicks the close button.
  35. done = False
  36.  
  37. # Used to manage how fast the screen updates
  38. clock = pygame.time.Clock()
  39.  
  40. # Hide the mouse cursor
  41. pygame.mouse.set_visible(0)
  42.  
  43. # Speed in pixels per frame
  44. x_speed = 0
  45. y_speed = 0
  46.  
  47. # Current position
  48. x_coord = 10
  49. y_coord = 10
  50.  
  51. # Main Program Loop
  52. while not done:
  53.     # Event Processing
  54.     for event in pygame.event.get():
  55.         if event.type == pygame.QUIT:
  56.             done = True
  57.             # User pressed down on a key
  58.  
  59.         elif event.type == pygame.KEYDOWN:
  60.             # Figure out if it was an arrow key. If so
  61.             # adjust speed.
  62.             if event.key == pygame.K_LEFT:
  63.                 x_speed = -3
  64.             elif event.key == pygame.K_RIGHT:
  65.                 x_speed = 3
  66.             elif event.key == pygame.K_UP:
  67.                 y_speed = -3
  68.             elif event.key == pygame.K_DOWN:
  69.                 y_speed = 3
  70.  
  71.         # User let up on a key
  72.         elif event.type == pygame.KEYUP:
  73.             # If it is an arrow key, reset vector back to zero
  74.             if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
  75.                 x_speed = 0
  76.             elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
  77.                 y_speed = 0
  78.  
  79.  
  80.  
  81.     # Move the object according to the speed vector.
  82.     x_coord = x_coord + x_speed
  83.     y_coord = y_coord + y_speed
  84.  
  85.  
  86.  
  87.     # First, clear the screen to WHITE. Don't put other drawing commands
  88.     # above this, or they will be erased with this command.
  89.     screen.fill(WHITE)
  90.  
  91.     draw_item(screen, x_coord, y_coord)
  92.  
  93.  
  94.     # Go ahead and update the screen with what we've drawn.
  95.     pygame.display.flip()
  96.  
  97.     # Limit frames per second
  98.     clock.tick(60)
  99.  
  100. # Close the window and quit.
  101. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement