Collision detection
This commit is contained in:
parent
c1e6210c83
commit
37d4573778
@ -20,6 +20,7 @@ PLAYER_SPEED = 10
|
||||
PLAYER_ROTATION_SPEED = 0.17
|
||||
RAY_LENGTH = 100
|
||||
MAP_SCALE = 40
|
||||
COLLISION = True
|
||||
|
||||
MAP = [
|
||||
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
||||
@ -28,9 +29,9 @@ MAP = [
|
||||
1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1,
|
||||
1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1,
|
||||
1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1,
|
||||
1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1,
|
||||
1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1,
|
||||
1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1,
|
||||
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1,
|
||||
1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1,
|
||||
1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1,
|
||||
1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1,
|
||||
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1,
|
||||
@ -88,11 +89,9 @@ class Main:
|
||||
|
||||
# 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)
|
||||
self.movePlayerRelative(player_delta_x, player_delta_y)
|
||||
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)
|
||||
self.movePlayerRelative(-player_delta_x, -player_delta_y)
|
||||
|
||||
# Limit position into dungeon bounds
|
||||
if self.player_position["x"] < 0:
|
||||
@ -122,6 +121,25 @@ class Main:
|
||||
|
||||
return 0
|
||||
|
||||
def movePlayerRelative(self, player_delta_x, player_delta_y):
|
||||
# Prevent player from going into walls (X axis)
|
||||
newPlayerX = int(self.player_position["x"] + player_delta_x)
|
||||
mapX = int(newPlayerX / MAP_SCALE)
|
||||
mapY = int(self.player_position["y"] / MAP_SCALE)
|
||||
mapArrayPosition = mapY * MAP_SIZE + mapX
|
||||
if mapArrayPosition >= 0 and mapArrayPosition < MAP_SIZE*MAP_SIZE-1 and MAP[mapArrayPosition] == 0:
|
||||
# Move player (X)
|
||||
self.player_position["x"] = newPlayerX
|
||||
|
||||
# Prevent player from going into walls (Y axis)
|
||||
newPlayerY = int(self.player_position["y"] + player_delta_y)
|
||||
mapX = int(self.player_position["x"] / MAP_SCALE)
|
||||
mapY = int(newPlayerY / MAP_SCALE)
|
||||
mapArrayPosition = mapY * MAP_SIZE + mapX
|
||||
if mapArrayPosition >= 0 and mapArrayPosition < MAP_SIZE*MAP_SIZE-1 and MAP[mapArrayPosition] == 0:
|
||||
# Move player (Y)
|
||||
self.player_position["y"] = newPlayerY
|
||||
|
||||
def draw(self):
|
||||
sdl2.ext.draw.fill(self.mapSurface, sdl2.ext.Color(0,0,0,255)) # Clears map screen
|
||||
sdl2.ext.draw.fill(self.raycastSurface, sdl2.ext.Color(0,0,128,255), (0, 0, RAYCAST_WIN_WIDTH, RAYCAST_WIN_HEIGHT/2)) # Clears upper raycast screen (draws ceiling)
|
||||
|
Loading…
Reference in New Issue
Block a user