-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBox.py
More file actions
139 lines (122 loc) · 4.44 KB
/
Box.py
File metadata and controls
139 lines (122 loc) · 4.44 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'''
Created on Feb 6, 2015
@author: SIU853541579
'''
import pygame
import random
class Box():
def __init__(self, x, y, width, height, vx, vy, color,boxType,health):
self.x = x
self.y = y
self.width = width
self.height = height
self.vx = vx
self.vy = vy
self.color = color
self.boxType = boxType #boxType is to tell if it is the ball or a paddle
self.health = health
def moveIt(self):
self.x += self.vx
self.y += self.vy
def checkBoundHit(self, s_wd, s_ht):
result = [0,0]
if(self.x <= 0):
result[0] = -1
elif(self.x+self.width >= s_wd):
result[0] = 1
if(self.y <= 0):
result[1] = -1
elif(self.y + self.height >= s_ht):
result[1] = 1
return result
def handleBoundHit(self, hitDir, s_width, s_height):
if(hitDir[0] == -1):
self.x = 1
self.vx *= -1
elif(hitDir[0] == 1):
self.x = s_width - self.width - 1
self.vx *= -1
if(hitDir[1] == -1):
self.y = 1
self.vy *= -1
elif(hitDir[1] == 1):
self.y = s_height - self.height - 1
self.vy *= -1
def toRect(self):
return (self.x, self.y, self.width, self.height)
def drawIt(self, drawDest):
pygame.draw.rect(drawDest, self.color, self.toRect())
def checkBoxCollision(self, other):
#min1--------------min1
# min2--------------max2
if(self.x <= (other.x + other.width)) and (other.x <= (self.x + self.width)):
if(self.y <= (other.y + other.height)) and (other.y <= (self.y + self.height)):
return True
else:
return False
else:
return False
def getHitDirection(self, other):
result = [0,0]
#everything is from self's perspective
dTop = abs(self.y - (other.y + other.height))
dBot = abs((self.y + self.height) - other.y)
dRight = abs((self.x + self.width) - other.x)
dLeft = abs(self.x - (other.x + other.width))
if((dTop <= dRight) and (dTop <= dLeft) and (dTop < dBot)):
#top
#check for corner collision
if(dTop == dRight): #right corner collision
result[0] = 1
elif(dTop == dLeft): #left corner collision
result[0] = -1
result[1] = -1
elif((dBot <= dRight) and (dBot <= dLeft)):
#bottom
if(dBot == dRight): #right corner collision
result[0] = 1
elif(dBot == dLeft): #left corner collision
result[0] = -1
result[1] = 1
elif(dRight < dLeft):
#right
result[0] = 1
else:
#left
result[0] = -1
return result
#You would reduce HP here too.
def handleBoxHit(self, other, direction):
#made the acceleration of the ball 5% only after it this a paddle
accel = 1.05
boxSound = pygame.mixer.Sound("paddleSound.ogg")
boxSound.play()
if(direction[0] == -1):
self.x = other.x + other.width
self.vx *= -accel
elif(direction[0] == 1):
self.x = other.x - self.width
self.vx *= -accel
if(direction[1] == -1):
self.y = other.y + other.height
self.vy *= -accel
elif(direction[1] == 1):
self.y = other.y - self.height
self.vy *= -accel
#self.color = (random.randint(0,255), random.randint(0,255), random.randint(0,255)) #just add this to Colors
def runBoxCollision(self, other):
dir = [0,0]
#only runs box collision for ball vs paddle, no paddle vs paddle
if(self.boxType=="ball"):
if(self.checkBoxCollision(other)):
dir = self.getHitDirection(other)
self.handleBoxHit(other, dir)
if(other.boxType == "brick"):
other.health -= 1
def update(self, drawDest, s_width, s_height, boxList):
self.moveIt()
for otherBox in boxList:
if(self != otherBox):
self.runBoxCollision(otherBox)
self.handleBoundHit(self.checkBoundHit(s_width, s_height), s_width, s_height)
self.drawIt(drawDest)