Advertisement
Guest User

Turn

a guest
Mar 14th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. import pygame
  2. import math
  3. import random
  4. from pygame.locals import *
  5.  
  6. #setup
  7.  
  8. background_colour = (0,0,0)
  9. (width, height) = (1000, 600)
  10.  
  11. screen = pygame.display.set_mode((width, height))#,pygame.FULLSCREEN)
  12. screen.fill(background_colour)
  13. pygame.display.set_caption('Turn')
  14. pygame.font.init()
  15.  
  16. myfont = pygame.font.SysFont("monospace", 20)
  17.  
  18. clock = pygame.time.Clock()
  19.  
  20. angle = 0
  21. rate_of_turn = 0.03
  22.  
  23. running = True
  24. while running:
  25.     for event in pygame.event.get():
  26.         if event.type == pygame.QUIT:
  27.             running = False
  28.         elif event.type == KEYDOWN:
  29.             if event.key == K_ESCAPE:
  30.                 running = False
  31.            
  32.  
  33.     screen.fill(background_colour)
  34.  
  35.     #the target is the mouse position
  36.     target = pygame.mouse.get_pos()
  37.     #work out the vector from the center of the screen to the target pos
  38.     direction = [target[0] - width/2, target[1] - height/2]
  39.     #work out the angle that vector is at
  40.     dir_angle = math.atan2(direction[1], direction[0])
  41.     #work out which direction to change your angle in to make it closer to the dir_angle
  42.     turn = dir_angle - angle
  43.     #keep all angles in [-pi, pi]
  44.     if turn < -math.pi:
  45.         turn += math.pi*2
  46.     if turn > math.pi:
  47.         turn -= math.pi*2
  48.     #add some amount of the turn
  49.     angle += rate_of_turn*(turn)
  50.  
  51.     #draw the result on the screen
  52.     dx = 200*math.cos(angle)
  53.     dy = 200*math.sin(angle)
  54.     pygame.draw.line(screen, [255,255,255], [int(width/2), int(height/2)],
  55.             [int(width/2 + dx), int(height/2 + dy)], 10)
  56.  
  57.     pygame.display.flip()
  58.  
  59.     clock.tick(60)
  60.    
  61.    
  62.  
  63. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement