Advertisement
ada1711

Untitled

Mar 15th, 2023
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. import pygame
  2. import random
  3.  
  4. SCREEN_WIDTH = 1024
  5. SCREEN_HEIGHT = 800
  6. vec = pygame.math.Vector2
  7.  
  8.  
  9. class Ball(pygame.sprite.Sprite):
  10. def __init__(self):
  11. super(Ball, self).__init__()
  12. self.image = pygame.image.load("images/ball.png")
  13. self.reset_position()
  14. self.r = 16
  15. self.lost = False
  16.  
  17. # resetting position
  18. def reset_position(self):
  19. self.coordinates = vec(SCREEN_WIDTH/2, SCREEN_HEIGHT-140)
  20. self.position = self.image.get_rect(center=self.coordinates)
  21. self.vector = vec(0, -10)
  22. self.incline_angle = random.randrange(-30, 30)
  23. self.vector.rotate_ip(self.incline_angle)
  24. self.lost = False
  25.  
  26. # update
  27. def update(self, platform):
  28. self.coordinates += self.vector
  29. self.position.center = self.coordinates
  30. self.check_collision(platform)
  31.  
  32. # check all possible collisions
  33. def check_collision(self, platform):
  34. # screen edges
  35. if self.position.x <= 0:
  36. self.vector.x *= -1
  37. if self.position.right >= SCREEN_WIDTH:
  38. self.vector.x *= -1
  39. if self.position.top <= 0:
  40. self.vector.y *= -1
  41. if self.position.bottom >= SCREEN_HEIGHT:
  42. self.lost = True
  43.  
  44. # platform
  45. if self.position.colliderect(platform.position):
  46. self.vector.y *= -1
  47. self.vector.x += platform.moving*5
  48. if self.vector.x < -10:
  49. self.vector.x = -10
  50. if self.vector.x > 10:
  51. self.vector.x = 10
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement