-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame_Movement_Clean_True.py
287 lines (240 loc) · 10.4 KB
/
Game_Movement_Clean_True.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import pygame
pygame.init()
win = pygame.display.set_mode((500, 480))
pygame.display.set_caption("Game Movement")
# Loading images
walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'), pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'), pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'), pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
bg = pygame.image.load('bg.jpg') # Background
char = pygame.image.load('standing.png') # character not moving or jumping
clock = pygame.time.Clock()
bulletSound = pygame.mixer.Sound('bullet.wav')
hitSound = pygame.mixer.Sound('hit.wav')
music = pygame.mixer.music.load('music.mp3')
pygame.mixer.music.set_volume(0.05)
pygame.mixer.music.play(-1) # the "-1" will continuously play the music
score = 0 # keeps track of how many bullets hit the enemy
class player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.left = False
self.right = False
self.walkCount = 0
self.isJump = False
self.jumpCount = 10
self.standing = True
self.hitbox = (self.x + 17, self.y + 11, 29, 52)
def draw(self, win):
if self.walkCount + 1 >= 27: #27
self.walkCount = 0
if not(self.standing):
if self.left:
win.blit(walkLeft[self.walkCount// 3], (self.x,self.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount// 3], (self.x,self.y)) # "//" divides and removes the remainder
self.walkCount += 1
else: #if standing
if self.right:
win.blit(walkRight[0], (self.x, self.y)) # Leaves the player looking right
elif self.left:
win.blit(walkLeft[0], (self.x, self.y))
else:
win.blit(char, (self.x, self.y))
self.hitbox = (self.x + 17, self.y + 11, 29, 52)
#pygame.draw.rect(win, (255,0,0), self.hitbox, 2)
def hit(self):
self.x = 60
self.y = 410
self.walkCount = 0
font1 = pygame.font.SysFont('comicsans', 100)
text = font1.render('-5', 1, (255,0,0))
win.blit(text, (250 - (text.get_width() / 2), 200))
pygame.display.update()
i = 0
while i < 100:
pygame.time.delay(10)
i += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
i = 101
pygame.quit()
class projectile(object):
def __init__(self, x , y, radius, color, facing):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.facing= facing # facing is either 1 or -1 to determine if the bullet goes left or right
self.vel = 8 * facing
def draw(self, win):
pygame.draw.circle(win, self.color, (self.x, self.y), self.radius)
class enemy(object):
walkRight = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'), pygame.image.load('R4E.png'), pygame.image.load('R5E.png'), pygame.image.load('R6E.png'), pygame.image.load('R7E.png'), pygame.image.load('R8E.png'), pygame.image.load('R9E.png'), pygame.image.load('R10E.png'), pygame.image.load('R11E.png')]
walkLeft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'), pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'), pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png'), pygame.image.load('L10E.png'), pygame.image.load('L11E.png')]
def __init__(self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.path = [self.x, self.end]
self.walkCount = 0
self.vel = 3
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
self.health = 9 #not 10 because of the loop reading order since if we count from 10 to 0 there are 11 elements
self.visible = True
def draw(self, win):
self.move()
if self.visible:
if self.walkCount >= 33: #33
self.walkCount = 0
if self.vel > 0:
win.blit(self.walkRight[self.walkCount // 3], (self.x,self.y))
self.walkCount += 1
else:
win.blit(self.walkLeft[self.walkCount //3], (self.x, self.y))
self.walkCount += 1
pygame.draw.rect(win, (255,0,0), (self.hitbox[0], self.hitbox[1] - 20, 50,10))
pygame.draw.rect(win, (0,128,0), (self.hitbox[0], self.hitbox[1] - 20, (5 * (self.health + 1)),10)) # Width of the green rectange is lowering
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
#pygame.draw.rect(win, (255,0,0), self.hitbox, 2)
def move(self):
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
def hit(self):
print("hit")
if self.health > 0:
self.health -= 1
else:
self.visible = False
#class platforms(object):
#def __init__(self, x, y, width, height):
#self.x = x
#self.y = y
#self.width = width
#self.height = height
#self.hitbox = (self.x, self.y, self.width, self.height)
#self.landing = False
#def draw(self, win):
#pygame.draw.rect(win, (150,0,0), (self.x, self.y, self.width, self.height))
#if self.landing == True:
#man.isJump = False
# pygame.draw.rect(win, (100,110,110), self.hitbox)
#def collision(self):
#if man.x < platform1.hitbox[0] + platform1.hitbox[2] and man.x + man.width > platform1.hitbox[0]:
#if man.y + height == platform1.hitbox[1]:
#self.landing = True
#else:
#self.landing = False
def redrawGameWindow():
global walkCount #defines the variable in the whole code
win.blit(bg, (0,0))
text = font.render("Score: " + str(score), 1, (0,0,0))
win.blit(text, (380,10))
man.draw(win) # Player
goblin.draw(win)
#platform1.draw(win)
for bullet in bullets:
bullet.draw(win)
pygame.display.update()
#Main Loop
font = pygame.font.SysFont('comicsans', 30, True) #(font, size, bold, italicized)
man = player(300, 410, 64, 64) # Character Specs (x,y,width,height)
goblin = enemy(100, 410, 64, 64, 450)
#platform1 = platforms(100, 360 + 64, 80, 10)
shootCount = 0
bullets = []
run = True
while run:
clock.tick(27) #27
if shootCount > 0:
shootCount += 1
if shootCount > 4:
shootCount = 0
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for bullet in bullets:
if goblin.visible:
if bullet.y - bullet.radius < goblin.hitbox[1] + goblin.hitbox[3] and bullet.y + bullet.radius > goblin.hitbox[1]:
if bullet.x + bullet.radius > goblin.hitbox[0] and bullet.x - bullet.radius < goblin.hitbox[0] + goblin.hitbox[2]:
goblin.hit()
hitSound.play()
score += 1
bullets.pop(bullets.index(bullet)) # remove the bullet
if bullet.x < 500 and bullet.x > 0:
bullet.x += bullet.vel
else:
bullets.pop(bullets.index(bullet)) # remove the bullet
if goblin.visible:
if man.hitbox[0] <= goblin.hitbox[0] + goblin.hitbox[2] and man.hitbox[0] + man.hitbox[2] >= goblin.hitbox[0]:
if man.hitbox[1] >= goblin.hitbox[1]:
man.hit()
score -= 5
#if man.x < platform1.hitbox[0] + platform1.hitbox[2] and man.x + man.width > platform1.hitbox[0]:
#if man.y + man.height == platform1.hitbox[1]:
# man.isJump = False
#man.y = platform1.hitbox[1]
# print("wooop")
#if keys[pygame.K_UP]:
# man.isJump = True */
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE] and shootCount == 0:
bulletSound.play()
if man.left:
facing = -1
else:
facing = 1
if len(bullets) < 50:
bullets.append(projectile(round(man.x + man.width // 2), round(man.y + man.height // 2), 6, (0,0,0), facing))
shootCount = 1
if keys[pygame.K_LEFT] and man.x >= man.vel:
man.x -= man.vel
man.left = True
man.right = False
man.standing = False
elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel:
man.x += man.vel
man.left = False
man.right = True
man.standing = False
else:
man.standing = True
man.walkCount = 0
if not(man.isJump):
# if keys[pygame.K_UP] and y > 0:
# y -= vel
# if keys[pygame.K_DOWN] and y < 440:
# y += vel
if keys[pygame.K_UP]:
man.isJump = True
man.right = False
man.left = False
man.walkCount = 0
else: #If jumping
if man.jumpCount >= -10:
neg = 1
if man.jumpCount < 0:
neg = -1
man.y -= (man.jumpCount ** 2) * 0.5* neg
man.jumpCount -= 1
else:
man.isJump = False
man.jumpCount = 10
redrawGameWindow()
pygame.quit()