Commit 5109a5f3 by BellCodeEditor

save project

parent 32785524
Showing with 26 additions and 5 deletions
......@@ -2,6 +2,7 @@ import pygame
import sys
from pygame.locals import *
from random import *
import math
class Ball(pygame.sprite.Sprite):
def __init__(self,image,position,speed,bg_size):
pygame.sprite.Sprite.__init__(self)
......@@ -20,6 +21,15 @@ class Ball(pygame.sprite.Sprite):
self.rect.top = self.height
elif self.rect.top > self.height:
self.rect.bottom = 0
def collide_check(item,target):
col_balls = []
for each in target:
distance = math.sqrt(\
math.pow((item.rect.center[0] - each.rect.center[0]),2) +\
math.pow((item.rect.center[1] - each.rect.center[1]),2))
if distance <= (item.rect.width + each.rect.width) / 2:
col_balls.append(each)
return col_balls
def main():
pygame.init()
ball_image = "gray_ball.png"
......@@ -30,11 +40,16 @@ def main():
pygame.display.set_caption("Play the ball - Demo")
background = pygame.image.load(bg_image).convert_alpha()
balls = []
for i in range(5):
group = pygame.sprite.Group()
BALL_NUM = 5
for i in range(BALL_NUM):
position = randint(0,width-100),randint(0,height-100)
speed = [randint(-10,10),randint(-10,10)]
ball = Ball(ball_image,position,speed,bg_size)
balls.append(ball)
while pygame.sprite.spritecollide(ball,group,False,pygame.sprite.collide_circle):
ball.rect.left,ball.rect.top = randint(0,width-100),randint(0,height-100)
balls.append(ball)
group.add(ball)
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
......@@ -44,7 +59,13 @@ def main():
for each in balls:
each.move()
screen.blit(each.image,each.rect)
for each in group:
group.remove(each)
if pygame.sprite.spritecollide(each,group,False,pygame.sprite.collide_circle):
each.speed[0] = -each.speed[0]
each.speed[1] = -each.speed[1]
group.add(each)
pygame.display.flip()
clock.tick(30)
if __name__ == "__main__":
main()
\ No newline at end of file
main()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment