2022-12-31 09:01:49 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# RAYCASTER
|
2022-12-31 09:53:18 +01:00
|
|
|
# Inspired by https://www.youtube.com/watch?v=gYRrGTC7GtA
|
2022-12-31 09:01:49 +01:00
|
|
|
#
|
|
|
|
# pip install pysdl2 pysdl2-dll
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import sdl2.ext
|
2022-12-31 09:53:18 +01:00
|
|
|
import math
|
2022-12-31 09:01:49 +01:00
|
|
|
|
|
|
|
WIN_WIDTH = 640
|
|
|
|
WIN_HEIGHT = 480
|
|
|
|
DUNGEON_WIDTH = WIN_WIDTH
|
|
|
|
DUNGEON_HEIGHT = WIN_HEIGHT
|
|
|
|
PLAYER_SPEED = 10
|
2022-12-31 09:53:18 +01:00
|
|
|
RAY_LENGTH = 100
|
2022-12-31 09:01:49 +01:00
|
|
|
|
|
|
|
class Main:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
# Graphics
|
|
|
|
sdl2.ext.init()
|
|
|
|
self.window = sdl2.ext.Window("Raycaster", size=(WIN_WIDTH, WIN_HEIGHT))
|
|
|
|
self.window.show()
|
|
|
|
self.surface = self.window.get_surface()
|
|
|
|
|
|
|
|
# Player
|
2022-12-31 09:53:18 +01:00
|
|
|
self.player_position = {"x": int(DUNGEON_WIDTH/2), "y": int(DUNGEON_HEIGHT/2), "r": 0} # r is rotation in radiants
|
2022-12-31 09:01:49 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
running = True
|
|
|
|
while running:
|
|
|
|
events = sdl2.ext.get_events()
|
|
|
|
for event in events:
|
|
|
|
if event.type == sdl2.SDL_QUIT:
|
|
|
|
running = False
|
|
|
|
break
|
|
|
|
if event.type == sdl2.SDL_KEYDOWN:
|
2022-12-31 09:53:18 +01:00
|
|
|
# Rotate player
|
2022-12-31 09:01:49 +01:00
|
|
|
if event.key.keysym.sym == sdl2.SDLK_LEFT:
|
2022-12-31 09:53:18 +01:00
|
|
|
self.player_position["r"] = self.player_position["r"] - 0.1
|
2022-12-31 09:01:49 +01:00
|
|
|
elif event.key.keysym.sym == sdl2.SDLK_RIGHT:
|
2022-12-31 09:53:18 +01:00
|
|
|
self.player_position["r"] = self.player_position["r"] + 0.1
|
|
|
|
|
|
|
|
# Compute deltax and deltay based on player direction
|
|
|
|
player_delta_x = math.cos(self.player_position["r"]) * PLAYER_SPEED
|
|
|
|
player_delta_y = math.sin(self.player_position["r"]) * PLAYER_SPEED
|
|
|
|
|
|
|
|
# Move player based on its direction
|
|
|
|
if event.key.keysym.sym == sdl2.SDLK_UP:
|
|
|
|
self.player_position["y"] = int(self.player_position["y"] + player_delta_y)
|
|
|
|
self.player_position["x"] = int(self.player_position["x"] + player_delta_x)
|
|
|
|
elif event.key.keysym.sym == sdl2.SDLK_DOWN:
|
|
|
|
self.player_position["y"] = int(self.player_position["y"] - player_delta_y)
|
|
|
|
self.player_position["x"] = int(self.player_position["x"] - player_delta_x)
|
|
|
|
|
|
|
|
# Limit position into dungeon bounds
|
2022-12-31 09:01:49 +01:00
|
|
|
if self.player_position["x"] < 0:
|
|
|
|
self.player_position["x"] = 0
|
|
|
|
if self.player_position["x"] > DUNGEON_WIDTH:
|
|
|
|
self.player_position["x"] = DUNGEON_WIDTH
|
|
|
|
if self.player_position["y"] < 0:
|
|
|
|
self.player_position["y"] = 0
|
|
|
|
if self.player_position["y"] > DUNGEON_HEIGHT:
|
|
|
|
self.player_position["y"] = DUNGEON_HEIGHT
|
2022-12-31 09:53:18 +01:00
|
|
|
if self.player_position["r"] > 2*math.pi:
|
|
|
|
self.player_position["r"] = 0
|
|
|
|
if self.player_position["r"] < 0:
|
|
|
|
self.player_position["r"] = 2*math.pi
|
|
|
|
print(self.player_position)
|
2022-12-31 09:01:49 +01:00
|
|
|
|
|
|
|
sdl2.ext.draw.fill(self.surface, sdl2.ext.Color(0,0,0,0)) # Clears screen
|
|
|
|
self.draw()
|
|
|
|
self.window.refresh()
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def draw(self):
|
2022-12-31 09:53:18 +01:00
|
|
|
# Player in 2D map
|
|
|
|
sdl2.ext.draw.fill(self.surface, sdl2.ext.Color(0,255,0,255), (self.player_position["x"] - 2, self.player_position["y"] - 2, 4, 4))
|
|
|
|
# Player line of sight in 2D map
|
|
|
|
ray = {
|
|
|
|
"x": int(self.player_position["x"] + math.cos(self.player_position["r"]) * 50), # deltaX + playerX
|
|
|
|
"y": int(self.player_position["y"] + math.sin(self.player_position["r"]) * 50) # deltaY + playerY
|
|
|
|
}
|
|
|
|
print(self.player_position)
|
|
|
|
sdl2.ext.draw.line(self.surface, sdl2.ext.Color(255,0,0,255), (self.player_position["x"], self.player_position["y"], ray["x"], ray["y"]))
|
2022-12-31 09:01:49 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
main = Main()
|
|
|
|
main.run()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
exit(0)
|