-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathball.py
53 lines (41 loc) · 1.31 KB
/
ball.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/python
# -*- coding: utf8 -*-
#
# Multimediaprogrammering i Python ST2010
# written by Jonatan Jansson, Anders Hassis & Johan Nenzén
# using Python 2.6.4
#
import random
import pygame
from pygame.locals import *
def randsign():
if(random.randint(0, 1)):
return 1
else:
return -1
class Ball:
def __init__(self, surface, (x, y), (vx,vy) ):
self.surface = surface
self.x = x
self.y = y
self.vx = vx
self.vy = vy
self.color = 255, 0, 0
self.radius = 5
self.active = True
@staticmethod
def createRandomBallsAsList(number, screen):
balls = []
for i in range (0,number):
balls.append( Ball(screen, (random.randint(50, 550), random.randint(50, 200)), (randsign()*random.uniform(1.0,3.0),random.uniform(1.0,3.0)) ) )
return balls
def update(self):
self.x += self.vx
self.y += self.vy
if self.y > self.surface.get_height() and self.active == True:
self.active = False
return True
else:
return False
def draw(self):
pygame.draw.circle(self.surface, self.color, (int(self.x), int(self.y)), self.radius)