Advertisement
Windspar

Example Pygame Tank Turret Pivoting With Mouse.

Apr 9th, 2023 (edited)
1,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | Gaming | 0 0
  1. import pygame
  2.  
  3. TRANSPARENT = 0, 0, 0, 0
  4.  
  5. class ImageHandler:
  6.     def __init__(self):
  7.         color = pygame.Color('darkgreen').lerp('white', 0.1)
  8.         self.tank = self.create_tank(color)
  9.         self.turret = self.create_turret('darkgreen')
  10.         self.shell = self.create_shell('grey10')
  11.  
  12.     def create_tank(self, color):
  13.         w, h = size = 50, 40
  14.         surface = pygame.Surface(size, pygame.SRCALPHA)
  15.         surface.fill(TRANSPARENT)
  16.         rect = -5, 0, w + 10, 10
  17.         pygame.draw.ellipse(surface, 'black', rect)
  18.         rect = -5, 30, w + 10, 10
  19.         pygame.draw.ellipse(surface, 'black', rect)
  20.         rect = 0, 8, w, h - 16
  21.         surface.fill(color, rect)
  22.         return surface
  23.  
  24.     def create_turret(self, color):
  25.         size = 35, 12
  26.         surface = pygame.Surface(size, pygame.SRCALPHA)
  27.         surface.fill(color)
  28.         return surface
  29.  
  30.     def create_shell(self, color):
  31.         size = 20, 10
  32.         surface = pygame.Surface(size, pygame.SRCALPHA)
  33.         surface.fill(color)
  34.         return surface
  35.  
  36. class Sprite:
  37.     def __init__(self, image, position, anchor):
  38.         self.oimage = image
  39.         self.image = image
  40.         self.rect = image.get_rect(**{anchor:position})
  41.  
  42.     def draw(self, surface):
  43.         surface.blit(self.image, self.rect)
  44.  
  45. class Tank:
  46.     def __init__(self, tank, turret, shell, center):
  47.         self.tank = Sprite(tank, center, 'center')
  48.         self.turret = Sprite(turret, center, 'midleft')
  49.         # turret pivot center and pivot point
  50.         self.pcenter = pygame.Vector2(center)
  51.         self.pivot = self.turret.rect.center - self.pcenter
  52.         # turret distance from center
  53.         self.distance = 1.4
  54.         # set turret from center
  55.         self.turret.rect.center += self.pivot * (self.distance - 1)
  56.         self.shell = shell
  57.  
  58.     def draw(self, surface):
  59.         self.tank.draw(surface)
  60.         self.turret.draw(surface)
  61.  
  62.     def rotate(self, mpos):
  63.         angle = (mpos - self.pcenter).as_polar()[1]
  64.         pivot = self.pivot.rotate(angle) * self.distance
  65.         self.turret.image = pygame.transform.rotate(self.turret.oimage, -angle)
  66.         self.turret.rect = self.turret.image.get_rect(center=pivot + self.pcenter)
  67.  
  68.     def shoot(self):
  69.         pass
  70.  
  71. def main():
  72.     # Basic Pygame Setup
  73.     pygame.init()
  74.     pygame.display.set_caption("Example")
  75.     surface = pygame.display.set_mode((800, 600))
  76.     clock = pygame.time.Clock()
  77.     rect = surface.get_rect()
  78.     running = True
  79.     delta = 0
  80.     fps = 60
  81.  
  82.     # Variables
  83.     background = 'lawngreen'
  84.     images = ImageHandler()
  85.     tank = Tank(images.tank, images.turret, images.shell, rect.center)
  86.  
  87.     # Main Loop
  88.     while running:
  89.         for event in pygame.event.get():
  90.             if event.type == pygame.MOUSEMOTION:
  91.                 tank.rotate(event.pos)
  92.             elif event.type == pygame.MOUSEBUTTONDOWN:
  93.                 if event.button == 1:
  94.                     tank.shoot()
  95.             elif event.type == pygame.QUIT:
  96.                 running = False
  97.  
  98.         surface.fill(background)
  99.         tank.draw(surface)
  100.  
  101.         pygame.display.flip()
  102.         delta = clock.tick(fps) * 0.001
  103.  
  104. main()
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement