-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathball.py
56 lines (48 loc) · 1.71 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
54
55
56
import pygame
import random
import t
pygame.init()
class Ball(pygame.sprite.Sprite):
def __init__(self, x_speed, y_speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((23, 23))
self.image.fill(t.RED)
self.image.set_colorkey(t.BLACK)
self.rect = self.image.get_rect()
self.rect.center = (random.randint(5, 400), random.randint(5, 400))
self.x_speed = x_speed
self.y_speed = y_speed
self.number = 0
def update(self):
self.rect.x += self.x_speed
self.rect.y += self.y_speed
if self.rect.bottom < t.HEIGHT:
self.y_speed = -self.y_speed
if self.rect.top > 0:
self.y_speed = -self.y_speed
if self.rect.right > t.WIDTH:
self.x_speed = -self.x_speed
if self.rect.left < 0:
self.x_speed = -self.x_speed
class BallSpeed(pygame.sprite.Sprite):
def __init__(self, x_speed, y_speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((20, 20))
self.image.fill(t.DARKRED)
self.image.set_colorkey(t.BLACK)
self.rect = self.image.get_rect()
self.rect.center = (random.randint(5, 400), random.randint(5, 400))
self.x_speed = x_speed
self.y_speed = y_speed
self.number = 0
def update(self):
self.rect.x += self.x_speed
self.rect.y += self.y_speed
if self.rect.bottom < t.HEIGHT:
self.y_speed = -self.y_speed
if self.rect.top > 0:
self.y_speed = -self.y_speed
if self.rect.right > t.WIDTH:
self.x_speed = -self.x_speed
if self.rect.left < 0:
self.x_speed = -self.x_speed