Windspar

Simple Example. Slow object down.

Jan 24th, 2025 (edited)
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | Gaming | 0 0
  1. import pygame
  2.  
  3. FRICTION = 20
  4.  
  5. class ImageHandler:
  6.     def __init__(self):
  7.         self.ball = self.create_ball(30, 'dodgerblue')
  8.  
  9.     def create_ball(self, diameter, color):
  10.         size = diameter + 1, diameter + 1
  11.         r = diameter // 2
  12.         surface = pygame.Surface(size, pygame.SRCALPHA)
  13.         surface.fill((0, 0, 0, 0))
  14.         pygame.draw.circle(surface, color, (r, r), r)
  15.         return surface
  16.  
  17. class Ball(pygame.sprite.Sprite):
  18.     def __init__(self, image, velocity, position, anchor):
  19.         super().__init__()
  20.         self.image = image
  21.         self.rect = image.get_rect(**{anchor: position})
  22.         self.center = pygame.Vector2(self.rect.center)
  23.         self.vector = pygame.Vector2()
  24.         self.velocity = 0
  25.         self.max_velocity = velocity
  26.  
  27.     def move(self, movement):
  28.         self.center += movement
  29.         self.rect.center = self.center
  30.  
  31.     def shoot(self, event):
  32.         self.vector = (event.pos - self.center).normalize()
  33.         self.velocity = self.max_velocity
  34.  
  35.     def update(self, delta):
  36.         if self.velocity > 0:
  37.             self.move(self.vector * self.velocity * delta)
  38.             self.velocity -= FRICTION * delta
  39.  
  40. class Game:
  41.     def __init__(self, caption, size, flags=0, fps=60):
  42.         pygame.display.set_caption(caption)
  43.         self.display = pygame.display.set_mode(size, flags)
  44.         self.rect = self.display.get_rect()
  45.         self.clock = pygame.time.Clock()
  46.         self.running = False
  47.         self.delta = 0
  48.         self.fps = fps
  49.  
  50.         self.image = ImageHandler()
  51.         self.ball = Ball(self.image.ball, 100, self.rect.center, 'center')
  52.         self.sprites = pygame.sprite.Group(self.ball)
  53.  
  54.     def on_draw(self, surface):
  55.         surface.fill('black')
  56.         self.sprites.draw(surface)
  57.  
  58.     def on_event(self, event):
  59.         if event.type != pygame.QUIT:
  60.             if event.type == pygame.MOUSEBUTTONDOWN:
  61.                 if event.button == 1:
  62.                     self.ball.shoot(event)
  63.         else:
  64.             self.running = False
  65.  
  66.     def on_update(self, delta):
  67.         self.ball.update(delta)
  68.  
  69.     def run(self):
  70.         self.running = True
  71.         while self.running:
  72.             for event in pygame.event.get():
  73.                 self.on_event(event)
  74.  
  75.             self.on_update(self.delta)
  76.             self.on_draw(self.display)
  77.             pygame.display.flip()
  78.             self.delta = self.clock.tick(self.fps) / 1000
  79.  
  80. pygame.init()
  81. game = Game("Slowing Down Ball", (800, 600))
  82. game.run()
  83. pygame.quit()
  84.  
Advertisement
Add Comment
Please, Sign In to add comment