This repository has been archived by the owner on Jun 30, 2019. It is now read-only.
/
demo_raycasting_3D.py
154 lines (137 loc) · 7.6 KB
/
demo_raycasting_3D.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
import pygame
from pygame.locals import *
from engine import load_image, Camera, WorldManager
import math
class View:
def __init__(self, wm):
self.wm = wm
def move(self, keys, dt, worldMap):
# speed modifiers
moveSpeed = dt * 6.0 # the constant value is in squares / second
rotSpeed = dt * 2.0 # the constant value is in radians / second
if keys[K_UP]:
# move forward if no wall in front of you
moveX = self.wm.camera.x + self.wm.camera.dirx * moveSpeed
if worldMap[int(moveX)][int(self.wm.camera.y)] == 0 and worldMap[int(moveX + 0.1)][int(self.wm.camera.y)] == 0:
self.wm.camera.x += self.wm.camera.dirx * moveSpeed
moveY = self.wm.camera.y + self.wm.camera.diry * moveSpeed
if worldMap[int(self.wm.camera.x)][int(moveY)] == 0 and worldMap[int(self.wm.camera.x)][int(moveY + 0.1)] == 0:
self.wm.camera.y += self.wm.camera.diry * moveSpeed
if keys[K_DOWN]:
# move backwards if no wall behind you
moveX = self.wm.camera.x + self.wm.camera.dirx * moveSpeed
if worldMap[int(moveX)][int(self.wm.camera.y)] == 0:
self.wm.camera.x -= self.wm.camera.dirx * moveSpeed
moveY = self.wm.camera.y + self.wm.camera.diry * moveSpeed
if worldMap[int(self.wm.camera.x)][int(moveY)] == 0:
self.wm.camera.y -= self.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 = self.wm.camera.dirx
self.wm.camera.dirx = self.wm.camera.dirx * math.cos(- rotSpeed) - self.wm.camera.diry * math.sin(- rotSpeed)
self.wm.camera.diry = oldDirX * math.sin(- rotSpeed) + self.wm.camera.diry * math.cos(- rotSpeed)
oldPlaneX = self.wm.camera.planex
self.wm.camera.planex = self.wm.camera.planex * math.cos(- rotSpeed) - self.wm.camera.planey * math.sin(- rotSpeed)
self.wm.camera.planey = oldPlaneX * math.sin(- rotSpeed) + self.wm.camera.planey * math.cos(- rotSpeed)
if (keys[K_RIGHT] and keys[K_DOWN]) or (keys[K_LEFT] and not keys[K_DOWN]):
# rotate to the left
# both camera direction and camera plane must be rotated
oldDirX = self.wm.camera.dirx
self.wm.camera.dirx = self.wm.camera.dirx * math.cos(rotSpeed) - self.wm.camera.diry * math.sin(rotSpeed)
self.wm.camera.diry = oldDirX * math.sin(rotSpeed) + self.wm.camera.diry * math.cos(rotSpeed)
oldPlaneX = self.wm.camera.planex
self.wm.camera.planex = self.wm.camera.planex * math.cos(rotSpeed) - self.wm.camera.planey * math.sin(rotSpeed)
self.wm.camera.planey = oldPlaneX * math.sin(rotSpeed) + self.wm.camera.planey * math.cos(rotSpeed)
class Demo:
def __init__(self, screen):
self.screen = screen
self.clock = pygame.time.Clock()
self.fps = 60
self.font = pygame.font.SysFont("arial", 12)
self.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,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(10.0, 4.5, 2, 0, 0., 0., 0),
(10.0, 12.5, 2, 0, 0., 0., 0),
(3.5, 6.5, 2, 0, 0., 0., 0),
(3.5, 20.5, 2, 0, 0., 0., 0),
(3.5, 14.5, 2, 0, 0., 0., 0),
(14.5, 20.5, 2, 0, 0., 0., 0),
# row of pillars in front of wall: fisheye test
(18.5, 10.5, 1, 0, 0., 0., 0),
(18.5, 11.5, 1, 0, 0., 0., 0),
(18.5, 12.5, 1, 0, 0., 0., 0)
]
sprites = [
load_image("pics/items/barrel.png", False, colorKey=(0,0,0)),
load_image("pics/items/pillar.png", False, colorKey=(0,0,0)),
load_image("pics/items/greenlight.png", False, colorKey=(0,0,0)),
load_image("pics/items/pinky_l.png", False, colorKey=(0,0,0)),
load_image("pics/items/pinky_r.png", False, colorKey=(0,0,0)),
]
images = [
load_image("pics/walls/eagle.png", False),
load_image("pics/walls/redbrick.png", False),
load_image("pics/walls/purplestone.png", False),
load_image("pics/walls/greystone.png", False),
load_image("pics/walls/bluestone.png", False),
load_image("pics/walls/mossy.png", False),
load_image("pics/walls/wood.png", False),
load_image("pics/walls/colorstone.png", False),
load_image("pics/walls/eagle.png", True),
load_image("pics/walls/redbrick.png", True),
load_image("pics/walls/purplestone.png", True),
load_image("pics/walls/greystone.png", True),
load_image("pics/walls/bluestone.png", True),
load_image("pics/walls/mossy.png", True),
load_image("pics/walls/wood.png", True),
load_image("pics/walls/colorstone.png", True),
]
self.wm = WorldManager(self.worldMap, x=22, y=11.5, dirx=-1, diry=0, planex=0, planey=0.66, sprites=sprites, images=images)
self.view = View(self.wm)
def run(self):
while 1:
self.clock.tick(self.fps)
frameTime = float(self.clock.get_time()) / 1000.0 # frameTime is the time this frame has taken, in seconds
self.screen.fill(0)
self.screen.blit(self.font.render(str(self.clock.get_fps()), 1, (180, 180, 255)), (0, 0))
self.wm.draw(self.screen, self.sprite_positions)
for event in pygame.event.get():
if event.type == QUIT:
return
keys = pygame.key.get_pressed()
self.view.move(keys, frameTime, self.worldMap)
pygame.display.flip()
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((640, 480))
demo = Demo(screen)
demo.run()