ssoni

turret.py

Mar 6th, 2026
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. import math
  2. import pygame
  3. import sys
  4.  
  5. pygame.init()
  6.  
  7. screen = pygame.display.set_mode((800,400))
  8. pygame.display.set_caption('Game Title')
  9.  
  10. clock = pygame.time.Clock()
  11. angle=0
  12. turretLen = 75
  13. moveAmt = 0
  14. x1=100
  15. y1=100
  16.  
  17. while True:
  18.     for event in pygame.event.get():
  19.         if event.type == pygame.QUIT:
  20.             pygame.quit()
  21.             sys.exit()
  22.  
  23.     keys = pygame.key.get_pressed()
  24.  
  25.     if keys[pygame.K_LEFT]:
  26.         angle -= 5
  27.     if keys[pygame.K_RIGHT]:
  28.         angle += 5
  29.     print(angle)
  30.     if keys[pygame.K_UP]:
  31.         moveAmt += 1
  32.     if keys[pygame.K_DOWN]:
  33.         moveAmt -= 1
  34.         if moveAmt <= 0:
  35.             moveAmt = 0
  36.  
  37.     moveAmt = moveAmt*.95
  38.    
  39.     x1 += math.cos(math.radians(angle))*moveAmt
  40.     y1 += math.sin(math.radians(angle))*moveAmt
  41.    
  42.     x2 = math.cos(math.radians(angle))*turretLen
  43.     y2 = math.sin(math.radians(angle))*turretLen
  44.  
  45.     screen.fill((70,70,70))
  46.    
  47.     pygame.draw.circle(screen, 'red', (x1,y1), 50, 1)
  48.     pygame.draw.line(screen, 'blue', (x1,y1), (x1+x2,y1+y2), 3)
  49.  
  50.     pygame.display.update()     ## vs. flip()
  51.     clock.tick(60)
  52.  
Advertisement
Add Comment
Please, Sign In to add comment