-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.py
236 lines (212 loc) · 9.16 KB
/
main.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
import pygame
from pygame.locals import *
import math
import worldManager
import time
worldMap =[
[8,8,8,8,8,8,8,8,8,8,8,4,4,6,4,4,6,4,6,4,4,4,6,4],
[8,0,0,0,0,0,0,0,0,0,8,4,0,0,0,0,0,0,0,0,0,0,0,4],
[8,0,3,3,0,0,0,0,0,8,8,4,0,0,0,0,0,0,0,0,0,0,0,6],
[8,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6],
[8,0,3,3,0,0,0,0,0,8,8,4,0,0,0,0,0,0,0,0,0,0,0,4],
[8,0,0,0,0,0,0,0,0,0,8,4,0,0,0,0,0,6,6,6,0,6,4,6],
[8,8,8,8,0,8,8,8,8,8,8,4,4,4,4,4,4,6,0,0,0,0,0,6],
[7,7,7,7,0,7,7,7,7,0,8,0,8,0,8,0,8,4,0,4,0,6,0,6],
[7,7,0,0,0,0,0,0,7,8,0,8,0,8,0,8,8,6,0,0,0,0,0,6],
[7,0,0,0,0,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2],
# [2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
#];
sprite_positions=[
(20.5, 11.5, 2), #green light in front of playerstart
#green lights in every room
(18.5,4.5, 2),
(10.0,4.5, 2),
(10.0,12.5,2),
(3.5, 6.5, 2),
(3.5, 20.5,2),
(3.5, 14.5,2),
(14.5,20.5,2),
#row of pillars in front of wall: fisheye test
(18.5, 10.5, 1),
(18.5, 11.5, 1),
(18.5, 12.5, 1),
#some barrels around the map
(21.5, 1.5, 0),
(15.5, 1.5, 0),
(16.0, 1.8, 0),
(16.2, 1.2, 0),
(3.5, 2.5, 0),
(9.5, 15.5, 0),
(10.0, 15.1,0),
(10.5, 15.8,0),
]
def load_image(image, darken, colorKey = None):
ret = []
if colorKey is not None:
image.set_colorkey(colorKey)
if darken:
image.set_alpha(127)
for i in range(image.get_width()):
s = pygame.Surface((1, image.get_height())).convert()
#s.fill((0,0,0))
s.blit(image, (- i, 0))
if colorKey is not None:
s.set_colorkey(colorKey)
ret.append(s)
return ret
def main():
clock = pygame.time.Clock()
oldTime = 0. #time of previous frame
size = w, h = 640,480
pygame.init()
window = pygame.display.set_mode(size)
pygame.display.set_caption("Raycaster")
screen = pygame.display.get_surface()
#pixScreen = pygame.surfarray.pixels2d(screen)
pygame.mouse.set_visible(False)
f = pygame.font.SysFont(pygame.font.get_default_font(), 20)
wm = worldManager.WorldManager(worldMap,sprite_positions, 22, 11.5, -1, 0, 0, .66)
weapons = [Weapon("fist"),
Weapon("pistol"),
Weapon("shotgun"),
Weapon("dbshotgun"),
Weapon("chaingun"),
Weapon("plasma"),
Weapon("rocket"),
Weapon("bfg"),
Weapon("chainsaw")
]
weapon_numbers = [K_1,K_2,K_3,K_4,K_5,K_6,K_7,K_8,K_0]
weapon = weapons[0]
t = 0
while(True):
clock.tick(60)
wm.draw(screen)
# timing for input and FPS counter
frameTime = float(clock.get_time()) / 1000.0 # frameTime is the time this frame has taken, in seconds
t+=frameTime
text = f.render(str(clock.get_fps()), False, (255, 255, 0))
screen.blit(text, text.get_rect(), text.get_rect())
weapon.draw(screen, t)
pygame.display.flip()
# speed modifiers
moveSpeed = frameTime * 5.0 # the constant value is in squares / second
rotSpeed = frameTime * 3.0 # the constant value is in radians / second
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
return
elif event.key == K_SPACE:
#shoot
weapon.play()
elif event.key in weapon_numbers:
weapon.stop()
weapon = weapons[weapon_numbers.index(event.key)]
elif event.type == KEYUP:
if event.key == K_SPACE:
weapon.stop()
else:
pass
keys = pygame.key.get_pressed()
if keys[K_UP]:
# move forward if no wall in front of you
moveX = wm.camera.x + wm.camera.dirx * moveSpeed
if(worldMap[int(moveX)][int(wm.camera.y)]==0 and worldMap[int(moveX + 0.1)][int(wm.camera.y)]==0):wm.camera.x += wm.camera.dirx * moveSpeed
moveY = wm.camera.y + wm.camera.diry * moveSpeed
if(worldMap[int(wm.camera.x)][int(moveY)]==0 and worldMap[int(wm.camera.x)][int(moveY + 0.1)]==0):wm.camera.y += wm.camera.diry * moveSpeed
if keys[K_DOWN]:
# move backwards if no wall behind you
if(worldMap[int(wm.camera.x - wm.camera.dirx * moveSpeed)][int(wm.camera.y)] == 0):wm.camera.x -= wm.camera.dirx * moveSpeed
if(worldMap[int(wm.camera.x)][int(wm.camera.y - wm.camera.diry * moveSpeed)] == 0):wm.camera.y -= wm.camera.diry * moveSpeed
if (keys[K_RIGHT] and not keys[K_DOWN]) or (keys[K_LEFT] and keys[K_DOWN]):
# rotate to the right
# both camera direction and camera plane must be rotated
oldDirX = wm.camera.dirx
wm.camera.dirx = wm.camera.dirx * math.cos(- rotSpeed) - wm.camera.diry * math.sin(- rotSpeed)
wm.camera.diry = oldDirX * math.sin(- rotSpeed) + wm.camera.diry * math.cos(- rotSpeed)
oldPlaneX = wm.camera.planex
wm.camera.planex = wm.camera.planex * math.cos(- rotSpeed) - wm.camera.planey * math.sin(- rotSpeed)
wm.camera.planey = oldPlaneX * math.sin(- rotSpeed) + wm.camera.planey * math.cos(- rotSpeed)
if (keys[K_LEFT] and not keys[K_DOWN]) or (keys[K_RIGHT] and keys[K_DOWN]):
# rotate to the left
# both camera direction and camera plane must be rotated
oldDirX = wm.camera.dirx
wm.camera.dirx = wm.camera.dirx * math.cos(rotSpeed) - wm.camera.diry * math.sin(rotSpeed)
wm.camera.diry = oldDirX * math.sin(rotSpeed) + wm.camera.diry * math.cos(rotSpeed)
oldPlaneX = wm.camera.planex
wm.camera.planex = wm.camera.planex * math.cos(rotSpeed) - wm.camera.planey * math.sin(rotSpeed)
wm.camera.planey = oldPlaneX * math.sin(rotSpeed) + wm.camera.planey * math.cos(rotSpeed)
fps = 8
class Weapon(object):
def __init__(self, weaponName="shotgun", frameCount = 5):
self.images = []
self.loop = False
self.playing = False
self.frame = 0
self.oldTime = 0
for i in range(frameCount):
img = pygame.image.load("resources/weapons/%s%s.bmp" % (weaponName, i+1)).convert()
img = pygame.transform.scale2x(img)
img = pygame.transform.scale2x(img)
img.set_colorkey(img.get_at((0,0)))
self.images.append(img)
def play(self):
self.playing = True
self.loop = True
def stop(self):
self.playing = False
self.loop = False
def draw(self, surface, time):
if(self.playing or self.frame > 0):
if(time > self.oldTime + 1./fps):
self.frame = (self.frame+1) % len(self.images)
if self.frame == 0:
if self.loop:
self.frame = 1
else:
self.playing = False
self.oldTime = time
img = self.images[self.frame]
surface.blit(img, (surface.get_width()/2 - img.get_width()/2, surface.get_height()-img.get_height()))
if __name__ == '__main__':
main()