Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e8cc45a

Browse files
Create Part-11-Hitboxes.py
1 parent a193baf commit e8cc45a

File tree

1 file changed

+244
-0
lines changed

1 file changed

+244
-0
lines changed

‎Part-11-Hitboxes.py

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
import pygame
2+
import os
3+
import random
4+
5+
# Init and Create Window (win)
6+
pygame.init()
7+
win_height = 400
8+
win_width = 800
9+
win = pygame.display.set_mode((win_width, win_height))
10+
11+
# Load and Size Images
12+
# Hero (Player)
13+
left = [pygame.image.load(os.path.join("Hero", "L1.png")),
14+
pygame.image.load(os.path.join("Hero", "L2.png")),
15+
pygame.image.load(os.path.join("Hero", "L3.png")),
16+
pygame.image.load(os.path.join("Hero", "L4.png")),
17+
pygame.image.load(os.path.join("Hero", "L5.png")),
18+
pygame.image.load(os.path.join("Hero", "L6.png")),
19+
pygame.image.load(os.path.join("Hero", "L7.png")),
20+
pygame.image.load(os.path.join("Hero", "L8.png")),
21+
pygame.image.load(os.path.join("Hero", "L9.png"))
22+
]
23+
right =[pygame.image.load(os.path.join("Hero", "R1.png")),
24+
pygame.image.load(os.path.join("Hero", "R2.png")),
25+
pygame.image.load(os.path.join("Hero", "R3.png")),
26+
pygame.image.load(os.path.join("Hero", "R4.png")),
27+
pygame.image.load(os.path.join("Hero", "R5.png")),
28+
pygame.image.load(os.path.join("Hero", "R6.png")),
29+
pygame.image.load(os.path.join("Hero", "R7.png")),
30+
pygame.image.load(os.path.join("Hero", "R8.png")),
31+
pygame.image.load(os.path.join("Hero", "R9.png"))
32+
]
33+
# Enemy
34+
left_enemy = [pygame.image.load(os.path.join("Enemy", "L1E.png")),
35+
pygame.image.load(os.path.join("Enemy", "L2E.png")),
36+
pygame.image.load(os.path.join("Enemy", "L3E.png")),
37+
pygame.image.load(os.path.join("Enemy", "L4E.png")),
38+
pygame.image.load(os.path.join("Enemy", "L5E.png")),
39+
pygame.image.load(os.path.join("Enemy", "L6E.png")),
40+
pygame.image.load(os.path.join("Enemy", "L7E.png")),
41+
pygame.image.load(os.path.join("Enemy", "L8E.png")),
42+
pygame.image.load(os.path.join("Enemy", "L9P.png")),
43+
pygame.image.load(os.path.join("Enemy", "L10P.png")),
44+
pygame.image.load(os.path.join("Enemy", "L11P.png"))
45+
]
46+
right_enemy = [pygame.image.load(os.path.join("Enemy", "R1E.png")),
47+
pygame.image.load(os.path.join("Enemy", "R2E.png")),
48+
pygame.image.load(os.path.join("Enemy", "R3E.png")),
49+
pygame.image.load(os.path.join("Enemy", "R4E.png")),
50+
pygame.image.load(os.path.join("Enemy", "R5E.png")),
51+
pygame.image.load(os.path.join("Enemy", "R6E.png")),
52+
pygame.image.load(os.path.join("Enemy", "R7E.png")),
53+
pygame.image.load(os.path.join("Enemy", "R8E.png")),
54+
pygame.image.load(os.path.join("Enemy", "R9P.png")),
55+
pygame.image.load(os.path.join("Enemy", "R10P.png")),
56+
pygame.image.load(os.path.join("Enemy", "R11P.png"))
57+
]
58+
# Bullet
59+
bullet_img = pygame.transform.scale(pygame.image.load(os.path.join("Bullets", "bullet.png")), (10, 10))
60+
# Background
61+
background = pygame.transform.scale(pygame.image.load('desert_BG.png'), (win_width, win_height))
62+
63+
64+
class Hero:
65+
def __init__(self, x, y):
66+
# Walk
67+
self.x = x
68+
self.y = y
69+
self.velx = 10
70+
self.vely = 10
71+
self.face_right = True
72+
self.face_left = False
73+
self.stepIndex = 0
74+
# Jump
75+
self.jump = False
76+
# Bullet
77+
self.bullets = []
78+
self.cool_down_count = 0
79+
# Health
80+
self.hitbox = (self.x, self.y, 64, 64)
81+
82+
def move_hero(self, userInput):
83+
if userInput[pygame.K_RIGHT] and self.x <= win_width - 62:
84+
self.x += self.velx
85+
self.face_right = True
86+
self.face_left = False
87+
elif userInput[pygame.K_LEFT] and self.x >= 0:
88+
self.x -= self.velx
89+
self.face_right = False
90+
self.face_left = True
91+
else:
92+
self.stepIndex = 0
93+
94+
def draw(self, win):
95+
self.hitbox = (self.x + 15, self.y + 15, 30, 40)
96+
pygame.draw.rect(win, (0,0,0), self.hitbox, 1)
97+
if self.stepIndex >= 9:
98+
self.stepIndex = 0
99+
if self.face_left:
100+
win.blit(left[self.stepIndex], (self.x, self.y))
101+
self.stepIndex += 1
102+
if self.face_right:
103+
win.blit(right[self.stepIndex], (self.x, self.y))
104+
self.stepIndex += 1
105+
106+
def jump_motion(self, userInput):
107+
if userInput[pygame.K_SPACE] and self.jump is False:
108+
self.jump = True
109+
if self.jump:
110+
self.y -= self.vely*4
111+
self.vely -= 1
112+
if self.vely < -10:
113+
self.jump = False
114+
self.vely = 10
115+
116+
def direction(self):
117+
if self.face_right:
118+
return 1
119+
if self.face_left:
120+
return -1
121+
122+
def cooldown(self):
123+
if self.cool_down_count >= 20:
124+
self.cool_down_count = 0
125+
elif self.cool_down_count > 0:
126+
self.cool_down_count += 1
127+
128+
def shoot(self):
129+
self.cooldown()
130+
if (userInput[pygame.K_f] and self.cool_down_count == 0):
131+
bullet = Bullet(self.x, self.y, self.direction())
132+
self.bullets.append(bullet)
133+
self.cool_down_count = 1
134+
for bullet in self.bullets:
135+
bullet.move()
136+
if bullet.off_screen():
137+
self.bullets.remove(bullet)
138+
139+
140+
class Bullet:
141+
def __init__(self, x, y, direction):
142+
self.x = x + 15
143+
self.y = y + 25
144+
self.direction = direction
145+
146+
def draw_bullet(self):
147+
win.blit(bullet_img, (self.x, self.y))
148+
149+
def move(self):
150+
if self.direction == 1:
151+
self.x += 15
152+
if self.direction == -1:
153+
self.x -= 15
154+
155+
def off_screen(self):
156+
return not(self.x >= 0 and self.x <= win_width)
157+
158+
159+
class Enemy:
160+
def __init__(self, x, y, direction):
161+
self.x = x
162+
self.y = y
163+
self.direction = direction
164+
self.stepIndex = 0
165+
# Health
166+
self.hitbox = (self.x, self.y, 64, 64)
167+
168+
def step(self):
169+
if self.stepIndex >= 33:
170+
self.stepIndex = 0
171+
172+
def draw(self, win):
173+
self.hitbox = (self.x + 15, self.y + 15, 30, 40)
174+
pygame.draw.rect(win, (0, 0, 0), self.hitbox, 1)
175+
self.step()
176+
if self.direction == left:
177+
win.blit(left_enemy[self.stepIndex//3], (self.x, self.y))
178+
if self.direction == right:
179+
win.blit(right_enemy[self.stepIndex // 3], (self.x, self.y))
180+
self.stepIndex += 1
181+
182+
def move(self):
183+
if self.direction == left:
184+
self.x -= 3
185+
if self.direction == right:
186+
self.x += 3
187+
188+
def off_screen(self):
189+
return not(self.x >= -50 and self.x <= win_width + 50)
190+
191+
192+
# Draw Game
193+
def draw_game():
194+
win.fill((0, 0, 0))
195+
win.blit(background, (0,0))
196+
player.draw(win)
197+
for bullet in player.bullets:
198+
bullet.draw_bullet()
199+
for enemy in enemies:
200+
enemy.draw(win)
201+
pygame.time.delay(30)
202+
pygame.display.update()
203+
204+
# Instance of Hero-Class
205+
player = Hero(250, 290)
206+
207+
# Instance of Enemy-Class
208+
enemies = []
209+
210+
# Mainloop
211+
run = True
212+
while run:
213+
214+
# Quit Game
215+
for event in pygame.event.get():
216+
if event.type == pygame.QUIT:
217+
run = False
218+
219+
# Input
220+
userInput = pygame.key.get_pressed()
221+
222+
# Shoot
223+
player.shoot()
224+
225+
# Movement
226+
player.move_hero(userInput)
227+
player.jump_motion(userInput)
228+
229+
# Enemy
230+
if len(enemies) == 0:
231+
rand_nr = random.randint(0,1)
232+
if rand_nr == 1:
233+
enemy = Enemy(750, 300, left)
234+
enemies.append(enemy)
235+
if rand_nr == 0:
236+
enemy = Enemy(50, 300, right)
237+
enemies.append(enemy)
238+
for enemy in enemies:
239+
enemy.move()
240+
if enemy.off_screen():
241+
enemies.remove(enemy)
242+
243+
# Draw Game in Window
244+
draw_game()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /