Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import math
- import random
- from pygame.locals import *
- #setup
- background_colour = (0,0,0)
- (width, height) = (1000, 600)
- screen = pygame.display.set_mode((width, height))#,pygame.FULLSCREEN)
- screen.fill(background_colour)
- pygame.display.set_caption('Turn')
- pygame.font.init()
- myfont = pygame.font.SysFont("monospace", 20)
- clock = pygame.time.Clock()
- angle = 0
- rate_of_turn = 0.03
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- elif event.type == KEYDOWN:
- if event.key == K_ESCAPE:
- running = False
- screen.fill(background_colour)
- #the target is the mouse position
- target = pygame.mouse.get_pos()
- #work out the vector from the center of the screen to the target pos
- direction = [target[0] - width/2, target[1] - height/2]
- #work out the angle that vector is at
- dir_angle = math.atan2(direction[1], direction[0])
- #work out which direction to change your angle in to make it closer to the dir_angle
- turn = dir_angle - angle
- #keep all angles in [-pi, pi]
- if turn < -math.pi:
- turn += math.pi*2
- if turn > math.pi:
- turn -= math.pi*2
- #add some amount of the turn
- angle += rate_of_turn*(turn)
- #draw the result on the screen
- dx = 200*math.cos(angle)
- dy = 200*math.sin(angle)
- pygame.draw.line(screen, [255,255,255], [int(width/2), int(height/2)],
- [int(width/2 + dx), int(height/2 + dy)], 10)
- pygame.display.flip()
- clock.tick(60)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement