-
Notifications
You must be signed in to change notification settings - Fork 23
/
Part-09-Cooldown.py
158 lines (136 loc) · 4.74 KB
/
Part-09-Cooldown.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
import pygame
import os
# Init and Create Window (win)
pygame.init()
win_height = 400
win_width = 800
win = pygame.display.set_mode((win_width, win_height))
# Load and Size Images
stationary = pygame.image.load(os.path.join("Assets/Hero", "standing.png"))
left = [pygame.image.load(os.path.join("Assets/Hero", "L1.png")),
pygame.image.load(os.path.join("Assets/Hero", "L2.png")),
pygame.image.load(os.path.join("Assets/Hero", "L3.png")),
pygame.image.load(os.path.join("Assets/Hero", "L4.png")),
pygame.image.load(os.path.join("Assets/Hero", "L5.png")),
pygame.image.load(os.path.join("Assets/Hero", "L6.png")),
pygame.image.load(os.path.join("Assets/Hero", "L7.png")),
pygame.image.load(os.path.join("Assets/Hero", "L8.png")),
pygame.image.load(os.path.join("Assets/Hero", "L9.png"))
]
right =[pygame.image.load(os.path.join("Assets/Hero", "R1.png")),
pygame.image.load(os.path.join("Assets/Hero", "R2.png")),
pygame.image.load(os.path.join("Assets/Hero", "R3.png")),
pygame.image.load(os.path.join("Assets/Hero", "R4.png")),
pygame.image.load(os.path.join("Assets/Hero", "R5.png")),
pygame.image.load(os.path.join("Assets/Hero", "R6.png")),
pygame.image.load(os.path.join("Assets/Hero", "R7.png")),
pygame.image.load(os.path.join("Assets/Hero", "R8.png")),
pygame.image.load(os.path.join("Assets/Hero", "R9.png"))
]
bullet_img = pygame.transform.scale(pygame.image.load(os.path.join("Assets/Bullets", "light_bullet.png")), (10, 10))
background = pygame.transform.scale(pygame.image.load(os.path.join("Assets", "Background.png")), (win_width, win_height))
class Hero:
def __init__(self, x, y):
# Walk
self.x = x
self.y = y
self.velx = 10
self.vely = 10
self.face_right = True
self.face_left = False
self.stepIndex = 0
# Jump
self.jump = False
# Bullet
self.bullets = []
self.cool_down_count = 0
def move_hero(self, userInput):
if userInput[pygame.K_RIGHT] and self.x <= win_width - 62:
self.x += self.velx
self.face_right = True
self.face_left = False
elif userInput[pygame.K_LEFT] and self.x >= 0:
self.x -= self.velx
self.face_right = False
self.face_left = True
else:
self.stepIndex = 0
def draw(self, win):
if self.stepIndex >= 9:
self.stepIndex = 0
if self.face_left:
win.blit(left[self.stepIndex], (self.x, self.y))
self.stepIndex += 1
if self.face_right:
win.blit(right[self.stepIndex], (self.x, self.y))
self.stepIndex += 1
def jump_motion(self, userInput):
if userInput[pygame.K_SPACE] and self.jump is False:
self.jump = True
if self.jump:
self.y -= self.vely*4
self.vely -= 1
if self.vely < -10:
self.jump = False
self.vely = 10
def direction(self):
if self.face_right:
return 1
if self.face_left:
return -1
def cooldown(self):
if self.cool_down_count >= 20:
self.cool_down_count = 0
elif self.cool_down_count > 0:
self.cool_down_count += 1
def shoot(self):
self.cooldown()
if (userInput[pygame.K_f] and self.cool_down_count == 0):
bullet = Bullet(self.x, self.y, self.direction())
self.bullets.append(bullet)
self.cool_down_count = 1
for bullet in self.bullets:
bullet.move()
if bullet.off_screen():
self.bullets.remove(bullet)
class Bullet:
def __init__(self, x, y, direction):
self.x = x + 15
self.y = y + 25
self.direction = direction
def draw_bullet(self):
win.blit(bullet_img, (self.x, self.y))
def move(self):
if self.direction == 1:
self.x += 15
if self.direction == -1:
self.x -= 15
def off_screen(self):
return not(self.x >= 0 and self.x <= win_width)
# Draw Game
def draw_game():
win.fill((0, 0, 0))
win.blit(background, (0,0))
player.draw(win)
for bullet in player.bullets:
bullet.draw_bullet()
pygame.time.delay(30)
pygame.display.update()
# Instance of Hero-Class
player = Hero(250, 290)
# Mainloop
run = True
while run:
# Quit Game
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# Input
userInput = pygame.key.get_pressed()
# Shoot
player.shoot()
# Movement
player.move_hero(userInput)
player.jump_motion(userInput)
# Draw Game in Window
draw_game()