SDL init, draw character in 2D map, no rotation
This commit is contained in:
commit
bfb038b036
74
raycaster.py
Executable file
74
raycaster.py
Executable file
@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# RAYCASTER
|
||||
#
|
||||
# pip install pysdl2 pysdl2-dll
|
||||
|
||||
import sys
|
||||
import sdl2.ext
|
||||
|
||||
WIN_WIDTH = 640
|
||||
WIN_HEIGHT = 480
|
||||
DUNGEON_WIDTH = WIN_WIDTH
|
||||
DUNGEON_HEIGHT = WIN_HEIGHT
|
||||
PLAYER_SPEED = 10
|
||||
|
||||
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
|
||||
self.player_position = {"x": WIN_WIDTH/2, "y": WIN_HEIGHT/2, "r": 0}
|
||||
|
||||
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:
|
||||
if event.key.keysym.sym == sdl2.SDLK_UP:
|
||||
self.player_position["y"] = int(self.player_position["y"] - PLAYER_SPEED)
|
||||
elif event.key.keysym.sym == sdl2.SDLK_DOWN:
|
||||
self.player_position["y"] = int(self.player_position["y"] + PLAYER_SPEED)
|
||||
if event.key.keysym.sym == sdl2.SDLK_LEFT:
|
||||
self.player_position["x"] = int(self.player_position["x"] - PLAYER_SPEED)
|
||||
elif event.key.keysym.sym == sdl2.SDLK_RIGHT:
|
||||
self.player_position["x"] = int(self.player_position["x"] + PLAYER_SPEED)
|
||||
# Limit position
|
||||
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
|
||||
|
||||
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):
|
||||
#sdl2.ext.draw.line(self.surface, sdl2.ext.Color(255,0,0,255), (0, 0, self.player_position["x"], self.player_position["y"]))
|
||||
sdl2.ext.draw.fill(self.surface, sdl2.ext.Color(0,255,0,255), (self.player_position["x"] - 4, self.player_position["y"] - 4, 4, 4))
|
||||
return
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main = Main()
|
||||
main.run()
|
||||
except KeyboardInterrupt:
|
||||
exit(0)
|
Loading…
Reference in New Issue
Block a user