-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
229 lines (182 loc) · 9.5 KB
/
main.py
File metadata and controls
229 lines (182 loc) · 9.5 KB
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
import pygame
import math
import pygame_shaders
import random
from scripts.player import Player
from scripts.bullet import Bullet
from scripts.enemy import Enemy
class Game:
def __init__(self, width, height):
self.width = width
self.height = height
self.global_time = 0
pygame.init()
self.display = pygame.display.set_mode((self.width, self.height))
pygame.display.set_caption(f"LIT BY TORCHLIGHT")
#self.display = pygame.Surface((self.width, self.height))
#self.display.set_colorkey((0, 0, 0))
#self.shader = pygame_shaders.Shader(size=(self.width, self.height), display=(self.width, self.height),
# pos=(0, 0), vertex_path="shaders/vertex.glsl",
# fragment_path="shaders/default_frag.glsl", target_texture=self.display)
self.clock = pygame.time.Clock()
self.map1 = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 1, 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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
self.enemy_kills = 0
self.enemy = Enemy(128, 128)
self.enemy2 = Enemy(200, 128)
self.enemy_rects = []
self.collide_indexers = [[0] * len(self.map1[0])] * len(self.map1)
self.imgs = [[0] * len(self.map1[0])] * len(self.map1)
self.img = pygame.image.load("assets/images/wall.png").convert()
self.img_painting = pygame.image.load("assets/images/door.png").convert()
self.game_over = False
self.colliders = []
self.door = []
self.door_indexes = [[0] * len(self.map1[0])] * len(self.map1)
self.font = pygame.font.Font("assets/font.ttf", 32)
self.text = self.font.render("Goal: Kill all enemies", False, (255, 255, 255))
i = 0
for y, row in enumerate(self.map1):
for x, col in enumerate(row):
if self.map1[y][x] == 1:
self.colliders.append(pygame.Rect(x * 32, y * 32, 32, 32))
self.collide_indexers[y][x] = pygame.Rect(x * 32, y * 32, 32, 32)
self.imgs[y][x] = self.img
if self.map1[y][x] == 2:
self.door.append(pygame.Rect(x * 32, y * 32, 32, 32))
self.door_indexes[y][x] = pygame.Rect(x * 32, y * 32, 16, 16)
if self.map1[y][x] == 9:
self.enemy_rects.append(Enemy(x * 32, y * 32))
self.map1[y][x] = 0
self.player = Player(128, 128)
self.torch = 100000
pygame.mixer.music.set_volume(3)
pygame.mixer.music.load("assets/sounds/disturb.ogg")
pygame.mixer.music.play(-1)
self.shot_sound = pygame.mixer.Sound("assets/sounds/shot.wav")
self.click = pygame.mixer.Sound("assets/sounds/click.wav")
self.footstep1 = pygame.mixer.Sound("assets/sounds/footstep1.wav")
self.footstep2 = pygame.mixer.Sound("assets/sounds/footsetp2.wav")
self.click.set_volume(0.05)
#images
self.floor = pygame.image.load("assets/images/floor.png").convert()
self.bullet = pygame.image.load("assets/images/bullet.png").convert_alpha()
self.bullets = []
self.firing = False
self.shot = 0
def main(self):
rand = random.randrange(200, 300)
pygame.mouse.set_visible(False)
forward = True
running = True
while running:
self.global_time += 1
#pygame_shaders.clear((0, 0, 0)) #Fill with the color you would like in the background
self.display.fill((0, 0, 0)) #Fill with the color you set in the colorkey
# for y, row in enumerate(self.map1):
# for x, col in enumerate(row):
# if self.map1[y][x] == 1:
# pygame.draw.rect(self.display, (255, 255, 255), (x * 32, y * 32, 32, 32))
# else:
# pygame.draw.rect(self.display, (100, 100, 100), (x * 32, y * 32, 32, 32))
self.player.draw(self)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if self.player.weapon == self.player.weapon_loaded:
self.firing = True
if event.button == 3:
self.player.weapon = self.player.weapon_loaded
if event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
self.firing = False
self.shot = 0
if event.button == 3:
self.player.weapon = self.player.weapon_idle
if self.firing:
if self.shot <= 0:
self.shot_sound.play()
self.player.shooting = 1
self.firing = True
for i in range(1):
self.player.camera[0] += random.random() / 40
self.player.camera[1] += random.random() / 40
self.shot = 5
else:
self.shot -= 1
keys = pygame.key.get_pressed()
self.player.movement = [0, 0]
self.player.moving = False
if keys[pygame.K_w]:
forward = True
self.player.x += math.sin(self.player.angle - self.player.fov) * ((7 * self.player.sprinting) + 1)
self.player.y += math.cos(self.player.angle - self.player.fov) * ((7 * self.player.sprinting)+ 1)
self.player.moving = True
if keys[pygame.K_s]:
forward = False
self.player.x -= math.sin(self.player.angle - self.player.fov) * ((7 * self.player.sprinting)+ 1)
self.player.y -= math.cos(self.player.angle - self.player.fov) * ((7 * self.player.sprinting)+ 1)
self.player.moving = True
if keys[pygame.K_SPACE] and self.game_over:
Game(1200, 800).main()
col = int(self.player.x // 32)
row = int(self.player.y // 32)
# player hits the wall (collision detection)
if self.map1[row][col] == 1:
if forward:
self.player.x -= math.sin(self.player.angle - self.player.fov) * 5
self.player.y -= math.cos(self.player.angle - self.player.fov) * 5
else:
self.player.x += math.sin(self.player.angle - self.player.fov) * 5
self.player.y += math.cos(self.player.angle - self.player.fov) * 5
if keys[pygame.K_t]:
self.torch -= 100
#print(self.player.angle)
#self.player.sprinting = False
if pygame.mouse.get_focused():
difference = pygame.mouse.get_pos()[0] - 600
differencey = pygame.mouse.get_pos()[1] - 500
pygame.mouse.set_pos((600, 500))
self.player.angle += difference * 0.01
self.player.y_off += differencey * 0.01
if self.global_time % 14 == 0:
rand = random.randrange(300, 301)
if self.global_time < 100:
self.display.blit(self.text, (400, 10))
if self.enemy_kills == 10:
self.game_over = self.font.render("CONGRATS! YOU HAVE WON!", False, (255, 255, 255))
self.display.blit(self.game_over, (300, 400))
if self.game_over:
if self.enemy_kills != 10:
self.player.camera[0] += random.random() / 40
self.player.camera[1] += random.random() / 40
self.game_over = self.font.render("GAME OVER! PRESS SPACE TO RESTART", False, (255, 255, 255))
self.display.blit(self.game_over, (150, 400))
#self.shader.send("random", [rand])
#self.shader.render(self.display) #Render the display onto the OpenGL display with the shaders!
pygame.display.flip()
self.clock.tick(30)
Game(1200, 800).main()