-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathBall.py
44 lines (35 loc) · 1.5 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
import pygame
from pygame.locals import *
import random
# Ball class
class Ball():
def __init__(self, window, windowWidth, windowHeight):
self.window = window # remember the window, so we can draw later
self.windowWidth = windowWidth
self.windowHeight = windowHeight
self.image = pygame.image.load('images/ball.png')
# A rect is made up of [x, y, width, height]
ballRect = self.image.get_rect()
self.width = ballRect.width
self.height = ballRect.height
self.maxWidth = windowWidth - self.width
self.maxHeight = windowHeight - self.height
# Pick a random starting position
self.x = random.randrange(0, self.maxWidth)
self.y = random.randrange(0, self.maxHeight)
# Choose a random speed between -4 and 4, but not zero
# in both the x and y directions
speedsList = [-4, -3, -2, -1, 1, 2, 3, 4]
self.xSpeed = random.choice(speedsList)
self.ySpeed = random.choice(speedsList)
def update(self):
# Check for hitting a wall. If so, change that direction.
if (self.x < 0) or (self.x >= self.maxWidth):
self.xSpeed = -self.xSpeed
if (self.y < 0) or (self.y >= self.maxHeight):
self.ySpeed = -self.ySpeed
# Update the Ball's x and y, using the speed in two directions
self.x = self.x + self.xSpeed
self.y = self.y + self.ySpeed
def draw(self):
self.window.blit(self.image, (self.x, self.y))