Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- SCREEN_WIDTH = 1024
- SCREEN_HEIGHT = 800
- vec = pygame.math.Vector2
- class Ball(pygame.sprite.Sprite):
- def __init__(self):
- super(Ball, self).__init__()
- self.surf = pygame.image.load("images/ball.png")
- self.resetPosition()
- self.r = 16
- self.lost = False
- #resetowanie pozycji
- def resetPosition(self):
- self.position = vec(SCREEN_WIDTH/2, SCREEN_HEIGHT-140)
- self.rect = self.surf.get_rect(center=self.position)
- self.vel = vec(0, -10)
- self.angle = random.randrange(-30, 30)
- self.vel.rotate_ip(self.angle)
- self.lost = False
- #aktualizacja
- def update(self, pad, bricks):
- self.position += self.vel
- self.rect.center = self.position
- self.checkCollision(pad, bricks)
- #sprawdz wszystkie mozliwe kolizje
- def checkCollision(self, pad, bricks):
- #krawedzie ekranu
- if self.rect.x <= 0:
- self.vel.x *= -1
- if self.rect.right >= SCREEN_WIDTH:
- self.vel.x *= -1
- if self.rect.top <= 0:
- self.vel.y *= -1
- if self.rect.bottom >= SCREEN_HEIGHT:
- self.lost = True
- #kolizja z platforma
- if self.rect.colliderect(pad.rect):
- self.vel.y *= -1
- self.vel.x += pad.moving*5
- if self.vel.x < -10: self.vel.x = -10
- if self.vel.x > 10: self.vel.x = 10
- #kolizja z klockami
- for brick in bricks:
- #nastapila kolizja
- if self.detect_collision(self, brick):
- brick.hit()
- break
- #funkcja pomocnicza do kolizji z klockami
- def detect_collision(self, ball, brick):
- xDist = abs(ball.rect.centerx - brick.rect.centerx) - brick.rect.w / 2
- yDist = abs(ball.rect.centery - brick.rect.centery) - brick.rect.h / 2
- if xDist < ball.r and yDist < ball.r:
- if xDist < yDist:
- self.vel.y *= -1
- else:
- self.vel.x *= -1
- return True
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement