Advertisement
zicaentu

Keyboard and mouse

Jan 12th, 2019
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | None | 0 0
  1. """
  2. Pygame base template for opening a window, done with functions
  3.  
  4.  
  5. """
  6.  
  7. import pygame
  8.  
  9.  
  10. BLACK = (0x00, 0x00, 0x00)
  11. WHITE = (0xff, 0xff, 0xff)
  12. GREEN = (0x00, 0xff, 0x00)
  13. RED =   (0xff, 0x00, 0x00)
  14.  
  15.  
  16. def draw_stick_figure(screen, x, y):
  17.     pygame.draw.ellipse(screen, BLACK, [1+x, y, 10, 10], 0)
  18.     pygame.draw.line(screen, BLACK, [5+x, 17+y], [10+x,27+y], 2)
  19.     pygame.draw.line(screen, BLACK, [5+x, 17+y], [x,27+y], 2)
  20.     pygame.draw.line(screen, RED, [5+x, 17+y], [5+x,7+y], 2)
  21.     pygame.draw.line(screen, RED, [5+x, 7+y], [9+x,17+y], 2)
  22.     pygame.draw.line(screen, RED, [5+x, 7+y], [1+x,17+y], 2)
  23.  
  24.  
  25.  
  26. def main():
  27.     """ Main function for the game. """
  28.     pygame.init()
  29.  
  30.     size = [800, 600]
  31.     screen = pygame.display.set_mode(size)
  32.  
  33.     pygame.display.set_caption("My Game")
  34.  
  35.     done = False
  36.  
  37.     clock = pygame.time.Clock()
  38.  
  39.     pygame.mouse.set_visible(False)
  40.  
  41.     x_speed = 0
  42.     y_speed = 0
  43.  
  44.     x_coord = 10
  45.     y_coord = 10
  46.  
  47.    
  48.     # -------- Main Program Loop -----------
  49.     while not done:
  50.         for event in pygame.event.get():
  51.             if event.type == pygame.QUIT:
  52.                 done = True
  53.  
  54.             if event.type == pygame.KEYDOWN:
  55.                 if event.key == pygame.K_LEFT:
  56.                     x_speed = -3
  57.                 if event.key == pygame.K_RIGHT:
  58.                     x_speed = 3
  59.                 if event.key == pygame.K_UP:
  60.                     y_speed = -3
  61.                 if event.key == pygame.K_DOWN:
  62.                     y_speed = 3
  63.             elif event.type == pygame.KEYUP:
  64.                 if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
  65.                     x_speed = 0
  66.                 if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
  67.                     y_speed = 0
  68.  
  69.         pos = pygame.mouse.get_pos()
  70.         x = pos[0]
  71.         y = pos[1]
  72.  
  73.         x_coord += x_speed
  74.         y_coord += y_speed
  75.  
  76.         screen.fill(WHITE)
  77.         draw_stick_figure(screen, x_coord, y_coord)
  78.         draw_stick_figure(screen, x, y)
  79.         pygame.display.flip()
  80.  
  81.         clock.tick(60)
  82.     pygame.quit()
  83.  
  84. if __name__ == "__main__":
  85.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement