Skip to content

Commit ab9b26e

Browse files
committed
How To Add Collision Feature - Python #PyGame Lesson 6
How To Add Collision Feature - Python #PyGame Lesson 6
1 parent cdcd06e commit ab9b26e

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

Python PyGame/main.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
clock = pygame.time.Clock()
3333

34-
score = 0 #new
34+
score = 0
3535

3636
class player(object):
3737
def __init__(self, x, y, width, height):
@@ -116,13 +116,13 @@ def __init__(self, x, y, width, height, end):
116116
self.walkCount = 0
117117
self.vel = 3
118118
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
119-
self.health = 10 #new
120-
self.visible = True #new
119+
self.health = 10
120+
self.visible = True
121121

122122
def draw(self, win):
123123
self.move()
124124

125-
if self.visible: # NEW
125+
if self.visible:
126126
if self.walkCount + 1 >= 33:
127127
self.walkCount = 0
128128

@@ -133,11 +133,12 @@ def draw(self, win):
133133
win.blit(self.walkLeft[self.walkCount // 3], (self.x, self.y))
134134
self.walkCount += 1
135135

136-
pygame.draw.rect(win, (255, 0, 0), (self.hitbox[0],
137-
self.hitbox[1] - 20, 50, 10)) # NEW
138-
pygame.draw.rect(win, (0, 128, 0),
139-
(self.hitbox[0], self.hitbox[1] - 20, 50 - (5 *
140-
(10 - self.health)), 10)) # NEW
136+
pygame.draw.rect(window, (255,0,0), (self.hitbox[0],
137+
self.hitbox[1]-20,50,10))
138+
139+
pygame.draw.rect(window,(0,128,0),
140+
(self.hitbox[0],self.hitbox[1]-20,50 - (5 *
141+
(10 - self.health)),10))
141142

142143
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
143144
#pygame.draw.rect(window, (255,0,0), self.hitbox, 2)
@@ -156,7 +157,7 @@ def move(self):
156157
self.vel = self.vel * -1
157158
self.walkCount = 0
158159

159-
def hit(self): #new
160+
def hit(self):
160161
if self.health > 0:
161162
self.health -= 1
162163
else:
@@ -166,16 +167,16 @@ def hit(self): #new
166167

167168
def redrawGameWindow():
168169
window.blit(bg, (0, 0))
169-
text = font.render('Score: ' + str(score), 1, (0, 0, 0)) #new
170-
window.blit(text, (350, 10)) #new
170+
text = font.render('Score:' + str(score), 1, (0, 0, 0))
171+
window.blit(text, (200,10))
171172
man.draw(window)
172173
goblin.draw(window)
173174
for bullet in bullets:
174175
bullet.draw(window)
175176
pygame.display.update()
176177

177178

178-
font = pygame.font.SysFont('comicsans', 30, True) #new
179+
font = pygame.font.SysFont('comicsans', 30, True)
179180
man = player(200, 410, 64, 64)
180181
goblin = enemy(100, 410, 64, 64, 300) #NEW
181182
shootLoop = 0
@@ -200,8 +201,8 @@ def redrawGameWindow():
200201
if bullet.x + bullet.radius > goblin.hitbox[0] and bullet.x - \
201202
bullet.radius < goblin.hitbox[0] + \
202203
goblin.hitbox[2]:
204+
score += 1
203205
goblin.hit()
204-
score += 1 #new
205206
bullets.pop(bullets.index(bullet))
206207

207208
if bullet.x < 500 and bullet.x > 0:

0 commit comments

Comments
 (0)