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 c40a76e

Browse files
Add files via upload
1 parent 303ee10 commit c40a76e

File tree

1 file changed

+236
-0
lines changed

1 file changed

+236
-0
lines changed

‎Game.py

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
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+
80+
def move_hero(self, userInput):
81+
if userInput[pygame.K_RIGHT] and self.x <= win_width - 62:
82+
self.x += self.velx
83+
self.face_right = True
84+
self.face_left = False
85+
elif userInput[pygame.K_LEFT] and self.x >= 0:
86+
self.x -= self.velx
87+
self.face_right = False
88+
self.face_left = True
89+
else:
90+
self.stepIndex = 0
91+
92+
def draw(self, win):
93+
if self.stepIndex >= 9:
94+
self.stepIndex = 0
95+
if self.face_left:
96+
win.blit(left[self.stepIndex], (self.x, self.y))
97+
self.stepIndex += 1
98+
if self.face_right:
99+
win.blit(right[self.stepIndex], (self.x, self.y))
100+
self.stepIndex += 1
101+
102+
def jump_motion(self, userInput):
103+
if userInput[pygame.K_SPACE] and self.jump is False:
104+
self.jump = True
105+
if self.jump:
106+
self.y -= self.vely*4
107+
self.vely -= 1
108+
if self.vely < -10:
109+
self.jump = False
110+
self.vely = 10
111+
112+
def direction(self):
113+
if self.face_right:
114+
return 1
115+
if self.face_left:
116+
return -1
117+
118+
def cooldown(self):
119+
if self.cool_down_count >= 20:
120+
self.cool_down_count = 0
121+
elif self.cool_down_count > 0:
122+
self.cool_down_count += 1
123+
124+
def shoot(self):
125+
self.cooldown()
126+
if (userInput[pygame.K_f] and self.cool_down_count == 0):
127+
bullet = Bullet(self.x, self.y, self.direction())
128+
self.bullets.append(bullet)
129+
self.cool_down_count = 1
130+
for bullet in self.bullets:
131+
bullet.move()
132+
if bullet.off_screen():
133+
self.bullets.remove(bullet)
134+
135+
136+
class Bullet:
137+
def __init__(self, x, y, direction):
138+
self.x = x + 15
139+
self.y = y + 25
140+
self.direction = direction
141+
142+
def draw_bullet(self):
143+
win.blit(bullet_img, (self.x, self.y))
144+
145+
def move(self):
146+
if self.direction == 1:
147+
self.x += 15
148+
if self.direction == -1:
149+
self.x -= 15
150+
151+
def off_screen(self):
152+
return not(self.x >= 0 and self.x <= win_width)
153+
154+
155+
class Enemy:
156+
def __init__(self, x, y, direction):
157+
self.x = x
158+
self.y = y
159+
self.direction = direction
160+
self.stepIndex = 0
161+
162+
def step(self):
163+
if self.stepIndex >= 33:
164+
self.stepIndex = 0
165+
166+
def draw(self, win):
167+
self.step()
168+
if self.direction == left:
169+
win.blit(left_enemy[self.stepIndex//3], (self.x, self.y))
170+
if self.direction == right:
171+
win.blit(right_enemy[self.stepIndex // 3], (self.x, self.y))
172+
self.stepIndex += 1
173+
174+
def move(self):
175+
if self.direction == left:
176+
self.x -= 3
177+
if self.direction == right:
178+
self.x += 3
179+
180+
def off_screen(self):
181+
return not(self.x >= -50 and self.x <= win_width + 50)
182+
183+
184+
# Draw Game
185+
def draw_game():
186+
win.fill((0, 0, 0))
187+
win.blit(background, (0,0))
188+
player.draw(win)
189+
for bullet in player.bullets:
190+
bullet.draw_bullet()
191+
for enemy in enemies:
192+
enemy.draw(win)
193+
pygame.time.delay(30)
194+
pygame.display.update()
195+
196+
# Instance of Hero-Class
197+
player = Hero(250, 290)
198+
199+
# Instance of Enemy-Class
200+
enemies = []
201+
202+
# Mainloop
203+
run = True
204+
while run:
205+
206+
# Quit Game
207+
for event in pygame.event.get():
208+
if event.type == pygame.QUIT:
209+
run = False
210+
211+
# Input
212+
userInput = pygame.key.get_pressed()
213+
214+
# Shoot
215+
player.shoot()
216+
217+
# Movement
218+
player.move_hero(userInput)
219+
player.jump_motion(userInput)
220+
221+
# Enemy
222+
if len(enemies) == 0:
223+
rand_nr = random.randint(0,1)
224+
if rand_nr == 1:
225+
enemy = Enemy(750, 300, left)
226+
enemies.append(enemy)
227+
if rand_nr == 0:
228+
enemy = Enemy(50, 300, right)
229+
enemies.append(enemy)
230+
for enemy in enemies:
231+
enemy.move()
232+
if enemy.off_screen():
233+
enemies.remove(enemy)
234+
235+
# Draw Game in Window
236+
draw_game()

0 commit comments

Comments
(0)

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